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 // These values are persisted to logs. Entries should not be renumbered and
38 // numeric values should never be reused. This maps to the enum UkmResetReason.
39 enum class ResetReason {
40 kOnSyncPrefsChanged = 0,
41 kUpdatePermissions = 1,
42 kMaxValue = kUpdatePermissions,
45 // The URL-Keyed Metrics (UKM) service is responsible for gathering and
46 // uploading reports that contain fine grained performance metrics including
47 // URLs for top-level navigations.
48 class UkmService : public UkmRecorderImpl {
50 // Constructs a UkmService.
51 // Calling code is responsible for ensuring that the lifetime of
52 // |pref_service| is longer than the lifetime of UkmService.
53 UkmService(PrefService* pref_service,
54 metrics::MetricsServiceClient* client,
55 bool restrict_to_whitelist_entries);
56 ~UkmService() override;
58 // Initializes the UKM service.
61 // Enables/disables transmission of accumulated logs. Logs that have already
62 // been created will remain persisted to disk.
63 void EnableReporting();
64 void DisableReporting();
66 #if defined(OS_ANDROID) || defined(OS_IOS)
67 void OnAppEnterBackground();
68 void OnAppEnterForeground();
71 // Records any collected data into logs, and writes to disk.
74 // Deletes any unsent local data.
77 // Resets the client prefs (client_id/session_id). |reason| should be passed
78 // to provide the reason of the reset - this is only used for UMA logging.
79 void ResetClientState(ResetReason reason);
81 // Registers the specified |provider| to provide additional metrics into the
82 // UKM log. Should be called during MetricsService initialization only.
83 void RegisterMetricsProvider(
84 std::unique_ptr<metrics::MetricsProvider> provider);
86 // Registers the names of all of the preferences used by UkmService in
87 // the provided PrefRegistry.
88 static void RegisterPrefs(PrefRegistrySimple* registry);
90 int32_t report_count() const { return report_count_; }
93 friend ::metrics::UkmBrowserTestBase;
94 friend ::metrics::UkmEGTestHelper;
95 friend ::ukm::debug::UkmDebugDataExtractor;
96 friend ::ukm::UkmUtilsForTest;
98 // Starts metrics client initialization.
101 // Called when initialization tasks are complete, to notify the scheduler
102 // that it can begin calling RotateLog.
103 void FinishedInitTask();
105 // Periodically called by scheduler_ to advance processing of logs.
108 // Constructs a new Report from available data and stores it in
110 void BuildAndStoreLog();
112 // Starts an upload of the next log from persisted_logs_.
113 void StartScheduledUpload();
115 // Called by log_uploader_ when the an upload is completed.
116 void OnLogUploadComplete(int response_code);
118 // ukm::UkmRecorderImpl:
119 bool ShouldRestrictToWhitelistedEntries() const override;
121 // A weak pointer to the PrefService used to read and write preferences.
122 PrefService* pref_service_;
124 // If true, only whitelisted Entries should be recorded.
125 bool restrict_to_whitelist_entries_;
127 // The UKM client id stored in prefs.
130 // The UKM session id stored in prefs.
133 // The number of reports generated this session.
134 int32_t report_count_;
136 // Used to interact with the embedder. Weak pointer; must outlive |this|
138 metrics::MetricsServiceClient* const client_;
140 // Registered metrics providers.
141 metrics::DelegatingProvider metrics_providers_;
143 // Log reporting service.
144 ukm::UkmReportingService reporting_service_;
146 // The scheduler for determining when uploads should happen.
147 std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_;
149 SEQUENCE_CHECKER(sequence_checker_);
151 bool initialize_started_;
152 bool initialize_complete_;
154 // Weak pointers factory used to post task on different threads. All weak
155 // pointers managed by this factory have the same lifetime as UkmService.
156 base::WeakPtrFactory<UkmService> self_ptr_factory_;
158 DISALLOW_COPY_AND_ASSIGN(UkmService);
163 #endif // COMPONENTS_UKM_UKM_SERVICE_H_