Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: components/ukm/ukm_service.h

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

Powered by Google App Engine
This is Rietveld 408576698