OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/browser/aw_metrics_log_uploader.h" | 7 #include "android_webview/browser/aw_metrics_log_uploader.h" |
8 #include "android_webview/common/aw_version_info_values.h" | 8 #include "android_webview/common/aw_version_info_values.h" |
9 #include "android_webview/jni/AwMetricsServiceClient_jni.h" | 9 #include "android_webview/jni/AwMetricsServiceClient_jni.h" |
10 #include "base/android/build_info.h" | 10 #include "base/android/build_info.h" |
| 11 #include "base/android/jni_string.h" |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/guid.h" | 14 #include "base/guid.h" |
14 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
16 #include "components/metrics/call_stack_profile_metrics_provider.h" | 17 #include "components/metrics/call_stack_profile_metrics_provider.h" |
17 #include "components/metrics/enabled_state_provider.h" | 18 #include "components/metrics/enabled_state_provider.h" |
18 #include "components/metrics/gpu/gpu_metrics_provider.h" | 19 #include "components/metrics/gpu/gpu_metrics_provider.h" |
19 #include "components/metrics/metrics_log_uploader.h" | 20 #include "components/metrics/metrics_log_uploader.h" |
20 #include "components/metrics/metrics_pref_names.h" | 21 #include "components/metrics/metrics_pref_names.h" |
21 #include "components/metrics/metrics_service.h" | 22 #include "components/metrics/metrics_service.h" |
22 #include "components/metrics/metrics_state_manager.h" | 23 #include "components/metrics/metrics_state_manager.h" |
23 #include "components/metrics/profiler/profiler_metrics_provider.h" | 24 #include "components/metrics/profiler/profiler_metrics_provider.h" |
24 #include "components/metrics/ui/screen_info_metrics_provider.h" | 25 #include "components/metrics/ui/screen_info_metrics_provider.h" |
25 #include "components/metrics/url_constants.h" | 26 #include "components/metrics/url_constants.h" |
| 27 #include "components/metrics/version_utils.h" |
26 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 29 #include "components/version_info/channel_android.h" |
| 30 #include "components/version_info/version_info.h" |
27 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
28 | 32 |
29 namespace android_webview { | 33 namespace android_webview { |
30 | 34 |
31 base::LazyInstance<AwMetricsServiceClient>::Leaky g_lazy_instance_; | 35 base::LazyInstance<AwMetricsServiceClient>::Leaky g_lazy_instance_; |
32 | 36 |
33 namespace { | 37 namespace { |
34 | 38 |
35 const int kUploadIntervalMinutes = 30; | 39 const int kUploadIntervalMinutes = 30; |
36 | 40 |
(...skipping 24 matching lines...) Expand all Loading... |
61 // We must write a new GUID. | 65 // We must write a new GUID. |
62 *guid = base::GenerateGUID(); | 66 *guid = base::GenerateGUID(); |
63 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) { | 67 if (!base::WriteFile(guid_file_path, guid->c_str(), guid->size())) { |
64 // If writing fails, proceed anyway with the new GUID. It won't be persisted | 68 // If writing fails, proceed anyway with the new GUID. It won't be persisted |
65 // to the next run, but we can still collect metrics with this 1-time GUID. | 69 // to the next run, but we can still collect metrics with this 1-time GUID. |
66 LOG(ERROR) << "Failed to write new GUID"; | 70 LOG(ERROR) << "Failed to write new GUID"; |
67 } | 71 } |
68 return; | 72 return; |
69 } | 73 } |
70 | 74 |
| 75 version_info::Channel GetChannelFromPackageName() { |
| 76 JNIEnv* env = base::android::AttachCurrentThread(); |
| 77 std::string package_name = base::android::ConvertJavaStringToUTF8( |
| 78 env, Java_AwMetricsServiceClient_getWebViewPackageName(env)); |
| 79 // We can't determine the channel for stand-alone WebView, since it has the |
| 80 // same package name across channels. It will always be "unknown". |
| 81 return version_info::ChannelFromPackageName(package_name.c_str()); |
| 82 } |
| 83 |
71 } // namespace | 84 } // namespace |
72 | 85 |
73 // static | 86 // static |
74 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { | 87 AwMetricsServiceClient* AwMetricsServiceClient::GetInstance() { |
75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
76 return g_lazy_instance_.Pointer(); | 89 return g_lazy_instance_.Pointer(); |
77 } | 90 } |
78 | 91 |
79 void AwMetricsServiceClient::Initialize( | 92 void AwMetricsServiceClient::Initialize( |
80 PrefService* pref_service, | 93 PrefService* pref_service, |
81 net::URLRequestContextGetter* request_context, | 94 net::URLRequestContextGetter* request_context, |
82 const base::FilePath guid_file_path) { | 95 const base::FilePath guid_file_path) { |
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
84 | 97 |
85 DCHECK(pref_service_ == nullptr); // Initialize should only happen once. | 98 DCHECK(pref_service_ == nullptr); // Initialize should only happen once. |
86 DCHECK(request_context_ == nullptr); | 99 DCHECK(request_context_ == nullptr); |
87 pref_service_ = pref_service; | 100 pref_service_ = pref_service; |
88 request_context_ = request_context; | 101 request_context_ = request_context; |
| 102 channel_ = GetChannelFromPackageName(); |
89 | 103 |
90 std::string* guid = new std::string; | 104 std::string* guid = new std::string; |
91 // Initialization happens on the UI thread, but getting the GUID should happen | 105 // Initialization happens on the UI thread, but getting the GUID should happen |
92 // on the file I/O thread. So we start to initialize, then post to get the | 106 // on the file I/O thread. So we start to initialize, then post to get the |
93 // GUID, and then pick up where we left off, back on the UI thread, in | 107 // GUID, and then pick up where we left off, back on the UI thread, in |
94 // InitializeWithGUID. | 108 // InitializeWithGUID. |
95 content::BrowserThread::PostTaskAndReply( | 109 content::BrowserThread::PostTaskAndReply( |
96 content::BrowserThread::FILE, FROM_HERE, | 110 content::BrowserThread::FILE, FROM_HERE, |
97 base::Bind(&GetOrCreateGUID, guid_file_path, guid), | 111 base::Bind(&GetOrCreateGUID, guid_file_path, guid), |
98 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, | 112 base::Bind(&AwMetricsServiceClient::InitializeWithGUID, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 std::string AwMetricsServiceClient::GetApplicationLocale() { | 193 std::string AwMetricsServiceClient::GetApplicationLocale() { |
180 return base::i18n::GetConfiguredLocale(); | 194 return base::i18n::GetConfiguredLocale(); |
181 } | 195 } |
182 | 196 |
183 bool AwMetricsServiceClient::GetBrand(std::string* brand_code) { | 197 bool AwMetricsServiceClient::GetBrand(std::string* brand_code) { |
184 // WebView doesn't use brand codes. | 198 // WebView doesn't use brand codes. |
185 return false; | 199 return false; |
186 } | 200 } |
187 | 201 |
188 metrics::SystemProfileProto::Channel AwMetricsServiceClient::GetChannel() { | 202 metrics::SystemProfileProto::Channel AwMetricsServiceClient::GetChannel() { |
189 // "Channel" means stable, beta, etc. WebView doesn't have channel info yet. | 203 return metrics::AsProtobufChannel(channel_); |
190 // TODO(paulmiller) Update this once we have channel info. | |
191 return metrics::SystemProfileProto::CHANNEL_UNKNOWN; | |
192 } | 204 } |
193 | 205 |
194 std::string AwMetricsServiceClient::GetVersionString() { | 206 std::string AwMetricsServiceClient::GetVersionString() { |
195 return PRODUCT_VERSION; | 207 return PRODUCT_VERSION; |
196 } | 208 } |
197 | 209 |
198 void AwMetricsServiceClient::InitializeSystemProfileMetrics( | 210 void AwMetricsServiceClient::InitializeSystemProfileMetrics( |
199 const base::Closure& done_callback) { | 211 const base::Closure& done_callback) { |
200 done_callback.Run(); | 212 done_callback.Run(); |
201 } | 213 } |
(...skipping 15 matching lines...) Expand all Loading... |
217 new AwMetricsLogUploader(on_upload_complete)); | 229 new AwMetricsLogUploader(on_upload_complete)); |
218 } | 230 } |
219 | 231 |
220 base::TimeDelta AwMetricsServiceClient::GetStandardUploadInterval() { | 232 base::TimeDelta AwMetricsServiceClient::GetStandardUploadInterval() { |
221 // The platform logging mechanism is responsible for upload frequency; this | 233 // The platform logging mechanism is responsible for upload frequency; this |
222 // just specifies how frequently to provide logs to the platform. | 234 // just specifies how frequently to provide logs to the platform. |
223 return base::TimeDelta::FromMinutes(kUploadIntervalMinutes); | 235 return base::TimeDelta::FromMinutes(kUploadIntervalMinutes); |
224 } | 236 } |
225 | 237 |
226 AwMetricsServiceClient::AwMetricsServiceClient() | 238 AwMetricsServiceClient::AwMetricsServiceClient() |
227 : is_enabled_(false), pref_service_(nullptr), request_context_(nullptr) {} | 239 : is_enabled_(false), |
| 240 pref_service_(nullptr), |
| 241 request_context_(nullptr), |
| 242 channel_(version_info::Channel::UNKNOWN) {} |
228 | 243 |
229 AwMetricsServiceClient::~AwMetricsServiceClient() {} | 244 AwMetricsServiceClient::~AwMetricsServiceClient() {} |
230 | 245 |
231 // static | 246 // static |
232 void SetMetricsEnabled(JNIEnv* env, | 247 void SetMetricsEnabled(JNIEnv* env, |
233 const base::android::JavaParamRef<jclass>& jcaller, | 248 const base::android::JavaParamRef<jclass>& jcaller, |
234 jboolean enabled) { | 249 jboolean enabled) { |
235 g_lazy_instance_.Pointer()->SetMetricsEnabled(enabled); | 250 g_lazy_instance_.Pointer()->SetMetricsEnabled(enabled); |
236 } | 251 } |
237 | 252 |
238 bool RegisterAwMetricsServiceClient(JNIEnv* env) { | 253 bool RegisterAwMetricsServiceClient(JNIEnv* env) { |
239 return RegisterNativesImpl(env); | 254 return RegisterNativesImpl(env); |
240 } | 255 } |
241 | 256 |
242 } // namespace android_webview | 257 } // namespace android_webview |
OLD | NEW |