From 277b23d74855b51ebe67ae0da7cd9e0b053044cf Mon Sep 17 00:00:00 2001 From: timvolodine Date: Mon, 8 Dec 2014 11:22:56 -0800 Subject: [PATCH] Implement HasPermission() method in PermissionService. This patch implements the HasPermission() method in the mojo PermissionService. This methiod will be required for the Permissions API, see https://w3c.github.io/permissions/ BUG=430238 Review URL: https://codereview.chromium.org/750633003 Cr-Commit-Position: refs/heads/master@{#307302} --- chrome/browser/chrome_content_browser_client.cc | 61 ++++++++++++++++++++++ chrome/browser/chrome_content_browser_client.h | 5 ++ content/browser/permissions/OWNERS | 2 + .../permissions/permission_service_context.cc | 24 ++++++++- .../permissions/permission_service_context.h | 6 +++ .../browser/permissions/permission_service_impl.cc | 11 +++- .../renderer_host/render_process_host_impl.cc | 2 +- content/common/permission_service.mojom | 6 +-- content/content.gyp | 25 +++++---- content/content_app.gypi | 7 +-- content/content_common_mojo_bindings.gypi | 1 + content/content_tests.gypi | 2 + content/public/browser/content_browser_client.cc | 8 +++ content/public/browser/content_browser_client.h | 7 +++ content/public/common/BUILD.gn | 1 + content/public/common/permission_status.mojom | 11 ++++ 16 files changed, 157 insertions(+), 22 deletions(-) create mode 100644 content/browser/permissions/OWNERS create mode 100644 content/public/common/permission_status.mojom diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index 94ccf123f1cf..9343853257ce 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -600,6 +600,24 @@ 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_DEFAULT: + case CONTENT_SETTING_NUM_SETTINGS: + break; + } + NOTREACHED(); + return content::PERMISSION_STATUS_DENIED; +} + } // namespace namespace chrome { @@ -1978,6 +1996,49 @@ void ChromeContentBrowserClient::RequestPermission( } } +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 = nullptr; + switch (permission) { + case content::PERMISSION_MIDI_SYSEX: + context = MidiPermissionContextFactory::GetForProfile(profile); + break; + case content::PERMISSION_NOTIFICATIONS: +#if defined(ENABLE_NOTIFICATIONS) + context = DesktopNotificationServiceFactory::GetForProfile(profile); +#else + NOTIMPLEMENTED(); +#endif + break; + case content::PERMISSION_GEOLOCATION: + context = GeolocationPermissionContextFactory::GetForProfile(profile); + break; + case content::PERMISSION_PROTECTED_MEDIA: + NOTIMPLEMENTED(); + break; + case content::PERMISSION_PUSH_MESSAGING: + context = gcm::PushMessagingPermissionContextFactory::GetForProfile( + profile); + break; + case content::PERMISSION_NUM: + NOTREACHED() << "Invalid RequestPermission for " << permission; + break; + } + + ContentSetting result = context + ? context->GetPermissionStatus(requesting_origin.GetOrigin(), + embedding_origin.GetOrigin()) + : CONTENT_SETTING_DEFAULT; + + return ContentSettingToPermissionStatus(result); +} + void ChromeContentBrowserClient::CancelPermissionRequest( content::PermissionType permission, content::WebContents* web_contents, diff --git a/chrome/browser/chrome_content_browser_client.h b/chrome/browser/chrome_content_browser_client.h index 4ea44b3fb3c4..15f761c02ed7 100644 --- a/chrome/browser/chrome_content_browser_client.h +++ b/chrome/browser/chrome_content_browser_client.h @@ -197,6 +197,11 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient { const GURL& requesting_frame, bool user_gesture, const base::Callback& result_callback) override; + content::PermissionStatus GetPermissionStatus( + 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, diff --git a/content/browser/permissions/OWNERS b/content/browser/permissions/OWNERS new file mode 100644 index 000000000000..fee7a6d84c1b --- /dev/null +++ b/content/browser/permissions/OWNERS @@ -0,0 +1,2 @@ +mlamouri@chromium.org +timvolodine@chromium.org \ No newline at end of file diff --git a/content/browser/permissions/permission_service_context.cc b/content/browser/permissions/permission_service_context.cc index 2c7e91b96845..50c73a92e382 100644 --- a/content/browser/permissions/permission_service_context.cc +++ b/content/browser/permissions/permission_service_context.cc @@ -7,6 +7,7 @@ #include "content/browser/permissions/permission_service_impl.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/render_frame_host.h" +#include "content/public/browser/render_process_host.h" #include "content/public/browser/web_contents.h" namespace content { @@ -14,7 +15,15 @@ namespace content { PermissionServiceContext::PermissionServiceContext( RenderFrameHost* render_frame_host) : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), - render_frame_host_(render_frame_host) { + render_frame_host_(render_frame_host), + render_process_host_(nullptr) { +} + +PermissionServiceContext::PermissionServiceContext( + RenderProcessHost* render_process_host) + : WebContentsObserver(nullptr), + render_frame_host_(nullptr), + render_process_host_(render_process_host) { } PermissionServiceContext::~PermissionServiceContext() { @@ -60,4 +69,17 @@ void PermissionServiceContext::CancelPendingRequests( service->CancelPendingRequests(); } +BrowserContext* PermissionServiceContext::GetBrowserContext() const { + if (!web_contents()) { + DCHECK(render_process_host_); + return render_process_host_->GetBrowserContext(); + } + return web_contents()->GetBrowserContext(); +} + +GURL PermissionServiceContext::GetEmbeddingOrigin() const { + return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() + : GURL(); +} + } // namespace content diff --git a/content/browser/permissions/permission_service_context.h b/content/browser/permissions/permission_service_context.h index 46d6b397026a..2e1fca1e56d5 100644 --- a/content/browser/permissions/permission_service_context.h +++ b/content/browser/permissions/permission_service_context.h @@ -15,6 +15,7 @@ namespace content { class PermissionService; class PermissionServiceImpl; class RenderFrameHost; +class RenderProcessHost; // Provides information to a PermissionService. It is used by the // PermissionService to handle request permission UI. @@ -23,6 +24,7 @@ class RenderFrameHost; class PermissionServiceContext : public WebContentsObserver { public: explicit PermissionServiceContext(RenderFrameHost* render_frame_host); + explicit PermissionServiceContext(RenderProcessHost* render_process_host); virtual ~PermissionServiceContext(); void CreateService(mojo::InterfaceRequest request); @@ -31,6 +33,9 @@ class PermissionServiceContext : public WebContentsObserver { // connection error in order to get unregistered and killed. void ServiceHadConnectionError(PermissionServiceImpl* service); + BrowserContext* GetBrowserContext() const; + GURL GetEmbeddingOrigin() const; + private: // WebContentsObserver void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; @@ -41,6 +46,7 @@ class PermissionServiceContext : public WebContentsObserver { void CancelPendingRequests(RenderFrameHost*) const; RenderFrameHost* render_frame_host_; + RenderProcessHost* render_process_host_; ScopedVector services_; DISALLOW_COPY_AND_ASSIGN(PermissionServiceContext); diff --git a/content/browser/permissions/permission_service_impl.cc b/content/browser/permissions/permission_service_impl.cc index 12638e8c4ee1..bb5b02774290 100644 --- a/content/browser/permissions/permission_service_impl.cc +++ b/content/browser/permissions/permission_service_impl.cc @@ -109,7 +109,16 @@ void PermissionServiceImpl::HasPermission( PermissionName permission, const mojo::String& origin, const mojo::Callback& callback) { - NOTIMPLEMENTED(); + DCHECK(context_->GetBrowserContext()); + + // If the embedding_origin is empty we'll use |origin| instead. + GURL embedding_origin = context_->GetEmbeddingOrigin(); + + callback.Run(GetContentClient()->browser()->GetPermissionStatus( + PermissionNameToPermissionType(permission), + context_->GetBrowserContext(), + GURL(origin), + embedding_origin.is_empty() ? GURL(origin) : embedding_origin)); } } // namespace content diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc index 09316f0c2322..a32b4d79b9cf 100644 --- a/content/browser/renderer_host/render_process_host_impl.cc +++ b/content/browser/renderer_host/render_process_host_impl.cc @@ -456,7 +456,7 @@ RenderProcessHostImpl::RenderProcessHostImpl( within_process_died_observer_(false), power_monitor_broadcaster_(this), worker_ref_count_(0), - permission_service_context_(new PermissionServiceContext(nullptr)), + permission_service_context_(new PermissionServiceContext(this)), weak_factory_(this) { widget_helper_ = new RenderWidgetHelper(); diff --git a/content/common/permission_service.mojom b/content/common/permission_service.mojom index b4c4006c9eb8..47b1d19e3ef8 100644 --- a/content/common/permission_service.mojom +++ b/content/common/permission_service.mojom @@ -4,11 +4,7 @@ module content; -enum PermissionStatus { - GRANTED, - DENIED, - ASK -}; +import "content/public/common/permission_status.mojom"; enum PermissionName { GEOLOCATION, diff --git a/content/content.gyp b/content/content.gyp index cb1012190363..0a7922dc3a42 100644 --- a/content/content.gyp +++ b/content/content.gyp @@ -21,11 +21,13 @@ }], ], }, + 'includes': [ + 'content_common_mojo_bindings.gypi', + ], 'conditions': [ ['OS != "ios"', { 'includes': [ '../build/win_precompile.gypi', - 'content_common_mojo_bindings.gypi', 'content_resources.gypi', ], }], @@ -67,15 +69,16 @@ 'dependencies': [ 'content_browser', 'content_common', + 'content_common_mojo_bindings', ], 'export_dependent_settings': [ 'content_common', + 'content_common_mojo_bindings', ], 'conditions': [ ['OS != "ios"', { 'dependencies': [ 'content_child', - 'content_common_mojo_bindings', 'content_gpu', 'content_plugin', 'content_ppapi_plugin', @@ -139,9 +142,11 @@ ], 'dependencies': [ 'content_common', + 'content_common_mojo_bindings', ], 'export_dependent_settings': [ 'content_common', + 'content_common_mojo_bindings', ], }, { @@ -157,9 +162,11 @@ ], 'dependencies': [ 'content_common', + 'content_common_mojo_bindings', ], 'export_dependent_settings': [ 'content_common', + 'content_common_mojo_bindings', ], 'conditions': [ ['java_bridge==1', { @@ -175,7 +182,6 @@ }], ['OS != "ios"', { 'dependencies': [ - 'content_common_mojo_bindings', 'content_resources', ], }], @@ -189,10 +195,12 @@ 'includes': [ 'content_common.gypi', ], + 'dependencies': [ + 'content_common_mojo_bindings', + ], 'conditions': [ ['OS != "ios"', { 'dependencies': [ - 'content_common_mojo_bindings', 'content_resources', ], }], @@ -304,19 +312,18 @@ 'type': 'shared_library', 'variables': { 'enable_wexit_time_destructors': 1, }, 'dependencies': [ + 'content_common_mojo_bindings', 'content_resources', ], + 'export_dependent_settings': [ + 'content_common_mojo_bindings', + ], 'conditions': [ ['chromium_enable_vtune_jit_for_v8==1', { 'dependencies': [ '../v8/src/third_party/vtune/v8vtune.gyp:v8_vtune', ], }], - ['OS != "ios"', { - 'dependencies': [ - 'content_common_mojo_bindings', - ] - }] ], 'includes': [ 'content_app.gypi', diff --git a/content/content_app.gypi b/content/content_app.gypi index d0a3296d1fbf..c68012f3405a 100644 --- a/content/content_app.gypi +++ b/content/content_app.gypi @@ -10,6 +10,8 @@ '../base/base.gyp:base', '../base/base.gyp:base_i18n', '../crypto/crypto.gyp:crypto', + '../mojo/edk/mojo_edk.gyp:mojo_system_impl', + '../mojo/mojo_base.gyp:mojo_environment_chromium', '../ui/base/ui_base.gyp:ui_base', '../ui/gfx/gfx.gyp:gfx', '../ui/gfx/gfx.gyp:gfx_geometry', @@ -63,11 +65,6 @@ 'app/mojo/mojo_init.cc', 'app/mojo/mojo_init.h', ], - }, { # OS!="ios" - 'dependencies': [ - '../mojo/edk/mojo_edk.gyp:mojo_system_impl', - '../mojo/mojo_base.gyp:mojo_environment_chromium', - ], }], ], } diff --git a/content/content_common_mojo_bindings.gypi b/content/content_common_mojo_bindings.gypi index cb0098cbdbed..1a969edbfc88 100644 --- a/content/content_common_mojo_bindings.gypi +++ b/content/content_common_mojo_bindings.gypi @@ -17,6 +17,7 @@ # NOTE: Sources duplicated in # //content/public/common/BUILD.gn:mojo_bindings. 'public/common/mojo_geoposition.mojom', + 'public/common/permission_status.mojom', ], }, 'includes': [ '../mojo/public/tools/bindings/mojom_bindings_generator_explicit.gypi' ], diff --git a/content/content_tests.gypi b/content/content_tests.gypi index ac3d87039f38..b9651bb27466 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -199,6 +199,8 @@ 'target_name': 'test_support_content', 'type': 'static_library', 'dependencies': [ + '../mojo/edk/mojo_edk.gyp:mojo_system_impl', + '../mojo/mojo_base.gyp:mojo_environment_chromium', '../net/net.gyp:net_test_support', '../skia/skia.gyp:skia', '../storage/storage_common.gyp:storage_common', diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc index 5c1a2d37c8ab..c5c672f6b3d5 100644 --- a/content/public/browser/content_browser_client.cc +++ b/content/public/browser/content_browser_client.cc @@ -229,6 +229,14 @@ void ContentBrowserClient::RequestPermission( result_callback.Run(true); } +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 3c5e1235396a..0934c9aee527 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -20,6 +20,7 @@ #include "content/public/browser/permission_type.h" #include "content/public/common/content_client.h" #include "content/public/common/media_stream_request.h" +#include "content/public/common/permission_status.mojom.h" #include "content/public/common/resource_type.h" #include "content/public/common/socket_permission_request.h" #include "content/public/common/window_container_type.h" @@ -453,6 +454,12 @@ class CONTENT_EXPORT ContentBrowserClient { 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); + // 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/common/BUILD.gn b/content/public/common/BUILD.gn index c348365ddcae..36eef892d798 100644 --- a/content/public/common/BUILD.gn +++ b/content/public/common/BUILD.gn @@ -52,5 +52,6 @@ source_set("common_sources") { mojom("mojo_bindings") { sources = [ "mojo_geoposition.mojom", + "permission_status.mojom", ] } diff --git a/content/public/common/permission_status.mojom b/content/public/common/permission_status.mojom new file mode 100644 index 000000000000..f7e56bfab19c --- /dev/null +++ b/content/public/common/permission_status.mojom @@ -0,0 +1,11 @@ +// Copyright 2014 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. + +module content; + +enum PermissionStatus { + GRANTED, + DENIED, + ASK +}; -- 2.11.4.GIT