| 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 "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | 17 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
| 17 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 18 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 18 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | 19 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
| 19 #include "components/variations/variations_associated_data.h" | 20 #include "components/variations/variations_associated_data.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "jni/ExternalDataUseObserver_jni.h" | 22 #include "jni/ExternalDataUseObserver_jni.h" |
| 22 | 23 |
| 23 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // Name of the external data use observer synthetic field trial, enabled and | 28 // Name of the external data use observer synthetic field trial, enabled and |
| 28 // disabled groups. | 29 // disabled groups. |
| 29 const char kSyntheticFieldTrial[] = "SyntheticExternalDataUseObserver"; | 30 const char kSyntheticFieldTrial[] = "SyntheticExternalDataUseObserver"; |
| 30 const char kSyntheticFieldTrialEnabledGroup[] = "Enabled"; | 31 const char kSyntheticFieldTrialEnabledGroup[] = "Enabled"; |
| 31 const char kSyntheticFieldTrialDisabledGroup[] = "Disabled"; | 32 const char kSyntheticFieldTrialDisabledGroup[] = "Disabled"; |
| 32 | 33 |
| 33 // Returns the package name of the control app from the field trial. | 34 // Returns the package name of the control app from the field trial. |
| 34 const std::string GetControlAppPackageName() { | 35 const std::string GetControlAppPackageName() { |
| 35 return variations::GetVariationParamValue( | 36 return variations::GetVariationParamValue( |
| 36 chrome::android::ExternalDataUseObserver:: | 37 chrome::android::ExternalDataUseObserver:: |
| 37 kExternalDataUseObserverFieldTrial, | 38 kExternalDataUseObserverFieldTrial, |
| 38 "control_app_package_name"); | 39 "control_app_package_name"); |
| 39 } | 40 } |
| 40 | 41 |
| 42 // Returns the google variation ID from the field trial. |
| 43 variations::VariationID GetGoogleVariationID() { |
| 44 variations::VariationID variation_id; |
| 45 std::string variation_value = variations::GetVariationParamValue( |
| 46 chrome::android::ExternalDataUseObserver:: |
| 47 kExternalDataUseObserverFieldTrial, |
| 48 "variation_id"); |
| 49 if (!variation_value.empty() && |
| 50 base::StringToInt(variation_value, &variation_id)) { |
| 51 return variation_id; |
| 52 } |
| 53 return variations::EMPTY_ID; |
| 54 } |
| 55 |
| 41 } // namespace | 56 } // namespace |
| 42 | 57 |
| 43 namespace chrome { | 58 namespace chrome { |
| 44 | 59 |
| 45 namespace android { | 60 namespace android { |
| 46 | 61 |
| 47 ExternalDataUseObserverBridge::ExternalDataUseObserverBridge() | 62 ExternalDataUseObserverBridge::ExternalDataUseObserverBridge() |
| 48 : construct_time_(base::TimeTicks::Now()), | 63 : construct_time_(base::TimeTicks::Now()), |
| 49 is_first_matching_rule_fetch_(true) { | 64 is_first_matching_rule_fetch_(true) { |
| 50 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 65 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 void ExternalDataUseObserverBridge::ShouldRegisterAsDataUseObserver( | 203 void ExternalDataUseObserverBridge::ShouldRegisterAsDataUseObserver( |
| 189 bool should_register) const { | 204 bool should_register) const { |
| 190 DCHECK(thread_checker_.CalledOnValidThread()); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 191 DCHECK(!j_external_data_use_observer_.is_null()); | 206 DCHECK(!j_external_data_use_observer_.is_null()); |
| 192 | 207 |
| 193 io_task_runner_->PostTask( | 208 io_task_runner_->PostTask( |
| 194 FROM_HERE, | 209 FROM_HERE, |
| 195 base::Bind(&ExternalDataUseObserver::ShouldRegisterAsDataUseObserver, | 210 base::Bind(&ExternalDataUseObserver::ShouldRegisterAsDataUseObserver, |
| 196 external_data_use_observer_, should_register)); | 211 external_data_use_observer_, should_register)); |
| 197 | 212 |
| 198 // Set or clear the variation id for the enabled group. | 213 variations::VariationID variation_id = GetGoogleVariationID(); |
| 199 JNIEnv* env = base::android::AttachCurrentThread(); | 214 if (variation_id != variations::EMPTY_ID) { |
| 200 variations::AssociateGoogleVariationID( | 215 // Set variation id for the enabled group if |should_register| is true. |
| 201 variations::GOOGLE_WEB_PROPERTIES, kSyntheticFieldTrial, | 216 // Otherwise clear the variation id for the enabled group by setting to |
| 202 kSyntheticFieldTrialEnabledGroup, | 217 // EMPTY_ID. |
| 203 should_register ? Java_ExternalDataUseObserver_getGoogleVariationID( | 218 variations::AssociateGoogleVariationID( |
| 204 env, j_external_data_use_observer_.obj()) | 219 variations::GOOGLE_WEB_PROPERTIES, kSyntheticFieldTrial, |
| 205 : variations::EMPTY_ID); | 220 kSyntheticFieldTrialEnabledGroup, |
| 206 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial( | 221 should_register ? variation_id : variations::EMPTY_ID); |
| 207 kSyntheticFieldTrial, should_register | 222 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial( |
| 208 ? kSyntheticFieldTrialEnabledGroup | 223 kSyntheticFieldTrial, should_register |
| 209 : kSyntheticFieldTrialDisabledGroup); | 224 ? kSyntheticFieldTrialEnabledGroup |
| 225 : kSyntheticFieldTrialDisabledGroup); |
| 226 } |
| 210 } | 227 } |
| 211 | 228 |
| 212 bool RegisterExternalDataUseObserver(JNIEnv* env) { | 229 bool RegisterExternalDataUseObserver(JNIEnv* env) { |
| 213 return RegisterNativesImpl(env); | 230 return RegisterNativesImpl(env); |
| 214 } | 231 } |
| 215 | 232 |
| 216 } // namespace android | 233 } // namespace android |
| 217 | 234 |
| 218 } // namespace chrome | 235 } // namespace chrome |
| OLD | NEW |