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/feature_list.h" | 9 #include "base/feature_list.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_infobar_delegate.h" | 14 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_infobar_delegate.h" |
15 #include "chrome/browser/permissions/permission_manager.h" | 15 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h
" |
| 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
18 #include "chrome/common/chrome_features.h" | 18 #include "chrome/common/chrome_features.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 21 #include "components/content_settings/core/common/content_settings.h" |
| 22 #include "components/content_settings/core/common/content_settings_types.h" |
20 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
21 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
22 #include "components/search_engines/template_url.h" | |
23 #include "components/search_engines/template_url_service.h" | |
24 #include "components/variations/variations_associated_data.h" | 25 #include "components/variations/variations_associated_data.h" |
25 #include "content/public/browser/permission_type.h" | |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 #include "jni/GeolocationHeader_jni.h" | 27 #include "jni/GeolocationHeader_jni.h" |
28 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" | 28 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" |
29 #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" |
| 30 #include "url/origin.h" |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 const int kDefaultMaxShowCount = 3; | 34 const int kDefaultMaxShowCount = 3; |
34 const int kDefaultDaysPerShow = 1; | 35 const int kDefaultDaysPerShow = 1; |
35 const char kMaxShowCountVariation[] = "MaxShowCount"; | 36 const char kMaxShowCountVariation[] = "MaxShowCount"; |
36 const char kDaysPerShowVariation[] = "DaysPerShow"; | 37 const char kDaysPerShowVariation[] = "DaysPerShow"; |
37 | 38 |
38 bool gIgnoreUrlChecksForTesting = false; | 39 bool gIgnoreUrlChecksForTesting = false; |
39 int gDayOffsetForTesting = 0; | 40 int gDayOffsetForTesting = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
70 content::WebContents* contents) | 71 content::WebContents* contents) |
71 : content::WebContentsObserver(contents) { | 72 : content::WebContentsObserver(contents) { |
72 consistent_geolocation_enabled_ = | 73 consistent_geolocation_enabled_ = |
73 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); | 74 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); |
74 } | 75 } |
75 | 76 |
76 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} | 77 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} |
77 | 78 |
78 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( | 79 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( |
79 const content::LoadCommittedDetails& load_details) { | 80 const content::LoadCommittedDetails& load_details) { |
80 if (consistent_geolocation_enabled_) { | 81 if (consistent_geolocation_enabled_) |
81 MaybeShowDefaultSearchGeolocationDisclosure( | 82 MaybeShowDisclosure(web_contents()->GetVisibleURL()); |
82 web_contents()->GetVisibleURL()); | |
83 } | |
84 } | 83 } |
85 | 84 |
86 // static | 85 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosure( |
87 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { | 86 const GURL& gurl) { |
88 PrefService* prefs = profile->GetPrefs(); | |
89 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); | |
90 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); | |
91 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); | |
92 } | |
93 | |
94 // static | |
95 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( | |
96 user_prefs::PrefRegistrySyncable* registry) { | |
97 registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, | |
98 false); | |
99 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, | |
100 0); | |
101 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, | |
102 0); | |
103 registry->RegisterBooleanPref( | |
104 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | |
105 registry->RegisterBooleanPref( | |
106 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | |
107 } | |
108 | |
109 // static | |
110 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | |
111 return RegisterNativesImpl(env); | |
112 } | |
113 | |
114 void SearchGeolocationDisclosureTabHelper:: | |
115 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | |
116 // Don't show in incognito. | |
117 if (GetProfile()->IsOffTheRecord()) | |
118 return; | |
119 | |
120 if (!ShouldShowDisclosureForUrl(gurl)) | 87 if (!ShouldShowDisclosureForUrl(gurl)) |
121 return; | 88 return; |
122 | 89 |
| 90 // Check that the Chrome app has geolocation permission. |
| 91 JNIEnv* env = base::android::AttachCurrentThread(); |
| 92 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) |
| 93 return; |
| 94 |
123 // Don't show the infobar if the user has dismissed it, or they've seen it | 95 // Don't show the infobar if the user has dismissed it, or they've seen it |
124 // enough times already. | 96 // enough times already. |
125 PrefService* prefs = GetProfile()->GetPrefs(); | 97 PrefService* prefs = GetProfile()->GetPrefs(); |
126 bool dismissed_already = | 98 bool dismissed_already = |
127 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 99 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |
128 int shown_count = | 100 int shown_count = |
129 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 101 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |
130 if (dismissed_already || shown_count >= GetMaxShowCount()) { | 102 if (dismissed_already || shown_count >= GetMaxShowCount()) { |
131 // Record metrics for the state of permissions after the disclosure has been | 103 // Record metrics for the state of permissions after the disclosure has been |
132 // shown. This is not done immediately after showing the last disclosure | 104 // shown. This is not done immediately after showing the last disclosure |
133 // (i.e. at the end of this function), but on the next omnibox search, to | 105 // (i.e. at the end of this function), but on the next omnibox search, to |
134 // allow the metric to capture changes to settings done by the user as a | 106 // allow the metric to capture changes to settings done by the user as a |
135 // result of clicking on the Settings link in the disclosure. | 107 // result of clicking on the Settings link in the disclosure. |
136 RecordPostDisclosureMetrics(gurl); | 108 RecordPostDisclosureMetrics(gurl); |
137 return; | 109 return; |
138 } | 110 } |
139 | 111 |
140 // Or if it has been shown too recently. | 112 // Or if it has been shown too recently. |
141 base::Time last_shown = base::Time::FromInternalValue( | 113 base::Time last_shown = base::Time::FromInternalValue( |
142 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); | 114 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); |
143 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { | 115 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { |
144 return; | 116 return; |
145 } | 117 } |
146 | 118 |
147 // Check that the Chrome app has geolocation permission. | |
148 JNIEnv* env = base::android::AttachCurrentThread(); | |
149 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) | |
150 return; | |
151 | |
152 // Record metrics for the state of permissions before the disclosure has been | 119 // Record metrics for the state of permissions before the disclosure has been |
153 // shown. | 120 // shown. |
154 RecordPreDisclosureMetrics(gurl); | 121 RecordPreDisclosureMetrics(gurl); |
155 | 122 |
156 // Only show the disclosure if the geolocation permission is set to ASK | 123 // Only show the disclosure if the geolocation permission is set ask (i.e. has |
157 // (i.e. has not been explicitly set or revoked). | 124 // not been explicitly set or revoked). |
158 blink::mojom::PermissionStatus status = | 125 ContentSetting status = |
159 PermissionManager::Get(GetProfile()) | 126 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
160 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 127 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
161 gurl); | 128 std::string()); |
162 if (status != blink::mojom::PermissionStatus::ASK) | 129 if (status != CONTENT_SETTING_ASK) |
| 130 return; |
| 131 |
| 132 // And only show disclosure if the DSE geolocation setting is on. |
| 133 SearchGeolocationService* service = |
| 134 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile()); |
| 135 if (!service->GetDSEGeolocationSetting()) |
163 return; | 136 return; |
164 | 137 |
165 // All good, let's show the disclosure and increment the shown count. | 138 // All good, let's show the disclosure and increment the shown count. |
166 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 139 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |
167 shown_count++; | 140 shown_count++; |
168 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 141 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |
169 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 142 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |
170 GetTimeNow().ToInternalValue()); | 143 GetTimeNow().ToInternalValue()); |
171 } | 144 } |
172 | 145 |
| 146 // static |
| 147 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { |
| 148 PrefService* prefs = profile->GetPrefs(); |
| 149 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); |
| 150 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); |
| 151 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); |
| 152 } |
| 153 |
| 154 // static |
| 155 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( |
| 156 user_prefs::PrefRegistrySyncable* registry) { |
| 157 registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, |
| 158 false); |
| 159 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, |
| 160 0); |
| 161 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, |
| 162 0); |
| 163 registry->RegisterBooleanPref( |
| 164 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); |
| 165 registry->RegisterBooleanPref( |
| 166 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); |
| 167 } |
| 168 |
| 169 // static |
| 170 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { |
| 171 return RegisterNativesImpl(env); |
| 172 } |
| 173 |
173 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( | 174 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( |
174 const GURL& gurl) { | 175 const GURL& gurl) { |
| 176 SearchGeolocationService* service = |
| 177 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile()); |
| 178 |
| 179 // Check the service first, as we don't want to show the infobar even when |
| 180 // testing if it does not exist. |
| 181 if (!service) |
| 182 return false; |
| 183 |
175 if (gIgnoreUrlChecksForTesting) | 184 if (gIgnoreUrlChecksForTesting) |
176 return true; | 185 return true; |
177 | 186 |
178 // Only show the disclosure for default search navigations from the omnibox. | 187 return service->UseDSEGeolocationSetting(url::Origin(gurl)); |
179 TemplateURLService* template_url_service = | |
180 TemplateURLServiceFactory::GetForProfile(GetProfile()); | |
181 bool is_search_url = | |
182 template_url_service->IsSearchResultsPageFromDefaultSearchProvider( | |
183 gurl); | |
184 if (!is_search_url) | |
185 return false; | |
186 | |
187 // Only show the disclosure if Google is the default search engine. | |
188 TemplateURL* default_search = | |
189 template_url_service->GetDefaultSearchProvider(); | |
190 if (!default_search || | |
191 !default_search->url_ref().HasGoogleBaseURLs( | |
192 template_url_service->search_terms_data())) { | |
193 return false; | |
194 } | |
195 | |
196 return true; | |
197 } | 188 } |
198 | 189 |
199 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 190 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( |
200 const GURL& gurl) { | 191 const GURL& gurl) { |
201 PrefService* prefs = GetProfile()->GetPrefs(); | 192 PrefService* prefs = GetProfile()->GetPrefs(); |
202 if (!prefs->GetBoolean( | 193 if (!prefs->GetBoolean( |
203 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 194 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { |
204 blink::mojom::PermissionStatus status = | 195 ContentSetting status = |
205 PermissionManager::Get(GetProfile()) | 196 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
206 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 197 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
207 gurl); | 198 std::string()); |
208 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", | 199 UMA_HISTOGRAM_ENUMERATION( |
209 static_cast<base::HistogramBase::Sample>(status), | 200 "GeolocationDisclosure.PreDisclosureContentSetting", |
210 static_cast<base::HistogramBase::Sample>( | 201 static_cast<base::HistogramBase::Sample>(status), |
211 blink::mojom::PermissionStatus::LAST) + | 202 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) + |
212 1); | 203 1); |
213 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, | 204 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, |
214 true); | 205 true); |
215 } | 206 } |
216 } | 207 } |
217 | 208 |
218 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( | 209 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( |
219 const GURL& gurl) { | 210 const GURL& gurl) { |
220 PrefService* prefs = GetProfile()->GetPrefs(); | 211 PrefService* prefs = GetProfile()->GetPrefs(); |
221 if (!prefs->GetBoolean( | 212 if (!prefs->GetBoolean( |
222 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { | 213 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { |
223 blink::mojom::PermissionStatus status = | 214 ContentSetting status = |
224 PermissionManager::Get(GetProfile()) | 215 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
225 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 216 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
226 gurl); | 217 std::string()); |
227 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PostDisclosurePermission", | 218 UMA_HISTOGRAM_ENUMERATION( |
228 static_cast<base::HistogramBase::Sample>(status), | 219 "GeolocationDisclosure.PostDisclosureContentSetting", |
229 static_cast<base::HistogramBase::Sample>( | 220 static_cast<base::HistogramBase::Sample>(status), |
230 blink::mojom::PermissionStatus::LAST) + | 221 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) + |
231 1); | 222 1); |
232 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | 223 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, |
233 true); | 224 true); |
234 } | 225 } |
235 } | 226 } |
236 | 227 |
237 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | 228 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |
238 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 229 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
239 } | 230 } |
240 | 231 |
241 // static | 232 // static |
242 void SetIgnoreUrlChecksForTesting( | 233 void SetIgnoreUrlChecksForTesting( |
243 JNIEnv* env, | 234 JNIEnv* env, |
244 const base::android::JavaParamRef<jclass>& clazz) { | 235 const base::android::JavaParamRef<jclass>& clazz) { |
245 gIgnoreUrlChecksForTesting = true; | 236 gIgnoreUrlChecksForTesting = true; |
246 } | 237 } |
247 | 238 |
248 // static | 239 // static |
249 void SetDayOffsetForTesting(JNIEnv* env, | 240 void SetDayOffsetForTesting(JNIEnv* env, |
250 const base::android::JavaParamRef<jclass>& clazz, | 241 const base::android::JavaParamRef<jclass>& clazz, |
251 jint days) { | 242 jint days) { |
252 gDayOffsetForTesting = days; | 243 gDayOffsetForTesting = days; |
253 } | 244 } |
OLD | NEW |