chrome_metrics_service_client.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#ifndef CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
6#define CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
7
8#include <string>
9
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/network_stats_uploader.h"
16#include "components/metrics/metrics_service_client.h"
17#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
19
20class ChromeOSMetricsProvider;
21class MetricsService;
22
23namespace metrics {
24class MetricsStateManager;
25}
26
27// ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
28// that depends on chrome/.
29class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
30                                   public content::NotificationObserver {
31 public:
32  virtual ~ChromeMetricsServiceClient();
33
34  // Factory function.
35  static scoped_ptr<ChromeMetricsServiceClient> Create(
36      metrics::MetricsStateManager* state_manager,
37      PrefService* local_state);
38
39  // metrics::MetricsServiceClient:
40  virtual void SetClientID(const std::string& client_id) OVERRIDE;
41  virtual bool IsOffTheRecordSessionActive() OVERRIDE;
42  virtual std::string GetApplicationLocale() OVERRIDE;
43  virtual bool GetBrand(std::string* brand_code) OVERRIDE;
44  virtual metrics::SystemProfileProto::Channel GetChannel() OVERRIDE;
45  virtual std::string GetVersionString() OVERRIDE;
46  virtual int64 GetInstallDate() OVERRIDE;
47  virtual void OnLogUploadComplete() OVERRIDE;
48  virtual void StartGatheringMetrics(
49      const base::Closure& done_callback) OVERRIDE;
50  virtual void CollectFinalMetrics(const base::Closure& done_callback)
51      OVERRIDE;
52  virtual scoped_ptr<metrics::MetricsLogUploader> CreateUploader(
53      const std::string& server_url,
54      const std::string& mime_type,
55      const base::Callback<void(int)>& on_upload_complete) OVERRIDE;
56
57  MetricsService* metrics_service() { return metrics_service_.get(); }
58
59 private:
60  explicit ChromeMetricsServiceClient(
61      metrics::MetricsStateManager* state_manager);
62
63  // Completes the two-phase initialization of ChromeMetricsServiceClient.
64  void Initialize();
65
66  // Callbacks for various stages of final log info collection. Do not call
67  // these directly.
68  void OnMemoryDetailCollectionDone();
69  void OnHistogramSynchronizationDone();
70
71  // Records metrics about the switches present on the command line.
72  void RecordCommandLineMetrics();
73
74  // Registers |this| as an observer for notifications which indicate that a
75  // user is performing work. This is useful to allow some features to sleep,
76  // until the machine becomes active, such as precluding UMA uploads unless
77  // there was recent activity.
78  void RegisterForNotifications();
79
80  // content::NotificationObserver:
81  virtual void Observe(int type,
82                       const content::NotificationSource& source,
83                       const content::NotificationDetails& details) OVERRIDE;
84
85#if defined(OS_WIN)
86  // Counts (and removes) the browser crash dump attempt signals left behind by
87  // any previous browser processes which generated a crash dump.
88  void CountBrowserCrashDumpAttempts();
89#endif  // OS_WIN
90
91  base::ThreadChecker thread_checker_;
92
93  // Weak pointer to the MetricsStateManager.
94  metrics::MetricsStateManager* metrics_state_manager_;
95
96  // The MetricsService that |this| is a client of.
97  scoped_ptr<MetricsService> metrics_service_;
98
99  content::NotificationRegistrar registrar_;
100
101  // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
102  // that has been registered with MetricsService. On other platforms, is NULL.
103  ChromeOSMetricsProvider* chromeos_metrics_provider_;
104
105  NetworkStatsUploader network_stats_uploader_;
106
107  // Saved callback received from CollectFinalMetrics().
108  base::Closure collect_final_metrics_done_callback_;
109
110  // Indicates that collect final metrics step is running.
111  bool waiting_for_collect_final_metrics_step_;
112
113  // Number of async histogram fetch requests in progress.
114  int num_async_histogram_fetches_in_progress_;
115
116  base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
117
118  DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
119};
120
121#endif  // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
122