Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: chrome/browser/android/search_geolocation/search_geolocation_disclosure_tab_helper.cc

Issue 2627853002: Show the search geolocation disclosure from geolocation API use. (Closed)
Patch Set: Feedback; use more of the new stuff Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 MaybeShowDisclosureForOmnibox(web_contents()->GetVisibleURL());
82 web_contents()->GetVisibleURL()); 83 }
83 } 84
85 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForAPIUsage(
86 const GURL& gurl) {
87 ShowDisclosureIfRulesPermit(gurl);
84 } 88 }
85 89
86 // static 90 // static
87 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { 91 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) {
88 PrefService* prefs = profile->GetPrefs(); 92 PrefService* prefs = profile->GetPrefs();
89 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); 93 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount);
90 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); 94 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate);
91 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); 95 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed);
92 } 96 }
93 97
(...skipping 10 matching lines...) Expand all
104 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); 108 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false);
105 registry->RegisterBooleanPref( 109 registry->RegisterBooleanPref(
106 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); 110 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false);
107 } 111 }
108 112
109 // static 113 // static
110 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { 114 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) {
111 return RegisterNativesImpl(env); 115 return RegisterNativesImpl(env);
112 } 116 }
113 117
114 void SearchGeolocationDisclosureTabHelper:: 118 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForOmnibox(
115 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { 119 const GURL& gurl) {
116 // Don't show in incognito. 120 // Don't show in incognito.
117 if (GetProfile()->IsOffTheRecord()) 121 if (GetProfile()->IsOffTheRecord())
118 return; 122 return;
119 123
120 if (!ShouldShowDisclosureForUrl(gurl)) 124 if (!ShouldShowDisclosureForUrl(gurl))
121 return; 125 return;
122 126
127 ShowDisclosureIfRulesPermit(gurl);
128 }
129
130 void SearchGeolocationDisclosureTabHelper::ShowDisclosureIfRulesPermit(
131 const GURL& gurl) {
132 // Check that the Chrome app has geolocation permission.
133 JNIEnv* env = base::android::AttachCurrentThread();
134 if (!Java_GeolocationHeader_hasGeolocationPermission(env))
135 return;
136
123 // Don't show the infobar if the user has dismissed it, or they've seen it 137 // Don't show the infobar if the user has dismissed it, or they've seen it
124 // enough times already. 138 // enough times already.
125 PrefService* prefs = GetProfile()->GetPrefs(); 139 PrefService* prefs = GetProfile()->GetPrefs();
126 bool dismissed_already = 140 bool dismissed_already =
127 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); 141 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed);
128 int shown_count = 142 int shown_count =
129 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); 143 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount);
130 if (dismissed_already || shown_count >= GetMaxShowCount()) { 144 if (dismissed_already || shown_count >= GetMaxShowCount()) {
131 // Record metrics for the state of permissions after the disclosure has been 145 // Record metrics for the state of permissions after the disclosure has been
132 // shown. This is not done immediately after showing the last disclosure 146 // 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 147 // (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 148 // 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. 149 // result of clicking on the Settings link in the disclosure.
136 RecordPostDisclosureMetrics(gurl); 150 RecordPostDisclosureMetrics(gurl);
137 return; 151 return;
138 } 152 }
139 153
140 // Or if it has been shown too recently. 154 // Or if it has been shown too recently.
141 base::Time last_shown = base::Time::FromInternalValue( 155 base::Time last_shown = base::Time::FromInternalValue(
142 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); 156 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate));
143 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { 157 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) {
144 return; 158 return;
145 } 159 }
146 160
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 161 // Record metrics for the state of permissions before the disclosure has been
153 // shown. 162 // shown.
154 RecordPreDisclosureMetrics(gurl); 163 RecordPreDisclosureMetrics(gurl);
155 164
156 // Only show the disclosure if the geolocation permission is set to ASK 165 // Only show the disclosure if the geolocation permission is set ask (i.e. has
157 // (i.e. has not been explicitly set or revoked). 166 // not been explicitly set or revoked).
158 blink::mojom::PermissionStatus status = 167 ContentSetting status =
159 PermissionManager::Get(GetProfile()) 168 HostContentSettingsMapFactory::GetForProfile(GetProfile())
160 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, 169 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION,
161 gurl); 170 std::string());
162 if (status != blink::mojom::PermissionStatus::ASK) 171 if (status != CONTENT_SETTING_ASK)
172 return;
173
174 // And only show disclosure if the DSE geolocation setting is on.
175 SearchGeolocationService* service =
176 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile());
177 if (!service->GetDSEGeolocationSetting())
163 return; 178 return;
164 179
165 // All good, let's show the disclosure and increment the shown count. 180 // All good, let's show the disclosure and increment the shown count.
166 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); 181 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl);
167 shown_count++; 182 shown_count++;
168 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); 183 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count);
169 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, 184 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate,
170 GetTimeNow().ToInternalValue()); 185 GetTimeNow().ToInternalValue());
171 } 186 }
172 187
173 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl( 188 bool SearchGeolocationDisclosureTabHelper::ShouldShowDisclosureForUrl(
174 const GURL& gurl) { 189 const GURL& gurl) {
175 if (gIgnoreUrlChecksForTesting) 190 if (gIgnoreUrlChecksForTesting)
176 return true; 191 return true;
177 192
178 // Only show the disclosure for default search navigations from the omnibox. 193 SearchGeolocationService* service =
179 TemplateURLService* template_url_service = 194 SearchGeolocationService::Factory::GetForBrowserContext(GetProfile());
180 TemplateURLServiceFactory::GetForProfile(GetProfile()); 195 return service->UseDSEGeolocationSetting(url::Origin(gurl));
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 } 196 }
198 197
199 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( 198 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics(
200 const GURL& gurl) { 199 const GURL& gurl) {
201 PrefService* prefs = GetProfile()->GetPrefs(); 200 PrefService* prefs = GetProfile()->GetPrefs();
202 if (!prefs->GetBoolean( 201 if (!prefs->GetBoolean(
203 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { 202 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) {
204 blink::mojom::PermissionStatus status = 203 ContentSetting status =
205 PermissionManager::Get(GetProfile()) 204 HostContentSettingsMapFactory::GetForProfile(GetProfile())
206 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, 205 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION,
207 gurl); 206 std::string());
208 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", 207 UMA_HISTOGRAM_ENUMERATION(
209 static_cast<base::HistogramBase::Sample>(status), 208 "GeolocationDisclosure.PreDisclosureContentSetting",
210 static_cast<base::HistogramBase::Sample>( 209 static_cast<base::HistogramBase::Sample>(status),
211 blink::mojom::PermissionStatus::LAST) + 210 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) +
212 1); 211 1);
213 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, 212 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded,
214 true); 213 true);
215 } 214 }
216 } 215 }
217 216
218 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( 217 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics(
219 const GURL& gurl) { 218 const GURL& gurl) {
220 PrefService* prefs = GetProfile()->GetPrefs(); 219 PrefService* prefs = GetProfile()->GetPrefs();
221 if (!prefs->GetBoolean( 220 if (!prefs->GetBoolean(
222 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { 221 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) {
223 blink::mojom::PermissionStatus status = 222 ContentSetting status =
224 PermissionManager::Get(GetProfile()) 223 HostContentSettingsMapFactory::GetForProfile(GetProfile())
225 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, 224 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION,
226 gurl); 225 std::string());
227 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PostDisclosurePermission", 226 UMA_HISTOGRAM_ENUMERATION(
228 static_cast<base::HistogramBase::Sample>(status), 227 "GeolocationDisclosure.PostDisclosureContentSetting",
229 static_cast<base::HistogramBase::Sample>( 228 static_cast<base::HistogramBase::Sample>(status),
230 blink::mojom::PermissionStatus::LAST) + 229 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) +
231 1); 230 1);
232 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, 231 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded,
233 true); 232 true);
234 } 233 }
235 } 234 }
236 235
237 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { 236 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() {
238 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 237 return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
239 } 238 }
240 239
241 // static 240 // static
242 void SetIgnoreUrlChecksForTesting( 241 void SetIgnoreUrlChecksForTesting(
243 JNIEnv* env, 242 JNIEnv* env,
244 const base::android::JavaParamRef<jclass>& clazz) { 243 const base::android::JavaParamRef<jclass>& clazz) {
245 gIgnoreUrlChecksForTesting = true; 244 gIgnoreUrlChecksForTesting = true;
246 } 245 }
247 246
248 // static 247 // static
249 void SetDayOffsetForTesting(JNIEnv* env, 248 void SetDayOffsetForTesting(JNIEnv* env,
250 const base::android::JavaParamRef<jclass>& clazz, 249 const base::android::JavaParamRef<jclass>& clazz,
251 jint days) { 250 jint days) {
252 gDayOffsetForTesting = days; 251 gDayOffsetForTesting = days;
253 } 252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698