Upload upstream chromium 71.0.3578.0
[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 // 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 {
41  public:
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;
49
50   // Initializes the UKM service.
51   void Initialize();
52
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();
57
58 #if defined(OS_ANDROID) || defined(OS_IOS)
59   void OnAppEnterBackground();
60   void OnAppEnterForeground();
61 #endif
62
63   // Records any collected data into logs, and writes to disk.
64   void Flush();
65
66   // Deletes any unsent local data.
67   void Purge();
68
69   // Resets the client id stored in prefs.
70   void ResetClientId();
71
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);
76
77   // Registers the names of all of the preferences used by UkmService in
78   // the provided PrefRegistry.
79   static void RegisterPrefs(PrefRegistrySimple* registry);
80
81   int32_t report_count() const { return report_count_; }
82
83  private:
84   friend ::metrics::UkmBrowserTestBase;
85   friend ::metrics::UkmEGTestHelper;
86   friend ::ukm::debug::UkmDebugDataExtractor;
87   friend ::ukm::UkmUtilsForTest;
88
89   // Starts metrics client initialization.
90   void StartInitTask();
91
92   // Called when initialization tasks are complete, to notify the scheduler
93   // that it can begin calling RotateLog.
94   void FinishedInitTask();
95
96   // Periodically called by scheduler_ to advance processing of logs.
97   void RotateLog();
98
99   // Constructs a new Report from available data and stores it in
100   // persisted_logs_.
101   void BuildAndStoreLog();
102
103   // Starts an upload of the next log from persisted_logs_.
104   void StartScheduledUpload();
105
106   // Called by log_uploader_ when the an upload is completed.
107   void OnLogUploadComplete(int response_code);
108
109   // ukm::UkmRecorderImpl:
110   bool ShouldRestrictToWhitelistedEntries() const override;
111
112   // A weak pointer to the PrefService used to read and write preferences.
113   PrefService* pref_service_;
114
115   // If true, only whitelisted Entries should be recorded.
116   bool restrict_to_whitelist_entries_;
117
118   // The UKM client id stored in prefs.
119   uint64_t client_id_;
120
121   // The UKM session id stored in prefs.
122   int32_t session_id_;
123
124   // The number of reports generated this session.
125   int32_t report_count_;
126
127   // Used to interact with the embedder. Weak pointer; must outlive |this|
128   // instance.
129   metrics::MetricsServiceClient* const client_;
130
131   // Registered metrics providers.
132   metrics::DelegatingProvider metrics_providers_;
133
134   // Log reporting service.
135   ukm::UkmReportingService reporting_service_;
136
137   // The scheduler for determining when uploads should happen.
138   std::unique_ptr<metrics::MetricsRotationScheduler> scheduler_;
139
140   SEQUENCE_CHECKER(sequence_checker_);
141
142   bool initialize_started_;
143   bool initialize_complete_;
144
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_;
148
149   DISALLOW_COPY_AND_ASSIGN(UkmService);
150 };
151
152 }  // namespace ukm
153
154 #endif  // COMPONENTS_UKM_UKM_SERVICE_H_