1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <string>
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/basictypes.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/callback.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/weak_ptr.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/threading/thread_checker.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/memory_details.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/metrics/network_stats_uploader.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/metrics_service_client.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/profiler/tracking_synchronizer_observer.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/notification_observer.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/notification_registrar.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ChromeOSMetricsProvider;
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class GoogleUpdateMetricsProviderWin;
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class PluginMetricsProvider;
25f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class PrefRegistrySimple;
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if !defined(OS_CHROMEOS) && !defined(OS_IOS)
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class SigninStatusMetricsProvider;
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)namespace base {
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class FilePath;
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace metrics {
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass MetricsService;
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class MetricsStateManager;
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ProfilerMetricsProvider;
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// that depends on chrome/.
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class ChromeMetricsServiceClient
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : public metrics::MetricsServiceClient,
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      public metrics::TrackingSynchronizerObserver,
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      public content::NotificationObserver {
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~ChromeMetricsServiceClient();
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Factory function.
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static scoped_ptr<ChromeMetricsServiceClient> Create(
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      metrics::MetricsStateManager* state_manager,
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      PrefService* local_state);
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Registers local state prefs used by this class.
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static void RegisterPrefs(PrefRegistrySimple* registry);
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // metrics::MetricsServiceClient:
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void SetMetricsClientId(const std::string& client_id) OVERRIDE;
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool IsOffTheRecordSessionActive() OVERRIDE;
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual std::string GetApplicationLocale() OVERRIDE;
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool GetBrand(std::string* brand_code) OVERRIDE;
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual metrics::SystemProfileProto::Channel GetChannel() OVERRIDE;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual std::string GetVersionString() OVERRIDE;
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnLogUploadComplete() OVERRIDE;
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void StartGatheringMetrics(
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const base::Closure& done_callback) OVERRIDE;
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void CollectFinalMetrics(const base::Closure& done_callback)
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      OVERRIDE;
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual scoped_ptr<metrics::MetricsLogUploader> CreateUploader(
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const std::string& server_url,
7246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const std::string& mime_type,
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const base::Callback<void(int)>& on_upload_complete) OVERRIDE;
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual base::string16 GetRegistryBackupKey() OVERRIDE;
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics::MetricsService* metrics_service() { return metrics_service_.get(); }
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void LogPluginLoadingError(const base::FilePath& plugin_path);
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit ChromeMetricsServiceClient(
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      metrics::MetricsStateManager* state_manager);
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Completes the two-phase initialization of ChromeMetricsServiceClient.
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void Initialize();
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Callback that continues the init task by loading plugin information.
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnInitTaskGotHardwareClass();
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Called after the Plugin init task has been completed that continues the
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // init task by launching a task to gather Google Update statistics.
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnInitTaskGotPluginInfo();
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Called after GoogleUpdate init task has been completed that continues the
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // init task by loading profiler data.
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnInitTaskGotGoogleUpdateData();
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TrackingSynchronizerObserver:
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void ReceivedProfilerData(
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const tracked_objects::ProcessDataSnapshot& process_data,
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      int process_type) OVERRIDE;
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void FinishedReceivingProfilerData() OVERRIDE;
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Callbacks for various stages of final log info collection. Do not call
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // these directly.
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnMemoryDetailCollectionDone();
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnHistogramSynchronizationDone();
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Records metrics about the switches present on the command line.
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void RecordCommandLineMetrics();
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Registers |this| as an observer for notifications which indicate that a
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // user is performing work. This is useful to allow some features to sleep,
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // until the machine becomes active, such as precluding UMA uploads unless
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // there was recent activity.
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void RegisterForNotifications();
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // content::NotificationObserver:
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void Observe(int type,
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       const content::NotificationSource& source,
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE;
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(OS_WIN)
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Counts (and removes) the browser crash dump attempt signals left behind by
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // any previous browser processes which generated a crash dump.
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void CountBrowserCrashDumpAttempts();
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // OS_WIN
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::ThreadChecker thread_checker_;
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Weak pointer to the MetricsStateManager.
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  metrics::MetricsStateManager* metrics_state_manager_;
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The MetricsService that |this| is a client of.
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<metrics::MetricsService> metrics_service_;
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::NotificationRegistrar registrar_;
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // that has been registered with MetricsService. On other platforms, is NULL.
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ChromeOSMetricsProvider* chromeos_metrics_provider_;
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NetworkStatsUploader network_stats_uploader_;
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Saved callback received from CollectFinalMetrics().
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::Closure collect_final_metrics_done_callback_;
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Indicates that collect final metrics step is running.
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool waiting_for_collect_final_metrics_step_;
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Number of async histogram fetch requests in progress.
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int num_async_histogram_fetches_in_progress_;
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
154f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The ProfilerMetricsProvider instance that was registered with
155f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // MetricsService. Has the same lifetime as |metrics_service_|.
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics::ProfilerMetricsProvider* profiler_metrics_provider_;
157f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(ENABLE_PLUGINS)
159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The PluginMetricsProvider instance that was registered with
160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // MetricsService. Has the same lifetime as |metrics_service_|.
161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PluginMetricsProvider* plugin_metrics_provider_;
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
163f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
164f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_WIN)
165f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The GoogleUpdateMetricsProviderWin instance that was registered with
166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // MetricsService. Has the same lifetime as |metrics_service_|.
167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GoogleUpdateMetricsProviderWin* google_update_metrics_provider_;
168f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
169f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
170f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Callback that is called when initial metrics gathering is complete.
171f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::Closure finished_gathering_initial_metrics_callback_;
172f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The MemoryGrowthTracker instance that tracks memory usage growth in
174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // MemoryDetails.
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  MemoryGrowthTracker memory_growth_tracker_;
176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
179cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
183