chrome_metrics_service_client.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 void OnLogUploadComplete() OVERRIDE;
47  virtual void StartGatheringMetrics(
48      const base::Closure& done_callback) OVERRIDE;
49  virtual void CollectFinalMetrics(const base::Closure& done_callback)
50      OVERRIDE;
51
52  MetricsService* metrics_service() { return metrics_service_.get(); }
53
54 private:
55  explicit ChromeMetricsServiceClient(
56      metrics::MetricsStateManager* state_manager);
57
58  // Completes the two-phase initialization of ChromeMetricsServiceClient.
59  void Initialize();
60
61  // Callbacks for various stages of final log info collection. Do not call
62  // these directly.
63  void OnMemoryDetailCollectionDone();
64  void OnHistogramSynchronizationDone();
65
66  // Records metrics about the switches present on the command line.
67  void RecordCommandLineMetrics();
68
69  // Registers |this| as an observer for notifications which indicate that a
70  // user is performing work. This is useful to allow some features to sleep,
71  // until the machine becomes active, such as precluding UMA uploads unless
72  // there was recent activity.
73  void RegisterForNotifications();
74
75  // content::NotificationObserver:
76  virtual void Observe(int type,
77                       const content::NotificationSource& source,
78                       const content::NotificationDetails& details) OVERRIDE;
79
80#if defined(OS_WIN)
81  // Counts (and removes) the browser crash dump attempt signals left behind by
82  // any previous browser processes which generated a crash dump.
83  void CountBrowserCrashDumpAttempts();
84#endif  // OS_WIN
85
86  base::ThreadChecker thread_checker_;
87
88  // Weak pointer to the MetricsStateManager.
89  metrics::MetricsStateManager* metrics_state_manager_;
90
91  // The MetricsService that |this| is a client of.
92  scoped_ptr<MetricsService> metrics_service_;
93
94  content::NotificationRegistrar registrar_;
95
96  // On ChromeOS, holds a weak pointer to the ChromeOSMetricsProvider instance
97  // that has been registered with MetricsService. On other platforms, is NULL.
98  ChromeOSMetricsProvider* chromeos_metrics_provider_;
99
100  NetworkStatsUploader network_stats_uploader_;
101
102  // Saved callback received from CollectFinalMetrics().
103  base::Closure collect_final_metrics_done_callback_;
104
105  // Indicates that collect final metrics step is running.
106  bool waiting_for_collect_final_metrics_step_;
107
108  // Number of async histogram fetch requests in progress.
109  int num_async_histogram_fetches_in_progress_;
110
111  base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
112
113  DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
114};
115
116#endif  // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_
117