diff options
53 files changed, 1085 insertions, 514 deletions
diff --git a/android_webview/android_webview.gyp b/android_webview/android_webview.gyp index ca004138..f20c3be 100644 --- a/android_webview/android_webview.gyp +++ b/android_webview/android_webview.gyp @@ -224,6 +224,8 @@ 'browser/aw_message_port_message_filter.cc', 'browser/aw_message_port_message_filter.h', 'browser/aw_message_port_service.h', + 'browser/aw_permission_manager.cc', + 'browser/aw_permission_manager.h', 'browser/aw_pref_store.cc', 'browser/aw_pref_store.h', 'browser/aw_printing_message_filter.cc', diff --git a/android_webview/browser/aw_browser_context.cc b/android_webview/browser/aw_browser_context.cc index 30f59cb..fd551e1 100644 --- a/android_webview/browser/aw_browser_context.cc +++ b/android_webview/browser/aw_browser_context.cc @@ -5,6 +5,7 @@ #include "android_webview/browser/aw_browser_context.h" #include "android_webview/browser/aw_form_database_service.h" +#include "android_webview/browser/aw_permission_manager.h" #include "android_webview/browser/aw_pref_store.h" #include "android_webview/browser/aw_quota_manager_bridge.h" #include "android_webview/browser/aw_resource_context.h" @@ -351,6 +352,12 @@ content::SSLHostStateDelegate* AwBrowserContext::GetSSLHostStateDelegate() { return ssl_host_state_delegate_.get(); } +content::PermissionManager* AwBrowserContext::GetPermissionManager() { + if (!permission_manager_.get()) + permission_manager_.reset(new AwPermissionManager()); + return permission_manager_.get(); +} + void AwBrowserContext::RebuildTable( const scoped_refptr<URLEnumerator>& enumerator) { // Android WebView rebuilds from WebChromeClient.getVisitedHistory. The client diff --git a/android_webview/browser/aw_browser_context.h b/android_webview/browser/aw_browser_context.h index 1e32255..687ae53f 100644 --- a/android_webview/browser/aw_browser_context.h +++ b/android_webview/browser/aw_browser_context.h @@ -24,6 +24,7 @@ class GURL; class PrefService; namespace content { +class PermissionManager; class ResourceContext; class SSLHostStateDelegate; class WebContents; @@ -121,6 +122,7 @@ class AwBrowserContext : public content::BrowserContext, storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; content::PushMessagingService* GetPushMessagingService() override; content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionManager* GetPermissionManager() override; // visitedlink::VisitedLinkDelegate implementation. void RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) override; @@ -157,6 +159,7 @@ class AwBrowserContext : public content::BrowserContext, data_reduction_proxy_io_data_; scoped_ptr<data_reduction_proxy::DataReductionProxyService> data_reduction_proxy_service_; + scoped_ptr<content::PermissionManager> permission_manager_; DISALLOW_COPY_AND_ASSIGN(AwBrowserContext); }; diff --git a/android_webview/browser/aw_content_browser_client.cc b/android_webview/browser/aw_content_browser_client.cc index cb156c4..832aa42 100644 --- a/android_webview/browser/aw_content_browser_client.cc +++ b/android_webview/browser/aw_content_browser_client.cc @@ -6,7 +6,6 @@ #include "android_webview/browser/aw_browser_context.h" #include "android_webview/browser/aw_browser_main_parts.h" -#include "android_webview/browser/aw_browser_permission_request_delegate.h" #include "android_webview/browser/aw_contents_client_bridge_base.h" #include "android_webview/browser/aw_contents_io_thread_client.h" #include "android_webview/browser/aw_cookie_access_policy.h" @@ -29,7 +28,6 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/client_certificate_delegate.h" -#include "content/public/browser/permission_type.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -141,13 +139,6 @@ class AwAccessTokenStore : public content::AccessTokenStore { DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore); }; -void CallbackPermisisonStatusWrapper( - const base::Callback<void(content::PermissionStatus)>& callback, - bool allowed) { - callback.Run(allowed ? content::PERMISSION_STATUS_GRANTED - : content::PERMISSION_STATUS_DENIED); -} - } // anonymous namespace std::string AwContentBrowserClient::GetAcceptLangsImpl() { @@ -379,83 +370,6 @@ void AwContentBrowserClient::SelectClientCertificate( client->SelectClientCertificate(cert_request_info, delegate.Pass()); } -void AwContentBrowserClient::RequestPermission( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(content::PermissionStatus)>& callback) { - int render_process_id = web_contents->GetRenderProcessHost()->GetID(); - int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); - GURL origin = requesting_frame.GetOrigin(); - AwBrowserPermissionRequestDelegate* delegate = - AwBrowserPermissionRequestDelegate::FromID(render_process_id, - render_view_id); - switch (permission) { - case content::PermissionType::GEOLOCATION: - if (!delegate) { - DVLOG(0) << "Dropping GeolocationPermission request"; - callback.Run(content::PERMISSION_STATUS_DENIED); - return; - } - delegate->RequestGeolocationPermission( - origin, base::Bind(&CallbackPermisisonStatusWrapper, callback)); - break; - case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: - if (!delegate) { - DVLOG(0) << "Dropping ProtectedMediaIdentifierPermission request"; - callback.Run(content::PERMISSION_STATUS_DENIED); - return; - } - delegate->RequestProtectedMediaIdentifierPermission( - origin, base::Bind(&CallbackPermisisonStatusWrapper, callback)); - break; - case content::PermissionType::MIDI_SYSEX: - case content::PermissionType::NOTIFICATIONS: - case content::PermissionType::PUSH_MESSAGING: - NOTIMPLEMENTED() << "RequestPermission not implemented for " - << static_cast<int>(permission); - break; - case content::PermissionType::NUM: - NOTREACHED() << "Invalid RequestPermission for " - << static_cast<int>(permission); - break; - } -} - -void AwContentBrowserClient::CancelPermissionRequest( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& origin) { - int render_process_id = web_contents->GetRenderProcessHost()->GetID(); - int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); - AwBrowserPermissionRequestDelegate* delegate = - AwBrowserPermissionRequestDelegate::FromID(render_process_id, - render_view_id); - if (!delegate) - return; - switch (permission) { - case content::PermissionType::GEOLOCATION: - delegate->CancelGeolocationPermissionRequests(origin); - break; - case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: - delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); - break; - case content::PermissionType::MIDI_SYSEX: - case content::PermissionType::NOTIFICATIONS: - case content::PermissionType::PUSH_MESSAGING: - NOTIMPLEMENTED() << "CancelPermission not implemented for " - << static_cast<int>(permission); - break; - case content::PermissionType::NUM: - NOTREACHED() << "Invalid CancelPermission for " - << static_cast<int>(permission); - break; - } -} - bool AwContentBrowserClient::CanCreateWindow( const GURL& opener_url, const GURL& opener_top_level_frame_url, diff --git a/android_webview/browser/aw_content_browser_client.h b/android_webview/browser/aw_content_browser_client.h index 2fc83b7..5d9897e 100644 --- a/android_webview/browser/aw_content_browser_client.h +++ b/android_webview/browser/aw_content_browser_client.h @@ -105,17 +105,6 @@ class AwContentBrowserClient : public content::ContentBrowserClient { content::WebContents* web_contents, net::SSLCertRequestInfo* cert_request_info, scoped_ptr<content::ClientCertificateDelegate> delegate) override; - void RequestPermission( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(content::PermissionStatus)>& callback) override; - void CancelPermissionRequest(content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& origin) override; bool CanCreateWindow(const GURL& opener_url, const GURL& opener_top_level_frame_url, const GURL& source_origin, diff --git a/android_webview/browser/aw_permission_manager.cc b/android_webview/browser/aw_permission_manager.cc new file mode 100644 index 0000000..4e78af4 --- /dev/null +++ b/android_webview/browser/aw_permission_manager.cc @@ -0,0 +1,127 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "android_webview/browser/aw_permission_manager.h" + +#include "android_webview/browser/aw_browser_permission_request_delegate.h" +#include "base/callback.h" +#include "content/public/browser/permission_type.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents.h" + +namespace android_webview { + +namespace { + +void CallbackPermisisonStatusWrapper( + const base::Callback<void(content::PermissionStatus)>& callback, + bool allowed) { + callback.Run(allowed ? content::PERMISSION_STATUS_GRANTED + : content::PERMISSION_STATUS_DENIED); +} + +} // anonymous namespace + +AwPermissionManager::AwPermissionManager() + : content::PermissionManager() { +} + +AwPermissionManager::~AwPermissionManager() { +} + +void AwPermissionManager::RequestPermission( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& origin, + bool user_gesture, + const base::Callback<void(content::PermissionStatus)>& callback) { + int render_process_id = web_contents->GetRenderProcessHost()->GetID(); + int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); + AwBrowserPermissionRequestDelegate* delegate = + AwBrowserPermissionRequestDelegate::FromID(render_process_id, + render_view_id); + if (!delegate) { + DVLOG(0) << "Dropping permission request for " + << static_cast<int>(permission); + callback.Run(content::PERMISSION_STATUS_DENIED); + return; + } + + switch (permission) { + case content::PermissionType::GEOLOCATION: + delegate->RequestGeolocationPermission( + origin, base::Bind(&CallbackPermisisonStatusWrapper, callback)); + break; + case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: + delegate->RequestProtectedMediaIdentifierPermission( + origin, base::Bind(&CallbackPermisisonStatusWrapper, callback)); + break; + case content::PermissionType::MIDI_SYSEX: + case content::PermissionType::NOTIFICATIONS: + case content::PermissionType::PUSH_MESSAGING: + NOTIMPLEMENTED() << "RequestPermission is not implemented for " + << static_cast<int>(permission); + callback.Run(content::PERMISSION_STATUS_DENIED); + break; + case content::PermissionType::NUM: + NOTREACHED() << "PermissionType::NUM was not expected here."; + callback.Run(content::PERMISSION_STATUS_DENIED); + break; + } +} + +void AwPermissionManager::CancelPermissionRequest( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& origin) { + int render_process_id = web_contents->GetRenderProcessHost()->GetID(); + int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); + AwBrowserPermissionRequestDelegate* delegate = + AwBrowserPermissionRequestDelegate::FromID(render_process_id, + render_view_id); + if (!delegate) + return; + + switch (permission) { + case content::PermissionType::GEOLOCATION: + delegate->CancelGeolocationPermissionRequests(origin); + break; + case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: + delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); + break; + case content::PermissionType::MIDI_SYSEX: + case content::PermissionType::NOTIFICATIONS: + case content::PermissionType::PUSH_MESSAGING: + NOTIMPLEMENTED() << "CancelPermission not implemented for " + << static_cast<int>(permission); + break; + case content::PermissionType::NUM: + NOTREACHED() << "PermissionType::NUM was not expected here."; + break; + } +} + +void AwPermissionManager::ResetPermission( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { +} + +content::PermissionStatus AwPermissionManager::GetPermissionStatus( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { + return content::PERMISSION_STATUS_DENIED; +} + +void AwPermissionManager::RegisterPermissionUsage( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { +} + +} // namespace android_webview diff --git a/android_webview/browser/aw_permission_manager.h b/android_webview/browser/aw_permission_manager.h new file mode 100644 index 0000000..9b7480e --- /dev/null +++ b/android_webview/browser/aw_permission_manager.h @@ -0,0 +1,48 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ANDROID_WEBVIEW_BROWSER_AW_PERMISSION_MANAGER_H_ +#define ANDROID_WEBVIEW_BROWSER_AW_PERMISSION_MANAGER_H_ + +#include "base/callback_forward.h" +#include "base/macros.h" +#include "content/public/browser/permission_manager.h" + +namespace android_webview { + +class AwPermissionManager : public content::PermissionManager { + public: + AwPermissionManager(); + ~AwPermissionManager() override; + + // PermissionManager implementation. + void RequestPermission( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(content::PermissionStatus)>& callback) override; + void CancelPermissionRequest(content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin) override; + void ResetPermission(content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + content::PermissionStatus GetPermissionStatus( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + void RegisterPermissionUsage(content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + + private: + DISALLOW_COPY_AND_ASSIGN(AwPermissionManager); +}; + +} // namespace android_webview + +#endif // ANDROID_WEBVIEW_BROWSER_AW_PERMISSION_MANAGER_H_ diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn index b5af194..0ac7177 100644 --- a/chrome/browser/BUILD.gn +++ b/chrome/browser/BUILD.gn @@ -196,6 +196,9 @@ static_library("browser") { sources += rebase_path(gypi_values.chrome_browser_password_manager_sources, ".", "//chrome") + sources += rebase_path(gypi_values.chrome_browser_permissions_sources, + ".", + "//chrome") sources += rebase_path(gypi_values.chrome_browser_predictor_sources, ".", "//chrome") diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index 950dfdb..b717c20 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -34,11 +34,7 @@ #include "chrome/browser/download/download_prefs.h" #include "chrome/browser/font_family_cache.h" #include "chrome/browser/geolocation/chrome_access_token_store.h" -#include "chrome/browser/geolocation/geolocation_permission_context.h" -#include "chrome/browser/geolocation/geolocation_permission_context_factory.h" #include "chrome/browser/media/media_capture_devices_dispatcher.h" -#include "chrome/browser/media/midi_permission_context.h" -#include "chrome/browser/media/midi_permission_context_factory.h" #include "chrome/browser/metrics/chrome_browser_main_extra_parts_metrics.h" #include "chrome/browser/nacl_host/nacl_browser_delegate_impl.h" #include "chrome/browser/net/chrome_net_log.h" @@ -56,8 +52,6 @@ #include "chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_io_data.h" -#include "chrome/browser/push_messaging/push_messaging_permission_context.h" -#include "chrome/browser/push_messaging/push_messaging_permission_context_factory.h" #include "chrome/browser/renderer_host/chrome_render_message_filter.h" #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h" #include "chrome/browser/safe_browsing/safe_browsing_service.h" @@ -95,11 +89,9 @@ #include "chromeos/chromeos_constants.h" #include "components/cdm/browser/cdm_message_filter_android.h" #include "components/cloud_devices/common/cloud_devices_switches.h" -#include "components/content_settings/core/browser/content_settings_provider.h" #include "components/content_settings/core/browser/content_settings_utils.h" #include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/common/content_settings.h" -#include "components/content_settings/core/common/permission_request_id.h" #include "components/data_reduction_proxy/content/browser/data_reduction_proxy_message_filter.h" #include "components/dom_distiller/core/url_constants.h" #include "components/google/core/browser/google_util.h" @@ -117,7 +109,6 @@ #include "content/public/browser/child_process_data.h" #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/client_certificate_delegate.h" -#include "content/public/browser/permission_type.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -171,11 +162,6 @@ #include "chrome/browser/chrome_browser_main_posix.h" #endif -#if defined(OS_ANDROID) || defined(OS_CHROMEOS) -#include "chrome/browser/media/protected_media_identifier_permission_context.h" -#include "chrome/browser/media/protected_media_identifier_permission_context_factory.h" -#endif - #if defined(OS_POSIX) && !defined(OS_MACOSX) #include "base/debug/leak_annotations.h" #include "components/crash/app/breakpad_linux.h" @@ -589,86 +575,6 @@ void GetGuestViewDefaultContentSettingRules( } #endif // defined(ENALBE_EXTENSIONS) -content::PermissionStatus -ContentSettingToPermissionStatus(ContentSetting setting) { - switch (setting) { - case CONTENT_SETTING_ALLOW: - case CONTENT_SETTING_SESSION_ONLY: - return content::PERMISSION_STATUS_GRANTED; - case CONTENT_SETTING_BLOCK: - return content::PERMISSION_STATUS_DENIED; - case CONTENT_SETTING_ASK: - return content::PERMISSION_STATUS_ASK; - case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: - case CONTENT_SETTING_DEFAULT: - case CONTENT_SETTING_NUM_SETTINGS: - break; - } - NOTREACHED(); - return content::PERMISSION_STATUS_DENIED; -} - -PermissionContextBase* GetPermissionContext(Profile* profile, - content::PermissionType permission) { - switch (permission) { - case content::PermissionType::MIDI_SYSEX: - return MidiPermissionContextFactory::GetForProfile(profile); - case content::PermissionType::NOTIFICATIONS: -#if defined(ENABLE_NOTIFICATIONS) - return DesktopNotificationServiceFactory::GetForProfile(profile); -#else - NOTIMPLEMENTED(); - break; -#endif - case content::PermissionType::GEOLOCATION: - return GeolocationPermissionContextFactory::GetForProfile(profile); - case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: -#if defined(OS_ANDROID) || defined(OS_CHROMEOS) - return ProtectedMediaIdentifierPermissionContextFactory::GetForProfile( - profile); -#else - NOTIMPLEMENTED(); - break; -#endif - case content::PermissionType::PUSH_MESSAGING: - return PushMessagingPermissionContextFactory::GetForProfile(profile); - case content::PermissionType::NUM: - NOTREACHED() << "Invalid RequestPermission for " - << static_cast<int>(permission); - break; - } - return nullptr; -} - -// Helper method to translate from Permissions to ContentSettings -ContentSettingsType PermissionToContentSetting( - content::PermissionType permission) { - switch (permission) { - case content::PermissionType::MIDI_SYSEX: - return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; - case content::PermissionType::PUSH_MESSAGING: - return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; - case content::PermissionType::NOTIFICATIONS: - return CONTENT_SETTINGS_TYPE_NOTIFICATIONS; - case content::PermissionType::GEOLOCATION: - return CONTENT_SETTINGS_TYPE_GEOLOCATION; -#if defined(OS_ANDROID) || defined(OS_CHROMEOS) - case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: - return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER; -#endif - default: - NOTREACHED() << "Unknown content setting for permission " - << static_cast<int>(permission); - return CONTENT_SETTINGS_TYPE_DEFAULT; - } -} - -void OnRequestPermission( - const base::Callback<void(content::PermissionStatus)>& callback, - ContentSetting content_setting) { - callback.Run(ContentSettingToPermissionStatus(content_setting)); -} - } // namespace namespace chrome { @@ -1888,94 +1794,6 @@ ChromeContentBrowserClient::GetPlatformNotificationService() { #endif } -void ChromeContentBrowserClient::RequestPermission( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(content::PermissionStatus)>& result_callback) { - int render_process_id = web_contents->GetRenderProcessHost()->GetID(); - int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); - const PermissionRequestID request_id(render_process_id, - render_view_id, - bridge_id, - requesting_frame); - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - PermissionContextBase* context = GetPermissionContext(profile, permission); - - if (!context) - return; - - context->RequestPermission(web_contents, request_id, requesting_frame, - user_gesture, - base::Bind(&OnRequestPermission, result_callback)); -} - -content::PermissionStatus ChromeContentBrowserClient::GetPermissionStatus( - content::PermissionType permission, - content::BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) { - DCHECK(browser_context); - Profile* profile = Profile::FromBrowserContext(browser_context); - PermissionContextBase* context = GetPermissionContext(profile, permission); - - if (!context) - return content::PERMISSION_STATUS_ASK; - - return ContentSettingToPermissionStatus( - context->GetPermissionStatus(requesting_origin.GetOrigin(), - embedding_origin.GetOrigin())); -} - -void ChromeContentBrowserClient::ResetPermission( - content::PermissionType permission, - content::BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) { - DCHECK(browser_context); - Profile* profile = Profile::FromBrowserContext(browser_context); - PermissionContextBase* context = GetPermissionContext(profile, permission); - - if (!context) - return; - - context->ResetPermission(requesting_origin.GetOrigin(), - embedding_origin.GetOrigin()); -} - -void ChromeContentBrowserClient::CancelPermissionRequest( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame) { - int render_process_id = web_contents->GetRenderProcessHost()->GetID(); - int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); - const PermissionRequestID request_id(render_process_id, - render_view_id, - bridge_id, - requesting_frame); - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - PermissionContextBase* context = GetPermissionContext(profile, permission); - if (!context) - return; - context->CancelPermissionRequest(web_contents, request_id); -} - -void ChromeContentBrowserClient::RegisterPermissionUsage( - content::PermissionType permission, - content::WebContents* web_contents, - const GURL& frame_url, - const GURL& main_frame_url) { - Profile::FromBrowserContext(web_contents->GetBrowserContext()) - ->GetHostContentSettingsMap() - ->UpdateLastUsage( - frame_url, main_frame_url, PermissionToContentSetting(permission)); -} - bool ChromeContentBrowserClient::CanCreateWindow( const GURL& opener_url, const GURL& opener_top_level_frame_url, diff --git a/chrome/browser/chrome_content_browser_client.h b/chrome/browser/chrome_content_browser_client.h index 8677c69..6af499c 100644 --- a/chrome/browser/chrome_content_browser_client.h +++ b/chrome/browser/chrome_content_browser_client.h @@ -177,31 +177,6 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient { content::MediaObserver* GetMediaObserver() override; content::PlatformNotificationService* GetPlatformNotificationService() override; - void RequestPermission( - content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(content::PermissionStatus)>& callback) override; - content::PermissionStatus GetPermissionStatus( - content::PermissionType permission, - content::BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) override; - void ResetPermission( - content::PermissionType permission, - content::BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) override; - void CancelPermissionRequest(content::PermissionType permission, - content::WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame) override; - void RegisterPermissionUsage(content::PermissionType permission, - content::WebContents* web_contents, - const GURL& frame_url, - const GURL& main_frame_url) override; bool CanCreateWindow(const GURL& opener_url, const GURL& opener_top_level_frame_url, const GURL& source_origin, diff --git a/chrome/browser/chrome_content_browser_client_unittest.cc b/chrome/browser/chrome_content_browser_client_unittest.cc index f1175a1..6d37b76 100644 --- a/chrome/browser/chrome_content_browser_client_unittest.cc +++ b/chrome/browser/chrome_content_browser_client_unittest.cc @@ -16,14 +16,11 @@ #include "components/variations/entropy_provider.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" -#include "content/public/browser/permission_type.h" #include "content/public/browser/web_contents.h" #include "content/public/common/content_switches.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" -using content::PermissionType; - namespace chrome { using ChromeContentBrowserClientTest = testing::Test; @@ -196,69 +193,3 @@ TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) { } // namespace content #endif // !defined(OS_IOS) && !defined(OS_ANDROID) - -namespace chrome { - -// For testing permissions related functionality. -class PermissionBrowserClientTest : public testing::Test { - public: - PermissionBrowserClientTest() : url_("https://www.google.com") {} - - void CheckPermissionStatus(PermissionType type, - content::PermissionStatus expected) { - EXPECT_EQ(expected, client_.GetPermissionStatus(type, &profile_, - url_.GetOrigin(), - url_.GetOrigin())); - } - - void SetPermission(ContentSettingsType type, ContentSetting value) { - profile_.GetHostContentSettingsMap()->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_), - ContentSettingsPattern::FromURLNoWildcard(url_), - type, std::string(), value); - } - - private: - content::TestBrowserThreadBundle thread_bundle_; - ChromeContentBrowserClient client_; - TestingProfile profile_; - GURL url_; -}; - -TEST_F(PermissionBrowserClientTest, GetPermissionStatusDefault) { - using namespace content; - CheckPermissionStatus(PermissionType::MIDI_SYSEX, PERMISSION_STATUS_ASK); - CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PERMISSION_STATUS_ASK); - CheckPermissionStatus(PermissionType::NOTIFICATIONS, PERMISSION_STATUS_ASK); - CheckPermissionStatus(PermissionType::GEOLOCATION, PERMISSION_STATUS_ASK); -#if defined(OS_ANDROID) - CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, - PERMISSION_STATUS_ASK); -#endif -} - -TEST_F(PermissionBrowserClientTest, GetPermissionStatusAfterSet) { - using namespace content; - SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); - CheckPermissionStatus(PermissionType::GEOLOCATION, PERMISSION_STATUS_GRANTED); - - SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); - CheckPermissionStatus(PermissionType::NOTIFICATIONS, - PERMISSION_STATUS_GRANTED); - - SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, CONTENT_SETTING_ALLOW); - CheckPermissionStatus(PermissionType::MIDI_SYSEX, PERMISSION_STATUS_GRANTED); - - SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); - CheckPermissionStatus(PermissionType::PUSH_MESSAGING, - PERMISSION_STATUS_GRANTED); - -#if defined(OS_ANDROID) - SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, - CONTENT_SETTING_ALLOW); - CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, - PERMISSION_STATUS_GRANTED); -#endif -} - -} // namespace chrome diff --git a/chrome/browser/permissions/OWNERS b/chrome/browser/permissions/OWNERS new file mode 100644 index 0000000..2d28246 --- /dev/null +++ b/chrome/browser/permissions/OWNERS @@ -0,0 +1 @@ +mlamouri@chromium.org diff --git a/chrome/browser/permissions/permission_context.cc b/chrome/browser/permissions/permission_context.cc new file mode 100644 index 0000000..0b3d76f3 --- /dev/null +++ b/chrome/browser/permissions/permission_context.cc @@ -0,0 +1,69 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/permissions/permission_context.h" + +#include "chrome/browser/geolocation/geolocation_permission_context.h" +#include "chrome/browser/geolocation/geolocation_permission_context_factory.h" +#include "chrome/browser/media/midi_permission_context.h" +#include "chrome/browser/media/midi_permission_context_factory.h" +#include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/push_messaging/push_messaging_permission_context.h" +#include "chrome/browser/push_messaging/push_messaging_permission_context_factory.h" +#include "content/public/browser/permission_type.h" + +#if defined(OS_ANDROID) || defined(OS_CHROMEOS) +#include "chrome/browser/media/protected_media_identifier_permission_context.h" +#include "chrome/browser/media/protected_media_identifier_permission_context_factory.h" +#endif + +// static +PermissionContextBase* PermissionContext::Get( + Profile* profile, + content::PermissionType permission_type) { + // NOTE: the factories used in this method have to stay in sync with + // ::GetFactories() below. + switch (permission_type) { + case content::PermissionType::GEOLOCATION: + return GeolocationPermissionContextFactory::GetForProfile(profile); + case content::PermissionType::NOTIFICATIONS: + return DesktopNotificationServiceFactory::GetForProfile(profile); + case content::PermissionType::MIDI_SYSEX: + return MidiPermissionContextFactory::GetForProfile(profile); + case content::PermissionType::PUSH_MESSAGING: + return PushMessagingPermissionContextFactory::GetForProfile(profile); +#if defined(OS_ANDROID) || defined(OS_CHROMEOS) + case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: + return ProtectedMediaIdentifierPermissionContextFactory::GetForProfile( + profile); +#endif + default: + NOTREACHED() << "No PermissionContext associated with " + << static_cast<int>(permission_type); + break; + } + + return nullptr; +} + +// static +const std::list<KeyedServiceBaseFactory*>& PermissionContext::GetFactories() { + // NOTE: this list has to stay in sync with the factories used by ::Get(). + CR_DEFINE_STATIC_LOCAL(std::list<KeyedServiceBaseFactory*>, factories, ()); + + if (factories.empty()) { + factories.push_back(GeolocationPermissionContextFactory::GetInstance()); + factories.push_back(DesktopNotificationServiceFactory::GetInstance()); + factories.push_back(MidiPermissionContextFactory::GetInstance()); + factories.push_back(PushMessagingPermissionContextFactory::GetInstance()); +#if defined(OS_ANDROID) || defined(OS_CHROMEOS) + factories.push_back( + ProtectedMediaIdentifierPermissionContextFactory::GetInstance()); +#endif + } + + return factories; +} diff --git a/chrome/browser/permissions/permission_context.h b/chrome/browser/permissions/permission_context.h new file mode 100644 index 0000000..e6e2dac --- /dev/null +++ b/chrome/browser/permissions/permission_context.h @@ -0,0 +1,39 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_CONTEXT_H_ +#define CHROME_BROWSER_PERMISSIONS_PERMISSION_CONTEXT_H_ + +#include <list> + +#include "base/macros.h" +#include "components/content_settings/core/common/content_settings_types.h" + +class PermissionContextBase; +class Profile; +class KeyedServiceBaseFactory; + +namespace content { +enum class PermissionType; +}; // namespace content + +class PermissionContext { + public: + // Helper method returning the PermissionContextBase object associated with + // the given ContentSettingsType. + static PermissionContextBase* Get( + Profile* profile, + content::PermissionType content_settings_type); + + // Return all the factories related to PermissionContext. These are the + // factories used by ::Get() to create a PermissionContextBase. + // This is meant to be used by callers of ::Get() that need to depend on these + // factories. + static const std::list<KeyedServiceBaseFactory*>& GetFactories(); + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(PermissionContext); +}; + +#endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_CONTEXT_H_ diff --git a/chrome/browser/permissions/permission_manager.cc b/chrome/browser/permissions/permission_manager.cc new file mode 100644 index 0000000..ebde9a7 --- /dev/null +++ b/chrome/browser/permissions/permission_manager.cc @@ -0,0 +1,165 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/permissions/permission_manager.h" + +#include "base/callback.h" +#include "chrome/browser/content_settings/permission_context_base.h" +#include "chrome/browser/permissions/permission_context.h" +#include "chrome/browser/profiles/profile.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/common/permission_request_id.h" +#include "content/public/browser/permission_type.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents.h" + +namespace { + +// Helper method to convert ContentSetting to content::PermissionStatus. +content::PermissionStatus +ContentSettingToPermissionStatus(ContentSetting setting) { + switch (setting) { + case CONTENT_SETTING_ALLOW: + case CONTENT_SETTING_SESSION_ONLY: + return content::PERMISSION_STATUS_GRANTED; + case CONTENT_SETTING_BLOCK: + return content::PERMISSION_STATUS_DENIED; + case CONTENT_SETTING_ASK: + return content::PERMISSION_STATUS_ASK; + case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: + case CONTENT_SETTING_DEFAULT: + case CONTENT_SETTING_NUM_SETTINGS: + break; + } + + NOTREACHED(); + return content::PERMISSION_STATUS_DENIED; +} + +// Helper method to convert content::PermissionType to ContentSettingType. +ContentSettingsType PermissionTypeToContentSetting( + content::PermissionType permission) { + switch (permission) { + case content::PermissionType::MIDI_SYSEX: + return CONTENT_SETTINGS_TYPE_MIDI_SYSEX; + case content::PermissionType::PUSH_MESSAGING: + return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; + case content::PermissionType::NOTIFICATIONS: + return CONTENT_SETTINGS_TYPE_NOTIFICATIONS; + case content::PermissionType::GEOLOCATION: + return CONTENT_SETTINGS_TYPE_GEOLOCATION; + case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: +#if defined(OS_ANDROID) || defined(OS_CHROMEOS) + return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER; +#else + NOTIMPLEMENTED(); + break; +#endif + case content::PermissionType::NUM: + // This will hit the NOTREACHED below. + break; + } + + NOTREACHED() << "Unknown content setting for permission " + << static_cast<int>(permission); + return CONTENT_SETTINGS_TYPE_DEFAULT; +} + +// Helper method that wraps a callback a void(content::PermissionStatus) +// callback into a void(ContentSetting) callback. +void PermissionStatusCallbackWrapper( + const base::Callback<void(content::PermissionStatus)>& callback, + ContentSetting content_setting) { + callback.Run(ContentSettingToPermissionStatus(content_setting)); +} + +} // anonymous namespace + +PermissionManager::PermissionManager(Profile* profile) + : profile_(profile) { +} + +PermissionManager::~PermissionManager() { +} + +void PermissionManager::RequestPermission( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(content::PermissionStatus)>& callback) { + PermissionContextBase* context = PermissionContext::Get(profile_, permission); + if (!context) { + callback.Run(content::PERMISSION_STATUS_DENIED); + return; + } + + int render_process_id = web_contents->GetRenderProcessHost()->GetID(); + int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); + const PermissionRequestID request(render_process_id, + render_view_id, + request_id, + requesting_origin); + + context->RequestPermission(web_contents, request, requesting_origin, + user_gesture, + base::Bind(&PermissionStatusCallbackWrapper, + callback)); +} + +void PermissionManager::CancelPermissionRequest( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin) { + PermissionContextBase* context = PermissionContext::Get(profile_, permission); + if (!context) + return; + + int render_process_id = web_contents->GetRenderProcessHost()->GetID(); + int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); + const PermissionRequestID request(render_process_id, + render_view_id, + request_id, + requesting_origin); + + context->CancelPermissionRequest(web_contents, request); +} + +void PermissionManager::ResetPermission( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { + PermissionContextBase* context = PermissionContext::Get(profile_, permission); + if (!context) + return; + + context->ResetPermission(requesting_origin.GetOrigin(), + embedding_origin.GetOrigin()); +} + +content::PermissionStatus PermissionManager::GetPermissionStatus( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { + PermissionContextBase* context = PermissionContext::Get(profile_, permission); + if (!context) + return content::PERMISSION_STATUS_DENIED; + + return ContentSettingToPermissionStatus( + context->GetPermissionStatus(requesting_origin.GetOrigin(), + embedding_origin.GetOrigin())); +} + +void PermissionManager::RegisterPermissionUsage( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { + profile_->GetHostContentSettingsMap()->UpdateLastUsage( + requesting_origin, + embedding_origin, + PermissionTypeToContentSetting(permission)); +} diff --git a/chrome/browser/permissions/permission_manager.h b/chrome/browser/permissions/permission_manager.h new file mode 100644 index 0000000..f4b3082 --- /dev/null +++ b/chrome/browser/permissions/permission_manager.h @@ -0,0 +1,54 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ +#define CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ + +#include "base/callback_forward.h" +#include "base/macros.h" +#include "components/keyed_service/core/keyed_service.h" +#include "content/public/browser/permission_manager.h" + +class Profile; + +namespace content { +enum class PermissionType; +}; // namespace content + +class PermissionManager : public KeyedService, + public content::PermissionManager { + public: + explicit PermissionManager(Profile* profile); + ~PermissionManager() override; + + // content::PermissionManager implementation. + void RequestPermission( + content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(content::PermissionStatus)>& callback) override; + void CancelPermissionRequest(content::PermissionType permission, + content::WebContents* web_contents, + int request_id, + const GURL& requesting_origin) override; + void ResetPermission(content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + content::PermissionStatus GetPermissionStatus( + content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + void RegisterPermissionUsage(content::PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + + private: + Profile* profile_; + + DISALLOW_COPY_AND_ASSIGN(PermissionManager); +}; + +#endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_H_ diff --git a/chrome/browser/permissions/permission_manager_factory.cc b/chrome/browser/permissions/permission_manager_factory.cc new file mode 100644 index 0000000..19dda4e --- /dev/null +++ b/chrome/browser/permissions/permission_manager_factory.cc @@ -0,0 +1,45 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/permissions/permission_manager_factory.h" + +#include "chrome/browser/permissions/permission_context.h" +#include "chrome/browser/permissions/permission_manager.h" +#include "chrome/browser/profiles/incognito_helpers.h" +#include "chrome/browser/profiles/profile.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" + +// static +PermissionManager* +PermissionManagerFactory::GetForProfile(Profile* profile) { + return static_cast<PermissionManager*>( + GetInstance()->GetServiceForBrowserContext(profile, true)); +} + +// static +PermissionManagerFactory* PermissionManagerFactory::GetInstance() { + return Singleton<PermissionManagerFactory>::get(); +} + +PermissionManagerFactory::PermissionManagerFactory() + : BrowserContextKeyedServiceFactory( + "PermissionManagerFactory", + BrowserContextDependencyManager::GetInstance()) { + for (KeyedServiceBaseFactory* factory : PermissionContext::GetFactories()) + DependsOn(factory); +} + +PermissionManagerFactory::~PermissionManagerFactory() { +} + +KeyedService* PermissionManagerFactory::BuildServiceInstanceFor( + content::BrowserContext* context) const { + return new PermissionManager(Profile::FromBrowserContext(context)); +} + +content::BrowserContext* +PermissionManagerFactory::GetBrowserContextToUse( + content::BrowserContext* context) const { + return chrome::GetBrowserContextOwnInstanceInIncognito(context); +} diff --git a/chrome/browser/permissions/permission_manager_factory.h b/chrome/browser/permissions/permission_manager_factory.h new file mode 100644 index 0000000..2774a9a --- /dev/null +++ b/chrome/browser/permissions/permission_manager_factory.h @@ -0,0 +1,38 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_FACTORY_H_ +#define CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_FACTORY_H_ + +#include "base/memory/singleton.h" +#include "components/keyed_service/content/browser_context_keyed_service_factory.h" + +namespace content { +class BrowserContext; +} + +class PermissionManager; +class Profile; + +class PermissionManagerFactory : public BrowserContextKeyedServiceFactory { + public: + static PermissionManager* GetForProfile(Profile* profile); + static PermissionManagerFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits<PermissionManagerFactory>; + + PermissionManagerFactory(); + ~PermissionManagerFactory() override; + + // BrowserContextKeyedBaseFactory methods: + KeyedService* BuildServiceInstanceFor( + content::BrowserContext* profile) const override; + content::BrowserContext* GetBrowserContextToUse( + content::BrowserContext* context) const override; + + DISALLOW_COPY_AND_ASSIGN(PermissionManagerFactory); +}; + +#endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_MANAGER_FACTORY_H_ diff --git a/chrome/browser/permissions/permission_manager_unittest.cc b/chrome/browser/permissions/permission_manager_unittest.cc new file mode 100644 index 0000000..b737ca1 --- /dev/null +++ b/chrome/browser/permissions/permission_manager_unittest.cc @@ -0,0 +1,95 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/permissions/permission_manager.h" + +#include "chrome/browser/permissions/permission_manager_factory.h" +#include "chrome/test/base/testing_profile.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" +#include "content/public/browser/permission_type.h" +#include "content/public/test/test_browser_thread_bundle.h" +#include "testing/gtest/include/gtest/gtest.h" + +using content::PermissionType; +using content::PermissionStatus; + +namespace { + +class PermissionManagerTestingProfile final : public TestingProfile { + public: + PermissionManagerTestingProfile() {} + ~PermissionManagerTestingProfile() override {} + + PermissionManager* GetPermissionManager() override { + return PermissionManagerFactory::GetForProfile(this); + } + + DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); +}; + +} // anonymous namespace + +class PermissionManagerTest : public testing::Test { + public: + PermissionManagerTest() : url_("https://example.com") {} + + void CheckPermissionStatus(PermissionType type, + PermissionStatus expected) { + EXPECT_EQ(expected, + profile_.GetPermissionManager()->GetPermissionStatus( + type, url_.GetOrigin(), url_.GetOrigin())); + } + + void SetPermission(ContentSettingsType type, ContentSetting value) { + profile_.GetHostContentSettingsMap()->SetContentSetting( + ContentSettingsPattern::FromURLNoWildcard(url_), + ContentSettingsPattern::FromURLNoWildcard(url_), + type, std::string(), value); + } + + private: + const GURL url_; + content::TestBrowserThreadBundle thread_bundle_; + PermissionManagerTestingProfile profile_; +}; + +TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { + CheckPermissionStatus(PermissionType::MIDI_SYSEX, + content::PERMISSION_STATUS_ASK); + CheckPermissionStatus(PermissionType::PUSH_MESSAGING, + content::PERMISSION_STATUS_ASK); + CheckPermissionStatus(PermissionType::NOTIFICATIONS, + content::PERMISSION_STATUS_ASK); + CheckPermissionStatus(PermissionType::GEOLOCATION, + content::PERMISSION_STATUS_ASK); +#if defined(OS_ANDROID) + CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, + content::PERMISSION_STATUS_ASK); +#endif +} + +TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) { + SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); + CheckPermissionStatus(PermissionType::GEOLOCATION, + content::PERMISSION_STATUS_GRANTED); + + SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); + CheckPermissionStatus(PermissionType::NOTIFICATIONS, + content::PERMISSION_STATUS_GRANTED); + + SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, CONTENT_SETTING_ALLOW); + CheckPermissionStatus(PermissionType::MIDI_SYSEX, + content::PERMISSION_STATUS_GRANTED); + + SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); + CheckPermissionStatus(PermissionType::PUSH_MESSAGING, + content::PERMISSION_STATUS_GRANTED); + +#if defined(OS_ANDROID) + SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, + CONTENT_SETTING_ALLOW); + CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, + content::PERMISSION_STATUS_GRANTED); +#endif +} diff --git a/chrome/browser/profiles/off_the_record_profile_impl.cc b/chrome/browser/profiles/off_the_record_profile_impl.cc index 909b894..a363530 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl.cc +++ b/chrome/browser/profiles/off_the_record_profile_impl.cc @@ -23,6 +23,8 @@ #include "chrome/browser/net/chrome_url_request_context_getter.h" #include "chrome/browser/net/pref_proxy_config_tracker.h" #include "chrome/browser/net/proxy_service_factory.h" +#include "chrome/browser/permissions/permission_manager.h" +#include "chrome/browser/permissions/permission_manager_factory.h" #include "chrome/browser/plugins/chrome_plugin_service_filter.h" #include "chrome/browser/plugins/plugin_prefs.h" #include "chrome/browser/prefs/incognito_mode_prefs.h" @@ -439,6 +441,12 @@ OffTheRecordProfileImpl::GetSSLHostStateDelegate() { return ChromeSSLHostStateDelegateFactory::GetForProfile(this); } +// TODO(mlamouri): we should all these BrowserContext implementation to Profile +// instead of repeating them inside all Profile implementations. +content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() { + return PermissionManagerFactory::GetForProfile(this); +} + bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) { return (profile == this) || (profile == profile_); } diff --git a/chrome/browser/profiles/off_the_record_profile_impl.h b/chrome/browser/profiles/off_the_record_profile_impl.h index db86507..ce70534 100644 --- a/chrome/browser/profiles/off_the_record_profile_impl.h +++ b/chrome/browser/profiles/off_the_record_profile_impl.h @@ -104,6 +104,7 @@ class OffTheRecordProfileImpl : public Profile { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; content::PushMessagingService* GetPushMessagingService() override; content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionManager* GetPermissionManager() override; private: void InitIoData(); diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index aa365dd..535d3c8 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -44,6 +44,8 @@ #include "chrome/browser/net/pref_proxy_config_tracker.h" #include "chrome/browser/net/proxy_service_factory.h" #include "chrome/browser/net/ssl_config_service_manager.h" +#include "chrome/browser/permissions/permission_manager.h" +#include "chrome/browser/permissions/permission_manager_factory.h" #include "chrome/browser/plugins/chrome_plugin_service_filter.h" #include "chrome/browser/plugins/plugin_prefs.h" #include "chrome/browser/policy/profile_policy_connector.h" @@ -1083,6 +1085,12 @@ content::SSLHostStateDelegate* ProfileImpl::GetSSLHostStateDelegate() { return ChromeSSLHostStateDelegateFactory::GetForProfile(this); } +// TODO(mlamouri): we should all these BrowserContext implementation to Profile +// instead of repeating them inside all Profile implementations. +content::PermissionManager* ProfileImpl::GetPermissionManager() { + return PermissionManagerFactory::GetForProfile(this); +} + bool ProfileImpl::IsSameProfile(Profile* profile) { if (profile == static_cast<Profile*>(this)) return true; diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h index c66f140..14599cf 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -88,6 +88,7 @@ class ProfileImpl : public Profile { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; content::PushMessagingService* GetPushMessagingService() override; content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionManager* GetPermissionManager() override; // Profile implementation: scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() override; diff --git a/chrome/browser/ui/app_list/test/fake_profile.cc b/chrome/browser/ui/app_list/test/fake_profile.cc index 43e5e61..b3082a3 100644 --- a/chrome/browser/ui/app_list/test/fake_profile.cc +++ b/chrome/browser/ui/app_list/test/fake_profile.cc @@ -80,6 +80,10 @@ content::SSLHostStateDelegate* FakeProfile::GetSSLHostStateDelegate() { return nullptr; } +content::PermissionManager* FakeProfile::GetPermissionManager() { + return nullptr; +} + scoped_refptr<base::SequencedTaskRunner> FakeProfile::GetIOTaskRunner() { return scoped_refptr<base::SequencedTaskRunner>(); diff --git a/chrome/browser/ui/app_list/test/fake_profile.h b/chrome/browser/ui/app_list/test/fake_profile.h index f24ec25..ddfc213 100644 --- a/chrome/browser/ui/app_list/test/fake_profile.h +++ b/chrome/browser/ui/app_list/test/fake_profile.h @@ -51,6 +51,7 @@ class FakeProfile : public Profile { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; content::PushMessagingService* GetPushMessagingService() override; content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionManager* GetPermissionManager() override; scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() override; Profile* GetOffTheRecordProfile() override; void DestroyOffTheRecordProfile() override; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 35f13c2..9aef5aa 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1960,6 +1960,14 @@ 'browser/password_manager/sync_metrics.cc', 'browser/password_manager/sync_metrics.h', ], + 'chrome_browser_permissions_sources': [ + 'browser/permissions/permission_context.cc', + 'browser/permissions/permission_context.h', + 'browser/permissions/permission_manager.cc', + 'browser/permissions/permission_manager.h', + 'browser/permissions/permission_manager_factory.cc', + 'browser/permissions/permission_manager_factory.h', + ], # See also the plugin_installation_sources list below. 'chrome_browser_plugins_sources': [ 'browser/browsing_data/browsing_data_flash_lso_helper.cc', @@ -3112,6 +3120,7 @@ '<@(chrome_browser_metrics_sources)', '<@(chrome_browser_net_sources)', '<@(chrome_browser_password_manager_sources)', + '<@(chrome_browser_permissions_sources)', '<@(chrome_browser_predictor_sources)', '<@(chrome_browser_pref_sources)', '<@(chrome_browser_profiles_sources)', diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 9d6ddb2..c28a001 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -172,6 +172,7 @@ 'browser/password_manager/password_store_mac_unittest.cc', 'browser/password_manager/password_store_win_unittest.cc', 'browser/password_manager/password_store_x_unittest.cc', + 'browser/permissions/permission_manager_unittest.cc', 'browser/predictors/autocomplete_action_predictor_table_unittest.cc', 'browser/predictors/autocomplete_action_predictor_unittest.cc', 'browser/predictors/resource_prefetch_common_unittest.cc', diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc index d33aeef..22b166d 100644 --- a/chrome/test/base/testing_profile.cc +++ b/chrome/test/base/testing_profile.cc @@ -1047,6 +1047,10 @@ content::SSLHostStateDelegate* TestingProfile::GetSSLHostStateDelegate() { return NULL; } +content::PermissionManager* TestingProfile::GetPermissionManager() { + return NULL; +} + bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { return true; } diff --git a/chrome/test/base/testing_profile.h b/chrome/test/base/testing_profile.h index 1cf11c4..6b5a41f 100644 --- a/chrome/test/base/testing_profile.h +++ b/chrome/test/base/testing_profile.h @@ -238,6 +238,7 @@ class TestingProfile : public Profile { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; content::PushMessagingService* GetPushMessagingService() override; content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionManager* GetPermissionManager() override; TestingProfile* AsTestingProfile() override; diff --git a/content/browser/download/download_manager_impl_unittest.cc b/content/browser/download/download_manager_impl_unittest.cc index 6b78505..8b8fb7d 100644 --- a/content/browser/download/download_manager_impl_unittest.cc +++ b/content/browser/download/download_manager_impl_unittest.cc @@ -420,6 +420,7 @@ class MockBrowserContext : public BrowserContext { MOCK_METHOD0(GetSpecialStoragePolicy, storage::SpecialStoragePolicy*()); MOCK_METHOD0(GetPushMessagingService, PushMessagingService*()); MOCK_METHOD0(GetSSLHostStateDelegate, SSLHostStateDelegate*()); + MOCK_METHOD0(GetPermissionManager, PermissionManager*()); scoped_ptr<ZoomLevelDelegate> CreateZoomLevelDelegate( const base::FilePath& path) override { diff --git a/content/browser/frame_host/render_frame_host_impl.cc b/content/browser/frame_host/render_frame_host_impl.cc index 0a6c18a..80bb605 100644 --- a/content/browser/frame_host/render_frame_host_impl.cc +++ b/content/browser/frame_host/render_frame_host_impl.cc @@ -53,6 +53,7 @@ #include "content/public/browser/browser_plugin_guest_manager.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/content_browser_client.h" +#include "content/public/browser/permission_manager.h" #include "content/public/browser/permission_type.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_widget_host_view.h" @@ -1942,12 +1943,16 @@ void RenderFrameHostImpl::CancelSuspendedNavigations() { } void RenderFrameHostImpl::DidUseGeolocationPermission() { - RenderFrameHost* top_frame = frame_tree_node()->frame_tree()->GetMainFrame(); - GetContentClient()->browser()->RegisterPermissionUsage( + PermissionManager* permission_manager = + GetSiteInstance()->GetBrowserContext()->GetPermissionManager(); + if (!permission_manager) + return; + + permission_manager->RegisterPermissionUsage( PermissionType::GEOLOCATION, - delegate_->GetAsWebContents(), GetLastCommittedURL().GetOrigin(), - top_frame->GetLastCommittedURL().GetOrigin()); + frame_tree_node()->frame_tree()->GetMainFrame() + ->GetLastCommittedURL().GetOrigin()); } } // namespace content diff --git a/content/browser/media/cdm/browser_cdm_manager.cc b/content/browser/media/cdm/browser_cdm_manager.cc index 6f3c28d..bf3bca6 100644 --- a/content/browser/media/cdm/browser_cdm_manager.cc +++ b/content/browser/media/cdm/browser_cdm_manager.cc @@ -11,8 +11,10 @@ #include "base/memory/scoped_ptr.h" #include "base/task_runner.h" #include "content/common/media/cdm_messages.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/content_browser_client.h" +#include "content/public/browser/permission_manager.h" #include "content/public/browser/permission_type.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" @@ -519,12 +521,17 @@ void BrowserCdmManager::CheckPermissionStatusOnUIThread( RenderFrameHost* rfh = RenderFrameHost::FromID(render_process_id_, render_frame_id); WebContents* web_contents = WebContents::FromRenderFrameHost(rfh); - GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); + PermissionManager* permission_manager = + web_contents->GetBrowserContext()->GetPermissionManager(); + if (!permission_manager) { + permission_status_cb.Run(false); + return; + } - PermissionStatus permission_status = - GetContentClient()->browser()->GetPermissionStatus( - content::PermissionType::PROTECTED_MEDIA_IDENTIFIER, - web_contents->GetBrowserContext(), security_origin, embedding_origin); + PermissionStatus permission_status = permission_manager->GetPermissionStatus( + content::PermissionType::PROTECTED_MEDIA_IDENTIFIER, + security_origin, + web_contents->GetLastCommittedURL().GetOrigin()); bool allowed = (permission_status == PERMISSION_STATUS_GRANTED); if (!task_runner_->RunsTasksOnCurrentThread()) { diff --git a/content/browser/permissions/permission_service_impl.cc b/content/browser/permissions/permission_service_impl.cc index 67a7379..8ed8ffd 100644 --- a/content/browser/permissions/permission_service_impl.cc +++ b/content/browser/permissions/permission_service_impl.cc @@ -4,7 +4,9 @@ #include "content/browser/permissions/permission_service_impl.h" -#include "content/public/browser/content_browser_client.h" +#include "base/bind.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/permission_manager.h" #include "content/public/browser/permission_type.h" namespace content { @@ -78,11 +80,18 @@ void PermissionServiceImpl::RequestPermission( return; } + BrowserContext* browser_context = context_->GetBrowserContext(); + DCHECK(browser_context); + if (!browser_context->GetPermissionManager()) { + callback.Run(content::PERMISSION_STATUS_DENIED); + return; + } + PermissionType permission_type = PermissionNameToPermissionType(permission); int request_id = pending_requests_.Add( new PendingRequest(permission_type, GURL(origin), callback)); - GetContentClient()->browser()->RequestPermission( + browser_context->GetPermissionManager()->RequestPermission( permission_type, context_->web_contents(), request_id, @@ -105,10 +114,16 @@ void PermissionServiceImpl::OnRequestPermissionResponse( void PermissionServiceImpl::CancelPendingRequests() { DCHECK(context_->web_contents()); + DCHECK(context_->GetBrowserContext()); + + PermissionManager* permission_manager = + context_->GetBrowserContext()->GetPermissionManager(); + if (!permission_manager) + return; for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); !it.IsAtEnd(); it.Advance()) { - GetContentClient()->browser()->CancelPermissionRequest( + permission_manager->CancelPermissionRequest( it.GetCurrentValue()->permission, context_->web_contents(), it.GetCurrentKey(), @@ -150,20 +165,28 @@ void PermissionServiceImpl::RevokePermission( PermissionStatus PermissionServiceImpl::GetPermissionStatus(PermissionType type, GURL origin) { + BrowserContext* browser_context = context_->GetBrowserContext(); + DCHECK(browser_context); + if (!browser_context->GetPermissionManager()) + return content::PERMISSION_STATUS_DENIED; + // If the embedding_origin is empty we'll use |origin| instead. GURL embedding_origin = context_->GetEmbeddingOrigin(); - return GetContentClient()->browser()->GetPermissionStatus( - type, context_->GetBrowserContext(), origin, - embedding_origin.is_empty() ? origin : embedding_origin); + return browser_context->GetPermissionManager()->GetPermissionStatus( + type, origin, embedding_origin.is_empty() ? origin : embedding_origin); } void PermissionServiceImpl::ResetPermissionStatus(PermissionType type, GURL origin) { + BrowserContext* browser_context = context_->GetBrowserContext(); + DCHECK(browser_context); + if (!browser_context->GetPermissionManager()) + return; + // If the embedding_origin is empty we'll use |origin| instead. GURL embedding_origin = context_->GetEmbeddingOrigin(); - GetContentClient()->browser()->ResetPermission( - type, context_->GetBrowserContext(), origin, - embedding_origin.is_empty() ? origin : embedding_origin); + browser_context->GetPermissionManager()->ResetPermission( + type, origin, embedding_origin.is_empty() ? origin : embedding_origin); } } // namespace content diff --git a/content/content_shell.gypi b/content/content_shell.gypi index c592ede..4043999 100644 --- a/content/content_shell.gypi +++ b/content/content_shell.gypi @@ -116,6 +116,8 @@ 'shell/browser/layout_test/layout_test_navigator_connect_service_factory.h', 'shell/browser/layout_test/layout_test_notification_manager.cc', 'shell/browser/layout_test/layout_test_notification_manager.h', + 'shell/browser/layout_test/layout_test_permission_manager.h', + 'shell/browser/layout_test/layout_test_permission_manager.cc', 'shell/browser/layout_test/layout_test_push_messaging_service.cc', 'shell/browser/layout_test/layout_test_push_messaging_service.h', 'shell/browser/layout_test/layout_test_resource_dispatcher_host_delegate.cc', @@ -162,6 +164,8 @@ 'shell/browser/shell_net_log.h', 'shell/browser/shell_network_delegate.cc', 'shell/browser/shell_network_delegate.h', + 'shell/browser/shell_permission_manager.cc', + 'shell/browser/shell_permission_manager.h', 'shell/browser/shell_platform_data_aura.cc', 'shell/browser/shell_platform_data_aura.h', 'shell/browser/shell_plugin_service_filter.cc', diff --git a/content/public/browser/browser_context.h b/content/public/browser/browser_context.h index 5a0524a..c15afef 100644 --- a/content/public/browser/browser_context.h +++ b/content/public/browser/browser_context.h @@ -39,6 +39,7 @@ class BrowserPluginGuestManager; class DownloadManager; class DownloadManagerDelegate; class IndexedDBContext; +class PermissionManager; class PushMessagingService; class ResourceContext; class SiteInstance; @@ -181,6 +182,10 @@ class CONTENT_EXPORT BrowserContext : public base::SupportsUserData { // Returns the SSL host state decisions for this context. The context may // return nullptr, implementing the default exception storage strategy. virtual SSLHostStateDelegate* GetSSLHostStateDelegate() = 0; + + // Returns the PermissionManager associated with that context if any, nullptr + // otherwise. + virtual PermissionManager* GetPermissionManager() = 0; }; } // namespace content diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc index 3e92557..e50b963 100644 --- a/content/public/browser/content_browser_client.cc +++ b/content/public/browser/content_browser_client.cc @@ -223,24 +223,6 @@ ContentBrowserClient::GetPlatformNotificationService() { return nullptr; } -void ContentBrowserClient::RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback) { - callback.Run(PERMISSION_STATUS_DENIED); -} - -PermissionStatus ContentBrowserClient::GetPermissionStatus( - PermissionType permission, - BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) { - return PERMISSION_STATUS_DENIED; -} - bool ContentBrowserClient::CanCreateWindow( const GURL& opener_url, const GURL& opener_top_level_frame_url, diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h index 8e37607e5..6d5ac21 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -428,35 +428,6 @@ class CONTENT_EXPORT ContentBrowserClient { // support this functionality. May be called from any thread. virtual PlatformNotificationService* GetPlatformNotificationService(); - virtual void RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback); - - virtual void CancelPermissionRequest(PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame) {} - - virtual void RegisterPermissionUsage(PermissionType permission, - WebContents* web_contents, - const GURL& frame_url, - const GURL& main_frame_url) {} - - virtual PermissionStatus GetPermissionStatus( - PermissionType permission, - BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin); - - virtual void ResetPermission(PermissionType permission, - BrowserContext* browser_context, - const GURL& requesting_origin, - const GURL& embedding_origin) {} - // Returns true if the given page is allowed to open a window of the given // type. If true is returned, |no_javascript_access| will indicate whether // the window that is created should be scriptable/in the same process. diff --git a/content/public/browser/permission_manager.h b/content/public/browser/permission_manager.h new file mode 100644 index 0000000..e730075 --- /dev/null +++ b/content/public/browser/permission_manager.h @@ -0,0 +1,50 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_PUBLIC_BROWSER_PERMISSION_MANAGER_H_ +#define CONTENT_PUBLIC_BROWSER_PERMISSION_MANAGER_H_ + +#include "content/common/content_export.h" +#include "content/public/common/permission_status.mojom.h" + +class GURL; + +namespace content { +enum class PermissionType; +class WebContents; + +class CONTENT_EXPORT PermissionManager { + public: + virtual ~PermissionManager() = default; + + virtual void RequestPermission( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(PermissionStatus)>& callback) = 0; + + virtual void CancelPermissionRequest(PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin) = 0; + + virtual PermissionStatus GetPermissionStatus( + PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) = 0; + + virtual void ResetPermission(PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) = 0; + + virtual void RegisterPermissionUsage(PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) = 0; +}; + +} // namespace content + +#endif // CONTENT_PUBLIC_BROWSER_PERMISSION_MANAGER_H_ diff --git a/content/public/test/test_browser_context.cc b/content/public/test/test_browser_context.cc index 9c22851..107dbb2 100644 --- a/content/public/test/test_browser_context.cc +++ b/content/public/test/test_browser_context.cc @@ -124,4 +124,8 @@ SSLHostStateDelegate* TestBrowserContext::GetSSLHostStateDelegate() { return NULL; } +PermissionManager* TestBrowserContext::GetPermissionManager() { + return NULL; +} + } // namespace content diff --git a/content/public/test/test_browser_context.h b/content/public/test/test_browser_context.h index 1310187..2e6d166 100644 --- a/content/public/test/test_browser_context.h +++ b/content/public/test/test_browser_context.h @@ -47,6 +47,7 @@ class TestBrowserContext : public BrowserContext { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; PushMessagingService* GetPushMessagingService() override; SSLHostStateDelegate* GetSSLHostStateDelegate() override; + PermissionManager* GetPermissionManager() override; private: FRIEND_TEST_ALL_PREFIXES(DOMStorageTest, SessionOnly); diff --git a/content/shell/BUILD.gn b/content/shell/BUILD.gn index 87d31bf..d4d1e51 100644 --- a/content/shell/BUILD.gn +++ b/content/shell/BUILD.gn @@ -63,6 +63,8 @@ static_library("content_shell_lib") { "browser/layout_test/layout_test_navigator_connect_service_factory.h", "browser/layout_test/layout_test_notification_manager.cc", "browser/layout_test/layout_test_notification_manager.h", + "browser/layout_test/layout_test_permission_manager.cc", + "browser/layout_test/layout_test_permission_manager.h", "browser/layout_test/layout_test_push_messaging_service.cc", "browser/layout_test/layout_test_push_messaging_service.h", "browser/layout_test/layout_test_resource_dispatcher_host_delegate.cc", @@ -108,6 +110,8 @@ static_library("content_shell_lib") { "browser/shell_net_log.h", "browser/shell_network_delegate.cc", "browser/shell_network_delegate.h", + "browser/shell_permission_manager.cc", + "browser/shell_permission_manager.h", "browser/shell_platform_data_aura.cc", "browser/shell_platform_data_aura.h", "browser/shell_plugin_service_filter.cc", diff --git a/content/shell/browser/layout_test/layout_test_browser_context.cc b/content/shell/browser/layout_test/layout_test_browser_context.cc index efde5e4..f6fbceb 100644 --- a/content/shell/browser/layout_test/layout_test_browser_context.cc +++ b/content/shell/browser/layout_test/layout_test_browser_context.cc @@ -12,6 +12,7 @@ #include "content/public/browser/push_messaging_service.h" #include "content/public/browser/resource_context.h" #include "content/shell/browser/layout_test/layout_test_download_manager_delegate.h" +#include "content/shell/browser/layout_test/layout_test_permission_manager.h" #include "content/shell/browser/layout_test/layout_test_push_messaging_service.h" #include "content/shell/browser/layout_test/layout_test_url_request_context_getter.h" #include "content/shell/browser/shell_url_request_context_getter.h" @@ -74,4 +75,10 @@ LayoutTestBrowserContext::GetLayoutTestPushMessagingService() { GetPushMessagingService()); } +PermissionManager* LayoutTestBrowserContext::GetPermissionManager() { + if (!permission_manager_.get()) + permission_manager_.reset(new LayoutTestPermissionManager()); + return permission_manager_.get(); +} + } // namespace content diff --git a/content/shell/browser/layout_test/layout_test_browser_context.h b/content/shell/browser/layout_test/layout_test_browser_context.h index 7bfb860..c23d960 100644 --- a/content/shell/browser/layout_test/layout_test_browser_context.h +++ b/content/shell/browser/layout_test/layout_test_browser_context.h @@ -16,6 +16,7 @@ namespace content { class DownloadManagerDelegate; class LayoutTestPushMessagingService; +class PermissionManager; class PushMessagingService; class LayoutTestBrowserContext : public ShellBrowserContext { @@ -26,6 +27,7 @@ class LayoutTestBrowserContext : public ShellBrowserContext { // BrowserContext implementation. DownloadManagerDelegate* GetDownloadManagerDelegate() override; PushMessagingService* GetPushMessagingService() override; + PermissionManager* GetPermissionManager() override; LayoutTestPushMessagingService* GetLayoutTestPushMessagingService(); @@ -36,6 +38,7 @@ class LayoutTestBrowserContext : public ShellBrowserContext { private: scoped_ptr<LayoutTestPushMessagingService> push_messaging_service_; + scoped_ptr<PermissionManager> permission_manager_; DISALLOW_COPY_AND_ASSIGN(LayoutTestBrowserContext); }; diff --git a/content/shell/browser/layout_test/layout_test_content_browser_client.cc b/content/shell/browser/layout_test/layout_test_content_browser_client.cc index 9389945..b98ce58 100644 --- a/content/shell/browser/layout_test/layout_test_content_browser_client.cc +++ b/content/shell/browser/layout_test/layout_test_content_browser_client.cc @@ -7,7 +7,6 @@ #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/navigator_connect_context.h" -#include "content/public/browser/permission_type.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/storage_partition.h" #include "content/shell/browser/layout_test/layout_test_browser_context.h" @@ -23,21 +22,6 @@ namespace { LayoutTestContentBrowserClient* g_layout_test_browser_client; -void RequestDesktopNotificationPermissionOnIO( - const GURL& source_origin, - const base::Callback<void(PermissionStatus)>& callback) { - LayoutTestNotificationManager* manager = - LayoutTestContentBrowserClient::Get()->GetLayoutTestNotificationManager(); - PermissionStatus result = manager ? manager->RequestPermission(source_origin) - : PERMISSION_STATUS_GRANTED; - - // The callback came from the UI thread, we need to run it from there again. - BrowserThread::PostTask( - BrowserThread::UI, - FROM_HERE, - base::Bind(callback, result)); -} - } // namespace LayoutTestContentBrowserClient::LayoutTestContentBrowserClient() { @@ -81,31 +65,6 @@ void LayoutTestContentBrowserClient::RenderProcessWillLaunch( host->Send(new ShellViewMsg_SetWebKitSourceDir(GetWebKitRootDirFilePath())); } -void LayoutTestContentBrowserClient::RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback) { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - if (permission == PermissionType::NOTIFICATIONS) { - BrowserThread::PostTask( - BrowserThread::IO, - FROM_HERE, - base::Bind(&RequestDesktopNotificationPermissionOnIO, - requesting_frame, - callback)); - return; - } - ShellContentBrowserClient::RequestPermission(permission, - web_contents, - bridge_id, - requesting_frame, - user_gesture, - callback); -} - PlatformNotificationService* LayoutTestContentBrowserClient::GetPlatformNotificationService() { return layout_test_notification_manager_.get(); diff --git a/content/shell/browser/layout_test/layout_test_content_browser_client.h b/content/shell/browser/layout_test/layout_test_content_browser_client.h index 328ce61..f41794d 100644 --- a/content/shell/browser/layout_test/layout_test_content_browser_client.h +++ b/content/shell/browser/layout_test/layout_test_content_browser_client.h @@ -9,7 +9,6 @@ namespace content { -enum class PermissionType; class LayoutTestBrowserContext; class LayoutTestNotificationManager; @@ -28,13 +27,6 @@ class LayoutTestContentBrowserClient : public ShellContentBrowserClient { // ContentBrowserClient overrides. void RenderProcessWillLaunch(RenderProcessHost* host) override; - void RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback) override; PlatformNotificationService* GetPlatformNotificationService() override; void GetAdditionalNavigatorConnectServices( diff --git a/content/shell/browser/layout_test/layout_test_permission_manager.cc b/content/shell/browser/layout_test/layout_test_permission_manager.cc new file mode 100644 index 0000000..ed4e7c6 --- /dev/null +++ b/content/shell/browser/layout_test/layout_test_permission_manager.cc @@ -0,0 +1,72 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/shell/browser/layout_test/layout_test_permission_manager.h" + +#include "base/bind.h" +#include "base/callback.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/permission_type.h" +#include "content/shell/browser/layout_test/layout_test_content_browser_client.h" +#include "content/shell/browser/layout_test/layout_test_notification_manager.h" +#include "url/gurl.h" + +namespace content { + +namespace { + +void RequestDesktopNotificationPermissionOnIO( + const GURL& origin, + const base::Callback<void(PermissionStatus)>& callback) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + + LayoutTestNotificationManager* manager = + LayoutTestContentBrowserClient::Get()->GetLayoutTestNotificationManager(); + PermissionStatus result = manager ? manager->RequestPermission(origin) + : PERMISSION_STATUS_GRANTED; + + // The callback came from the UI thread, we need to run it from there again. + BrowserThread::PostTask( + BrowserThread::UI, + FROM_HERE, + base::Bind(callback, result)); +} + +} // anonymous namespace + +LayoutTestPermissionManager::LayoutTestPermissionManager() + : ShellPermissionManager() { +} + +LayoutTestPermissionManager::~LayoutTestPermissionManager() { +} + +void LayoutTestPermissionManager::RequestPermission( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(PermissionStatus)>& callback) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + + if (permission == PermissionType::NOTIFICATIONS) { + BrowserThread::PostTask( + BrowserThread::IO, + FROM_HERE, + base::Bind(&RequestDesktopNotificationPermissionOnIO, + requesting_origin, + callback)); + return; + } + + ShellPermissionManager::RequestPermission(permission, + web_contents, + request_id, + requesting_origin, + user_gesture, + callback); +} + +} // namespace content diff --git a/content/shell/browser/layout_test/layout_test_permission_manager.h b/content/shell/browser/layout_test/layout_test_permission_manager.h new file mode 100644 index 0000000..49e8189 --- /dev/null +++ b/content/shell/browser/layout_test/layout_test_permission_manager.h @@ -0,0 +1,34 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_PERMISSION_MANAGER_H_ +#define CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_PERMISSION_MANAGER_H_ + +#include "base/callback_forward.h" +#include "base/macros.h" +#include "content/shell/browser/shell_permission_manager.h" + +namespace content { + +class LayoutTestPermissionManager : public ShellPermissionManager { + public: + LayoutTestPermissionManager(); + ~LayoutTestPermissionManager() override; + + // ShellPermissionManager overrides. + void RequestPermission( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(PermissionStatus)>& callback) override; + + private: + DISALLOW_COPY_AND_ASSIGN(LayoutTestPermissionManager); +}; + +} // namespace content + +#endif // CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_PERMISSION_MANAGER_H_ diff --git a/content/shell/browser/shell_browser_context.cc b/content/shell/browser/shell_browser_context.cc index d8b895e..b81ae1c 100644 --- a/content/shell/browser/shell_browser_context.cc +++ b/content/shell/browser/shell_browser_context.cc @@ -15,6 +15,7 @@ #include "content/public/browser/storage_partition.h" #include "content/public/common/content_switches.h" #include "content/shell/browser/shell_download_manager_delegate.h" +#include "content/shell/browser/shell_permission_manager.h" #include "content/shell/common/shell_switches.h" #if defined(OS_WIN) @@ -199,4 +200,10 @@ SSLHostStateDelegate* ShellBrowserContext::GetSSLHostStateDelegate() { return NULL; } +PermissionManager* ShellBrowserContext::GetPermissionManager() { + if (!permission_manager_.get()) + permission_manager_.reset(new ShellPermissionManager()); + return permission_manager_.get(); +} + } // namespace content diff --git a/content/shell/browser/shell_browser_context.h b/content/shell/browser/shell_browser_context.h index 2162ea0..07802ee 100644 --- a/content/shell/browser/shell_browser_context.h +++ b/content/shell/browser/shell_browser_context.h @@ -22,6 +22,7 @@ class NetLog; namespace content { class DownloadManagerDelegate; +class PermissionManager; class ShellDownloadManagerDelegate; class ZoomLevelDelegate; @@ -55,6 +56,7 @@ class ShellBrowserContext : public BrowserContext { storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; PushMessagingService* GetPushMessagingService() override; SSLHostStateDelegate* GetSSLHostStateDelegate() override; + PermissionManager* GetPermissionManager() override; net::URLRequestContextGetter* CreateRequestContext( ProtocolHandlerMap* protocol_handlers, @@ -105,6 +107,7 @@ class ShellBrowserContext : public BrowserContext { scoped_ptr<ShellResourceContext> resource_context_; bool ignore_certificate_errors_; scoped_ptr<ShellDownloadManagerDelegate> download_manager_delegate_; + scoped_ptr<PermissionManager> permission_manager_; private: // Performs initialization of the ShellBrowserContext while IO is still diff --git a/content/shell/browser/shell_content_browser_client.cc b/content/shell/browser/shell_content_browser_client.cc index f7fa9db1..c10761f 100644 --- a/content/shell/browser/shell_content_browser_client.cc +++ b/content/shell/browser/shell_content_browser_client.cc @@ -12,7 +12,6 @@ #include "base/strings/utf_string_conversions.h" #include "content/public/browser/client_certificate_delegate.h" #include "content/public/browser/page_navigator.h" -#include "content/public/browser/permission_type.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/resource_dispatcher_host.h" #include "content/public/browser/storage_partition.h" @@ -297,25 +296,6 @@ void ShellContentBrowserClient::SelectClientCertificate( select_client_certificate_callback_.Run(); } -void ShellContentBrowserClient::RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback) { - // Some Geolocation tests on Android are still expecting to have the - // permission granted. See https://crbug.com/463514. - if (permission == PermissionType::GEOLOCATION) { - callback.Run(PERMISSION_STATUS_GRANTED); - return; - } - - ContentBrowserClient::RequestPermission( - permission, web_contents, bridge_id, - requesting_frame, user_gesture, callback); -} - SpeechRecognitionManagerDelegate* ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() { return new ShellSpeechRecognitionManagerDelegate(); diff --git a/content/shell/browser/shell_content_browser_client.h b/content/shell/browser/shell_content_browser_client.h index 6360f2c..dd25767 100644 --- a/content/shell/browser/shell_content_browser_client.h +++ b/content/shell/browser/shell_content_browser_client.h @@ -58,13 +58,6 @@ class ShellContentBrowserClient : public ContentBrowserClient { WebContents* web_contents, net::SSLCertRequestInfo* cert_request_info, scoped_ptr<ClientCertificateDelegate> delegate) override; - void RequestPermission( - PermissionType permission, - WebContents* web_contents, - int bridge_id, - const GURL& requesting_frame, - bool user_gesture, - const base::Callback<void(PermissionStatus)>& callback) override; SpeechRecognitionManagerDelegate* CreateSpeechRecognitionManagerDelegate() override; diff --git a/content/shell/browser/shell_permission_manager.cc b/content/shell/browser/shell_permission_manager.cc new file mode 100644 index 0000000..609622b --- /dev/null +++ b/content/shell/browser/shell_permission_manager.cc @@ -0,0 +1,56 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/shell/browser/shell_permission_manager.h" + +#include "base/callback.h" +#include "content/public/browser/permission_type.h" + +namespace content { + +ShellPermissionManager::ShellPermissionManager() + : PermissionManager() { +} + +ShellPermissionManager::~ShellPermissionManager() { +} + +void ShellPermissionManager::RequestPermission( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(PermissionStatus)>& callback) { + callback.Run(permission == PermissionType::GEOLOCATION + ? PERMISSION_STATUS_GRANTED : PERMISSION_STATUS_DENIED); +} + +void ShellPermissionManager::CancelPermissionRequest( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin) { +} + +void ShellPermissionManager::ResetPermission( + PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { +} + +PermissionStatus ShellPermissionManager::GetPermissionStatus( + PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { + return PERMISSION_STATUS_DENIED; +} + +void ShellPermissionManager::RegisterPermissionUsage( + PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) { +} + +} // namespace content diff --git a/content/shell/browser/shell_permission_manager.h b/content/shell/browser/shell_permission_manager.h new file mode 100644 index 0000000..88d413d --- /dev/null +++ b/content/shell/browser/shell_permission_manager.h @@ -0,0 +1,47 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_SHELL_BROWSER_SHELL_PERMISSION_MANAGER_H_ +#define CONTENT_SHELL_BROWSER_SHELL_PERMISSION_MANAGER_H_ + +#include "base/callback_forward.h" +#include "base/macros.h" +#include "content/public/browser/permission_manager.h" + +namespace content { + +class ShellPermissionManager : public PermissionManager { + public: + ShellPermissionManager(); + ~ShellPermissionManager() override; + + // PermissionManager implementation. + void RequestPermission( + PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin, + bool user_gesture, + const base::Callback<void(PermissionStatus)>& callback) override; + void CancelPermissionRequest(PermissionType permission, + WebContents* web_contents, + int request_id, + const GURL& requesting_origin) override; + void ResetPermission(PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + PermissionStatus GetPermissionStatus(PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + void RegisterPermissionUsage(PermissionType permission, + const GURL& requesting_origin, + const GURL& embedding_origin) override; + + private: + DISALLOW_COPY_AND_ASSIGN(ShellPermissionManager); +}; + +} // namespace content + +#endif // CONTENT_SHELL_BROWSER_SHELL_PERMISSION_MANAGER_H |