[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / ukm / ukm_service.h
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.
4
5 #ifndef COMPONENTS_UKM_UKM_SERVICE_H_
6 #define COMPONENTS_UKM_UKM_SERVICE_H_
7
8 #include <stddef.h>
9 #include <memory>
10 #include <vector>
11
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"
21
22 class PrefRegistrySimple;
23 class PrefService;
24
25 namespace metrics {
26 class MetricsServiceClient;
27 class UkmBrowserTestBase;
28 class UkmEGTestHelper;
29 }
30
31 namespace ukm {
32
33 namespace debug {
34 class UkmDebugDataExtractor;
35 }
36
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,
43 };
44
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 {
49  public:
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;
57
58   // Initializes the UKM service.
59   void Initialize();
60
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();
65
66 #if defined(OS_ANDROID) || defined(OS_IOS)
67   void OnAppEnterBackground();
68   void OnAppEnterForeground();
69 #endif
70
71   // Records any collected data into logs, and writes to disk.
72   void Flush();
73
74   // Deletes any unsent local data.
75   void Purge();
76
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);
80
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);
85
86   // Registers the names of all of the preferences used by UkmService in
87   // the provided PrefRegistry.
88   static void RegisterPrefs(PrefRegistrySimple* registry);
89
90   int32_t report_count() const { return report_count_; }
91
92  private:
93   friend ::metrics::UkmBrowserTestBase;
94   friend ::metrics::UkmEGTestHelper;
95   friend ::ukm::debug::UkmDebugDataExtractor;
96   friend ::ukm::UkmUtilsForTest;
97
98   // Starts metrics client initialization.
99   void StartInitTask();
100
101   // Called when initialization tasks are complete, to notify the scheduler
102   // that it can begin calling RotateLog.
103   void FinishedInitTask();
104
105   // Periodically called by scheduler_ to advance processing of logs.
106   void RotateLog();
107
108   // Constructs a new Report from available data and stores it in
109   // persisted_logs_.
110   void BuildAndStoreLog();
111
112   // Starts an upload of the next log from persisted_logs_.
113   void StartScheduledUpload();
114
115   // Called by log_uploader_ when the an upload is completed.
116   void OnLogUploadComplete(int response_code);
117
118   // ukm::UkmRecorderImpl:
119   bool ShouldRestrictToWhitelistedEntries() const override;
120
121   // A weak pointer to the PrefService used to read and write preferences.
122   PrefService* pref_service_;
123
124   // If true, only whitelisted Entries should be recorded.
125   bool restrict_to_whitelist_entries_;
126
127   // The UKM client id stored in prefs.
128   uint64_t client_id_;
129
130   // The UKM session id stored in prefs.
131   int32_t session_id_;
132
133   // The number of reports generated this session.
134   int32_t report_count_;
135
136   // Used to interact with the embedder. Weak pointer; must outlive |this|
137   // instance.
138   metrics::MetricsServiceClient* const client_;
139
140   // Registered metrics providers.
141   metrics::DelegatingProvider metrics_providers_;
142
143   // Log reporting service.
144   ukm::UkmReportingService reporting_service_;
145
146   // The scheduler for determining when uploads should happen.
147   std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_;
148
149   SEQUENCE_CHECKER(sequence_checker_);
150
151   bool initialize_started_;
152   bool initialize_complete_;
153
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_;
157
158   DISALLOW_COPY_AND_ASSIGN(UkmService);
159 };
160
161 }  // namespace ukm
162
163 #endif  // COMPONENTS_UKM_UKM_SERVICE_H_