| 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/search_geolocation_disclosur
e_tab_helper.h" | 5 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_infobar_delegate.h" | 15 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_infobar_delegate.h" |
| 16 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h
" | 16 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h
" |
| 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/common/chrome_features.h" | 20 #include "chrome/common/chrome_features.h" |
| 20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 22 #include "components/content_settings/core/browser/host_content_settings_map.h" | 23 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 23 #include "components/content_settings/core/common/content_settings.h" | 24 #include "components/content_settings/core/common/content_settings.h" |
| 24 #include "components/content_settings/core/common/content_settings_types.h" | 25 #include "components/content_settings/core/common/content_settings_types.h" |
| 25 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 26 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 28 #include "components/search_engines/template_url_service.h" |
| 27 #include "components/variations/variations_associated_data.h" | 29 #include "components/variations/variations_associated_data.h" |
| 28 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 29 #include "jni/GeolocationHeader_jni.h" | 31 #include "jni/GeolocationHeader_jni.h" |
| 30 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" | 32 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" |
| 31 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" | 33 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" |
| 32 #include "url/origin.h" | 34 #include "url/origin.h" |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 const int kDefaultMaxShowCount = 3; | 38 const int kDefaultMaxShowCount = 3; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 consistent_geolocation_disclosure_enabled_ = | 77 consistent_geolocation_disclosure_enabled_ = |
| 76 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation) && | 78 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation) && |
| 77 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 79 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 78 switches::kDisableSearchGeolocationDisclosure); | 80 switches::kDisableSearchGeolocationDisclosure); |
| 79 } | 81 } |
| 80 | 82 |
| 81 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} | 83 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} |
| 82 | 84 |
| 83 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( | 85 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( |
| 84 const content::LoadCommittedDetails& load_details) { | 86 const content::LoadCommittedDetails& load_details) { |
| 85 MaybeShowDisclosure(web_contents()->GetVisibleURL()); | 87 MaybeShowDisclosureForNavigation(web_contents()->GetVisibleURL()); |
| 86 } | 88 } |
| 87 | 89 |
| 88 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosure( | 90 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForAPIAccess( |
| 89 const GURL& gurl) { | 91 const GURL& gurl) { |
| 90 if (!consistent_geolocation_disclosure_enabled_) | 92 if (!consistent_geolocation_disclosure_enabled_) |
| 91 return; | 93 return; |
| 92 | 94 |
| 93 if (!ShouldShowDisclosureForUrl(gurl)) | 95 if (!ShouldShowDisclosureForAPIAccess(gurl)) |
| 94 return; | 96 return; |
| 95 | 97 |
| 98 MaybeShowDisclosureForValidUrl(gurl); |
| 99 } |
| 100 |
| 101 // static |
| 102 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { |
| 103 PrefService* prefs = profile->GetPrefs(); |
| 104 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); |
| 105 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); |
| 106 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); |
| 107 } |
| 108 |
| 109 // static |
| 110 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( |
| 111 user_prefs::PrefRegistrySyncable* registry) { |
| 112 registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, |
| 113 false); |
| 114 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, |
| 115 0); |
| 116 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 117 0); |
| 118 registry->RegisterBooleanPref( |
| 119 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); |
| 120 registry->RegisterBooleanPref( |
| 121 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); |
| 122 } |
| 123 |
| 124 // static |
| 125 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { |
| 126 return RegisterNativesImpl(env); |
| 127 } |
| 128 |
| 129 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForNavigation( |
| 130 const GURL& gurl) { |
| 131 if (!consistent_geolocation_disclosure_enabled_) |
| 132 return; |
| 133 |
| 134 if (!ShouldShowDisclosureForNavigation(gurl)) |
| 135 return; |
| 136 |
| 137 MaybeShowDisclosureForValidUrl(gurl); |
| 138 } |
| 139 |
| 140 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForValidUrl( |
| 141 const GURL& gurl) { |
| 96 // Don't show the infobar if the user has dismissed it, or they've seen it | 142 // Don't show the infobar if the user has dismissed it, or they've seen it |
| 97 // enough times already. | 143 // enough times already. |
| 98 PrefService* prefs = GetProfile()->GetPrefs(); | 144 PrefService* prefs = GetProfile()->GetPrefs(); |
| 99 bool dismissed_already = | 145 bool dismissed_already = |
| 100 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 146 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |
| 101 int shown_count = | 147 int shown_count = |
| 102 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 148 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |
| 103 if (dismissed_already || shown_count >= GetMaxShowCount()) { | 149 if (dismissed_already || shown_count >= GetMaxShowCount()) { |
| 104 // Record metrics for the state of permissions after the disclosure has been | 150 // Record metrics for the state of permissions after the disclosure has been |
| 105 // shown. This is not done immediately after showing the last disclosure | 151 // shown. This is not done immediately after showing the last disclosure |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 return; | 188 return; |
| 143 | 189 |
| 144 // All good, let's show the disclosure and increment the shown count. | 190 // All good, let's show the disclosure and increment the shown count. |
| 145 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 191 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |
| 146 shown_count++; | 192 shown_count++; |
| 147 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 193 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |
| 148 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 194 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 149 GetTimeNow().ToInternalValue()); | 195 GetTimeNow().ToInternalValue()); |
| 150 } | 196 } |
| 151 | 197 |
| 152 // static | 198 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForAPIAccess( |
| 153 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { | |
| 154 PrefService* prefs = profile->GetPrefs(); | |
| 155 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); | |
| 156 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); | |
| 157 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); | |
| 158 } | |
| 159 | |
| 160 // static | |
| 161 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( | |
| 162 user_prefs::PrefRegistrySyncable* registry) { | |
| 163 registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, | |
| 164 false); | |
| 165 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, | |
| 166 0); | |
| 167 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, | |
| 168 0); | |
| 169 registry->RegisterBooleanPref( | |
| 170 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | |
| 171 registry->RegisterBooleanPref( | |
| 172 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | |
| 173 } | |
| 174 | |
| 175 // static | |
| 176 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | |
| 177 return RegisterNativesImpl(env); | |
| 178 } | |
| 179 | |
| 180 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( | |
| 181 const GURL& gurl) { | 199 const GURL& gurl) { |
| 182 SearchGeolocationService* service = | 200 SearchGeolocationService* service = |
| 183 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile()); | 201 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile()); |
| 184 | 202 |
| 185 // Check the service first, as we don't want to show the infobar even when | 203 // Check the service first, as we don't want to show the infobar even when |
| 186 // testing if it does not exist. | 204 // testing if it does not exist. |
| 187 if (!service) | 205 if (!service) |
| 188 return false; | 206 return false; |
| 189 | 207 |
| 190 if (gIgnoreUrlChecksForTesting) | 208 if (gIgnoreUrlChecksForTesting) |
| 191 return true; | 209 return true; |
| 192 | 210 |
| 193 return service->UseDSEGeolocationSetting(url::Origin(gurl)); | 211 return service->UseDSEGeolocationSetting(url::Origin(gurl)); |
| 194 } | 212 } |
| 195 | 213 |
| 214 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForNavigation( |
| 215 const GURL& gurl) { |
| 216 if (!ShouldShowDisclosureForAPIAccess(gurl)) |
| 217 return false; |
| 218 |
| 219 if (gIgnoreUrlChecksForTesting) |
| 220 return true; |
| 221 |
| 222 // Only show the disclosure for default search navigations from the omnibox. |
| 223 TemplateURLService* template_url_service = |
| 224 TemplateURLServiceFactory::GetForProfile(GetProfile()); |
| 225 return template_url_service->IsSearchResultsPageFromDefaultSearchProvider( |
| 226 gurl); |
| 227 } |
| 228 |
| 196 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 229 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( |
| 197 const GURL& gurl) { | 230 const GURL& gurl) { |
| 198 PrefService* prefs = GetProfile()->GetPrefs(); | 231 PrefService* prefs = GetProfile()->GetPrefs(); |
| 199 if (!prefs->GetBoolean( | 232 if (!prefs->GetBoolean( |
| 200 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 233 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { |
| 201 ContentSetting status = | 234 ContentSetting status = |
| 202 HostContentSettingsMapFactory::GetForProfile(GetProfile()) | 235 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
| 203 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, | 236 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 204 std::string()); | 237 std::string()); |
| 205 UMA_HISTOGRAM_ENUMERATION( | 238 UMA_HISTOGRAM_ENUMERATION( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 const base::android::JavaParamRef<jclass>& clazz) { | 286 const base::android::JavaParamRef<jclass>& clazz) { |
| 254 gIgnoreUrlChecksForTesting = true; | 287 gIgnoreUrlChecksForTesting = true; |
| 255 } | 288 } |
| 256 | 289 |
| 257 // static | 290 // static |
| 258 void SetDayOffsetForTesting(JNIEnv* env, | 291 void SetDayOffsetForTesting(JNIEnv* env, |
| 259 const base::android::JavaParamRef<jclass>& clazz, | 292 const base::android::JavaParamRef<jclass>& clazz, |
| 260 jint days) { | 293 jint days) { |
| 261 gDayOffsetForTesting = days; | 294 gDayOffsetForTesting = days; |
| 262 } | 295 } |
| OLD | NEW |