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" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 *guid = base::GenerateGUID(); | 57 *guid = base::GenerateGUID(); |
58 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) | 58 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) |
59 LOG(ERROR) << "Failed to write new GUID"; | 59 LOG(ERROR) << "Failed to write new GUID"; |
60 return; | 60 return; |
61 } | 61 } |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 // static | 65 // static |
66 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { | 66 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
67 return g_lazy_instance_.Pointer(); | 68 return g_lazy_instance_.Pointer(); |
68 } | 69 } |
69 | 70 |
70 void AwMetricsServiceClient::Initialize( | 71 void AwMetricsServiceClient::Initialize( |
71 PrefService* pref_service, | 72 PrefService* pref_service, |
72 net::URLRequestContextGetter* request_context, | 73 net::URLRequestContextGetter* request_context, |
73 const base::FilePath guid_file_path) { | 74 const base::FilePath guid_file_path) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
74 DCHECK(!is_initialized_); | 76 DCHECK(!is_initialized_); |
75 | 77 |
76 pref_service_ = pref_service; | 78 pref_service_ = pref_service; |
77 request_context_ = request_context; | 79 request_context_ = request_context; |
78 | 80 |
79 std::string* guid = new std::string; | 81 std::string* guid = new std::string; |
80 // Initialization happens on the UI thread, but getting the GUID should happen | 82 // Initialization happens on the UI thread, but getting the GUID should happen |
81 // on the file I/O thread. So we start to initialize, then post to get the | 83 // on the file I/O thread. So we start to initialize, then post to get the |
82 // GUID, and then pick up where we left off, back on the UI thread, in | 84 // GUID, and then pick up where we left off, back on the UI thread, in |
83 // InitializeWithGUID. | 85 // InitializeWithGUID. |
84 content::BrowserThread::PostTaskAndReply( | 86 content::BrowserThread::PostTaskAndReply( |
85 content::BrowserThread::FILE, | 87 content::BrowserThread::FILE, |
86 FROM_HERE, | 88 FROM_HERE, |
87 base::Bind(&GetOrCreateGUID, guid_file_path, guid), | 89 base::Bind(&GetOrCreateGUID, guid_file_path, guid), |
88 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, | 90 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, |
89 base::Unretained(this), base::Owned(guid))); | 91 base::Unretained(this), base::Owned(guid))); |
90 } | 92 } |
91 | 93 |
92 void AwMetricsServiceClient::InitializeWithGUID(std::string* guid) { | 94 void AwMetricsServiceClient::InitializeWithGUID(std::string* guid) { |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
93 DCHECK(!is_initialized_); | 96 DCHECK(!is_initialized_); |
94 | 97 |
95 pref_service_->SetString(metrics::prefs::kMetricsClientID, *guid); | 98 pref_service_->SetString(metrics::prefs::kMetricsClientID, *guid); |
96 | 99 |
97 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 100 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
98 pref_service_, base::Bind(&AwMetricsServiceClient::is_reporting_enabled, | 101 pref_service_, base::Bind(&AwMetricsServiceClient::is_reporting_enabled, |
99 base::Unretained(this)), | 102 base::Unretained(this)), |
100 base::Bind(&StoreClientInfo), base::Bind(&LoadClientInfo)); | 103 base::Bind(&StoreClientInfo), base::Bind(&LoadClientInfo)); |
101 | 104 |
102 metrics_service_.reset(new ::metrics::MetricsService( | 105 metrics_service_.reset(new ::metrics::MetricsService( |
(...skipping 19 matching lines...) Expand all Loading... |
122 new metrics::CallStackProfileMetricsProvider)); | 125 new metrics::CallStackProfileMetricsProvider)); |
123 | 126 |
124 metrics_service_->InitializeMetricsRecordingState(); | 127 metrics_service_->InitializeMetricsRecordingState(); |
125 | 128 |
126 is_initialized_ = true; | 129 is_initialized_ = true; |
127 | 130 |
128 if (is_reporting_enabled()) | 131 if (is_reporting_enabled()) |
129 metrics_service_->Start(); | 132 metrics_service_->Start(); |
130 } | 133 } |
131 | 134 |
132 void AwMetricsServiceClient::Finalize() { | 135 void AwMetricsServiceClient::SetMetricsEnabled(bool enabled) { |
133 DCHECK(is_initialized_); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
134 metrics_service_->Stop(); | |
135 } | |
136 | 137 |
137 void AwMetricsServiceClient::SetMetricsEnabled(bool enabled) { | |
138 // If the client is already initialized, apply the setting immediately. | 138 // If the client is already initialized, apply the setting immediately. |
139 // Otherwise, it will be applied on initialization. | 139 // Otherwise, it will be applied on initialization. |
140 if (is_initialized_ && is_enabled_ != enabled) { | 140 if (is_initialized_ && is_enabled_ != enabled) { |
141 if (enabled) | 141 if (enabled) |
142 metrics_service_->Start(); | 142 metrics_service_->Start(); |
143 else | 143 else |
144 metrics_service_->Stop(); | 144 metrics_service_->Stop(); |
145 } | 145 } |
146 is_enabled_ = enabled; | 146 is_enabled_ = enabled; |
147 } | 147 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 request_context_(nullptr) {} | 223 request_context_(nullptr) {} |
224 | 224 |
225 AwMetricsServiceClient::~AwMetricsServiceClient() {} | 225 AwMetricsServiceClient::~AwMetricsServiceClient() {} |
226 | 226 |
227 bool AwMetricsServiceClient::is_reporting_enabled() { | 227 bool AwMetricsServiceClient::is_reporting_enabled() { |
228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 228 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
229 return is_enabled_; | 229 return is_enabled_; |
230 } | 230 } |
231 | 231 |
232 } // namespace android_webview | 232 } // namespace android_webview |
OLD | NEW |