ukm_source_id.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_METRICS_UKM_SOURCE_ID_H_
  5. #define BASE_METRICS_UKM_SOURCE_ID_H_
  6. #include <stdint.h>
  7. #include "base/base_export.h"
  8. namespace base {
  9. // An ID used to identify a Source to UKM, for recording information about it.
  10. // These objects are copyable, assignable, and occupy 64-bits per instance.
  11. // Prefer passing them by value. When a new type is added, please also update
  12. // the enum type in third_party/metrics_proto/ukm/source.proto and the
  13. // converting function ToProtobufSourceType.
  14. class BASE_EXPORT UkmSourceId {
  15. public:
  16. enum class Type : int64_t {
  17. // Source ids of this type are created via ukm::AssignNewSourceId, to denote
  18. // 'custom' source other than the types below. Source of this type has
  19. // additional restrictions with logging, as determined by
  20. // IsWhitelistedSourceId.
  21. DEFAULT = 0,
  22. // Sources created by navigation. They will be kept in memory as long as
  23. // the associated tab is still alive and the number of sources are within
  24. // the max threshold.
  25. NAVIGATION_ID = 1,
  26. // Source ID used by AppLaunchEventLogger::Log. A new source of this type
  27. // and associated events are expected to be recorded within the same report
  28. // interval; it will not be kept in memory between different reports.
  29. APP_ID = 2,
  30. // Source ID for background events that don't have an open tab but the
  31. // associated URL is still present in the browser's history. A new source of
  32. // this type and associated events are expected to be recorded within the
  33. // same report interval; it will not be kept in memory between different
  34. // reports.
  35. HISTORY_ID = 3,
  36. // Source ID used by WebApkUkmRecorder. A new source of this type and
  37. // associated events are expected to be recorded within the same report
  38. // interval; it will not be kept in memory between different reports.
  39. WEBAPK_ID = 4,
  40. // Source ID for service worker based payment handlers. A new source of this
  41. // type and associated events are expected to be recorded within the same
  42. // report interval; it will not be kept in memory between different reports.
  43. PAYMENT_APP_ID = 5,
  44. // Source ID for desktop web apps, based on the start_url in the web app
  45. // manifest. A new source of this type and associated events are expected to
  46. // be recorded within the same report interval; it will not be kept in
  47. // memory between different reports.
  48. DESKTOP_WEB_APP_ID = 6,
  49. // Source ID for web workers, namely SharedWorkers and ServiceWorkers. Web
  50. // workers may inherit a source ID from the spawner context (in the case of
  51. // dedicated workers), or may have their own source IDs (in the case of
  52. // shared workers and service workers). Shared workers and service workers
  53. // can be connected to multiple clients (e.g. documents or other workers).
  54. WORKER_ID = 7,
  55. kMaxValue = WORKER_ID,
  56. };
  57. // Default constructor has the invalid value.
  58. constexpr UkmSourceId() : value_(0) {}
  59. constexpr UkmSourceId& operator=(UkmSourceId other) {
  60. value_ = other.value_;
  61. return *this;
  62. }
  63. // Allow identity comparisons.
  64. constexpr bool operator==(UkmSourceId other) const {
  65. return value_ == other.value_;
  66. }
  67. constexpr bool operator!=(UkmSourceId other) const {
  68. return value_ != other.value_;
  69. }
  70. // Allow coercive comparisons to simplify test migration.
  71. // TODO(crbug/873866): Remove these once callers are migrated.
  72. constexpr bool operator==(int64_t other) const { return value_ == other; }
  73. constexpr bool operator!=(int64_t other) const { return value_ == other; }
  74. // Extract the Type of the SourceId.
  75. Type GetType() const;
  76. // Return the ID as an int64.
  77. constexpr int64_t ToInt64() const { return value_; }
  78. // Convert an int64 ID value to an ID.
  79. static constexpr UkmSourceId FromInt64(int64_t internal_value) {
  80. return UkmSourceId(internal_value);
  81. }
  82. // Get a new UKM-Type SourceId, which is unique within the scope of a
  83. // browser session.
  84. static UkmSourceId New();
  85. // Utility for converting other unique ids to source ids.
  86. static UkmSourceId FromOtherId(int64_t value, Type type);
  87. private:
  88. constexpr explicit UkmSourceId(int64_t value) : value_(value) {}
  89. int64_t value_;
  90. };
  91. constexpr UkmSourceId kInvalidUkmSourceId = UkmSourceId();
  92. } // namespace base
  93. #endif // BASE_METRICS_UKM_SOURCE_ID_H_