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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_UKM_UKM_SERVICE_H_
6 #define COMPONENTS_UKM_UKM_SERVICE_H_
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequence_checker.h"
15 #include "build/build_config.h"
16 #include "components/metrics/delegating_provider.h"
17 #include "components/metrics/metrics_provider.h"
18 #include "components/metrics/metrics_rotation_scheduler.h"
19 #include "components/ukm/ukm_recorder_impl.h"
20 #include "components/ukm/ukm_reporting_service.h"
22 class PrefRegistrySimple;
26 class MetricsServiceClient;
27 class UkmBrowserTestBase;
28 class UkmEGTestHelper;
34 class UkmDebugDataExtractor;
37 // The URL-Keyed Metrics (UKM) service is responsible for gathering and
38 // uploading reports that contain fine grained performance metrics including
39 // URLs for top-level navigations.
40 class UkmService : public UkmRecorderImpl {
42 // Constructs a UkmService.
43 // Calling code is responsible for ensuring that the lifetime of
44 // |pref_service| is longer than the lifetime of UkmService.
45 UkmService(PrefService* pref_service,
46 metrics::MetricsServiceClient* client,
47 bool restrict_to_whitelist_entries);
48 ~UkmService() override;
50 // Initializes the UKM service.
53 // Enables/disables transmission of accumulated logs. Logs that have already
54 // been created will remain persisted to disk.
55 void EnableReporting();
56 void DisableReporting();
58 #if defined(OS_ANDROID) || defined(OS_IOS)
59 void OnAppEnterBackground();
60 void OnAppEnterForeground();
63 // Records any collected data into logs, and writes to disk.
66 // Deletes any unsent local data.
69 // Resets the client id stored in prefs.
72 // Registers the specified |provider| to provide additional metrics into the
73 // UKM log. Should be called during MetricsService initialization only.
74 void RegisterMetricsProvider(
75 std::unique_ptr<metrics::MetricsProvider> provider);
77 // Registers the names of all of the preferences used by UkmService in
78 // the provided PrefRegistry.
79 static void RegisterPrefs(PrefRegistrySimple* registry);
81 int32_t report_count() const { return report_count_; }
84 friend ::metrics::UkmBrowserTestBase;
85 friend ::metrics::UkmEGTestHelper;
86 friend ::ukm::debug::UkmDebugDataExtractor;
87 friend ::ukm::UkmUtilsForTest;
89 // Starts metrics client initialization.
92 // Called when initialization tasks are complete, to notify the scheduler
93 // that it can begin calling RotateLog.
94 void FinishedInitTask();
96 // Periodically called by scheduler_ to advance processing of logs.
99 // Constructs a new Report from available data and stores it in
101 void BuildAndStoreLog();
103 // Starts an upload of the next log from persisted_logs_.
104 void StartScheduledUpload();
106 // Called by log_uploader_ when the an upload is completed.
107 void OnLogUploadComplete(int response_code);
109 // ukm::UkmRecorderImpl:
110 bool ShouldRestrictToWhitelistedEntries() const override;
112 // A weak pointer to the PrefService used to read and write preferences.
113 PrefService* pref_service_;
115 // If true, only whitelisted Entries should be recorded.
116 bool restrict_to_whitelist_entries_;
118 // The UKM client id stored in prefs.
121 // The UKM session id stored in prefs.
124 // The number of reports generated this session.
125 int32_t report_count_;
127 // Used to interact with the embedder. Weak pointer; must outlive |this|
129 metrics::MetricsServiceClient* const client_;
131 // Registered metrics providers.
132 metrics::DelegatingProvider metrics_providers_;
134 // Log reporting service.
135 ukm::UkmReportingService reporting_service_;
137 // The scheduler for determining when uploads should happen.
138 std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_;
140 SEQUENCE_CHECKER(sequence_checker_);
142 bool initialize_started_;
143 bool initialize_complete_;
145 // Weak pointers factory used to post task on different threads. All weak
146 // pointers managed by this factory have the same lifetime as UkmService.
147 base::WeakPtrFactory<UkmService> self_ptr_factory_;
149 DISALLOW_COPY_AND_ASSIGN(UkmService);
154 #endif // COMPONENTS_UKM_UKM_SERVICE_H_