blob: c9cbba95202cfa842d2ad0cc1fde7fcac8d483e4 [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
dcheng1edb03a2016-04-14 17:20:2910#include <memory>
[email protected]5bdaa2d2014-05-19 14:59:5111#include <string>
12
[email protected]4b4892b2014-05-22 15:06:1513#include "base/callback.h"
avi6846aef2015-12-26 01:09:3814#include "base/macros.h"
[email protected]4b4892b2014-05-22 15:06:1515#include "base/memory/weak_ptr.h"
[email protected]d7ea39e2014-05-22 03:59:1816#include "base/threading/thread_checker.h"
avi6846aef2015-12-26 01:09:3817#include "build/build_config.h"
asvitkine89406d1f2015-01-17 06:57:1018#include "chrome/browser/metrics/metrics_memory_details.h"
[email protected]5bdaa2d2014-05-19 14:59:5119#include "components/metrics/metrics_service_client.h"
gunsch840bc412014-09-18 19:38:0620#include "components/metrics/profiler/tracking_synchronizer_observer.h"
blundell26b95242015-08-17 10:29:2421#include "components/omnibox/browser/omnibox_event_global_tracker.h"
[email protected]d7ea39e2014-05-22 03:59:1822#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
24
wfhc768983fa2016-06-08 16:40:3325class AntiVirusMetricsProvider;
[email protected]51994b22014-05-30 13:24:2126class ChromeOSMetricsProvider;
[email protected]4a55a712014-06-08 16:50:3427class GoogleUpdateMetricsProviderWin;
[email protected]4a55a712014-06-08 16:50:3428class PluginMetricsProvider;
29class PrefRegistrySimple;
ishermanfc021e912015-03-25 23:33:3330class PrefService;
[email protected]4a55a712014-06-08 16:50:3431
jam1c5a91492016-02-24 20:47:5332#if !defined(OS_CHROMEOS)
[email protected]7aee4f7e2014-08-12 01:15:0333class SigninStatusMetricsProvider;
34#endif
35
manzagopf2322662016-09-27 11:39:5936namespace browser_watcher {
37class WatcherMetricsProviderWin;
38} // namespace browser_watcher
39
[email protected]037642f2014-05-29 20:40:5040namespace metrics {
blundellb5c6b5a2015-07-30 20:18:3041class DriveMetricsProvider;
asvitkinecbd420732014-08-26 22:15:4042class MetricsService;
[email protected]037642f2014-05-29 20:40:5043class MetricsStateManager;
gunsch840bc412014-09-18 19:38:0644class ProfilerMetricsProvider;
siggi5e62f7e2014-11-21 21:55:4545} // namespace metrics
[email protected]037642f2014-05-29 20:40:5046
[email protected]5bdaa2d2014-05-19 14:59:5147// ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
48// that depends on chrome/.
[email protected]4a55a712014-06-08 16:50:3449class ChromeMetricsServiceClient
50 : public metrics::MetricsServiceClient,
gunsch840bc412014-09-18 19:38:0651 public metrics::TrackingSynchronizerObserver,
[email protected]4a55a712014-06-08 16:50:3452 public content::NotificationObserver {
[email protected]5bdaa2d2014-05-19 14:59:5153 public:
Daniel Chenga542fca2014-10-21 09:51:2954 ~ChromeMetricsServiceClient() override;
[email protected]5bdaa2d2014-05-19 14:59:5155
[email protected]037642f2014-05-29 20:40:5056 // Factory function.
dcheng1edb03a2016-04-14 17:20:2957 static std::unique_ptr<ChromeMetricsServiceClient> Create(
calvimei5b30e0d2016-08-17 21:24:3358 metrics::MetricsStateManager* state_manager);
[email protected]037642f2014-05-29 20:40:5059
[email protected]4a55a712014-06-08 16:50:3460 // Registers local state prefs used by this class.
61 static void RegisterPrefs(PrefRegistrySimple* registry);
62
[email protected]5bdaa2d2014-05-19 14:59:5163 // metrics::MetricsServiceClient:
blundellfecea528d2015-10-21 10:10:2264 metrics::MetricsService* GetMetricsService() override;
Daniel Chenga542fca2014-10-21 09:51:2965 void SetMetricsClientId(const std::string& client_id) override;
avi6846aef2015-12-26 01:09:3866 int32_t GetProduct() override;
Daniel Chenga542fca2014-10-21 09:51:2967 std::string GetApplicationLocale() override;
68 bool GetBrand(std::string* brand_code) override;
69 metrics::SystemProfileProto::Channel GetChannel() override;
70 std::string GetVersionString() override;
71 void OnLogUploadComplete() override;
ishermanb6705682015-08-29 00:01:0072 void InitializeSystemProfileMetrics(
73 const base::Closure& done_callback) override;
74 void CollectFinalMetricsForLog(const base::Closure& done_callback) override;
dcheng1edb03a2016-04-14 17:20:2975 std::unique_ptr<metrics::MetricsLogUploader> CreateUploader(
mostynb2b52d1db2014-10-07 02:47:1776 const base::Callback<void(int)>& on_upload_complete) override;
gunsch7cbdcb22015-03-13 17:02:0577 base::TimeDelta GetStandardUploadInterval() override;
Daniel Chenga542fca2014-10-21 09:51:2978 base::string16 GetRegistryBackupKey() override;
blundellfecea528d2015-10-21 10:10:2279 void OnPluginLoadingError(const base::FilePath& plugin_path) override;
jwd421086f2016-03-21 14:40:4280 bool IsReportingPolicyManaged() override;
gayanedaaf3a02016-06-15 16:30:2181 metrics::EnableMetricsDefault GetMetricsReportingDefaultState() override;
gayane0b46091c2016-04-07 21:01:0582 bool IsUMACellularUploadLogicEnabled() override;
[email protected]4a55a712014-06-08 16:50:3483
bcwhite374d5fe2016-05-20 17:03:2484 // Persistent browser metrics need to be persisted somewhere. This constant
85 // provides a known string to be used for both the allocator's internal name
86 // and for a file on disk (relative to chrome::DIR_USER_DATA) to which they
87 // can be saved.
88 static const char kBrowserMetricsName[];
89
[email protected]5bdaa2d2014-05-19 14:59:5190 private:
[email protected]037642f2014-05-29 20:40:5091 explicit ChromeMetricsServiceClient(
92 metrics::MetricsStateManager* state_manager);
93
[email protected]51994b22014-05-30 13:24:2194 // Completes the two-phase initialization of ChromeMetricsServiceClient.
95 void Initialize();
96
ortuno4958ca4f2016-02-26 02:15:4397 // Callback that continues the init task by getting a Bluetooth Adapter.
[email protected]4a55a712014-06-08 16:50:3498 void OnInitTaskGotHardwareClass();
99
ortuno4958ca4f2016-02-26 02:15:43100 // Callback that continues the init task by loading plugin information.
101 void OnInitTaskGotBluetoothAdapter();
102
[email protected]4a55a712014-06-08 16:50:34103 // Called after the Plugin init task has been completed that continues the
104 // init task by launching a task to gather Google Update statistics.
105 void OnInitTaskGotPluginInfo();
106
107 // Called after GoogleUpdate init task has been completed that continues the
wfhc768983fa2016-06-08 16:40:33108 // init task by loading AntiVirus metrics.
[email protected]4a55a712014-06-08 16:50:34109 void OnInitTaskGotGoogleUpdateData();
110
wfhc768983fa2016-06-08 16:40:33111 // Called after AntiVirus init task has been completed that continues the
112 // init task by loading drive metrics.
113 void OnInitTaskGotAntiVirusData();
114
manzagopf2322662016-09-27 11:39:59115 // Called after the drive metrics init task has been completed to continue
116 // the init task by optionally collecting postmortem reports.
ishermanfcf4639e82015-08-26 00:33:49117 void OnInitTaskGotDriveMetrics();
amistry9e2a8b42015-06-13 01:11:27118
manzagopf2322662016-09-27 11:39:59119 // Called after the postmortem report collection task has been completed to
120 // continue the init task by loading profiler data.
121 void OnInitTaskCollectedPostmortemReports();
122
isherman213b47c2015-08-28 08:20:46123 // Returns true iff profiler data should be included in the next metrics log.
124 // NOTE: This method is probabilistic and also updates internal state as a
125 // side-effect when called, so it should only be called once per log.
126 bool ShouldIncludeProfilerDataInLog();
127
[email protected]4a55a712014-06-08 16:50:34128 // TrackingSynchronizerObserver:
Daniel Chenga542fca2014-10-21 09:51:29129 void ReceivedProfilerData(
vadimte2de4732015-04-27 21:43:02130 const metrics::ProfilerDataAttributes& attributes,
vadimt379d7fe2015-04-01 00:09:35131 const tracked_objects::ProcessDataPhaseSnapshot& process_data_phase,
vadimt379d7fe2015-04-01 00:09:35132 const metrics::ProfilerEvents& past_profiler_events) override;
Daniel Chenga542fca2014-10-21 09:51:29133 void FinishedReceivingProfilerData() override;
[email protected]4a55a712014-06-08 16:50:34134
[email protected]4b4892b2014-05-22 15:06:15135 // Callbacks for various stages of final log info collection. Do not call
136 // these directly.
ishermanfcf4639e82015-08-26 00:33:49137 void CollectFinalHistograms();
bcwhite81e14852016-08-01 19:30:23138 void MergeHistogramDeltas();
[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
151 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29152 void Observe(int type,
153 const content::NotificationSource& source,
154 const content::NotificationDetails& details) override;
[email protected]d7ea39e2014-05-22 03:59:18155
blundell26b95242015-08-17 10:29:24156 // Called when a URL is opened from the Omnibox.
157 void OnURLOpenedFromOmnibox(OmniboxLog* log);
158
[email protected]e2481a702014-05-23 21:06:50159#if defined(OS_WIN)
160 // Counts (and removes) the browser crash dump attempt signals left behind by
161 // any previous browser processes which generated a crash dump.
162 void CountBrowserCrashDumpAttempts();
163#endif // OS_WIN
164
[email protected]4b4892b2014-05-22 15:06:15165 base::ThreadChecker thread_checker_;
166
[email protected]51994b22014-05-30 13:24:21167 // Weak pointer to the MetricsStateManager.
168 metrics::MetricsStateManager* metrics_state_manager_;
169
[email protected]037642f2014-05-29 20:40:50170 // The MetricsService that |this| is a client of.
dcheng1edb03a2016-04-14 17:20:29171 std::unique_ptr<metrics::MetricsService> metrics_service_;
[email protected]d7ea39e2014-05-22 03:59:18172
173 content::NotificationRegistrar registrar_;
[email protected]d7ea39e2014-05-22 03:59:18174
mlamouriba00ad12016-01-16 15:06:08175#if defined(OS_CHROMEOS)
[email protected]51994b22014-05-30 13:24:21176 // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
177 // that has been registered with MetricsService. On other platforms, is NULL.
178 ChromeOSMetricsProvider* chromeos_metrics_provider_;
mlamouriba00ad12016-01-16 15:06:08179#endif
[email protected]51994b22014-05-30 13:24:21180
ishermanb6705682015-08-29 00:01:00181 // Saved callback received from CollectFinalMetricsForLog().
[email protected]4b4892b2014-05-22 15:06:15182 base::Closure collect_final_metrics_done_callback_;
183
184 // Indicates that collect final metrics step is running.
185 bool waiting_for_collect_final_metrics_step_;
186
187 // Number of async histogram fetch requests in progress.
188 int num_async_histogram_fetches_in_progress_;
189
[email protected]4a55a712014-06-08 16:50:34190 // The ProfilerMetricsProvider instance that was registered with
191 // MetricsService. Has the same lifetime as |metrics_service_|.
gunsch840bc412014-09-18 19:38:06192 metrics::ProfilerMetricsProvider* profiler_metrics_provider_;
[email protected]4a55a712014-06-08 16:50:34193
194#if defined(ENABLE_PLUGINS)
195 // The PluginMetricsProvider instance that was registered with
196 // MetricsService. Has the same lifetime as |metrics_service_|.
197 PluginMetricsProvider* plugin_metrics_provider_;
198#endif
199
200#if defined(OS_WIN)
201 // The GoogleUpdateMetricsProviderWin instance that was registered with
202 // MetricsService. Has the same lifetime as |metrics_service_|.
203 GoogleUpdateMetricsProviderWin* google_update_metrics_provider_;
wfhc768983fa2016-06-08 16:40:33204
manzagopf2322662016-09-27 11:39:59205 // The WatcherMetricsProviderWin instance that was registered with
206 // MetricsService. Has the same lifetime as |metrics_service_|.
207 browser_watcher::WatcherMetricsProviderWin* watcher_metrics_provider_;
208
wfhc768983fa2016-06-08 16:40:33209 // The AntiVirusMetricsProvider instance that was registered with
210 // MetricsService. Has the same lifetime as |metrics_service_|.
211 AntiVirusMetricsProvider* antivirus_metrics_provider_;
[email protected]4a55a712014-06-08 16:50:34212#endif
213
dbeam64c37842015-03-18 02:23:35214 // The DriveMetricsProvider instance that was registered with MetricsService.
215 // Has the same lifetime as |metrics_service_|.
blundellb5c6b5a2015-07-30 20:18:30216 metrics::DriveMetricsProvider* drive_metrics_provider_;
dbeam64c37842015-03-18 02:23:35217
[email protected]4a55a712014-06-08 16:50:34218 // Callback that is called when initial metrics gathering is complete.
ishermanb6705682015-08-29 00:01:00219 base::Closure finished_init_task_callback_;
[email protected]4a55a712014-06-08 16:50:34220
[email protected]8a5b2da2014-07-07 10:56:51221 // The MemoryGrowthTracker instance that tracks memory usage growth in
222 // MemoryDetails.
223 MemoryGrowthTracker memory_growth_tracker_;
224
gunsch7cbdcb22015-03-13 17:02:05225 // Callback to determine whether or not a cellular network is currently being
226 // used.
227 base::Callback<void(bool*)> cellular_callback_;
228
vadimte2de4732015-04-27 21:43:02229 // Time of this object's creation.
230 const base::TimeTicks start_time_;
231
blundell26b95242015-08-17 10:29:24232 // Subscription for receiving callbacks that a URL was opened from the
233 // omnibox.
dcheng1edb03a2016-04-14 17:20:29234 std::unique_ptr<base::CallbackList<void(OmniboxLog*)>::Subscription>
blundell26b95242015-08-17 10:29:24235 omnibox_url_opened_subscription_;
236
isherman213b47c2015-08-28 08:20:46237 // Whether this client has already uploaded profiler data during this session.
238 // Profiler data is uploaded at most once per session.
239 bool has_uploaded_profiler_data_;
240
[email protected]4b4892b2014-05-22 15:06:15241 base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
242
[email protected]5bdaa2d2014-05-19 14:59:51243 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
244};
245
246#endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_