[email protected] | c3cac95 | 2014-05-09 01:51:18 | [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 | |
blundell | 695d61f | 2015-10-21 11:25:53 | [diff] [blame] | 5 | #ifndef COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_ |
| 6 | #define COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_ |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 7 | |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 10 | #include "base/macros.h" |
robliao | 4bee5d9 | 2016-09-15 14:37:37 | [diff] [blame] | 11 | #include "base/metrics/field_trial.h" |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 12 | #include "base/threading/thread_checker.h" |
| 13 | |
[email protected] | 544246e | 2014-06-06 11:22:28 | [diff] [blame] | 14 | namespace base { |
| 15 | class FilePath; |
| 16 | } |
| 17 | |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 18 | namespace metrics { |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 19 | class MetricsService; |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 20 | class MetricsServiceClient; |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 21 | class MetricsStateManager; |
| 22 | } |
| 23 | |
| 24 | namespace rappor { |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 25 | class RapporServiceImpl; |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 26 | } |
| 27 | |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 28 | namespace ukm { |
| 29 | class UkmService; |
| 30 | } |
| 31 | |
blundell | 57bcfed | 2015-09-04 08:44:45 | [diff] [blame] | 32 | namespace variations { |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 33 | class VariationsService; |
| 34 | } |
| 35 | |
blundell | 695d61f | 2015-10-21 11:25:53 | [diff] [blame] | 36 | namespace metrics_services_manager { |
| 37 | |
| 38 | class MetricsServicesManagerClient; |
| 39 | |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 40 | // MetricsServicesManager is a helper class for embedders that use the various |
| 41 | // metrics-related services in a Chrome-like fashion: MetricsService (via its |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 42 | // client), RapporServiceImpl and VariationsService. |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 43 | class MetricsServicesManager { |
| 44 | public: |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 45 | // Creates the MetricsServicesManager with the given client. |
| 46 | explicit MetricsServicesManager( |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 47 | std::unique_ptr<MetricsServicesManagerClient> client); |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 48 | virtual ~MetricsServicesManager(); |
| 49 | |
robliao | 4bee5d9 | 2016-09-15 14:37:37 | [diff] [blame] | 50 | // Returns the preferred entropy provider used to seed persistent activities |
| 51 | // based on whether or not metrics reporting is permitted on this client. |
| 52 | // |
| 53 | // If there's consent to report metrics, this method returns an entropy |
| 54 | // provider that has a high source of entropy, partially based on the client |
| 55 | // ID. Otherwise, it returns an entropy provider that is based on a low |
| 56 | // entropy source. |
| 57 | std::unique_ptr<const base::FieldTrial::EntropyProvider> |
| 58 | CreateEntropyProvider(); |
| 59 | |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 60 | // Returns the MetricsService, creating it if it hasn't been created yet (and |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 61 | // additionally creating the MetricsServiceClient in that case). |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 62 | metrics::MetricsService* GetMetricsService(); |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 63 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 64 | // Returns the RapporServiceImpl, creating it if it hasn't been created yet. |
| 65 | rappor::RapporServiceImpl* GetRapporServiceImpl(); |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 66 | |
holte | 7b74c62 | 2017-01-23 23:13:07 | [diff] [blame] | 67 | // Returns the UkmService, creating it if it hasn't been created yet. |
| 68 | ukm::UkmService* GetUkmService(); |
| 69 | |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 70 | // Returns the VariationsService, creating it if it hasn't been created yet. |
blundell | 57bcfed | 2015-09-04 08:44:45 | [diff] [blame] | 71 | variations::VariationsService* GetVariationsService(); |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 72 | |
[email protected] | 544246e | 2014-06-06 11:22:28 | [diff] [blame] | 73 | // Should be called when a plugin loading error occurs. |
| 74 | void OnPluginLoadingError(const base::FilePath& plugin_path); |
| 75 | |
blundell | 6eb91b7b | 2015-10-23 07:04:43 | [diff] [blame] | 76 | // Some embedders use this method to notify the metrics system when a |
| 77 | // renderer process exits unexpectedly. |
| 78 | void OnRendererProcessCrash(); |
| 79 | |
anthonyvd | 40dd030 | 2015-02-19 16:11:34 | [diff] [blame] | 80 | // Update the managed services when permissions for uploading metrics change. |
| 81 | void UpdateUploadPermissions(bool may_upload); |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 82 | |
Brian White | c459cf5 | 2018-01-24 16:55:14 | [diff] [blame^] | 83 | // Gets the current state of metric reporting. |
| 84 | bool IsMetricsReportingEnabled() const; |
| 85 | |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 86 | private: |
holte | 873fb7c9 | 2015-02-19 23:41:26 | [diff] [blame] | 87 | // Update the managed services when permissions for recording/uploading |
| 88 | // metrics change. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 89 | void UpdateRapporServiceImpl(); |
holte | 873fb7c9 | 2015-02-19 23:41:26 | [diff] [blame] | 90 | |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 91 | // Returns the MetricsServiceClient, creating it if it hasn't been |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 92 | // created yet (and additionally creating the MetricsService in that case). |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 93 | metrics::MetricsServiceClient* GetMetricsServiceClient(); |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 94 | |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 95 | metrics::MetricsStateManager* GetMetricsStateManager(); |
| 96 | |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 97 | // Update which services are running to match current permissions. |
| 98 | void UpdateRunningServices(); |
| 99 | |
holte | 1334c0aa | 2017-02-09 22:52:41 | [diff] [blame] | 100 | // Update the state of UkmService to match current permissions. |
| 101 | void UpdateUkmService(); |
| 102 | |
Robert Kaplow | c998692 | 2017-09-29 02:05:49 | [diff] [blame] | 103 | // Update the managed services when permissions for recording/uploading |
| 104 | // metrics change. |
| 105 | void UpdatePermissions(bool current_may_record, |
| 106 | bool current_consent_given, |
| 107 | bool current_may_upload); |
| 108 | |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 109 | // The client passed in from the embedder. |
François Degros | e1dc3bb | 2017-11-29 00:10:38 | [diff] [blame] | 110 | const std::unique_ptr<MetricsServicesManagerClient> client_; |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 111 | |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 112 | // Ensures that all functions are called from the same thread. |
| 113 | base::ThreadChecker thread_checker_; |
| 114 | |
holte | 873fb7c9 | 2015-02-19 23:41:26 | [diff] [blame] | 115 | // The current metrics reporting setting. |
| 116 | bool may_upload_; |
| 117 | |
| 118 | // The current metrics recording setting. |
| 119 | bool may_record_; |
| 120 | |
Robert Kaplow | c998692 | 2017-09-29 02:05:49 | [diff] [blame] | 121 | // The current metrics setting reflecting if consent was given. |
| 122 | bool consent_given_; |
| 123 | |
blundell | fecea528d | 2015-10-21 10:10:22 | [diff] [blame] | 124 | // The MetricsServiceClient. Owns the MetricsService. |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 125 | std::unique_ptr<metrics::MetricsServiceClient> metrics_service_client_; |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 126 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 127 | // The RapporServiceImpl, for RAPPOR metric uploads. |
| 128 | std::unique_ptr<rappor::RapporServiceImpl> rappor_service_; |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 129 | |
| 130 | // The VariationsService, for server-side experiments infrastructure. |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 131 | std::unique_ptr<variations::VariationsService> variations_service_; |
[email protected] | c3cac95 | 2014-05-09 01:51:18 | [diff] [blame] | 132 | |
| 133 | DISALLOW_COPY_AND_ASSIGN(MetricsServicesManager); |
| 134 | }; |
| 135 | |
blundell | 695d61f | 2015-10-21 11:25:53 | [diff] [blame] | 136 | } // namespace metrics_services_manager |
| 137 | |
| 138 | #endif // COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_ |