blob: de354d8f27f7a98c93f45e87b86974b00e9aafb5 [file] [log] [blame]
[email protected]5bdaa2d2014-05-19 14:59:511// 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
avi6846aef2015-12-26 01:09:388#include <stdint.h>
9
dcheng1edb03a2016-04-14 17:20:2910#include <memory>
huangse0fa6fd2016-11-04 20:41:1011#include <queue>
[email protected]5bdaa2d2014-05-19 14:59:5112#include <string>
13
[email protected]4b4892b2014-05-22 15:06:1514#include "base/callback.h"
Brett Wilson275a1372017-09-01 20:27:5415#include "base/containers/circular_deque.h"
avi6846aef2015-12-26 01:09:3816#include "base/macros.h"
[email protected]4b4892b2014-05-22 15:06:1517#include "base/memory/weak_ptr.h"
isherman8c542b72017-06-28 22:40:2618#include "base/sequence_checker.h"
avi6846aef2015-12-26 01:09:3819#include "build/build_config.h"
asvitkine89406d1f2015-01-17 06:57:1020#include "chrome/browser/metrics/metrics_memory_details.h"
rkaplow9c420822017-02-24 15:29:5721#include "components/metrics/metrics_log_uploader.h"
[email protected]5bdaa2d2014-05-19 14:59:5122#include "components/metrics/metrics_service_client.h"
blundell26b95242015-08-17 10:29:2423#include "components/omnibox/browser/omnibox_event_global_tracker.h"
holte7b74c622017-01-23 23:13:0724#include "components/ukm/observers/history_delete_observer.h"
holte1334c0aa2017-02-09 22:52:4125#include "components/ukm/observers/sync_disable_observer.h"
[email protected]d7ea39e2014-05-22 03:59:1826#include "content/public/browser/notification_observer.h"
27#include "content/public/browser/notification_registrar.h"
brettw4b461082016-11-19 18:55:1628#include "ppapi/features/features.h"
Steven Holtef9d5ed62017-10-21 02:02:3029#include "third_party/metrics_proto/system_profile.pb.h"
[email protected]d7ea39e2014-05-22 03:59:1830
[email protected]4a55a712014-06-08 16:50:3431class PluginMetricsProvider;
holte7b74c622017-01-23 23:13:0732class Profile;
[email protected]4a55a712014-06-08 16:50:3433class PrefRegistrySimple;
[email protected]7aee4f7e2014-08-12 01:15:0334
Steven Holte1afaeb7f2017-07-13 01:40:5135#if defined(OS_ANDROID)
36class TabModelListObserver;
37#endif // defined(OS_ANDROID)
38
manzagopf2322662016-09-27 11:39:5939
[email protected]037642f2014-05-29 20:40:5040namespace metrics {
asvitkinecbd420732014-08-26 22:15:4041class MetricsService;
[email protected]037642f2014-05-29 20:40:5042class MetricsStateManager;
siggi5e62f7e2014-11-21 21:55:4543} // namespace metrics
[email protected]037642f2014-05-29 20:40:5044
[email protected]5bdaa2d2014-05-19 14:59:5145// ChromeMetricsServiceClient provides an implementation of MetricsServiceClient
46// that depends on chrome/.
holte7b74c622017-01-23 23:13:0747class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
holte7b74c622017-01-23 23:13:0748 public content::NotificationObserver,
holte1334c0aa2017-02-09 22:52:4149 public ukm::HistoryDeleteObserver,
50 public ukm::SyncDisableObserver {
[email protected]5bdaa2d2014-05-19 14:59:5151 public:
Daniel Chenga542fca2014-10-21 09:51:2952 ~ChromeMetricsServiceClient() override;
[email protected]5bdaa2d2014-05-19 14:59:5153
[email protected]037642f2014-05-29 20:40:5054 // Factory function.
dcheng1edb03a2016-04-14 17:20:2955 static std::unique_ptr<ChromeMetricsServiceClient> Create(
calvimei5b30e0d2016-08-17 21:24:3356 metrics::MetricsStateManager* state_manager);
[email protected]037642f2014-05-29 20:40:5057
[email protected]4a55a712014-06-08 16:50:3458 // Registers local state prefs used by this class.
59 static void RegisterPrefs(PrefRegistrySimple* registry);
60
holte8d28e6692017-05-04 23:50:4361 // Checks if the user has forced metrics collection on via the override flag.
62 static bool IsMetricsReportingForceEnabled();
63
[email protected]5bdaa2d2014-05-19 14:59:5164 // metrics::MetricsServiceClient:
blundellfecea528d2015-10-21 10:10:2265 metrics::MetricsService* GetMetricsService() override;
holte7b74c622017-01-23 23:13:0766 ukm::UkmService* GetUkmService() override;
Daniel Chenga542fca2014-10-21 09:51:2967 void SetMetricsClientId(const std::string& client_id) override;
avi6846aef2015-12-26 01:09:3868 int32_t GetProduct() override;
Daniel Chenga542fca2014-10-21 09:51:2969 std::string GetApplicationLocale() override;
70 bool GetBrand(std::string* brand_code) override;
71 metrics::SystemProfileProto::Channel GetChannel() override;
72 std::string GetVersionString() override;
manzagopa5d6688d2016-10-25 20:16:0373 void OnEnvironmentUpdate(std::string* serialized_environment) override;
manzagop14aff5d2016-11-23 17:27:0074 void OnLogCleanShutdown() override;
ishermanb6705682015-08-29 00:01:0075 void CollectFinalMetricsForLog(const base::Closure& done_callback) override;
dcheng1edb03a2016-04-14 17:20:2976 std::unique_ptr<metrics::MetricsLogUploader> CreateUploader(
holte4ae63f52017-03-08 00:25:0877 base::StringPiece server_url,
78 base::StringPiece mime_type,
rkaplow9c420822017-02-24 15:29:5779 metrics::MetricsLogUploader::MetricServiceType service_type,
holte035ec7fb2017-04-04 20:16:5980 const metrics::MetricsLogUploader::UploadCallback& on_upload_complete)
81 override;
gunsch7cbdcb22015-03-13 17:02:0582 base::TimeDelta GetStandardUploadInterval() override;
blundellfecea528d2015-10-21 10:10:2283 void OnPluginLoadingError(const base::FilePath& plugin_path) override;
jwd421086f2016-03-21 14:40:4284 bool IsReportingPolicyManaged() override;
gayanedaaf3a02016-06-15 16:30:2185 metrics::EnableMetricsDefault GetMetricsReportingDefaultState() override;
gayane0b46091c2016-04-07 21:01:0586 bool IsUMACellularUploadLogicEnabled() override;
holte08137d792017-02-15 21:43:5687 bool IsHistorySyncEnabledOnAllProfiles() override;
[email protected]4a55a712014-06-08 16:50:3488
holte1334c0aa2017-02-09 22:52:4189 // ukm::HistoryDeleteObserver:
holte7b74c622017-01-23 23:13:0790 void OnHistoryDeleted() override;
91
holte1334c0aa2017-02-09 22:52:4192 // ukm::SyncDisableObserver:
93 void OnSyncPrefsChanged(bool must_purge) override;
94
bcwhite374d5fe2016-05-20 17:03:2495 // Persistent browser metrics need to be persisted somewhere. This constant
96 // provides a known string to be used for both the allocator's internal name
97 // and for a file on disk (relative to chrome::DIR_USER_DATA) to which they
98 // can be saved.
99 static const char kBrowserMetricsName[];
100
[email protected]5bdaa2d2014-05-19 14:59:51101 private:
[email protected]037642f2014-05-29 20:40:50102 explicit ChromeMetricsServiceClient(
103 metrics::MetricsStateManager* state_manager);
104
[email protected]51994b22014-05-30 13:24:21105 // Completes the two-phase initialization of ChromeMetricsServiceClient.
106 void Initialize();
107
rkaplowb8d63f432017-02-06 19:00:42108 // Registers providers to the MetricsService. These provide data from
109 // alternate sources.
110 void RegisterMetricsServiceProviders();
111
112 // Registers providers to the UkmService. These provide data from alternate
113 // sources.
114 void RegisterUKMProviders();
115
isherman213b47c2015-08-28 08:20:46116 // Returns true iff profiler data should be included in the next metrics log.
117 // NOTE: This method is probabilistic and also updates internal state as a
118 // side-effect when called, so it should only be called once per log.
119 bool ShouldIncludeProfilerDataInLog();
120
[email protected]4b4892b2014-05-22 15:06:15121 // Callbacks for various stages of final log info collection. Do not call
122 // these directly.
ishermanfcf4639e82015-08-26 00:33:49123 void CollectFinalHistograms();
[email protected]4b4892b2014-05-22 15:06:15124 void OnMemoryDetailCollectionDone();
125 void OnHistogramSynchronizationDone();
126
[email protected]daed87e2014-05-22 19:41:22127 // Records metrics about the switches present on the command line.
128 void RecordCommandLineMetrics();
129
[email protected]d7ea39e2014-05-22 03:59:18130 // Registers |this| as an observer for notifications which indicate that a
131 // user is performing work. This is useful to allow some features to sleep,
132 // until the machine becomes active, such as precluding UMA uploads unless
133 // there was recent activity.
134 void RegisterForNotifications();
135
holte1334c0aa2017-02-09 22:52:41136 // Call to listen for events on the selected profile's services.
137 void RegisterForProfileEvents(Profile* profile);
holte7b74c622017-01-23 23:13:07138
[email protected]d7ea39e2014-05-22 03:59:18139 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29140 void Observe(int type,
141 const content::NotificationSource& source,
142 const content::NotificationDetails& details) override;
[email protected]d7ea39e2014-05-22 03:59:18143
blundell26b95242015-08-17 10:29:24144 // Called when a URL is opened from the Omnibox.
145 void OnURLOpenedFromOmnibox(OmniboxLog* log);
146
[email protected]e2481a702014-05-23 21:06:50147#if defined(OS_WIN)
148 // Counts (and removes) the browser crash dump attempt signals left behind by
149 // any previous browser processes which generated a crash dump.
150 void CountBrowserCrashDumpAttempts();
151#endif // OS_WIN
152
isherman8c542b72017-06-28 22:40:26153 SEQUENCE_CHECKER(sequence_checker_);
[email protected]4b4892b2014-05-22 15:06:15154
[email protected]51994b22014-05-30 13:24:21155 // Weak pointer to the MetricsStateManager.
156 metrics::MetricsStateManager* metrics_state_manager_;
157
[email protected]037642f2014-05-29 20:40:50158 // The MetricsService that |this| is a client of.
dcheng1edb03a2016-04-14 17:20:29159 std::unique_ptr<metrics::MetricsService> metrics_service_;
[email protected]d7ea39e2014-05-22 03:59:18160
holte7b74c622017-01-23 23:13:07161 // The UkmService that |this| is a client of.
162 std::unique_ptr<ukm::UkmService> ukm_service_;
163
[email protected]d7ea39e2014-05-22 03:59:18164 content::NotificationRegistrar registrar_;
[email protected]d7ea39e2014-05-22 03:59:18165
Steven Holte1afaeb7f2017-07-13 01:40:51166#if defined(OS_ANDROID)
167 // Listener for changes in incognito activity.
168 // Desktop platform use BrowserList, and can listen for
169 // chrome::NOTIFICATION_BROWSER_OPENED instead.
170 std::unique_ptr<TabModelListObserver> incognito_observer_;
171#endif // defined(OS_ANDROID)
172
huangse0fa6fd2016-11-04 20:41:10173 // A queue of tasks for initial metrics gathering. These may be asynchronous
174 // or synchronous.
Brett Wilson275a1372017-09-01 20:27:54175 base::circular_deque<base::Closure> initialize_task_queue_;
huangse0fa6fd2016-11-04 20:41:10176
ishermanb6705682015-08-29 00:01:00177 // Saved callback received from CollectFinalMetricsForLog().
[email protected]4b4892b2014-05-22 15:06:15178 base::Closure collect_final_metrics_done_callback_;
179
180 // Indicates that collect final metrics step is running.
181 bool waiting_for_collect_final_metrics_step_;
182
183 // Number of async histogram fetch requests in progress.
184 int num_async_histogram_fetches_in_progress_;
185
brettw4b461082016-11-19 18:55:16186#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]4a55a712014-06-08 16:50:34187 // The PluginMetricsProvider instance that was registered with
188 // MetricsService. Has the same lifetime as |metrics_service_|.
189 PluginMetricsProvider* plugin_metrics_provider_;
190#endif
191
[email protected]8a5b2da2014-07-07 10:56:51192 // The MemoryGrowthTracker instance that tracks memory usage growth in
193 // MemoryDetails.
194 MemoryGrowthTracker memory_growth_tracker_;
195
gunsch7cbdcb22015-03-13 17:02:05196 // Callback to determine whether or not a cellular network is currently being
197 // used.
198 base::Callback<void(bool*)> cellular_callback_;
199
blundell26b95242015-08-17 10:29:24200 // Subscription for receiving callbacks that a URL was opened from the
201 // omnibox.
dcheng1edb03a2016-04-14 17:20:29202 std::unique_ptr<base::CallbackList<void(OmniboxLog*)>::Subscription>
blundell26b95242015-08-17 10:29:24203 omnibox_url_opened_subscription_;
204
[email protected]4b4892b2014-05-22 15:06:15205 base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_;
206
[email protected]5bdaa2d2014-05-19 14:59:51207 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient);
208};
209
210#endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_