OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/search_geolocation_disclosure_tab_helper.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/feature_list.h" | |
10 #include "base/logging.h" | |
11 #include "base/metrics/histogram_macros.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/time/time.h" | |
14 #include "chrome/browser/android/search_geolocation_disclosure_infobar_delegate.
h" | |
15 #include "chrome/browser/permissions/permission_manager.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
18 #include "chrome/common/chrome_features.h" | |
19 #include "chrome/common/pref_names.h" | |
20 #include "components/pref_registry/pref_registry_syncable.h" | |
21 #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 "content/public/browser/permission_type.h" | |
26 #include "content/public/browser/web_contents.h" | |
27 #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" | |
30 | |
31 namespace { | |
32 | |
33 const int kDefaultMaxShowCount = 3; | |
34 const int kDefaultDaysPerShow = 1; | |
35 const char kMaxShowCountVariation[] = "MaxShowCount"; | |
36 const char kDaysPerShowVariation[] = "DaysPerShow"; | |
37 | |
38 bool gIgnoreUrlChecksForTesting = false; | |
39 int gDayOffsetForTesting = 0; | |
40 | |
41 int GetMaxShowCount() { | |
42 std::string variation = variations::GetVariationParamValueByFeature( | |
43 features::kConsistentOmniboxGeolocation, kMaxShowCountVariation); | |
44 int max_show; | |
45 if (!variation.empty() && base::StringToInt(variation, &max_show)) | |
46 return max_show; | |
47 | |
48 return kDefaultMaxShowCount; | |
49 } | |
50 | |
51 int GetDaysPerShow() { | |
52 std::string variation = variations::GetVariationParamValueByFeature( | |
53 features::kConsistentOmniboxGeolocation, kDaysPerShowVariation); | |
54 int days_per_show; | |
55 if (!variation.empty() && base::StringToInt(variation, &days_per_show)) | |
56 return days_per_show; | |
57 | |
58 return kDefaultDaysPerShow; | |
59 } | |
60 | |
61 base::Time GetTimeNow() { | |
62 return base::Time::Now() + base::TimeDelta::FromDays(gDayOffsetForTesting); | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchGeolocationDisclosureTabHelper); | |
68 | |
69 SearchGeolocationDisclosureTabHelper::SearchGeolocationDisclosureTabHelper( | |
70 content::WebContents* contents) | |
71 : content::WebContentsObserver(contents) { | |
72 consistent_geolocation_enabled_ = | |
73 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); | |
74 } | |
75 | |
76 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} | |
77 | |
78 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( | |
79 const content::LoadCommittedDetails& load_details) { | |
80 if (consistent_geolocation_enabled_) { | |
81 MaybeShowDefaultSearchGeolocationDisclosure( | |
82 web_contents()->GetVisibleURL()); | |
83 } | |
84 } | |
85 | |
86 // static | |
87 void SearchGeolocationDisclosureTabHelper::RegisterProfilePrefs( | |
88 user_prefs::PrefRegistrySyncable* registry) { | |
89 registry->RegisterBooleanPref(prefs::kSearchGeolocationDisclosureDismissed, | |
90 false); | |
91 registry->RegisterIntegerPref(prefs::kSearchGeolocationDisclosureShownCount, | |
92 0); | |
93 registry->RegisterInt64Pref(prefs::kSearchGeolocationDisclosureLastShowDate, | |
94 0); | |
95 registry->RegisterBooleanPref( | |
96 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | |
97 registry->RegisterBooleanPref( | |
98 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | |
99 } | |
100 | |
101 // static | |
102 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | |
103 return RegisterNativesImpl(env); | |
104 } | |
105 | |
106 void SearchGeolocationDisclosureTabHelper:: | |
107 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | |
108 // Don't show in incognito. | |
109 if (GetProfile()->IsOffTheRecord()) | |
110 return; | |
111 | |
112 if (!ShouldShowDisclosureForUrl(gurl)) | |
113 return; | |
114 | |
115 // Don't show the infobar if the user has dismissed it, or they've seen it | |
116 // enough times already. | |
117 PrefService* prefs = GetProfile()->GetPrefs(); | |
118 bool dismissed_already = | |
119 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | |
120 int shown_count = | |
121 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | |
122 if (dismissed_already || shown_count >= GetMaxShowCount()) { | |
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 | |
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 | |
127 // result of clicking on the Settings link in the disclosure. | |
128 RecordPostDisclosureMetrics(gurl); | |
129 return; | |
130 } | |
131 | |
132 // Or if it has been shown too recently. | |
133 base::Time last_shown = base::Time::FromInternalValue( | |
134 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); | |
135 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { | |
136 return; | |
137 } | |
138 | |
139 // Check that the Chrome app has geolocation permission. | |
140 JNIEnv* env = base::android::AttachCurrentThread(); | |
141 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) | |
142 return; | |
143 | |
144 // Record metrics for the state of permissions before the disclosure has been | |
145 // shown. | |
146 RecordPreDisclosureMetrics(gurl); | |
147 | |
148 // Only show the disclosure if the geolocation permission is set to ASK | |
149 // (i.e. has not been explicitly set or revoked). | |
150 blink::mojom::PermissionStatus status = | |
151 PermissionManager::Get(GetProfile()) | |
152 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | |
153 gurl); | |
154 if (status != blink::mojom::PermissionStatus::ASK) | |
155 return; | |
156 | |
157 // All good, let's show the disclosure and increment the shown count. | |
158 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | |
159 shown_count++; | |
160 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | |
161 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | |
162 GetTimeNow().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 } | |
190 | |
191 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | |
192 const GURL& gurl) { | |
193 PrefService* prefs = GetProfile()->GetPrefs(); | |
194 if (!prefs->GetBoolean( | |
195 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | |
196 blink::mojom::PermissionStatus status = | |
197 PermissionManager::Get(GetProfile()) | |
198 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | |
199 gurl); | |
200 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", | |
201 static_cast<base::HistogramBase::Sample>(status), | |
202 static_cast<base::HistogramBase::Sample>( | |
203 blink::mojom::PermissionStatus::LAST) + | |
204 1); | |
205 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, | |
206 true); | |
207 } | |
208 } | |
209 | |
210 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( | |
211 const GURL& gurl) { | |
212 PrefService* prefs = GetProfile()->GetPrefs(); | |
213 if (!prefs->GetBoolean( | |
214 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { | |
215 blink::mojom::PermissionStatus status = | |
216 PermissionManager::Get(GetProfile()) | |
217 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | |
218 gurl); | |
219 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PostDisclosurePermission", | |
220 static_cast<base::HistogramBase::Sample>(status), | |
221 static_cast<base::HistogramBase::Sample>( | |
222 blink::mojom::PermissionStatus::LAST) + | |
223 1); | |
224 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | |
225 true); | |
226 } | |
227 } | |
228 | |
229 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | |
230 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
231 } | |
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 |