Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/search_geolocation_disclosure_tab_helper.h" | 5 #include "chrome/browser/android/search_geolocation_disclosure_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 7 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 12 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate. h" | 14 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate. h" |
| 13 #include "chrome/browser/permissions/permission_manager.h" | 15 #include "chrome/browser/permissions/permission_manager.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/common/chrome_features.h" | 18 #include "chrome/common/chrome_features.h" |
| 17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 18 #include "components/pref_registry/pref_registry_syncable.h" | 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 19 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 20 #include "components/search_engines/template_url.h" | 22 #include "components/search_engines/template_url.h" |
| 21 #include "components/search_engines/template_url_service.h" | 23 #include "components/search_engines/template_url_service.h" |
| 22 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| 23 #include "content/public/browser/permission_type.h" | 25 #include "content/public/browser/permission_type.h" |
| 24 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 25 #include "jni/GeolocationHeader_jni.h" | 27 #include "jni/GeolocationHeader_jni.h" |
| 28 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" | |
| 26 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h" | 29 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h" |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 const int kDefaultMaxShowCount = 3; | 33 const int kDefaultMaxShowCount = 3; |
| 31 const int kDefaultDaysPerShow = 1; | 34 const int kDefaultDaysPerShow = 1; |
| 32 const char kMaxShowCountVariation[] = "MaxShowCount"; | 35 const char kMaxShowCountVariation[] = "MaxShowCount"; |
| 33 const char kDaysPerShowVariation[] = "DaysPerShow"; | 36 const char kDaysPerShowVariation[] = "DaysPerShow"; |
| 34 | 37 |
| 38 bool gIgnoreUrlChecksForTesting = false; | |
| 39 | |
| 35 int GetMaxShowCount() { | 40 int GetMaxShowCount() { |
| 36 std::string variation = variations::GetVariationParamValueByFeature( | 41 std::string variation = variations::GetVariationParamValueByFeature( |
| 37 features::kConsistentOmniboxGeolocation, kMaxShowCountVariation); | 42 features::kConsistentOmniboxGeolocation, kMaxShowCountVariation); |
| 38 int max_show; | 43 int max_show; |
| 39 if (!variation.empty() && base::StringToInt(variation, &max_show)) | 44 if (!variation.empty() && base::StringToInt(variation, &max_show)) |
| 40 return max_show; | 45 return max_show; |
| 41 | 46 |
| 42 return kDefaultMaxShowCount; | 47 return kDefaultMaxShowCount; |
| 43 } | 48 } |
| 44 | 49 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 registry->RegisterBooleanPref( | 92 registry->RegisterBooleanPref( |
| 88 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | 93 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); |
| 89 } | 94 } |
| 90 | 95 |
| 91 void SearchGeolocationDisclosureTabHelper:: | 96 void SearchGeolocationDisclosureTabHelper:: |
| 92 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | 97 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { |
| 93 // Don't show in incognito. | 98 // Don't show in incognito. |
| 94 if (GetProfile()->IsOffTheRecord()) | 99 if (GetProfile()->IsOffTheRecord()) |
| 95 return; | 100 return; |
| 96 | 101 |
| 97 // Only show the disclosure for default search navigations from the omnibox. | 102 if (!ShouldShowDisclosureForUrl(gurl)) |
| 98 TemplateURLService* template_url_service = | |
| 99 TemplateURLServiceFactory::GetForProfile(GetProfile()); | |
| 100 bool is_search_url = | |
| 101 template_url_service->IsSearchResultsPageFromDefaultSearchProvider( | |
| 102 gurl); | |
| 103 if (!is_search_url) | |
| 104 return; | 103 return; |
| 105 | 104 |
| 106 // Only show the disclosure if Google is the default search engine. | 105 PrefService* prefs = GetProfile()->GetPrefs(); |
| 107 TemplateURL* default_search = | |
| 108 template_url_service->GetDefaultSearchProvider(); | |
| 109 if (!default_search || | |
| 110 !default_search->url_ref().HasGoogleBaseURLs( | |
| 111 template_url_service->search_terms_data())) { | |
| 112 return; | |
| 113 } | |
| 114 | 106 |
| 115 // Don't show the infobar if the user has dismissed it, or they've seen it | |
| 116 // enough times already. | |
|
benwells
2016/12/29 01:42:01
Losing this comment was a merge error; I'll put it
benwells
2016/12/29 01:50:14
Done.
| |
| 117 PrefService* prefs = GetProfile()->GetPrefs(); | |
| 118 bool dismissed_already = | 107 bool dismissed_already = |
| 119 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 108 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |
| 109 | |
|
benwells
2016/12/29 01:42:01
I'll also remove these empty lines, not sure how t
benwells
2016/12/29 01:50:14
Done.
| |
| 120 int shown_count = | 110 int shown_count = |
| 121 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 111 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |
| 112 | |
| 122 if (dismissed_already || shown_count >= GetMaxShowCount()) { | 113 if (dismissed_already || shown_count >= GetMaxShowCount()) { |
| 123 // Record metrics for the state of permissions after the disclosure has been | 114 // Record metrics for the state of permissions after the disclosure has been |
| 124 // shown. This is not done immediately after showing the last disclosure | 115 // shown. This is not done immediately after showing the last disclosure |
| 125 // (i.e. at the end of this function), but on the next omnibox search, to | 116 // (i.e. at the end of this function), but on the next omnibox search, to |
| 126 // allow the metric to capture changes to settings done by the user as a | 117 // allow the metric to capture changes to settings done by the user as a |
| 127 // result of clicking on the Settings link in the disclosure. | 118 // result of clicking on the Settings link in the disclosure. |
| 128 RecordPostDisclosureMetrics(gurl); | 119 RecordPostDisclosureMetrics(gurl); |
| 129 return; | 120 return; |
| 130 } | 121 } |
| 131 | 122 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 156 return; | 147 return; |
| 157 | 148 |
| 158 // All good, let's show the disclosure and increment the shown count. | 149 // All good, let's show the disclosure and increment the shown count. |
| 159 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 150 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |
| 160 shown_count++; | 151 shown_count++; |
| 161 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 152 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |
| 162 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 153 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 163 base::Time::Now().ToInternalValue()); | 154 base::Time::Now().ToInternalValue()); |
| 164 } | 155 } |
| 165 | 156 |
| 157 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( | |
| 158 const GURL& gurl) { | |
| 159 if (gIgnoreUrlChecksForTesting) | |
| 160 return true; | |
| 161 | |
| 162 // Only show the disclosure for default search navigations from the omnibox. | |
| 163 TemplateURLService* template_url_service = | |
| 164 TemplateURLServiceFactory::GetForProfile(GetProfile()); | |
| 165 bool is_search_url = | |
| 166 template_url_service->IsSearchResultsPageFromDefaultSearchProvider( | |
| 167 gurl); | |
| 168 if (!is_search_url) | |
| 169 return false; | |
| 170 | |
| 171 // Only show the disclosure if Google is the default search engine. | |
| 172 TemplateURL* default_search = | |
| 173 template_url_service->GetDefaultSearchProvider(); | |
| 174 if (!default_search || | |
| 175 !default_search->url_ref().HasGoogleBaseURLs( | |
| 176 template_url_service->search_terms_data())) { | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 166 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 183 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( |
| 167 const GURL& gurl) { | 184 const GURL& gurl) { |
| 168 PrefService* prefs = GetProfile()->GetPrefs(); | 185 PrefService* prefs = GetProfile()->GetPrefs(); |
| 169 if (!prefs->GetBoolean( | 186 if (!prefs->GetBoolean( |
| 170 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 187 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { |
| 171 blink::mojom::PermissionStatus status = | 188 blink::mojom::PermissionStatus status = |
| 172 PermissionManager::Get(GetProfile()) | 189 PermissionManager::Get(GetProfile()) |
| 173 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 190 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, |
| 174 gurl); | 191 gurl); |
| 175 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", | 192 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 197 blink::mojom::PermissionStatus::LAST) + | 214 blink::mojom::PermissionStatus::LAST) + |
| 198 1); | 215 1); |
| 199 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | 216 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, |
| 200 true); | 217 true); |
| 201 } | 218 } |
| 202 } | 219 } |
| 203 | 220 |
| 204 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | 221 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |
| 205 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 222 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 206 } | 223 } |
| 224 | |
| 225 // static | |
| 226 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | |
|
benwells
2016/12/29 01:42:01
I'll move this up in the file to be under Register
benwells
2016/12/29 01:50:14
Done.
| |
| 227 return RegisterNativesImpl(env); | |
| 228 } | |
| 229 | |
| 230 // static | |
| 231 void SetIgnoreUrlChecks(JNIEnv* env, | |
| 232 const base::android::JavaParamRef<jclass>& clazz, | |
| 233 jboolean ignore) { | |
| 234 gIgnoreUrlChecksForTesting = ignore; | |
| 235 } | |
| OLD | NEW |