Chromium Code Reviews| Index: chrome/browser/geolocation/geolocation_permission_context_android.cc |
| diff --git a/chrome/browser/geolocation/geolocation_permission_context_android.cc b/chrome/browser/geolocation/geolocation_permission_context_android.cc |
| index 3023c94b59d89b9670817cf4fdd41460e3c5cc4d..ed7edd5ec387ce6020ee530f383520a5efca1351 100644 |
| --- a/chrome/browser/geolocation/geolocation_permission_context_android.cc |
| +++ b/chrome/browser/geolocation/geolocation_permission_context_android.cc |
| @@ -8,6 +8,7 @@ |
| #include <vector> |
| #include "base/bind.h" |
| +#include "base/feature_list.h" |
| #include "chrome/browser/android/location_settings.h" |
| #include "chrome/browser/android/location_settings_impl.h" |
| #include "chrome/browser/android/search_geolocation/search_geolocation_disclosure_tab_helper.h" |
| @@ -15,7 +16,11 @@ |
| #include "chrome/browser/permissions/permission_request_id.h" |
| #include "chrome/browser/permissions/permission_update_infobar_delegate_android.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/common/chrome_features.h" |
| #include "components/infobars/core/infobar.h" |
| +#include "components/search_engines/template_url.h" |
| +#include "components/search_engines/template_url_service.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/web_contents.h" |
| @@ -77,7 +82,8 @@ void GeolocationPermissionContextAndroid::RequestPermission( |
| const GURL& requesting_frame_origin, |
| bool user_gesture, |
| const BrowserPermissionCallback& callback) { |
| - if (!location_settings_->CanSitesRequestLocationPermission(web_contents)) { |
| + if (!IsLocationAccessPossible(web_contents, requesting_frame_origin, |
| + user_gesture)) { |
| PermissionDecided(id, requesting_frame_origin, |
| web_contents->GetLastCommittedURL().GetOrigin(), |
| user_gesture, callback, false /* persist */, |
| @@ -128,24 +134,55 @@ void GeolocationPermissionContextAndroid::NotifyPermissionSet( |
| const BrowserPermissionCallback& callback, |
| bool persist, |
| ContentSetting content_setting) { |
| - GeolocationPermissionContext::NotifyPermissionSet(id, requesting_origin, |
| - embedding_origin, callback, |
| - persist, content_setting); |
| - |
| - // If this is the default search origin, and the DSE Geolocation setting is |
| - // being used, potentially show the disclosure. |
| - if (requesting_origin == embedding_origin) { |
| + if (content_setting == CONTENT_SETTING_ALLOW && |
| + !location_settings_->IsSystemLocationSettingEnabled()) { |
| + // There is no need to check CanShowLocationSettingsDialog here again, as it |
| + // must have been checked to get this far. |
| content::WebContents* web_contents = |
| content::WebContents::FromRenderFrameHost( |
| content::RenderFrameHost::FromID(id.render_process_id(), |
| id.render_frame_id())); |
| - SearchGeolocationDisclosureTabHelper* disclosure_helper = |
| - SearchGeolocationDisclosureTabHelper::FromWebContents(web_contents); |
| + location_settings_->PromptToEnableSystemLocationSetting( |
| + GetLocationSettingsDialogContext(requesting_origin), web_contents, |
| + base::BindOnce( |
| + &GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown, |
| + weak_factory_.GetWeakPtr(), id, requesting_origin, embedding_origin, |
| + 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
|
| + return; |
| + } |
| - // The tab helper can be null in tests. |
| - if (disclosure_helper) |
| - disclosure_helper->MaybeShowDisclosure(requesting_origin); |
| + FinishNotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| + persist, content_setting); |
| +} |
| + |
| +bool GeolocationPermissionContextAndroid::IsLocationAccessPossible( |
| + content::WebContents* web_contents, |
| + const GURL& requesting_origin, |
| + bool user_gesture) { |
| + return (location_settings_->HasAndroidLocationPermission() || |
| + location_settings_->CanPromptForAndroidLocationPermission( |
| + web_contents)) && |
| + (location_settings_->IsSystemLocationSettingEnabled() || |
| + CanShowLocationSettingsDialog(requesting_origin, user_gesture)); |
| +} |
| + |
| +LocationSettingsDialogContext |
| +GeolocationPermissionContextAndroid::GetLocationSettingsDialogContext( |
| + const GURL& requesting_origin) { |
| + bool is_dse_origin = false; |
| + TemplateURLService* template_url_service = |
| + TemplateURLServiceFactory::GetForProfile(profile()); |
| + if (template_url_service) { |
| + const TemplateURL* template_url = |
| + template_url_service->GetDefaultSearchProvider(); |
| + if (template_url) { |
| + GURL search_url = template_url->GenerateSearchURL( |
| + template_url_service->search_terms_data()); |
| + is_dse_origin = url::IsSameOriginWith(requesting_origin, search_url); |
| + } |
| } |
| + |
| + return is_dse_origin ? SEARCH : DEFAULT; |
| } |
| void GeolocationPermissionContextAndroid::HandleUpdateAndroidPermissions( |
| @@ -164,6 +201,69 @@ void GeolocationPermissionContextAndroid::HandleUpdateAndroidPermissions( |
| false /* persist */, new_setting); |
| } |
| +bool GeolocationPermissionContextAndroid::CanShowLocationSettingsDialog( |
| + const GURL& requesting_origin, |
| + bool user_gesture) { |
| + if (!base::FeatureList::IsEnabled(features::kLsdPermissionPrompt)) |
| + return false; |
| + |
| + // If this isn't the default search engine, a gesture is needed. |
| + if (GetLocationSettingsDialogContext(requesting_origin) == DEFAULT && |
| + !user_gesture) { |
| + return false; |
| + } |
| + |
| + // TODO(benwells): Also check backoff for |requesting_origin|. |
| + return location_settings_->CanPromptToEnableSystemLocationSetting(); |
| +} |
| + |
| +void GeolocationPermissionContextAndroid::OnLocationSettingsDialogShown( |
| + const PermissionRequestID& id, |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const BrowserPermissionCallback& callback, |
| + bool persist, |
| + ContentSetting content_setting, |
| + LocationSettingsDialogOutcome prompt_outcome) { |
| + if (prompt_outcome != GRANTED) { |
| + content_setting = CONTENT_SETTING_BLOCK; |
| + persist = false; |
| + } |
| + |
| + FinishNotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| + persist, content_setting); |
| +} |
| + |
| +void GeolocationPermissionContextAndroid::FinishNotifyPermissionSet( |
| + const PermissionRequestID& id, |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const BrowserPermissionCallback& callback, |
| + bool persist, |
| + ContentSetting content_setting) { |
| + GeolocationPermissionContext::NotifyPermissionSet(id, requesting_origin, |
| + embedding_origin, callback, |
| + persist, content_setting); |
| + |
| + // If this is the default search origin, and the DSE Geolocation setting is |
| + // being used, potentially show the disclosure. |
| + 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
|
| + content::WebContents* web_contents = |
| + content::WebContents::FromRenderFrameHost( |
| + content::RenderFrameHost::FromID(id.render_process_id(), |
| + id.render_frame_id())); |
| + if (!web_contents) |
| + return; |
| + |
| + SearchGeolocationDisclosureTabHelper* disclosure_helper = |
| + SearchGeolocationDisclosureTabHelper::FromWebContents(web_contents); |
| + |
| + // The tab helper can be null in tests. |
| + if (disclosure_helper) |
| + disclosure_helper->MaybeShowDisclosure(requesting_origin); |
| + } |
| +} |
| + |
| void GeolocationPermissionContextAndroid::SetLocationSettingsForTesting( |
| std::unique_ptr<LocationSettings> settings) { |
| location_settings_ = std::move(settings); |