[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 1edb03a | 2016-04-14 17:20:29 | [diff] [blame] | 10 | #include <memory> |
huangs | e0fa6fd | 2016-11-04 20:41:10 | [diff] [blame] | 11 | #include <queue> |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 12 | #include <string> |
| 13 | |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 14 | #include "base/callback.h" |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 15 | #include "base/containers/circular_deque.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
isherman | 8c542b7 | 2017-06-28 22:40:26 | [diff] [blame] | 18 | #include "base/sequence_checker.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 19 | #include "build/build_config.h" |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 20 | #include "chrome/browser/metrics/metrics_memory_details.h" |
rkaplow | 9c42082 | 2017-02-24 15:29:57 | [diff] [blame] | 21 | #include "components/metrics/metrics_log_uploader.h" |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 22 | #include "components/metrics/metrics_service_client.h" |
blundell | 26b9524 | 2015-08-17 10:29:24 | [diff] [blame] | 23 | #include "components/omnibox/browser/omnibox_event_global_tracker.h" |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 24 | #include "components/ukm/observers/history_delete_observer.h" |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 25 | #include "components/ukm/observers/sync_disable_observer.h" |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 26 | #include "content/public/browser/notification_observer.h" |
| 27 | #include "content/public/browser/notification_registrar.h" |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 28 | #include "ppapi/features/features.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame^] | 29 | #include "third_party/metrics_proto/system_profile.pb.h" |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 30 | |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 31 | class PluginMetricsProvider; |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 32 | class Profile; |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 33 | class PrefRegistrySimple; |
[email protected] | 7aee4f7e | 2014-08-12 01:15:03 | [diff] [blame] | 34 | |
Steven Holte | 1afaeb7f | 2017-07-13 01:40:51 | [diff] [blame] | 35 | #if defined(OS_ANDROID) |
| 36 | class TabModelListObserver; |
| 37 | #endif // defined(OS_ANDROID) |
| 38 | |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 39 | |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 40 | namespace metrics { |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 41 | class MetricsService; |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 42 | class MetricsStateManager; |
siggi | 5e62f7e | 2014-11-21 21:55:45 | [diff] [blame] | 43 | } // namespace metrics |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 44 | |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 45 | // ChromeMetricsServiceClient provides an implementation of MetricsServiceClient |
| 46 | // that depends on chrome/. |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 47 | class ChromeMetricsServiceClient : public metrics::MetricsServiceClient, |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 48 | public content::NotificationObserver, |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 49 | public ukm::HistoryDeleteObserver, |
| 50 | public ukm::SyncDisableObserver { |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 51 | public: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 52 | ~ChromeMetricsServiceClient() override; |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 53 | |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 54 | // Factory function. |
dcheng | 1edb03a | 2016-04-14 17:20:29 | [diff] [blame] | 55 | static std::unique_ptr<ChromeMetricsServiceClient> Create( |
calvimei | 5b30e0d | 2016-08-17 21:24:33 | [diff] [blame] | 56 | metrics::MetricsStateManager* state_manager); |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 57 | |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 58 | // Registers local state prefs used by this class. |
| 59 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 60 | |
holte | 8d28e669 | 2017-05-04 23:50:43 | [diff] [blame] | 61 | // Checks if the user has forced metrics collection on via the override flag. |
| 62 | static bool IsMetricsReportingForceEnabled(); |
| 63 | |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 64 | // metrics::MetricsServiceClient: |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 65 | metrics::MetricsService* GetMetricsService() override; |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 66 | ukm::UkmService* GetUkmService() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 67 | void SetMetricsClientId(const std::string& client_id) override; |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 68 | int32_t GetProduct() override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 69 | std::string GetApplicationLocale() override; |
| 70 | bool GetBrand(std::string* brand_code) override; |
| 71 | metrics::SystemProfileProto::Channel GetChannel() override; |
| 72 | std::string GetVersionString() override; |
manzagop | a5d6688d | 2016-10-25 20:16:03 | [diff] [blame] | 73 | void OnEnvironmentUpdate(std::string* serialized_environment) override; |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame] | 74 | void OnLogCleanShutdown() override; |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 75 | void CollectFinalMetricsForLog(const base::Closure& done_callback) override; |
dcheng | 1edb03a | 2016-04-14 17:20:29 | [diff] [blame] | 76 | std::unique_ptr<metrics::MetricsLogUploader> CreateUploader( |
holte | 4ae63f5 | 2017-03-08 00:25:08 | [diff] [blame] | 77 | base::StringPiece server_url, |
| 78 | base::StringPiece mime_type, |
rkaplow | 9c42082 | 2017-02-24 15:29:57 | [diff] [blame] | 79 | metrics::MetricsLogUploader::MetricServiceType service_type, |
holte | 035ec7fb | 2017-04-04 20:16:59 | [diff] [blame] | 80 | const metrics::MetricsLogUploader::UploadCallback& on_upload_complete) |
| 81 | override; |
gunsch | 7cbdcb2 | 2015-03-13 17:02:05 | [diff] [blame] | 82 | base::TimeDelta GetStandardUploadInterval() override; |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 83 | void OnPluginLoadingError(const base::FilePath& plugin_path) override; |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 84 | bool IsReportingPolicyManaged() override; |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 85 | metrics::EnableMetricsDefault GetMetricsReportingDefaultState() override; |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 86 | bool IsUMACellularUploadLogicEnabled() override; |
holte | 08137d79 | 2017-02-15 21:43:56 | [diff] [blame] | 87 | bool IsHistorySyncEnabledOnAllProfiles() override; |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 88 | |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 89 | // ukm::HistoryDeleteObserver: |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 90 | void OnHistoryDeleted() override; |
| 91 | |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 92 | // ukm::SyncDisableObserver: |
| 93 | void OnSyncPrefsChanged(bool must_purge) override; |
| 94 | |
bcwhite | 374d5fe | 2016-05-20 17:03:24 | [diff] [blame] | 95 | // 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] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 101 | private: |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 102 | explicit ChromeMetricsServiceClient( |
| 103 | metrics::MetricsStateManager* state_manager); |
| 104 | |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 105 | // Completes the two-phase initialization of ChromeMetricsServiceClient. |
| 106 | void Initialize(); |
| 107 | |
rkaplow | b8d63f43 | 2017-02-06 19:00:42 | [diff] [blame] | 108 | // 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 | |
isherman | 213b47c | 2015-08-28 08:20:46 | [diff] [blame] | 116 | // 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] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 121 | // Callbacks for various stages of final log info collection. Do not call |
| 122 | // these directly. |
isherman | fcf4639e8 | 2015-08-26 00:33:49 | [diff] [blame] | 123 | void CollectFinalHistograms(); |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 124 | void OnMemoryDetailCollectionDone(); |
| 125 | void OnHistogramSynchronizationDone(); |
| 126 | |
[email protected] | daed87e | 2014-05-22 19:41:22 | [diff] [blame] | 127 | // Records metrics about the switches present on the command line. |
| 128 | void RecordCommandLineMetrics(); |
| 129 | |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 130 | // 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 | |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 136 | // Call to listen for events on the selected profile's services. |
| 137 | void RegisterForProfileEvents(Profile* profile); |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 138 | |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 139 | // content::NotificationObserver: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 140 | void Observe(int type, |
| 141 | const content::NotificationSource& source, |
| 142 | const content::NotificationDetails& details) override; |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 143 | |
blundell | 26b9524 | 2015-08-17 10:29:24 | [diff] [blame] | 144 | // Called when a URL is opened from the Omnibox. |
| 145 | void OnURLOpenedFromOmnibox(OmniboxLog* log); |
| 146 | |
[email protected] | e2481a70 | 2014-05-23 21:06:50 | [diff] [blame] | 147 | #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 | |
isherman | 8c542b7 | 2017-06-28 22:40:26 | [diff] [blame] | 153 | SEQUENCE_CHECKER(sequence_checker_); |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 154 | |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 155 | // Weak pointer to the MetricsStateManager. |
| 156 | metrics::MetricsStateManager* metrics_state_manager_; |
| 157 | |
[email protected] | 037642f | 2014-05-29 20:40:50 | [diff] [blame] | 158 | // The MetricsService that |this| is a client of. |
dcheng | 1edb03a | 2016-04-14 17:20:29 | [diff] [blame] | 159 | std::unique_ptr<metrics::MetricsService> metrics_service_; |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 160 | |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 161 | // The UkmService that |this| is a client of. |
| 162 | std::unique_ptr<ukm::UkmService> ukm_service_; |
| 163 | |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 164 | content::NotificationRegistrar registrar_; |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 165 | |
Steven Holte | 1afaeb7f | 2017-07-13 01:40:51 | [diff] [blame] | 166 | #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 | |
huangs | e0fa6fd | 2016-11-04 20:41:10 | [diff] [blame] | 173 | // A queue of tasks for initial metrics gathering. These may be asynchronous |
| 174 | // or synchronous. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 175 | base::circular_deque<base::Closure> initialize_task_queue_; |
huangs | e0fa6fd | 2016-11-04 20:41:10 | [diff] [blame] | 176 | |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 177 | // Saved callback received from CollectFinalMetricsForLog(). |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 178 | 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 | |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 186 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 187 | // 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] | 8a5b2da | 2014-07-07 10:56:51 | [diff] [blame] | 192 | // The MemoryGrowthTracker instance that tracks memory usage growth in |
| 193 | // MemoryDetails. |
| 194 | MemoryGrowthTracker memory_growth_tracker_; |
| 195 | |
gunsch | 7cbdcb2 | 2015-03-13 17:02:05 | [diff] [blame] | 196 | // Callback to determine whether or not a cellular network is currently being |
| 197 | // used. |
| 198 | base::Callback<void(bool*)> cellular_callback_; |
| 199 | |
blundell | 26b9524 | 2015-08-17 10:29:24 | [diff] [blame] | 200 | // Subscription for receiving callbacks that a URL was opened from the |
| 201 | // omnibox. |
dcheng | 1edb03a | 2016-04-14 17:20:29 | [diff] [blame] | 202 | std::unique_ptr<base::CallbackList<void(OmniboxLog*)>::Subscription> |
blundell | 26b9524 | 2015-08-17 10:29:24 | [diff] [blame] | 203 | omnibox_url_opened_subscription_; |
| 204 | |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 205 | base::WeakPtrFactory<ChromeMetricsServiceClient> weak_ptr_factory_; |
| 206 | |
[email protected] | 5bdaa2d | 2014-05-19 14:59:51 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceClient); |
| 208 | }; |
| 209 | |
| 210 | #endif // CHROME_BROWSER_METRICS_CHROME_METRICS_SERVICE_CLIENT_H_ |