1 // 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.
5 #ifndef CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
6 #define CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/metrics/metrics_memory_details.h"
16 #include "components/metrics/metrics_service_client.h"
17 #include "components/metrics/profiler/tracking_synchronizer_observer.h"
18 #include "components/omnibox/browser/omnibox_event_global_tracker.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
22 class ChromeOSMetricsProvider
;
23 class GoogleUpdateMetricsProviderWin
;
24 class PluginMetricsProvider
;
25 class PrefRegistrySimple
;
28 #if !defined(OS_CHROMEOS) && !defined(OS_IOS)
29 class SigninStatusMetricsProvider
;
37 class DriveMetricsProvider
;
39 class MetricsStateManager
;
40 class ProfilerMetricsProvider
;
41 } // namespace metrics
43 // ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
44 // that depends on chrome/.
45 class ChromeMetricsServiceClient
46 : public metrics::MetricsServiceClient
,
47 public metrics::TrackingSynchronizerObserver
,
48 public content::NotificationObserver
{
50 ~ChromeMetricsServiceClient() override
;
53 static scoped_ptr
<ChromeMetricsServiceClient
> Create(
54 metrics::MetricsStateManager
* state_manager
,
55 PrefService
* local_state
);
57 // Registers local state prefs used by this class.
58 static void RegisterPrefs(PrefRegistrySimple
* registry
);
60 // metrics::MetricsServiceClient:
61 void SetMetricsClientId(const std::string
& client_id
) override
;
62 void OnRecordingDisabled() override
;
63 bool IsOffTheRecordSessionActive() override
;
64 int32
GetProduct() override
;
65 std::string
GetApplicationLocale() override
;
66 bool GetBrand(std::string
* brand_code
) override
;
67 metrics::SystemProfileProto::Channel
GetChannel() override
;
68 std::string
GetVersionString() override
;
69 void OnLogUploadComplete() override
;
70 void InitializeSystemProfileMetrics(
71 const base::Closure
& done_callback
) override
;
72 void CollectFinalMetricsForLog(const base::Closure
& done_callback
) override
;
73 scoped_ptr
<metrics::MetricsLogUploader
> CreateUploader(
74 const base::Callback
<void(int)>& on_upload_complete
) override
;
75 base::TimeDelta
GetStandardUploadInterval() override
;
76 base::string16
GetRegistryBackupKey() override
;
78 metrics::MetricsService
* metrics_service() { return metrics_service_
.get(); }
80 void LogPluginLoadingError(const base::FilePath
& plugin_path
);
83 explicit ChromeMetricsServiceClient(
84 metrics::MetricsStateManager
* state_manager
);
86 // Completes the two-phase initialization of ChromeMetricsServiceClient.
89 // Callback that continues the init task by loading plugin information.
90 void OnInitTaskGotHardwareClass();
92 // Called after the Plugin init task has been completed that continues the
93 // init task by launching a task to gather Google Update statistics.
94 void OnInitTaskGotPluginInfo();
96 // Called after GoogleUpdate init task has been completed that continues the
97 // init task by loading drive metrics.
98 void OnInitTaskGotGoogleUpdateData();
100 // Called after the drive metrics init task has been completed that continues
101 // the init task by loading profiler data.
102 void OnInitTaskGotDriveMetrics();
104 // Returns true iff profiler data should be included in the next metrics log.
105 // NOTE: This method is probabilistic and also updates internal state as a
106 // side-effect when called, so it should only be called once per log.
107 bool ShouldIncludeProfilerDataInLog();
109 // TrackingSynchronizerObserver:
110 void ReceivedProfilerData(
111 const metrics::ProfilerDataAttributes
& attributes
,
112 const tracked_objects::ProcessDataPhaseSnapshot
& process_data_phase
,
113 const metrics::ProfilerEvents
& past_profiler_events
) override
;
114 void FinishedReceivingProfilerData() override
;
116 // Callbacks for various stages of final log info collection. Do not call
118 void CollectFinalHistograms();
119 void OnMemoryDetailCollectionDone();
120 void OnHistogramSynchronizationDone();
122 // Records metrics about the switches present on the command line.
123 void RecordCommandLineMetrics();
125 // Registers |this| as an observer for notifications which indicate that a
126 // user is performing work. This is useful to allow some features to sleep,
127 // until the machine becomes active, such as precluding UMA uploads unless
128 // there was recent activity.
129 void RegisterForNotifications();
131 // content::NotificationObserver:
132 void Observe(int type
,
133 const content::NotificationSource
& source
,
134 const content::NotificationDetails
& details
) override
;
136 // Called when a URL is opened from the Omnibox.
137 void OnURLOpenedFromOmnibox(OmniboxLog
* log
);
140 // Counts (and removes) the browser crash dump attempt signals left behind by
141 // any previous browser processes which generated a crash dump.
142 void CountBrowserCrashDumpAttempts();
145 base::ThreadChecker thread_checker_
;
147 // Weak pointer to the MetricsStateManager.
148 metrics::MetricsStateManager
* metrics_state_manager_
;
150 // The MetricsService that |this| is a client of.
151 scoped_ptr
<metrics::MetricsService
> metrics_service_
;
153 content::NotificationRegistrar registrar_
;
155 // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
156 // that has been registered with MetricsService. On other platforms, is NULL.
157 ChromeOSMetricsProvider
* chromeos_metrics_provider_
;
159 // Saved callback received from CollectFinalMetricsForLog().
160 base::Closure collect_final_metrics_done_callback_
;
162 // Indicates that collect final metrics step is running.
163 bool waiting_for_collect_final_metrics_step_
;
165 // Number of async histogram fetch requests in progress.
166 int num_async_histogram_fetches_in_progress_
;
168 // The ProfilerMetricsProvider instance that was registered with
169 // MetricsService. Has the same lifetime as |metrics_service_|.
170 metrics::ProfilerMetricsProvider
* profiler_metrics_provider_
;
172 #if defined(ENABLE_PLUGINS)
173 // The PluginMetricsProvider instance that was registered with
174 // MetricsService. Has the same lifetime as |metrics_service_|.
175 PluginMetricsProvider
* plugin_metrics_provider_
;
179 // The GoogleUpdateMetricsProviderWin instance that was registered with
180 // MetricsService. Has the same lifetime as |metrics_service_|.
181 GoogleUpdateMetricsProviderWin
* google_update_metrics_provider_
;
184 // The DriveMetricsProvider instance that was registered with MetricsService.
185 // Has the same lifetime as |metrics_service_|.
186 metrics::DriveMetricsProvider
* drive_metrics_provider_
;
188 // Callback that is called when initial metrics gathering is complete.
189 base::Closure finished_init_task_callback_
;
191 // The MemoryGrowthTracker instance that tracks memory usage growth in
193 MemoryGrowthTracker memory_growth_tracker_
;
195 // Callback to determine whether or not a cellular network is currently being
197 base::Callback
<void(bool*)> cellular_callback_
;
199 // Time of this object's creation.
200 const base::TimeTicks start_time_
;
202 // Subscription for receiving callbacks that a URL was opened from the
204 scoped_ptr
<base::CallbackList
<void(OmniboxLog
*)>::Subscription
>
205 omnibox_url_opened_subscription_
;
207 // Whether this client has already uploaded profiler data during this session.
208 // Profiler data is uploaded at most once per session.
209 bool has_uploaded_profiler_data_
;
211 base::WeakPtrFactory
<ChromeMetricsServiceClient
> weak_ptr_factory_
;
213 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient
);
216 #endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_