| 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" | |
| 9 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 10 #include "base/logging.h" | 8 #include "base/logging.h" |
| 11 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 14 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
h" | 12 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
h" |
| 15 #include "chrome/browser/permissions/permission_manager.h" | 13 #include "chrome/browser/permissions/permission_manager.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 18 #include "chrome/common/chrome_features.h" | 16 #include "chrome/common/chrome_features.h" |
| 19 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 22 #include "components/search_engines/template_url.h" | 20 #include "components/search_engines/template_url.h" |
| 23 #include "components/search_engines/template_url_service.h" | 21 #include "components/search_engines/template_url_service.h" |
| 24 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
| 25 #include "content/public/browser/permission_type.h" | 23 #include "content/public/browser/permission_type.h" |
| 26 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 27 #include "jni/GeolocationHeader_jni.h" | 25 #include "jni/GeolocationHeader_jni.h" |
| 28 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" | |
| 29 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" | 26 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" |
| 30 | 27 |
| 31 namespace { | 28 namespace { |
| 32 | 29 |
| 33 const int kDefaultMaxShowCount = 3; | 30 const int kDefaultMaxShowCount = 3; |
| 34 const int kDefaultDaysPerShow = 1; | 31 const int kDefaultDaysPerShow = 1; |
| 35 const char kMaxShowCountVariation[] = "MaxShowCount"; | 32 const char kMaxShowCountVariation[] = "MaxShowCount"; |
| 36 const char kDaysPerShowVariation[] = "DaysPerShow"; | 33 const char kDaysPerShowVariation[] = "DaysPerShow"; |
| 37 | 34 |
| 38 bool gIgnoreUrlChecksForTesting = false; | |
| 39 int gDayOffsetForTesting = 0; | |
| 40 | |
| 41 int GetMaxShowCount() { | 35 int GetMaxShowCount() { |
| 42 std::string variation = variations::GetVariationParamValueByFeature( | 36 std::string variation = variations::GetVariationParamValueByFeature( |
| 43 features::kConsistentOmniboxGeolocation, kMaxShowCountVariation); | 37 features::kConsistentOmniboxGeolocation, kMaxShowCountVariation); |
| 44 int max_show; | 38 int max_show; |
| 45 if (!variation.empty() && base::StringToInt(variation, &max_show)) | 39 if (!variation.empty() && base::StringToInt(variation, &max_show)) |
| 46 return max_show; | 40 return max_show; |
| 47 | 41 |
| 48 return kDefaultMaxShowCount; | 42 return kDefaultMaxShowCount; |
| 49 } | 43 } |
| 50 | 44 |
| 51 int GetDaysPerShow() { | 45 int GetDaysPerShow() { |
| 52 std::string variation = variations::GetVariationParamValueByFeature( | 46 std::string variation = variations::GetVariationParamValueByFeature( |
| 53 features::kConsistentOmniboxGeolocation, kDaysPerShowVariation); | 47 features::kConsistentOmniboxGeolocation, kDaysPerShowVariation); |
| 54 int days_per_show; | 48 int days_per_show; |
| 55 if (!variation.empty() && base::StringToInt(variation, &days_per_show)) | 49 if (!variation.empty() && base::StringToInt(variation, &days_per_show)) |
| 56 return days_per_show; | 50 return days_per_show; |
| 57 | 51 |
| 58 return kDefaultDaysPerShow; | 52 return kDefaultDaysPerShow; |
| 59 } | 53 } |
| 60 | 54 |
| 61 base::Time GetTimeNow() { | |
| 62 return base::Time::Now() + base::TimeDelta::FromDays(gDayOffsetForTesting); | |
| 63 } | |
| 64 | |
| 65 } // namespace | 55 } // namespace |
| 66 | 56 |
| 67 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchGeolocationDisclosureTabHelper); | 57 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchGeolocationDisclosureTabHelper); |
| 68 | 58 |
| 69 SearchGeolocationDisclosureTabHelper::SearchGeolocationDisclosureTabHelper( | 59 SearchGeolocationDisclosureTabHelper::SearchGeolocationDisclosureTabHelper( |
| 70 content::WebContents* contents) | 60 content::WebContents* contents) |
| 71 : content::WebContentsObserver(contents) { | 61 : content::WebContentsObserver(contents) { |
| 72 consistent_geolocation_enabled_ = | 62 consistent_geolocation_enabled_ = |
| 73 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); | 63 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); |
| 74 } | 64 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 91 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, | 81 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, |
| 92 0); | 82 0); |
| 93 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, | 83 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 94 0); | 84 0); |
| 95 registry->RegisterBooleanPref( | 85 registry->RegisterBooleanPref( |
| 96 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | 86 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); |
| 97 registry->RegisterBooleanPref( | 87 registry->RegisterBooleanPref( |
| 98 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | 88 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); |
| 99 } | 89 } |
| 100 | 90 |
| 101 // static | |
| 102 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | |
| 103 return RegisterNativesImpl(env); | |
| 104 } | |
| 105 | |
| 106 void SearchGeolocationDisclosureTabHelper:: | 91 void SearchGeolocationDisclosureTabHelper:: |
| 107 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | 92 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { |
| 108 // Don't show in incognito. | 93 // Don't show in incognito. |
| 109 if (GetProfile()->IsOffTheRecord()) | 94 if (GetProfile()->IsOffTheRecord()) |
| 110 return; | 95 return; |
| 111 | 96 |
| 112 if (!ShouldShowDisclosureForUrl(gurl)) | 97 // Only show the disclosure for default search navigations from the omnibox. |
| 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) |
| 113 return; | 104 return; |
| 114 | 105 |
| 106 // Only show the disclosure if Google is the default search engine. |
| 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 |
| 115 // Don't show the infobar if the user has dismissed it, or they've seen it | 115 // Don't show the infobar if the user has dismissed it, or they've seen it |
| 116 // enough times already. | 116 // enough times already. |
| 117 PrefService* prefs = GetProfile()->GetPrefs(); | 117 PrefService* prefs = GetProfile()->GetPrefs(); |
| 118 bool dismissed_already = | 118 bool dismissed_already = |
| 119 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 119 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |
| 120 int shown_count = | 120 int shown_count = |
| 121 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 121 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |
| 122 if (dismissed_already || shown_count >= GetMaxShowCount()) { | 122 if (dismissed_already || shown_count >= GetMaxShowCount()) { |
| 123 // Record metrics for the state of permissions after the disclosure has been | 123 // Record metrics for the state of permissions after the disclosure has been |
| 124 // shown. This is not done immediately after showing the last disclosure | 124 // 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 | 125 // (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 | 126 // 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. | 127 // result of clicking on the Settings link in the disclosure. |
| 128 RecordPostDisclosureMetrics(gurl); | 128 RecordPostDisclosureMetrics(gurl); |
| 129 return; | 129 return; |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Or if it has been shown too recently. | 132 // Or if it has been shown too recently. |
| 133 base::Time last_shown = base::Time::FromInternalValue( | 133 base::Time last_shown = base::Time::FromInternalValue( |
| 134 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); | 134 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); |
| 135 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { | 135 if (base::Time::Now() - last_shown < |
| 136 base::TimeDelta::FromDays(GetDaysPerShow())) { |
| 136 return; | 137 return; |
| 137 } | 138 } |
| 138 | 139 |
| 139 // Check that the Chrome app has geolocation permission. | 140 // Check that the Chrome app has geolocation permission. |
| 140 JNIEnv* env = base::android::AttachCurrentThread(); | 141 JNIEnv* env = base::android::AttachCurrentThread(); |
| 141 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) | 142 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) |
| 142 return; | 143 return; |
| 143 | 144 |
| 144 // Record metrics for the state of permissions before the disclosure has been | 145 // Record metrics for the state of permissions before the disclosure has been |
| 145 // shown. | 146 // shown. |
| 146 RecordPreDisclosureMetrics(gurl); | 147 RecordPreDisclosureMetrics(gurl); |
| 147 | 148 |
| 148 // Only show the disclosure if the geolocation permission is set to ASK | 149 // Only show the disclosure if the geolocation permission is set to ASK |
| 149 // (i.e. has not been explicitly set or revoked). | 150 // (i.e. has not been explicitly set or revoked). |
| 150 blink::mojom::PermissionStatus status = | 151 blink::mojom::PermissionStatus status = |
| 151 PermissionManager::Get(GetProfile()) | 152 PermissionManager::Get(GetProfile()) |
| 152 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 153 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, |
| 153 gurl); | 154 gurl); |
| 154 if (status != blink::mojom::PermissionStatus::ASK) | 155 if (status != blink::mojom::PermissionStatus::ASK) |
| 155 return; | 156 return; |
| 156 | 157 |
| 157 // All good, let's show the disclosure and increment the shown count. | 158 // All good, let's show the disclosure and increment the shown count. |
| 158 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 159 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |
| 159 shown_count++; | 160 shown_count++; |
| 160 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 161 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |
| 161 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 162 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 162 GetTimeNow().ToInternalValue()); | 163 base::Time::Now().ToInternalValue()); |
| 163 } | |
| 164 | |
| 165 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( | |
| 166 const GURL& gurl) { | |
| 167 if (gIgnoreUrlChecksForTesting) | |
| 168 return true; | |
| 169 | |
| 170 // Only show the disclosure for default search navigations from the omnibox. | |
| 171 TemplateURLService* template_url_service = | |
| 172 TemplateURLServiceFactory::GetForProfile(GetProfile()); | |
| 173 bool is_search_url = | |
| 174 template_url_service->IsSearchResultsPageFromDefaultSearchProvider( | |
| 175 gurl); | |
| 176 if (!is_search_url) | |
| 177 return false; | |
| 178 | |
| 179 // Only show the disclosure if Google is the default search engine. | |
| 180 TemplateURL* default_search = | |
| 181 template_url_service->GetDefaultSearchProvider(); | |
| 182 if (!default_search || | |
| 183 !default_search->url_ref().HasGoogleBaseURLs( | |
| 184 template_url_service->search_terms_data())) { | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 return true; | |
| 189 } | 164 } |
| 190 | 165 |
| 191 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 166 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( |
| 192 const GURL& gurl) { | 167 const GURL& gurl) { |
| 193 PrefService* prefs = GetProfile()->GetPrefs(); | 168 PrefService* prefs = GetProfile()->GetPrefs(); |
| 194 if (!prefs->GetBoolean( | 169 if (!prefs->GetBoolean( |
| 195 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 170 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { |
| 196 blink::mojom::PermissionStatus status = | 171 blink::mojom::PermissionStatus status = |
| 197 PermissionManager::Get(GetProfile()) | 172 PermissionManager::Get(GetProfile()) |
| 198 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 173 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 222 blink::mojom::PermissionStatus::LAST) + | 197 blink::mojom::PermissionStatus::LAST) + |
| 223 1); | 198 1); |
| 224 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | 199 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, |
| 225 true); | 200 true); |
| 226 } | 201 } |
| 227 } | 202 } |
| 228 | 203 |
| 229 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | 204 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |
| 230 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 205 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 231 } | 206 } |
| 232 | |
| 233 // static | |
| 234 void SetIgnoreUrlChecksForTesting( | |
| 235 JNIEnv* env, | |
| 236 const base::android::JavaParamRef<jclass>& clazz) { | |
| 237 gIgnoreUrlChecksForTesting = true; | |
| 238 } | |
| 239 | |
| 240 // static | |
| 241 void SetDayOffsetForTesting(JNIEnv* env, | |
| 242 const base::android::JavaParamRef<jclass>& clazz, | |
| 243 jint days) { | |
| 244 gDayOffsetForTesting = days; | |
| 245 } | |
| OLD | NEW |