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/content_settings/host_content_settings_map_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.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" | 25 #include "components/search_engines/template_url.h" |
23 #include "components/search_engines/template_url_service.h" | 26 #include "components/search_engines/template_url_service.h" |
24 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
25 #include "content/public/browser/permission_type.h" | |
26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
27 #include "jni/GeolocationHeader_jni.h" | 29 #include "jni/GeolocationHeader_jni.h" |
28 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" | 30 #include "jni/SearchGeolocationDisclosureTabHelper_jni.h" |
29 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h" | 31 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h" |
30 | 32 |
31 namespace { | 33 namespace { |
32 | 34 |
33 const int kDefaultMaxShowCount = 3; | 35 const int kDefaultMaxShowCount = 3; |
34 const int kDefaultDaysPerShow = 1; | 36 const int kDefaultDaysPerShow = 1; |
35 const char kMaxShowCountVariation[] = "MaxShowCount"; | 37 const char kMaxShowCountVariation[] = "MaxShowCount"; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 content::WebContents* contents) | 72 content::WebContents* contents) |
71 : content::WebContentsObserver(contents) { | 73 : content::WebContentsObserver(contents) { |
72 consistent_geolocation_enabled_ = | 74 consistent_geolocation_enabled_ = |
73 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); | 75 base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); |
74 } | 76 } |
75 | 77 |
76 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} | 78 SearchGeolocationDisclosureTabHelper::~SearchGeolocationDisclosureTabHelper() {} |
77 | 79 |
78 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( | 80 void SearchGeolocationDisclosureTabHelper::NavigationEntryCommitted( |
79 const content::LoadCommittedDetails& load_details) { | 81 const content::LoadCommittedDetails& load_details) { |
80 if (consistent_geolocation_enabled_) { | 82 if (consistent_geolocation_enabled_) |
81 MaybeShowDefaultSearchGeolocationDisclosure( | 83 MaybeShowDisclosureForOmnibox(web_contents()->GetVisibleURL()); |
82 web_contents()->GetVisibleURL()); | 84 } |
83 } | 85 |
86 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForAPIUsage( | |
87 const GURL& gurl) { | |
88 ShowDisclosureIfRulesPermit(gurl); | |
raymes
2017/01/12 05:28:14
I guess we don't want to check the android permiss
benwells
2017/01/12 06:10:33
I thought about this last night and deliberately d
| |
84 } | 89 } |
85 | 90 |
86 // static | 91 // static |
87 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { | 92 void SearchGeolocationDisclosureTabHelper::ResetDisclosure(Profile* profile) { |
88 PrefService* prefs = profile->GetPrefs(); | 93 PrefService* prefs = profile->GetPrefs(); |
89 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); | 94 prefs->ClearPref(prefs::kSearchGeolocationDisclosureShownCount); |
90 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); | 95 prefs->ClearPref(prefs::kSearchGeolocationDisclosureLastShowDate); |
91 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); | 96 prefs->ClearPref(prefs::kSearchGeolocationDisclosureDismissed); |
92 } | 97 } |
93 | 98 |
(...skipping 10 matching lines...) Expand all Loading... | |
104 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); | 109 prefs::kSearchGeolocationPreDisclosureMetricsRecorded, false); |
105 registry->RegisterBooleanPref( | 110 registry->RegisterBooleanPref( |
106 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); | 111 prefs::kSearchGeolocationPostDisclosureMetricsRecorded, false); |
107 } | 112 } |
108 | 113 |
109 // static | 114 // static |
110 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { | 115 bool SearchGeolocationDisclosureTabHelper::Register(JNIEnv* env) { |
111 return RegisterNativesImpl(env); | 116 return RegisterNativesImpl(env); |
112 } | 117 } |
113 | 118 |
114 void SearchGeolocationDisclosureTabHelper:: | 119 void SearchGeolocationDisclosureTabHelper::MaybeShowDisclosureForOmnibox( |
115 MaybeShowDefaultSearchGeolocationDisclosure(const GURL& gurl) { | 120 const GURL& gurl) { |
116 // Don't show in incognito. | 121 // Don't show in incognito. |
117 if (GetProfile()->IsOffTheRecord()) | 122 if (GetProfile()->IsOffTheRecord()) |
118 return; | 123 return; |
119 | 124 |
120 if (!ShouldShowDisclosureForUrl(gurl)) | 125 if (!ShouldShowDisclosureForUrl(gurl)) |
121 return; | 126 return; |
122 | 127 |
128 // Check that the Chrome app has geolocation permission. | |
129 JNIEnv* env = base::android::AttachCurrentThread(); | |
130 if (!Java_GeolocationHeader_hasGeolocationPermission(env)) | |
131 return; | |
132 | |
133 ShowDisclosureIfRulesPermit(gurl); | |
134 } | |
135 | |
136 void SearchGeolocationDisclosureTabHelper::ShowDisclosureIfRulesPermit( | |
137 const GURL& gurl) { | |
123 // Don't show the infobar if the user has dismissed it, or they've seen it | 138 // Don't show the infobar if the user has dismissed it, or they've seen it |
124 // enough times already. | 139 // enough times already. |
125 PrefService* prefs = GetProfile()->GetPrefs(); | 140 PrefService* prefs = GetProfile()->GetPrefs(); |
126 bool dismissed_already = | 141 bool dismissed_already = |
127 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); | 142 prefs->GetBoolean(prefs::kSearchGeolocationDisclosureDismissed); |
128 int shown_count = | 143 int shown_count = |
129 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); | 144 prefs->GetInteger(prefs::kSearchGeolocationDisclosureShownCount); |
130 if (dismissed_already || shown_count >= GetMaxShowCount()) { | 145 if (dismissed_already || shown_count >= GetMaxShowCount()) { |
131 // Record metrics for the state of permissions after the disclosure has been | 146 // Record metrics for the state of permissions after the disclosure has been |
132 // shown. This is not done immediately after showing the last disclosure | 147 // 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 | 148 // (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 | 149 // 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. | 150 // result of clicking on the Settings link in the disclosure. |
136 RecordPostDisclosureMetrics(gurl); | 151 RecordPostDisclosureMetrics(gurl); |
137 return; | 152 return; |
138 } | 153 } |
139 | 154 |
140 // Or if it has been shown too recently. | 155 // Or if it has been shown too recently. |
141 base::Time last_shown = base::Time::FromInternalValue( | 156 base::Time last_shown = base::Time::FromInternalValue( |
142 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); | 157 prefs->GetInt64(prefs::kSearchGeolocationDisclosureLastShowDate)); |
143 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { | 158 if (GetTimeNow() - last_shown < base::TimeDelta::FromDays(GetDaysPerShow())) { |
144 return; | 159 return; |
145 } | 160 } |
146 | 161 |
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 | 162 // Record metrics for the state of permissions before the disclosure has been |
153 // shown. | 163 // shown. |
154 RecordPreDisclosureMetrics(gurl); | 164 RecordPreDisclosureMetrics(gurl); |
155 | 165 |
156 // Only show the disclosure if the geolocation permission is set to ASK | 166 // Only show the disclosure if the geolocation permission is set ask (i.e. has |
157 // (i.e. has not been explicitly set or revoked). | 167 // not been explicitly set or revoked). |
158 blink::mojom::PermissionStatus status = | 168 ContentSetting status = |
159 PermissionManager::Get(GetProfile()) | 169 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
160 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 170 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
161 gurl); | 171 std::string()); |
162 if (status != blink::mojom::PermissionStatus::ASK) | 172 if (status != CONTENT_SETTING_ASK) |
163 return; | 173 return; |
164 | 174 |
165 // All good, let's show the disclosure and increment the shown count. | 175 // All good, let's show the disclosure and increment the shown count. |
166 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); | 176 SearchGeolocationDisclosureInfoBarDelegate::Create(web_contents(), gurl); |
167 shown_count++; | 177 shown_count++; |
168 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); | 178 prefs->SetInteger(prefs::kSearchGeolocationDisclosureShownCount, shown_count); |
169 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, | 179 prefs->SetInt64(prefs::kSearchGeolocationDisclosureLastShowDate, |
170 GetTimeNow().ToInternalValue()); | 180 GetTimeNow().ToInternalValue()); |
171 } | 181 } |
172 | 182 |
(...skipping 21 matching lines...) Expand all Loading... | |
194 } | 204 } |
195 | 205 |
196 return true; | 206 return true; |
197 } | 207 } |
198 | 208 |
199 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( | 209 void SearchGeolocationDisclosureTabHelper::RecordPreDisclosureMetrics( |
200 const GURL& gurl) { | 210 const GURL& gurl) { |
201 PrefService* prefs = GetProfile()->GetPrefs(); | 211 PrefService* prefs = GetProfile()->GetPrefs(); |
202 if (!prefs->GetBoolean( | 212 if (!prefs->GetBoolean( |
203 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { | 213 prefs::kSearchGeolocationPreDisclosureMetricsRecorded)) { |
204 blink::mojom::PermissionStatus status = | 214 ContentSetting status = |
205 PermissionManager::Get(GetProfile()) | 215 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
206 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 216 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
207 gurl); | 217 std::string()); |
208 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PreDisclosurePermission", | 218 UMA_HISTOGRAM_ENUMERATION( |
209 static_cast<base::HistogramBase::Sample>(status), | 219 "GeolocationDisclosure.PreDisclosureContentSetting", |
raymes
2017/01/12 05:28:14
Do we need to update histograms.xml?
benwells
2017/01/12 06:10:33
Done.
| |
210 static_cast<base::HistogramBase::Sample>( | 220 static_cast<base::HistogramBase::Sample>(status), |
211 blink::mojom::PermissionStatus::LAST) + | 221 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) + |
212 1); | 222 1); |
213 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, | 223 prefs->SetBoolean(prefs::kSearchGeolocationPreDisclosureMetricsRecorded, |
214 true); | 224 true); |
215 } | 225 } |
216 } | 226 } |
217 | 227 |
218 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( | 228 void SearchGeolocationDisclosureTabHelper::RecordPostDisclosureMetrics( |
219 const GURL& gurl) { | 229 const GURL& gurl) { |
220 PrefService* prefs = GetProfile()->GetPrefs(); | 230 PrefService* prefs = GetProfile()->GetPrefs(); |
221 if (!prefs->GetBoolean( | 231 if (!prefs->GetBoolean( |
222 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { | 232 prefs::kSearchGeolocationPostDisclosureMetricsRecorded)) { |
223 blink::mojom::PermissionStatus status = | 233 ContentSetting status = |
224 PermissionManager::Get(GetProfile()) | 234 HostContentSettingsMapFactory::GetForProfile(GetProfile()) |
225 ->GetPermissionStatus(content::PermissionType::GEOLOCATION, gurl, | 235 ->GetContentSetting(gurl, gurl, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
226 gurl); | 236 std::string()); |
227 UMA_HISTOGRAM_ENUMERATION("GeolocationDisclosure.PostDisclosurePermission", | 237 UMA_HISTOGRAM_ENUMERATION( |
228 static_cast<base::HistogramBase::Sample>(status), | 238 "GeolocationDisclosure.PostDisclosureContentSetting", |
229 static_cast<base::HistogramBase::Sample>( | 239 static_cast<base::HistogramBase::Sample>(status), |
230 blink::mojom::PermissionStatus::LAST) + | 240 static_cast<base::HistogramBase::Sample>(CONTENT_SETTING_NUM_SETTINGS) + |
231 1); | 241 1); |
232 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, | 242 prefs->SetBoolean(prefs::kSearchGeolocationPostDisclosureMetricsRecorded, |
233 true); | 243 true); |
234 } | 244 } |
235 } | 245 } |
236 | 246 |
237 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { | 247 Profile* SearchGeolocationDisclosureTabHelper::GetProfile() { |
238 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 248 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
239 } | 249 } |
240 | 250 |
241 // static | 251 // static |
242 void SetIgnoreUrlChecksForTesting( | 252 void SetIgnoreUrlChecksForTesting( |
243 JNIEnv* env, | 253 JNIEnv* env, |
244 const base::android::JavaParamRef<jclass>& clazz) { | 254 const base::android::JavaParamRef<jclass>& clazz) { |
245 gIgnoreUrlChecksForTesting = true; | 255 gIgnoreUrlChecksForTesting = true; |
246 } | 256 } |
247 | 257 |
248 // static | 258 // static |
249 void SetDayOffsetForTesting(JNIEnv* env, | 259 void SetDayOffsetForTesting(JNIEnv* env, |
250 const base::android::JavaParamRef<jclass>& clazz, | 260 const base::android::JavaParamRef<jclass>& clazz, |
251 jint days) { | 261 jint days) { |
252 gDayOffsetForTesting = days; | 262 gDayOffsetForTesting = days; |
253 } | 263 } |
OLD | NEW |