Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/geolocation/geolocation_permission_context_android.h" | 5 #include "chrome/browser/geolocation/geolocation_permission_context_android.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/feature_list.h" | |
| 11 #include "chrome/browser/android/location_settings.h" | 12 #include "chrome/browser/android/location_settings.h" |
| 12 #include "chrome/browser/android/location_settings_impl.h" | 13 #include "chrome/browser/android/location_settings_impl.h" |
| 13 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur e_tab_helper.h" | 14 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur e_tab_helper.h" |
| 14 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h " | 15 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h " |
| 15 #include "chrome/browser/permissions/permission_request_id.h" | 16 #include "chrome/browser/permissions/permission_request_id.h" |
| 16 #include "chrome/browser/permissions/permission_update_infobar_delegate_android. h" | 17 #include "chrome/browser/permissions/permission_update_infobar_delegate_android. h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 20 #include "chrome/common/chrome_features.h" | |
| 18 #include "components/infobars/core/infobar.h" | 21 #include "components/infobars/core/infobar.h" |
| 22 #include "components/search_engines/template_url.h" | |
| 23 #include "components/search_engines/template_url_service.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 22 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 23 | 28 |
| 24 GeolocationPermissionContextAndroid:: | 29 GeolocationPermissionContextAndroid:: |
| 25 GeolocationPermissionContextAndroid(Profile* profile) | 30 GeolocationPermissionContextAndroid(Profile* profile) |
| 26 : GeolocationPermissionContext(profile), | 31 : GeolocationPermissionContext(profile), |
| 27 location_settings_(new LocationSettingsImpl()), | 32 location_settings_(new LocationSettingsImpl()), |
| 28 permission_update_infobar_(nullptr), | 33 permission_update_infobar_(nullptr), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 75 |
| 71 return value; | 76 return value; |
| 72 } | 77 } |
| 73 | 78 |
| 74 void GeolocationPermissionContextAndroid::RequestPermission( | 79 void GeolocationPermissionContextAndroid::RequestPermission( |
| 75 content::WebContents* web_contents, | 80 content::WebContents* web_contents, |
| 76 const PermissionRequestID& id, | 81 const PermissionRequestID& id, |
| 77 const GURL& requesting_frame_origin, | 82 const GURL& requesting_frame_origin, |
| 78 bool user_gesture, | 83 bool user_gesture, |
| 79 const BrowserPermissionCallback& callback) { | 84 const BrowserPermissionCallback& callback) { |
| 80 if (!location_settings_->CanSitesRequestLocationPermission(web_contents)) { | 85 if (!IsLocationAccessPossible(web_contents, requesting_frame_origin, |
| 86 user_gesture)) { | |
| 81 PermissionDecided(id, requesting_frame_origin, | 87 PermissionDecided(id, requesting_frame_origin, |
| 82 web_contents->GetLastCommittedURL().GetOrigin(), | 88 web_contents->GetLastCommittedURL().GetOrigin(), |
| 83 user_gesture, callback, false /* persist */, | 89 user_gesture, callback, false /* persist */, |
| 84 CONTENT_SETTING_BLOCK); | 90 CONTENT_SETTING_BLOCK); |
| 85 return; | 91 return; |
| 86 } | 92 } |
| 87 | 93 |
| 88 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); | 94 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); |
| 89 ContentSetting content_setting = | 95 ContentSetting content_setting = |
| 90 GeolocationPermissionContext::GetPermissionStatus(requesting_frame_origin, | 96 GeolocationPermissionContext::GetPermissionStatus(requesting_frame_origin, |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 121 GeolocationPermissionContext::CancelPermissionRequest(web_contents, id); | 127 GeolocationPermissionContext::CancelPermissionRequest(web_contents, id); |
| 122 } | 128 } |
| 123 | 129 |
| 124 void GeolocationPermissionContextAndroid::NotifyPermissionSet( | 130 void GeolocationPermissionContextAndroid::NotifyPermissionSet( |
| 125 const PermissionRequestID& id, | 131 const PermissionRequestID& id, |
| 126 const GURL& requesting_origin, | 132 const GURL& requesting_origin, |
| 127 const GURL& embedding_origin, | 133 const GURL& embedding_origin, |
| 128 const BrowserPermissionCallback& callback, | 134 const BrowserPermissionCallback& callback, |
| 129 bool persist, | 135 bool persist, |
| 130 ContentSetting content_setting) { | 136 ContentSetting content_setting) { |
| 131 GeolocationPermissionContext::NotifyPermissionSet(id, requesting_origin, | 137 if (content_setting == CONTENT_SETTING_ALLOW && |
| 132 embedding_origin, callback, | 138 !location_settings_->IsSystemLocationSettingEnabled()) { |
| 133 persist, content_setting); | 139 // There is no need to check CanShowLocationSettingsDialog here again, as it |
| 134 | 140 // must have been checked to get this far. |
| 135 // If this is the default search origin, and the DSE Geolocation setting is | |
| 136 // being used, potentially show the disclosure. | |
| 137 if (requesting_origin == embedding_origin) { | |
| 138 content::WebContents* web_contents = | 141 content::WebContents* web_contents = |
| 139 content::WebContents::FromRenderFrameHost( | 142 content::WebContents::FromRenderFrameHost( |
| 140 content::RenderFrameHost::FromID(id.render_process_id(), | 143 content::RenderFrameHost::FromID(id.render_process_id(), |
| 141 id.render_frame_id())); | 144 id.render_frame_id())); |
| 142 SearchGeolocationDisclosureTabHelper* disclosure_helper = | 145 location_settings_->PromptToEnableSystemLocationSetting( |
| 143 SearchGeolocationDisclosureTabHelper::FromWebContents(web_contents); | 146 GetLocationSettingsDialogContext(requesting_origin), web_contents, |
| 147 base::BindOnce( | |
| 148 &GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown, | |
| 149 weak_factory_.GetWeakPtr(), id, requesting_origin, embedding_origin, | |
| 150 callback, persist, content_setting)); | |
|
Ted C
2017/03/03 19:37:26
for my own knowledge if the weakptr is dead by the
benwells
2017/03/03 23:19:13
The callback won't be called, it will just deleted
Ted C
2017/03/03 23:41:13
And I guess if a caller wanted to ensure their cal
| |
| 151 return; | |
| 152 } | |
| 144 | 153 |
| 145 // The tab helper can be null in tests. | 154 FinishNotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 146 if (disclosure_helper) | 155 persist, content_setting); |
| 147 disclosure_helper->MaybeShowDisclosure(requesting_origin); | 156 } |
| 157 | |
| 158 bool GeolocationPermissionContextAndroid::IsLocationAccessPossible( | |
| 159 content::WebContents* web_contents, | |
| 160 const GURL& requesting_origin, | |
| 161 bool user_gesture) { | |
| 162 return (location_settings_->HasAndroidLocationPermission() || | |
| 163 location_settings_->CanPromptForAndroidLocationPermission( | |
| 164 web_contents)) && | |
| 165 (location_settings_->IsSystemLocationSettingEnabled() || | |
| 166 CanShowLocationSettingsDialog(requesting_origin, user_gesture)); | |
| 167 } | |
| 168 | |
| 169 LocationSettingsDialogContext | |
| 170 GeolocationPermissionContextAndroid::GetLocationSettingsDialogContext( | |
| 171 const GURL& requesting_origin) { | |
| 172 bool is_dse_origin = false; | |
| 173 TemplateURLService* template_url_service = | |
| 174 TemplateURLServiceFactory::GetForProfile(profile()); | |
| 175 if (template_url_service) { | |
| 176 const TemplateURL* template_url = | |
| 177 template_url_service->GetDefaultSearchProvider(); | |
| 178 if (template_url) { | |
| 179 GURL search_url = template_url->GenerateSearchURL( | |
| 180 template_url_service->search_terms_data()); | |
| 181 is_dse_origin = url::IsSameOriginWith(requesting_origin, search_url); | |
| 182 } | |
| 148 } | 183 } |
| 184 | |
| 185 return is_dse_origin ? SEARCH : DEFAULT; | |
| 149 } | 186 } |
| 150 | 187 |
| 151 void GeolocationPermissionContextAndroid::HandleUpdateAndroidPermissions( | 188 void GeolocationPermissionContextAndroid::HandleUpdateAndroidPermissions( |
| 152 const PermissionRequestID& id, | 189 const PermissionRequestID& id, |
| 153 const GURL& requesting_frame_origin, | 190 const GURL& requesting_frame_origin, |
| 154 const GURL& embedding_origin, | 191 const GURL& embedding_origin, |
| 155 const BrowserPermissionCallback& callback, | 192 const BrowserPermissionCallback& callback, |
| 156 bool permissions_updated) { | 193 bool permissions_updated) { |
| 157 permission_update_infobar_ = nullptr; | 194 permission_update_infobar_ = nullptr; |
| 158 | 195 |
| 159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 196 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 160 ContentSetting new_setting = permissions_updated | 197 ContentSetting new_setting = permissions_updated |
| 161 ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; | 198 ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; |
| 162 | 199 |
| 163 NotifyPermissionSet(id, requesting_frame_origin, embedding_origin, callback, | 200 NotifyPermissionSet(id, requesting_frame_origin, embedding_origin, callback, |
| 164 false /* persist */, new_setting); | 201 false /* persist */, new_setting); |
| 165 } | 202 } |
| 166 | 203 |
| 204 bool GeolocationPermissionContextAndroid::CanShowLocationSettingsDialog( | |
| 205 const GURL& requesting_origin, | |
| 206 bool user_gesture) { | |
| 207 if (!base::FeatureList::IsEnabled(features::kLsdPermissionPrompt)) | |
| 208 return false; | |
| 209 | |
| 210 // If this isn't the default search engine, a gesture is needed. | |
| 211 if (GetLocationSettingsDialogContext(requesting_origin) == DEFAULT && | |
| 212 !user_gesture) { | |
| 213 return false; | |
| 214 } | |
| 215 | |
| 216 // TODO(benwells): Also check backoff for |requesting_origin|. | |
| 217 return location_settings_->CanPromptToEnableSystemLocationSetting(); | |
| 218 } | |
| 219 | |
| 220 void GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown( | |
| 221 const PermissionRequestID& id, | |
| 222 const GURL& requesting_origin, | |
| 223 const GURL& embedding_origin, | |
| 224 const BrowserPermissionCallback& callback, | |
| 225 bool persist, | |
| 226 ContentSetting content_setting, | |
| 227 LocationSettingsDialogOutcome prompt_outcome) { | |
| 228 if (prompt_outcome != GRANTED) { | |
| 229 content_setting = CONTENT_SETTING_BLOCK; | |
| 230 persist = false; | |
| 231 } | |
| 232 | |
| 233 FinishNotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 234 persist, content_setting); | |
| 235 } | |
| 236 | |
| 237 void GeolocationPermissionContextAndroid::FinishNotifyPermissionSet( | |
| 238 const PermissionRequestID& id, | |
| 239 const GURL& requesting_origin, | |
| 240 const GURL& embedding_origin, | |
| 241 const BrowserPermissionCallback& callback, | |
| 242 bool persist, | |
| 243 ContentSetting content_setting) { | |
| 244 GeolocationPermissionContext::NotifyPermissionSet(id, requesting_origin, | |
| 245 embedding_origin, callback, | |
| 246 persist, content_setting); | |
| 247 | |
| 248 // If this is the default search origin, and the DSE Geolocation setting is | |
| 249 // being used, potentially show the disclosure. | |
| 250 if (requesting_origin == embedding_origin) { | |
|
Ted C
2017/03/03 19:37:26
Again curious, but should we only be showing this
benwells
2017/03/03 23:19:13
The code in MaybeShowDisclosure calls back into th
| |
| 251 content::WebContents* web_contents = | |
| 252 content::WebContents::FromRenderFrameHost( | |
| 253 content::RenderFrameHost::FromID(id.render_process_id(), | |
| 254 id.render_frame_id())); | |
| 255 if (!web_contents) | |
| 256 return; | |
| 257 | |
| 258 SearchGeolocationDisclosureTabHelper* disclosure_helper = | |
| 259 SearchGeolocationDisclosureTabHelper::FromWebContents(web_contents); | |
| 260 | |
| 261 // The tab helper can be null in tests. | |
| 262 if (disclosure_helper) | |
| 263 disclosure_helper->MaybeShowDisclosure(requesting_origin); | |
| 264 } | |
| 265 } | |
| 266 | |
| 167 void GeolocationPermissionContextAndroid::SetLocationSettingsForTesting( | 267 void GeolocationPermissionContextAndroid::SetLocationSettingsForTesting( |
| 168 std::unique_ptr<LocationSettings> settings) { | 268 std::unique_ptr<LocationSettings> settings) { |
| 169 location_settings_ = std::move(settings); | 269 location_settings_ = std::move(settings); |
| 170 } | 270 } |
| OLD | NEW |