blob: 24e87b3a560a18fbf8bc8928d5a1cca93f80629e [file] [log] [blame]
[email protected]5bdaa2d2014-05-19 14:59:511// Copyright 2014 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 CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
6#define CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
7
avi6846aef2015-12-26 01:09:388#include <stdint.h>
9
manzagop14aff5d2016-11-23 17:27:0010#include <deque>
dcheng1edb03a2016-04-14 17:20:2911#include <memory>
huangse0fa6fd2016-11-04 20:41:1012#include <queue>
[email protected]5bdaa2d2014-05-19 14:59:5113#include <string>
14
[email protected]4b4892b2014-05-22 15:06:1515#include "base/callback.h"
avi6846aef2015-12-26 01:09:3816#include "base/macros.h"
[email protected]4b4892b2014-05-22 15:06:1517#include "base/memory/weak_ptr.h"
[email protected]d7ea39e2014-05-22 03:59:1818#include "base/threading/thread_checker.h"
avi6846aef2015-12-26 01:09:3819#include "build/build_config.h"
asvitkine89406d1f2015-01-17 06:57:1020#include "chrome/browser/metrics/metrics_memory_details.h"
[email protected]5bdaa2d2014-05-19 14:59:5121#include "components/metrics/metrics_service_client.h"
gunsch840bc412014-09-18 19:38:0622#include "components/metrics/profiler/tracking_synchronizer_observer.h"
manzagopa5d6688d2016-10-25 20:16:0323#include "components/metrics/proto/system_profile.pb.h"
blundell26b95242015-08-17 10:29:2424#include "components/omnibox/browser/omnibox_event_global_tracker.h"
holte7b74c622017-01-23 23:13:0725#include "components/ukm/observers/history_delete_observer.h"
holte1334c0aa2017-02-09 22:52:4126#include "components/ukm/observers/sync_disable_observer.h"
[email protected]d7ea39e2014-05-22 03:59:1827#include "content/public/browser/notification_observer.h"
28#include "content/public/browser/notification_registrar.h"
brettw4b461082016-11-19 18:55:1629#include "ppapi/features/features.h"
[email protected]d7ea39e2014-05-22 03:59:1830
wfhc768983fa2016-06-08 16:40:3331class AntiVirusMetricsProvider;
[email protected]51994b22014-05-30 13:24:2132class ChromeOSMetricsProvider;
[email protected]4a55a712014-06-08 16:50:3433class GoogleUpdateMetricsProviderWin;
[email protected]4a55a712014-06-08 16:50:3434class PluginMetricsProvider;
holte7b74c622017-01-23 23:13:0735class Profile;
[email protected]4a55a712014-06-08 16:50:3436class PrefRegistrySimple;
[email protected]7aee4f7e2014-08-12 01:15:0337
manzagopf2322662016-09-27 11:39:5938namespace browser_watcher {
39class WatcherMetricsProviderWin;
40} // namespace browser_watcher
41
[email protected]037642f2014-05-29 20:40:5042namespace metrics {
blundellb5c6b5a2015-07-30 20:18:3043class DriveMetricsProvider;
asvitkinecbd420732014-08-26 22:15:4044class MetricsService;
[email protected]037642f2014-05-29 20:40:5045class MetricsStateManager;
gunsch840bc412014-09-18 19:38:0646class ProfilerMetricsProvider;
siggi5e62f7e2014-11-21 21:55:4547} // namespace metrics
[email protected]037642f2014-05-29 20:40:5048
[email protected]5bdaa2d2014-05-19 14:59:5149// ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
50// that depends on chrome/.
holte7b74c622017-01-23 23:13:0751class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
52 public metrics::TrackingSynchronizerObserver,
53 public content::NotificationObserver,
holte1334c0aa2017-02-09 22:52:4154 public ukm::HistoryDeleteObserver,
55 public ukm::SyncDisableObserver {
[email protected]5bdaa2d2014-05-19 14:59:5156 public:
Daniel Chenga542fca2014-10-21 09:51:2957 ~ChromeMetricsServiceClient() override;
[email protected]5bdaa2d2014-05-19 14:59:5158
[email protected]037642f2014-05-29 20:40:5059 // Factory function.
dcheng1edb03a2016-04-14 17:20:2960 static std::unique_ptr<ChromeMetricsServiceClient> Create(
calvimei5b30e0d2016-08-17 21:24:3361 metrics::MetricsStateManager* state_manager);
[email protected]037642f2014-05-29 20:40:5062
[email protected]4a55a712014-06-08 16:50:3463 // Registers local state prefs used by this class.
64 static void RegisterPrefs(PrefRegistrySimple* registry);
65
[email protected]5bdaa2d2014-05-19 14:59:5166 // metrics::MetricsServiceClient:
blundellfecea528d2015-10-21 10:10:2267 metrics::MetricsService* GetMetricsService() override;
holte7b74c622017-01-23 23:13:0768 ukm::UkmService* GetUkmService() override;
Daniel Chenga542fca2014-10-21 09:51:2969 void SetMetricsClientId(const std::string& client_id) override;
avi6846aef2015-12-26 01:09:3870 int32_t GetProduct() override;
Daniel Chenga542fca2014-10-21 09:51:2971 std::string GetApplicationLocale() override;
72 bool GetBrand(std::string* brand_code) override;
73 metrics::SystemProfileProto::Channel GetChannel() override;
74 std::string GetVersionString() override;
manzagopa5d6688d2016-10-25 20:16:0375 void OnEnvironmentUpdate(std::string* serialized_environment) override;
manzagop14aff5d2016-11-23 17:27:0076 void OnLogCleanShutdown() override;
ishermanb6705682015-08-29 00:01:0077 void InitializeSystemProfileMetrics(
78 const base::Closure& done_callback) override;
79 void CollectFinalMetricsForLog(const base::Closure& done_callback) override;
dcheng1edb03a2016-04-14 17:20:2980 std::unique_ptr<metrics::MetricsLogUploader> CreateUploader(
holte567a16f22017-01-06 01:53:4581 const std::string& server_url,
82 const std::string& mime_type,
mostynb2b52d1db2014-10-07 02:47:1783 const base::Callback<void(int)>& on_upload_complete) override;
gunsch7cbdcb22015-03-13 17:02:0584 base::TimeDelta GetStandardUploadInterval() override;
Daniel Chenga542fca2014-10-21 09:51:2985 base::string16 GetRegistryBackupKey() override;
blundellfecea528d2015-10-21 10:10:2286 void OnPluginLoadingError(const base::FilePath& plugin_path) override;
jwd421086f2016-03-21 14:40:4287 bool IsReportingPolicyManaged() override;
gayanedaaf3a02016-06-15 16:30:2188 metrics::EnableMetricsDefault GetMetricsReportingDefaultState() override;
gayane0b46091c2016-04-07 21:01:0589 bool IsUMACellularUploadLogicEnabled() override;
holte08137d792017-02-15 21:43:5690 bool IsHistorySyncEnabledOnAllProfiles() override;
[email protected]4a55a712014-06-08 16:50:3491
holte1334c0aa2017-02-09 22:52:4192 // ukm::HistoryDeleteObserver:
holte7b74c622017-01-23 23:13:0793 void OnHistoryDeleted() override;
94
holte1334c0aa2017-02-09 22:52:4195 // ukm::SyncDisableObserver:
96 void OnSyncPrefsChanged(bool must_purge) override;
97
bcwhite374d5fe2016-05-20 17:03:2498 // Persistent browser metrics need to be persisted somewhere. This constant
99 // provides a known string to be used for both the allocator's internal name
100 // and for a file on disk (relative to chrome::DIR_USER_DATA) to which they
101 // can be saved.
102 static const char kBrowserMetricsName[];
103
[email protected]5bdaa2d2014-05-19 14:59:51104 private:
[email protected]037642f2014-05-29 20:40:50105 explicit ChromeMetricsServiceClient(
106 metrics::MetricsStateManager* state_manager);
107
[email protected]51994b22014-05-30 13:24:21108 // Completes the two-phase initialization of ChromeMetricsServiceClient.
109 void Initialize();
110
rkaplowb8d63f432017-02-06 19:00:42111 // Registers providers to the MetricsService. These provide data from
112 // alternate sources.
113 void RegisterMetricsServiceProviders();
114
115 // Registers providers to the UkmService. These provide data from alternate
116 // sources.
117 void RegisterUKMProviders();
118
huangse0fa6fd2016-11-04 20:41:10119 // Callback to chain init tasks: Pops and executes the next init task from
120 // |initialize_task_queue_|, then passes itself as callback for each init task
121 // to call upon completion.
122 void OnInitNextTask();
manzagopf2322662016-09-27 11:39:59123
isherman213b47c2015-08-28 08:20:46124 // Returns true iff profiler data should be included in the next metrics log.
125 // NOTE: This method is probabilistic and also updates internal state as a
126 // side-effect when called, so it should only be called once per log.
127 bool ShouldIncludeProfilerDataInLog();
128
[email protected]4a55a712014-06-08 16:50:34129 // TrackingSynchronizerObserver:
Daniel Chenga542fca2014-10-21 09:51:29130 void ReceivedProfilerData(
vadimte2de4732015-04-27 21:43:02131 const metrics::ProfilerDataAttributes& attributes,
vadimt379d7fe2015-04-01 00:09:35132 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase,
vadimt379d7fe2015-04-01 00:09:35133 const metrics::ProfilerEvents& past_profiler_events) override;
Daniel Chenga542fca2014-10-21 09:51:29134 void FinishedReceivingProfilerData() override;
[email protected]4a55a712014-06-08 16:50:34135
[email protected]4b4892b2014-05-22 15:06:15136 // Callbacks for various stages of final log info collection. Do not call
137 // these directly.
ishermanfcf4639e82015-08-26 00:33:49138 void CollectFinalHistograms();
[email protected]4b4892b2014-05-22 15:06:15139 void OnMemoryDetailCollectionDone();
140 void OnHistogramSynchronizationDone();
141
[email protected]daed87e2014-05-22 19:41:22142 // Records metrics about the switches present on the command line.
143 void RecordCommandLineMetrics();
144
[email protected]d7ea39e2014-05-22 03:59:18145 // Registers |this| as an observer for notifications which indicate that a
146 // user is performing work. This is useful to allow some features to sleep,
147 // until the machine becomes active, such as precluding UMA uploads unless
148 // there was recent activity.
149 void RegisterForNotifications();
150
holte1334c0aa2017-02-09 22:52:41151 // Call to listen for events on the selected profile's services.
152 void RegisterForProfileEvents(Profile* profile);
holte7b74c622017-01-23 23:13:07153
[email protected]d7ea39e2014-05-22 03:59:18154 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29155 void Observe(int type,
156 const content::NotificationSource& source,
157 const content::NotificationDetails& details) override;
[email protected]d7ea39e2014-05-22 03:59:18158
blundell26b95242015-08-17 10:29:24159 // Called when a URL is opened from the Omnibox.
160 void OnURLOpenedFromOmnibox(OmniboxLog* log);
161
[email protected]e2481a702014-05-23 21:06:50162#if defined(OS_WIN)
163 // Counts (and removes) the browser crash dump attempt signals left behind by
164 // any previous browser processes which generated a crash dump.
165 void CountBrowserCrashDumpAttempts();
166#endif // OS_WIN
167
[email protected]4b4892b2014-05-22 15:06:15168 base::ThreadChecker thread_checker_;
169
[email protected]51994b22014-05-30 13:24:21170 // Weak pointer to the MetricsStateManager.
171 metrics::MetricsStateManager* metrics_state_manager_;
172
[email protected]037642f2014-05-29 20:40:50173 // The MetricsService that |this| is a client of.
dcheng1edb03a2016-04-14 17:20:29174 std::unique_ptr<metrics::MetricsService> metrics_service_;
[email protected]d7ea39e2014-05-22 03:59:18175
holte7b74c622017-01-23 23:13:07176 // The UkmService that |this| is a client of.
177 std::unique_ptr<ukm::UkmService> ukm_service_;
178
[email protected]d7ea39e2014-05-22 03:59:18179 content::NotificationRegistrar registrar_;
[email protected]d7ea39e2014-05-22 03:59:18180
mlamouriba00ad12016-01-16 15:06:08181#if defined(OS_CHROMEOS)
[email protected]51994b22014-05-30 13:24:21182 // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
183 // that has been registered with MetricsService. On other platforms, is NULL.
184 ChromeOSMetricsProvider* chromeos_metrics_provider_;
mlamouriba00ad12016-01-16 15:06:08185#endif
[email protected]51994b22014-05-30 13:24:21186
huangse0fa6fd2016-11-04 20:41:10187 // A queue of tasks for initial metrics gathering. These may be asynchronous
188 // or synchronous.
189 std::deque<base::Closure> initialize_task_queue_;
190
ishermanb6705682015-08-29 00:01:00191 // Saved callback received from CollectFinalMetricsForLog().
[email protected]4b4892b2014-05-22 15:06:15192 base::Closure collect_final_metrics_done_callback_;
193
194 // Indicates that collect final metrics step is running.
195 bool waiting_for_collect_final_metrics_step_;
196
197 // Number of async histogram fetch requests in progress.
198 int num_async_histogram_fetches_in_progress_;
199
[email protected]4a55a712014-06-08 16:50:34200 // The ProfilerMetricsProvider instance that was registered with
201 // MetricsService. Has the same lifetime as |metrics_service_|.
gunsch840bc412014-09-18 19:38:06202 metrics::ProfilerMetricsProvider* profiler_metrics_provider_;
[email protected]4a55a712014-06-08 16:50:34203
brettw4b461082016-11-19 18:55:16204#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]4a55a712014-06-08 16:50:34205 // The PluginMetricsProvider instance that was registered with
206 // MetricsService. Has the same lifetime as |metrics_service_|.
207 PluginMetricsProvider* plugin_metrics_provider_;
208#endif
209
210#if defined(OS_WIN)
211 // The GoogleUpdateMetricsProviderWin instance that was registered with
212 // MetricsService. Has the same lifetime as |metrics_service_|.
213 GoogleUpdateMetricsProviderWin* google_update_metrics_provider_;
wfhc768983fa2016-06-08 16:40:33214
manzagopf2322662016-09-27 11:39:59215 // The WatcherMetricsProviderWin instance that was registered with
216 // MetricsService. Has the same lifetime as |metrics_service_|.
217 browser_watcher::WatcherMetricsProviderWin* watcher_metrics_provider_;
218
wfhc768983fa2016-06-08 16:40:33219 // The AntiVirusMetricsProvider instance that was registered with
220 // MetricsService. Has the same lifetime as |metrics_service_|.
221 AntiVirusMetricsProvider* antivirus_metrics_provider_;
[email protected]4a55a712014-06-08 16:50:34222#endif
223
dbeam64c37842015-03-18 02:23:35224 // The DriveMetricsProvider instance that was registered with MetricsService.
225 // Has the same lifetime as |metrics_service_|.
blundellb5c6b5a2015-07-30 20:18:30226 metrics::DriveMetricsProvider* drive_metrics_provider_;
dbeam64c37842015-03-18 02:23:35227
[email protected]8a5b2da2014-07-07 10:56:51228 // The MemoryGrowthTracker instance that tracks memory usage growth in
229 // MemoryDetails.
230 MemoryGrowthTracker memory_growth_tracker_;
231
gunsch7cbdcb22015-03-13 17:02:05232 // Callback to determine whether or not a cellular network is currently being
233 // used.
234 base::Callback<void(bool*)> cellular_callback_;
235
vadimte2de4732015-04-27 21:43:02236 // Time of this object's creation.
237 const base::TimeTicks start_time_;
238
blundell26b95242015-08-17 10:29:24239 // Subscription for receiving callbacks that a URL was opened from the
240 // omnibox.
dcheng1edb03a2016-04-14 17:20:29241 std::unique_ptr<base::CallbackList<void(OmniboxLog*)>::Subscription>
blundell26b95242015-08-17 10:29:24242 omnibox_url_opened_subscription_;
243
isherman213b47c2015-08-28 08:20:46244 // Whether this client has already uploaded profiler data during this session.
245 // Profiler data is uploaded at most once per session.
246 bool has_uploaded_profiler_data_;
247
[email protected]4b4892b2014-05-22 15:06:15248 base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
249
[email protected]5bdaa2d2014-05-19 14:59:51250 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
251};
252
253#endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_