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/containers/scoped_ptr_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "chrome/browser/metrics/metrics_memory_details.h"
17 #include "components/metrics/metrics_service_client.h"
18 #include "components/metrics/profiler/tracking_synchronizer_observer.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
;
27 class ProcessResourceUsage
;
29 #if !defined(OS_CHROMEOS) && !defined(OS_IOS)
30 class SigninStatusMetricsProvider
;
38 class DriveMetricsProvider
;
40 class MetricsStateManager
;
41 class ProfilerMetricsProvider
;
42 } // namespace metrics
44 // ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
45 // that depends on chrome/.
46 class ChromeMetricsServiceClient
47 : public metrics::MetricsServiceClient
,
48 public metrics::TrackingSynchronizerObserver
,
49 public content::NotificationObserver
{
51 ~ChromeMetricsServiceClient() override
;
54 static scoped_ptr
<ChromeMetricsServiceClient
> Create(
55 metrics::MetricsStateManager
* state_manager
,
56 PrefService
* local_state
);
58 // Registers local state prefs used by this class.
59 static void RegisterPrefs(PrefRegistrySimple
* registry
);
61 // metrics::MetricsServiceClient:
62 void SetMetricsClientId(const std::string
& client_id
) override
;
63 void OnRecordingDisabled() override
;
64 bool IsOffTheRecordSessionActive() override
;
65 int32
GetProduct() override
;
66 std::string
GetApplicationLocale() override
;
67 bool GetBrand(std::string
* brand_code
) override
;
68 metrics::SystemProfileProto::Channel
GetChannel() override
;
69 std::string
GetVersionString() override
;
70 void OnLogUploadComplete() override
;
71 void StartGatheringMetrics(const base::Closure
& done_callback
) override
;
72 void CollectFinalMetrics(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 profiler data.
98 void OnInitTaskGotGoogleUpdateData();
100 // Called after WebCache statistics have been received from a renderer
102 void OnWebCacheStatsRefresh(int host_id
);
104 // TrackingSynchronizerObserver:
105 void ReceivedProfilerData(
106 const metrics::ProfilerDataAttributes
& attributes
,
107 const tracked_objects::ProcessDataPhaseSnapshot
& process_data_phase
,
108 const metrics::ProfilerEvents
& past_profiler_events
) override
;
109 void FinishedReceivingProfilerData() override
;
111 // Callbacks for various stages of final log info collection. Do not call
113 void OnMemoryDetailCollectionDone();
114 void OnHistogramSynchronizationDone();
116 // Records metrics about the switches present on the command line.
117 void RecordCommandLineMetrics();
119 // Registers |this| as an observer for notifications which indicate that a
120 // user is performing work. This is useful to allow some features to sleep,
121 // until the machine becomes active, such as precluding UMA uploads unless
122 // there was recent activity.
123 void RegisterForNotifications();
125 // content::NotificationObserver:
126 void Observe(int type
,
127 const content::NotificationSource
& source
,
128 const content::NotificationDetails
& details
) override
;
131 // Counts (and removes) the browser crash dump attempt signals left behind by
132 // any previous browser processes which generated a crash dump.
133 void CountBrowserCrashDumpAttempts();
136 base::ThreadChecker thread_checker_
;
138 // Weak pointer to the MetricsStateManager.
139 metrics::MetricsStateManager
* metrics_state_manager_
;
141 // The MetricsService that |this| is a client of.
142 scoped_ptr
<metrics::MetricsService
> metrics_service_
;
144 content::NotificationRegistrar registrar_
;
146 // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
147 // that has been registered with MetricsService. On other platforms, is NULL.
148 ChromeOSMetricsProvider
* chromeos_metrics_provider_
;
150 // Saved callback received from CollectFinalMetrics().
151 base::Closure collect_final_metrics_done_callback_
;
153 // Indicates that collect final metrics step is running.
154 bool waiting_for_collect_final_metrics_step_
;
156 // Number of async histogram fetch requests in progress.
157 int num_async_histogram_fetches_in_progress_
;
159 // The ProfilerMetricsProvider instance that was registered with
160 // MetricsService. Has the same lifetime as |metrics_service_|.
161 metrics::ProfilerMetricsProvider
* profiler_metrics_provider_
;
163 #if defined(ENABLE_PLUGINS)
164 // The PluginMetricsProvider instance that was registered with
165 // MetricsService. Has the same lifetime as |metrics_service_|.
166 PluginMetricsProvider
* plugin_metrics_provider_
;
170 // The GoogleUpdateMetricsProviderWin instance that was registered with
171 // MetricsService. Has the same lifetime as |metrics_service_|.
172 GoogleUpdateMetricsProviderWin
* google_update_metrics_provider_
;
175 // The DriveMetricsProvider instance that was registered with MetricsService.
176 // Has the same lifetime as |metrics_service_|.
177 metrics::DriveMetricsProvider
* drive_metrics_provider_
;
179 // Callback that is called when initial metrics gathering is complete.
180 base::Closure finished_gathering_initial_metrics_callback_
;
182 // The MemoryGrowthTracker instance that tracks memory usage growth in
184 MemoryGrowthTracker memory_growth_tracker_
;
186 // Callback to determine whether or not a cellular network is currently being
188 base::Callback
<void(bool*)> cellular_callback_
;
190 // Time of this object's creation.
191 const base::TimeTicks start_time_
;
193 // Map of ProcessResourceUsage from render process host IDs.
194 base::ScopedPtrMap
<int, scoped_ptr
<ProcessResourceUsage
>>
195 host_resource_usage_map_
;
197 base::WeakPtrFactory
<ChromeMetricsServiceClient
> weak_ptr_factory_
;
199 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient
);
202 #endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_