| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "android_webview/browser/aw_metrics_service_client.h" | 5 #include "android_webview/browser/aw_metrics_service_client.h" |
| 6 | 6 |
| 7 #include "android_webview/common/aw_version_info_values.h" | 7 #include "android_webview/common/aw_version_info_values.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| 11 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
| 12 #include "components/metrics/call_stack_profile_metrics_provider.h" | 12 #include "components/metrics/call_stack_profile_metrics_provider.h" |
| 13 #include "components/metrics/enabled_state_provider.h" |
| 13 #include "components/metrics/gpu/gpu_metrics_provider.h" | 14 #include "components/metrics/gpu/gpu_metrics_provider.h" |
| 14 #include "components/metrics/metrics_pref_names.h" | 15 #include "components/metrics/metrics_pref_names.h" |
| 15 #include "components/metrics/metrics_service.h" | 16 #include "components/metrics/metrics_service.h" |
| 16 #include "components/metrics/metrics_state_manager.h" | 17 #include "components/metrics/metrics_state_manager.h" |
| 17 #include "components/metrics/net/net_metrics_log_uploader.h" | 18 #include "components/metrics/net/net_metrics_log_uploader.h" |
| 18 #include "components/metrics/profiler/profiler_metrics_provider.h" | 19 #include "components/metrics/profiler/profiler_metrics_provider.h" |
| 19 #include "components/metrics/ui/screen_info_metrics_provider.h" | 20 #include "components/metrics/ui/screen_info_metrics_provider.h" |
| 20 #include "components/metrics/url_constants.h" | 21 #include "components/metrics/url_constants.h" |
| 21 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 56 |
| 56 // We must write a new GUID. | 57 // We must write a new GUID. |
| 57 *guid = base::GenerateGUID(); | 58 *guid = base::GenerateGUID(); |
| 58 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) | 59 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) |
| 59 LOG(ERROR) << "Failed to write new GUID"; | 60 LOG(ERROR) << "Failed to write new GUID"; |
| 60 return; | 61 return; |
| 61 } | 62 } |
| 62 | 63 |
| 63 } // namespace | 64 } // namespace |
| 64 | 65 |
| 66 class AwMetricsServiceClient::AwEnabledStateProvider |
| 67 : public metrics::EnabledStateProvider { |
| 68 public: |
| 69 AwEnabledStateProvider(AwMetricsServiceClient* metrics_service_client) |
| 70 : metrics_service_client_(metrics_service_client) {} |
| 71 ~AwEnabledStateProvider() override {} |
| 72 |
| 73 bool IsConsentGiven() override { |
| 74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 75 return metrics_service_client_->is_enabled_; |
| 76 } |
| 77 |
| 78 private: |
| 79 AwMetricsServiceClient* metrics_service_client_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(AwEnabledStateProvider); |
| 82 }; |
| 83 |
| 65 // static | 84 // static |
| 66 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { | 85 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 68 return g_lazy_instance_.Pointer(); | 87 return g_lazy_instance_.Pointer(); |
| 69 } | 88 } |
| 70 | 89 |
| 71 void AwMetricsServiceClient::Initialize( | 90 void AwMetricsServiceClient::Initialize( |
| 72 PrefService* pref_service, | 91 PrefService* pref_service, |
| 73 net::URLRequestContextGetter* request_context, | 92 net::URLRequestContextGetter* request_context, |
| 74 const base::FilePath guid_file_path) { | 93 const base::FilePath guid_file_path) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 DCHECK(!is_initialized_); | 95 DCHECK(!is_initialized_); |
| 77 | 96 |
| 78 pref_service_ = pref_service; | 97 pref_service_ = pref_service; |
| 79 request_context_ = request_context; | 98 request_context_ = request_context; |
| 80 | 99 |
| 100 enabled_state_provider_.reset(new AwEnabledStateProvider(this)); |
| 101 |
| 81 std::string* guid = new std::string; | 102 std::string* guid = new std::string; |
| 82 // Initialization happens on the UI thread, but getting the GUID should happen | 103 // Initialization happens on the UI thread, but getting the GUID should happen |
| 83 // on the file I/O thread. So we start to initialize, then post to get the | 104 // on the file I/O thread. So we start to initialize, then post to get the |
| 84 // GUID, and then pick up where we left off, back on the UI thread, in | 105 // GUID, and then pick up where we left off, back on the UI thread, in |
| 85 // InitializeWithGUID. | 106 // InitializeWithGUID. |
| 86 content::BrowserThread::PostTaskAndReply( | 107 content::BrowserThread::PostTaskAndReply( |
| 87 content::BrowserThread::FILE, | 108 content::BrowserThread::FILE, |
| 88 FROM_HERE, | 109 FROM_HERE, |
| 89 base::Bind(&GetOrCreateGUID, guid_file_path, guid), | 110 base::Bind(&GetOrCreateGUID, guid_file_path, guid), |
| 90 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, | 111 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, |
| 91 base::Unretained(this), base::Owned(guid))); | 112 base::Unretained(this), base::Owned(guid))); |
| 92 } | 113 } |
| 93 | 114 |
| 94 void AwMetricsServiceClient::InitializeWithGUID(std::string* guid) { | 115 void AwMetricsServiceClient::InitializeWithGUID(std::string* guid) { |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 96 DCHECK(!is_initialized_); | 117 DCHECK(!is_initialized_); |
| 97 | 118 |
| 98 pref_service_->SetString(metrics::prefs::kMetricsClientID, *guid); | 119 pref_service_->SetString(metrics::prefs::kMetricsClientID, *guid); |
| 99 | 120 |
| 100 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 121 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
| 101 pref_service_, base::Bind(&AwMetricsServiceClient::is_reporting_enabled, | 122 pref_service_, enabled_state_provider_.get(), |
| 102 base::Unretained(this)), | |
| 103 base::Bind(&StoreClientInfo), base::Bind(&LoadClientInfo)); | 123 base::Bind(&StoreClientInfo), base::Bind(&LoadClientInfo)); |
| 104 | 124 |
| 105 metrics_service_.reset(new ::metrics::MetricsService( | 125 metrics_service_.reset(new ::metrics::MetricsService( |
| 106 metrics_state_manager_.get(), this, pref_service_)); | 126 metrics_state_manager_.get(), this, pref_service_)); |
| 107 | 127 |
| 108 metrics_service_->RegisterMetricsProvider( | 128 metrics_service_->RegisterMetricsProvider( |
| 109 std::unique_ptr<metrics::MetricsProvider>( | 129 std::unique_ptr<metrics::MetricsProvider>( |
| 110 new metrics::NetworkMetricsProvider( | 130 new metrics::NetworkMetricsProvider( |
| 111 content::BrowserThread::GetBlockingPool()))); | 131 content::BrowserThread::GetBlockingPool()))); |
| 112 | 132 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 123 new metrics::ProfilerMetricsProvider())); | 143 new metrics::ProfilerMetricsProvider())); |
| 124 | 144 |
| 125 metrics_service_->RegisterMetricsProvider( | 145 metrics_service_->RegisterMetricsProvider( |
| 126 std::unique_ptr<metrics::MetricsProvider>( | 146 std::unique_ptr<metrics::MetricsProvider>( |
| 127 new metrics::CallStackProfileMetricsProvider)); | 147 new metrics::CallStackProfileMetricsProvider)); |
| 128 | 148 |
| 129 metrics_service_->InitializeMetricsRecordingState(); | 149 metrics_service_->InitializeMetricsRecordingState(); |
| 130 | 150 |
| 131 is_initialized_ = true; | 151 is_initialized_ = true; |
| 132 | 152 |
| 133 if (is_reporting_enabled()) | 153 if (enabled_state_provider_->IsReportingEnabled()) |
| 134 metrics_service_->Start(); | 154 metrics_service_->Start(); |
| 135 } | 155 } |
| 136 | 156 |
| 137 void AwMetricsServiceClient::SetMetricsEnabled(bool enabled) { | 157 void AwMetricsServiceClient::SetMetricsEnabled(bool enabled) { |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 139 | 159 |
| 140 // If the client is already initialized, apply the setting immediately. | 160 // If the client is already initialized, apply the setting immediately. |
| 141 // Otherwise, it will be applied on initialization. | 161 // Otherwise, it will be applied on initialization. |
| 142 if (is_initialized_ && is_enabled_ != enabled) { | 162 if (is_initialized_ && is_enabled_ != enabled) { |
| 143 if (enabled) | 163 if (enabled) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 } | 231 } |
| 212 | 232 |
| 213 base::TimeDelta AwMetricsServiceClient::GetStandardUploadInterval() { | 233 base::TimeDelta AwMetricsServiceClient::GetStandardUploadInterval() { |
| 214 return base::TimeDelta::FromMinutes(kUploadIntervalMinutes); | 234 return base::TimeDelta::FromMinutes(kUploadIntervalMinutes); |
| 215 } | 235 } |
| 216 | 236 |
| 217 AwMetricsServiceClient::AwMetricsServiceClient() | 237 AwMetricsServiceClient::AwMetricsServiceClient() |
| 218 : is_initialized_(false), | 238 : is_initialized_(false), |
| 219 is_enabled_(false), | 239 is_enabled_(false), |
| 220 pref_service_(nullptr), | 240 pref_service_(nullptr), |
| 221 request_context_(nullptr) {} | 241 request_context_(nullptr), |
| 242 enabled_state_provider_(nullptr) {} |
| 222 | 243 |
| 223 AwMetricsServiceClient::~AwMetricsServiceClient() {} | 244 AwMetricsServiceClient::~AwMetricsServiceClient() {} |
| 224 | 245 |
| 225 bool AwMetricsServiceClient::is_reporting_enabled() { | |
| 226 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 227 return is_enabled_; | |
| 228 } | |
| 229 | |
| 230 } // namespace android_webview | 246 } // namespace android_webview |
| OLD | NEW |