| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_UKM_UKM_SERVICE_H_ | 5 #ifndef COMPONENTS_UKM_UKM_SERVICE_H_ |
| 6 #define COMPONENTS_UKM_UKM_SERVICE_H_ | 6 #define COMPONENTS_UKM_UKM_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | |
| 13 #include "base/feature_list.h" | |
| 14 #include "base/macros.h" | 12 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 17 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 18 #include "components/metrics/metrics_provider.h" | 16 #include "components/metrics/metrics_provider.h" |
| 19 #include "components/metrics/metrics_rotation_scheduler.h" | 17 #include "components/metrics/metrics_rotation_scheduler.h" |
| 18 #include "components/ukm/ukm_recorder_impl.h" |
| 20 #include "components/ukm/ukm_reporting_service.h" | 19 #include "components/ukm/ukm_reporting_service.h" |
| 21 #include "url/gurl.h" | |
| 22 | 20 |
| 23 class ContextualSearchRankerLoggerImpl; | |
| 24 class PluginInfoMessageFilter; | |
| 25 class PrefRegistrySimple; | 21 class PrefRegistrySimple; |
| 26 class PrefService; | 22 class PrefService; |
| 27 class UkmPageLoadMetricsObserver; | |
| 28 | |
| 29 namespace autofill { | |
| 30 class AutofillMetrics; | |
| 31 } // namespace autofill | |
| 32 | |
| 33 namespace translate { | |
| 34 class TranslateRankerImpl; | |
| 35 } | |
| 36 | |
| 37 namespace payments { | |
| 38 class JourneyLogger; | |
| 39 } // namespace payments | |
| 40 | 23 |
| 41 namespace metrics { | 24 namespace metrics { |
| 42 class MetricsServiceClient; | 25 class MetricsServiceClient; |
| 43 } | 26 } |
| 44 | 27 |
| 45 namespace ukm { | 28 namespace ukm { |
| 46 | 29 |
| 47 class UkmEntry; | |
| 48 class UkmEntryBuilder; | |
| 49 class UkmSource; | |
| 50 | |
| 51 namespace debug { | 30 namespace debug { |
| 52 class DebugPage; | 31 class DebugPage; |
| 53 } | 32 } |
| 54 | 33 |
| 55 // This feature controls whether UkmService should be created. | |
| 56 extern const base::Feature kUkmFeature; | |
| 57 | |
| 58 // The URL-Keyed Metrics (UKM) service is responsible for gathering and | 34 // The URL-Keyed Metrics (UKM) service is responsible for gathering and |
| 59 // uploading reports that contain fine grained performance metrics including | 35 // uploading reports that contain fine grained performance metrics including |
| 60 // URLs for top-level navigations. | 36 // URLs for top-level navigations. |
| 61 class UkmService { | 37 class UkmService : public UkmRecorderImpl { |
| 62 public: | 38 public: |
| 63 // Constructs a UkmService. | 39 // Constructs a UkmService. |
| 64 // Calling code is responsible for ensuring that the lifetime of | 40 // Calling code is responsible for ensuring that the lifetime of |
| 65 // |pref_service| is longer than the lifetime of UkmService. | 41 // |pref_service| is longer than the lifetime of UkmService. |
| 66 UkmService(PrefService* pref_service, metrics::MetricsServiceClient* client); | 42 UkmService(PrefService* pref_service, metrics::MetricsServiceClient* client); |
| 67 virtual ~UkmService(); | 43 ~UkmService() override; |
| 68 | |
| 69 // Get the new source ID, which is unique for the duration of a browser | |
| 70 // session. | |
| 71 static int32_t GetNewSourceID(); | |
| 72 | |
| 73 // Update the URL on the source keyed to the given source ID. If the source | |
| 74 // does not exist, it will create a new UkmSource object. | |
| 75 void UpdateSourceURL(int32_t source_id, const GURL& url); | |
| 76 | 44 |
| 77 // Initializes the UKM service. | 45 // Initializes the UKM service. |
| 78 void Initialize(); | 46 void Initialize(); |
| 79 | 47 |
| 80 // Enables/disables recording control if data is allowed to be collected. | |
| 81 void EnableRecording(); | |
| 82 void DisableRecording(); | |
| 83 | |
| 84 // Enables/disables transmission of accumulated logs. Logs that have already | 48 // Enables/disables transmission of accumulated logs. Logs that have already |
| 85 // been created will remain persisted to disk. | 49 // been created will remain persisted to disk. |
| 86 void EnableReporting(); | 50 void EnableReporting(); |
| 87 void DisableReporting(); | 51 void DisableReporting(); |
| 88 | 52 |
| 89 #if defined(OS_ANDROID) || defined(OS_IOS) | 53 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 90 void OnAppEnterBackground(); | 54 void OnAppEnterBackground(); |
| 91 void OnAppEnterForeground(); | 55 void OnAppEnterForeground(); |
| 92 #endif | 56 #endif |
| 93 | 57 |
| 94 // Records any collected data into logs, and writes to disk. | 58 // Records any collected data into logs, and writes to disk. |
| 95 void Flush(); | 59 void Flush(); |
| 96 | 60 |
| 97 // Deletes any unsent local data. | 61 // Deletes any unsent local data. |
| 98 void Purge(); | 62 void Purge(); |
| 99 | 63 |
| 100 // Resets the client id stored in prefs. | 64 // Resets the client id stored in prefs. |
| 101 void ResetClientId(); | 65 void ResetClientId(); |
| 102 | 66 |
| 103 // Registers the specified |provider| to provide additional metrics into the | 67 // Registers the specified |provider| to provide additional metrics into the |
| 104 // UKM log. Should be called during MetricsService initialization only. | 68 // UKM log. Should be called during MetricsService initialization only. |
| 105 void RegisterMetricsProvider( | 69 void RegisterMetricsProvider( |
| 106 std::unique_ptr<metrics::MetricsProvider> provider); | 70 std::unique_ptr<metrics::MetricsProvider> provider); |
| 107 | 71 |
| 108 // Registers the names of all of the preferences used by UkmService in | 72 // Registers the names of all of the preferences used by UkmService in |
| 109 // the provided PrefRegistry. | 73 // the provided PrefRegistry. |
| 110 static void RegisterPrefs(PrefRegistrySimple* registry); | 74 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 111 | 75 |
| 112 using AddEntryCallback = base::Callback<void(std::unique_ptr<UkmEntry>)>; | |
| 113 | |
| 114 protected: | |
| 115 const std::map<int32_t, std::unique_ptr<UkmSource>>& sources_for_testing() | |
| 116 const { | |
| 117 return sources_; | |
| 118 } | |
| 119 | |
| 120 const std::vector<std::unique_ptr<UkmEntry>>& entries_for_testing() const { | |
| 121 return entries_; | |
| 122 } | |
| 123 | |
| 124 private: | 76 private: |
| 125 friend ::ukm::debug::DebugPage; | 77 friend ::ukm::debug::DebugPage; |
| 126 friend autofill::AutofillMetrics; | 78 |
| 127 friend payments::JourneyLogger; | |
| 128 friend ContextualSearchRankerLoggerImpl; | |
| 129 friend PluginInfoMessageFilter; | |
| 130 friend UkmPageLoadMetricsObserver; | |
| 131 friend translate::TranslateRankerImpl; | |
| 132 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, AddEntryWithEmptyMetrics); | 79 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, AddEntryWithEmptyMetrics); |
| 133 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, EntryBuilderAndSerialization); | 80 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, EntryBuilderAndSerialization); |
| 134 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, | 81 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, |
| 135 LogsUploadedOnlyWhenHavingSourcesOrEntries); | 82 LogsUploadedOnlyWhenHavingSourcesOrEntries); |
| 136 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, MetricsProviderTest); | 83 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, MetricsProviderTest); |
| 137 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, PersistAndPurge); | 84 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, PersistAndPurge); |
| 138 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, WhitelistEntryTest); | 85 FRIEND_TEST_ALL_PREFIXES(UkmServiceTest, WhitelistEntryTest); |
| 139 | 86 |
| 140 // Get a new UkmEntryBuilder object for the specified source ID and event, | |
| 141 // which can get metrics added to. | |
| 142 // | |
| 143 // This API being private is intentional. Any client using UKM needs to | |
| 144 // declare itself to be a friend of UkmService and go through code review | |
| 145 // process. | |
| 146 std::unique_ptr<UkmEntryBuilder> GetEntryBuilder(int32_t source_id, | |
| 147 const char* event_name); | |
| 148 | |
| 149 // Starts metrics client initialization. | 87 // Starts metrics client initialization. |
| 150 void StartInitTask(); | 88 void StartInitTask(); |
| 151 | 89 |
| 152 // Called when initialization tasks are complete, to notify the scheduler | 90 // Called when initialization tasks are complete, to notify the scheduler |
| 153 // that it can begin calling RotateLog. | 91 // that it can begin calling RotateLog. |
| 154 void FinishedInitTask(); | 92 void FinishedInitTask(); |
| 155 | 93 |
| 156 // Periodically called by scheduler_ to advance processing of logs. | 94 // Periodically called by scheduler_ to advance processing of logs. |
| 157 void RotateLog(); | 95 void RotateLog(); |
| 158 | 96 |
| 159 // Constructs a new Report from available data and stores it in | 97 // Constructs a new Report from available data and stores it in |
| 160 // persisted_logs_. | 98 // persisted_logs_. |
| 161 void BuildAndStoreLog(); | 99 void BuildAndStoreLog(); |
| 162 | 100 |
| 163 // Starts an upload of the next log from persisted_logs_. | 101 // Starts an upload of the next log from persisted_logs_. |
| 164 void StartScheduledUpload(); | 102 void StartScheduledUpload(); |
| 165 | 103 |
| 166 // Called by log_uploader_ when the an upload is completed. | 104 // Called by log_uploader_ when the an upload is completed. |
| 167 void OnLogUploadComplete(int response_code); | 105 void OnLogUploadComplete(int response_code); |
| 168 | 106 |
| 169 // Add an entry to the UkmEntry list. | |
| 170 void AddEntry(std::unique_ptr<UkmEntry> entry); | |
| 171 | |
| 172 // Cache the list of whitelisted entries from the field trial parameter. | |
| 173 void StoreWhitelistedEntries(); | |
| 174 | |
| 175 // A weak pointer to the PrefService used to read and write preferences. | 107 // A weak pointer to the PrefService used to read and write preferences. |
| 176 PrefService* pref_service_; | 108 PrefService* pref_service_; |
| 177 | 109 |
| 178 // Whether recording new data is currently allowed. | |
| 179 bool recording_enabled_; | |
| 180 | |
| 181 // The UKM client id stored in prefs. | 110 // The UKM client id stored in prefs. |
| 182 uint64_t client_id_; | 111 uint64_t client_id_; |
| 183 | 112 |
| 184 // The UKM session id stored in prefs. | 113 // The UKM session id stored in prefs. |
| 185 int32_t session_id_; | 114 int32_t session_id_; |
| 186 | 115 |
| 187 // Used to interact with the embedder. Weak pointer; must outlive |this| | 116 // Used to interact with the embedder. Weak pointer; must outlive |this| |
| 188 // instance. | 117 // instance. |
| 189 metrics::MetricsServiceClient* const client_; | 118 metrics::MetricsServiceClient* const client_; |
| 190 | 119 |
| 191 // Registered metrics providers. | 120 // Registered metrics providers. |
| 192 std::vector<std::unique_ptr<metrics::MetricsProvider>> metrics_providers_; | 121 std::vector<std::unique_ptr<metrics::MetricsProvider>> metrics_providers_; |
| 193 | 122 |
| 194 // Log reporting service. | 123 // Log reporting service. |
| 195 ukm::UkmReportingService reporting_service_; | 124 ukm::UkmReportingService reporting_service_; |
| 196 | 125 |
| 197 // The scheduler for determining when uploads should happen. | 126 // The scheduler for determining when uploads should happen. |
| 198 std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_; | 127 std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_; |
| 199 | 128 |
| 200 base::ThreadChecker thread_checker_; | 129 base::ThreadChecker thread_checker_; |
| 201 | 130 |
| 202 bool initialize_started_; | 131 bool initialize_started_; |
| 203 bool initialize_complete_; | 132 bool initialize_complete_; |
| 204 | 133 |
| 205 // Contains newly added sources and entries of UKM metrics which periodically | |
| 206 // get serialized and cleared by BuildAndStoreLog(). | |
| 207 std::map<int32_t, std::unique_ptr<UkmSource>> sources_; | |
| 208 std::vector<std::unique_ptr<UkmEntry>> entries_; | |
| 209 | |
| 210 // Whitelisted Entry hashes, only the ones in this set will be recorded. | |
| 211 std::set<uint64_t> whitelisted_entry_hashes_; | |
| 212 | |
| 213 // Weak pointers factory used to post task on different threads. All weak | 134 // Weak pointers factory used to post task on different threads. All weak |
| 214 // pointers managed by this factory have the same lifetime as UkmService. | 135 // pointers managed by this factory have the same lifetime as UkmService. |
| 215 base::WeakPtrFactory<UkmService> self_ptr_factory_; | 136 base::WeakPtrFactory<UkmService> self_ptr_factory_; |
| 216 | 137 |
| 217 DISALLOW_COPY_AND_ASSIGN(UkmService); | 138 DISALLOW_COPY_AND_ASSIGN(UkmService); |
| 218 }; | 139 }; |
| 219 | 140 |
| 220 } // namespace ukm | 141 } // namespace ukm |
| 221 | 142 |
| 222 #endif // COMPONENTS_UKM_UKM_SERVICE_H_ | 143 #endif // COMPONENTS_UKM_UKM_SERVICE_H_ |
| OLD | NEW |