diff options
author | sammc <sammc@chromium.org> | 2015-02-13 01:42:38 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-13 09:43:05 +0000 |
commit | 143f3c531ee479be7f4ca61f9614de0850ef04a4 (patch) | |
tree | 5d5a7b4e192c298c02afda523f5f9e6f99781149 /extensions/browser/mojo | |
parent | f2b0697dffb81b2bcfe0ac997eb593b96841a1c4 (diff) | |
download | chromium_src-143f3c531ee479be7f4ca61f9614de0850ef04a4.zip chromium_src-143f3c531ee479be7f4ca61f9614de0850ef04a4.tar.gz chromium_src-143f3c531ee479be7f4ca61f9614de0850ef04a4.tar.bz2 |
Remove extensions::ServiceRegistrationManager.
Review URL: https://codereview.chromium.org/919943002
Cr-Commit-Position: refs/heads/master@{#316197}
Diffstat (limited to 'extensions/browser/mojo')
-rw-r--r-- | extensions/browser/mojo/service_registration.cc | 79 | ||||
-rw-r--r-- | extensions/browser/mojo/service_registration.h | 18 | ||||
-rw-r--r-- | extensions/browser/mojo/service_registration_manager.cc | 98 | ||||
-rw-r--r-- | extensions/browser/mojo/service_registration_manager.h | 112 |
4 files changed, 97 insertions, 210 deletions
diff --git a/extensions/browser/mojo/service_registration.cc b/extensions/browser/mojo/service_registration.cc new file mode 100644 index 0000000..6b1c3d8 --- /dev/null +++ b/extensions/browser/mojo/service_registration.cc @@ -0,0 +1,79 @@ +// 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 "extensions/browser/mojo/service_registration.h" + +#include <string> + +#include "base/bind.h" +#include "base/command_line.h" +#include "content/public/browser/render_frame_host.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/site_instance.h" +#include "content/public/common/service_registry.h" +#include "extensions/browser/api/serial/serial_service_factory.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/extensions_browser_client.h" +#include "extensions/browser/mojo/keep_alive_impl.h" +#include "extensions/browser/process_map.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension_api.h" +#include "extensions/common/switches.h" + +namespace extensions { +namespace { + +const Extension* GetExtension(content::RenderFrameHost* render_frame_host) { + content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); + GURL url = render_frame_host->GetLastCommittedURL(); + if (!url.is_empty()) { + if (site_instance->GetSiteURL().GetOrigin() != url.GetOrigin()) + return nullptr; + } else { + url = site_instance->GetSiteURL(); + } + content::BrowserContext* browser_context = site_instance->GetBrowserContext(); + if (!url.SchemeIs(kExtensionScheme)) + return nullptr; + + return ExtensionRegistry::Get(browser_context) + ->enabled_extensions() + .GetExtensionOrAppByURL(url); +} + +bool ExtensionHasPermission(const Extension* extension, + content::RenderProcessHost* render_process_host, + const std::string& permission_name) { + Feature::Context context = + ProcessMap::Get(render_process_host->GetBrowserContext()) + ->GetMostLikelyContextType(extension, render_process_host->GetID()); + + return ExtensionAPI::GetSharedInstance() + ->IsAvailable(permission_name, extension, context, extension->url()) + .is_available(); +} + +} // namespace + +void RegisterCoreExtensionServices( + content::RenderFrameHost* render_frame_host) { + const Extension* extension = GetExtension(render_frame_host); + if (!extension) + return; + + content::ServiceRegistry* service_registry = + render_frame_host->GetServiceRegistry(); + if (base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kEnableMojoSerialService)) { + if (ExtensionHasPermission(extension, render_frame_host->GetProcess(), + "serial")) { + service_registry->AddService(base::Bind(&BindToSerialServiceRequest)); + } + } + service_registry->AddService(base::Bind( + KeepAliveImpl::Create, + render_frame_host->GetProcess()->GetBrowserContext(), extension)); +} + +} // namespace extensions diff --git a/extensions/browser/mojo/service_registration.h b/extensions/browser/mojo/service_registration.h new file mode 100644 index 0000000..39cfe0a --- /dev/null +++ b/extensions/browser/mojo/service_registration.h @@ -0,0 +1,18 @@ +// 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 EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_H_ +#define EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_H_ + +namespace content { +class RenderFrameHost; +} + +namespace extensions { + +void RegisterCoreExtensionServices(content::RenderFrameHost* render_frame_host); + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_H_ diff --git a/extensions/browser/mojo/service_registration_manager.cc b/extensions/browser/mojo/service_registration_manager.cc deleted file mode 100644 index ee3e55a..0000000 --- a/extensions/browser/mojo/service_registration_manager.cc +++ /dev/null @@ -1,98 +0,0 @@ -// 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. - -#include "extensions/browser/mojo/service_registration_manager.h" - -#include "base/command_line.h" -#include "base/lazy_instance.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/render_frame_host.h" -#include "content/public/browser/render_process_host.h" -#include "content/public/browser/site_instance.h" -#include "device/serial/serial_service_impl.h" -#include "extensions/browser/extension_registry.h" -#include "extensions/browser/process_map.h" -#include "extensions/common/constants.h" -#include "extensions/common/extension_api.h" -#include "extensions/common/switches.h" - -namespace extensions { -namespace { - -base::LazyInstance<ServiceRegistrationManager> g_lazy_instance = - LAZY_INSTANCE_INITIALIZER; - -ServiceRegistrationManager* g_test_instance = nullptr; - -} // namespace - -ServiceRegistrationManager::ServiceRegistrationManager() { - if (base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kEnableMojoSerialService)) { - AddServiceFactory( - "serial", - base::Bind(device::SerialServiceImpl::CreateOnMessageLoop, - content::BrowserThread::GetMessageLoopProxyForThread( - content::BrowserThread::FILE), - content::BrowserThread::GetMessageLoopProxyForThread( - content::BrowserThread::IO), - content::BrowserThread::GetMessageLoopProxyForThread( - content::BrowserThread::UI))); - } -} - -ServiceRegistrationManager::~ServiceRegistrationManager() { -} - -ServiceRegistrationManager* ServiceRegistrationManager::GetSharedInstance() { - if (g_test_instance) - return g_test_instance; - return g_lazy_instance.Pointer(); -} - -void ServiceRegistrationManager::AddServicesToRenderFrame( - content::RenderFrameHost* render_frame_host) { - content::BrowserContext* context = - render_frame_host->GetProcess()->GetBrowserContext(); - content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); - GURL extension_url = site_instance->GetSiteURL(); - ExtensionRegistry* registry = ExtensionRegistry::Get(context); - - // TODO(sammc): Handle content scripts and web pages that have access to some - // extension APIs. - if (!extension_url.SchemeIs(kExtensionScheme)) { - return; - } - - const Extension* extension = - registry->enabled_extensions().GetByID(extension_url.host()); - if (!extension) - return; - - Feature::Context context_type = - ProcessMap::Get(context)->GetMostLikelyContextType( - extension, render_frame_host->GetProcess()->GetID()); - for (const auto& factory : factories_) { - auto availability = ExtensionAPI::GetSharedInstance()->IsAvailable( - factory.first, extension, context_type, extension_url); - if (availability.is_available()) { - AddServiceToServiceRegistry(render_frame_host->GetServiceRegistry(), - factory.second.get()); - } - } -} - -void ServiceRegistrationManager::AddServiceToServiceRegistry( - content::ServiceRegistry* service_registry, - internal::ServiceFactoryBase* factory) { - factory->Register(service_registry); -} - -// static -void ServiceRegistrationManager::SetServiceRegistrationManagerForTest( - ServiceRegistrationManager* service_registration_manager) { - g_test_instance = service_registration_manager; -} - -} // namespace extensions diff --git a/extensions/browser/mojo/service_registration_manager.h b/extensions/browser/mojo/service_registration_manager.h deleted file mode 100644 index e57fba2..0000000 --- a/extensions/browser/mojo/service_registration_manager.h +++ /dev/null @@ -1,112 +0,0 @@ -// 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. - -#ifndef EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_ -#define EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_ - -#include <string> -#include <utility> -#include <vector> - -#include "base/callback.h" -#include "base/memory/linked_ptr.h" -#include "content/public/common/service_registry.h" -#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" - -namespace content { -class RenderFrameHost; -} - -namespace extensions { - -namespace internal { - -// A base class for forwarding calls to ServiceRegistry::AddService(). -class ServiceFactoryBase { - public: - virtual ~ServiceFactoryBase() {} - - // Add this service factory to |service_registry|. - virtual void Register(content::ServiceRegistry* service_registry) const = 0; - - virtual std::string GetName() const = 0; -}; - -template <typename Interface> -class ServiceFactory : public ServiceFactoryBase { - public: - explicit ServiceFactory( - const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) - : factory_(factory) {} - ~ServiceFactory() override {} - - void Register(content::ServiceRegistry* service_registry) const override { - service_registry->AddService(factory_); - } - - std::string GetName() const override { return Interface::Name_; } - - private: - const base::Callback<void(mojo::InterfaceRequest<Interface>)> factory_; - DISALLOW_COPY_AND_ASSIGN(ServiceFactory); -}; - -} // namespace internal - -// A meta service registry. This allows registration of service factories and -// their associated extensions API permission name. Whenever a RenderFrameHost -// is created, each service that the render frame should have access to (based -// on its SiteInstance), is added to the ServiceRegistry for that -// RenderFrameHost. -class ServiceRegistrationManager { - public: - ServiceRegistrationManager(); - virtual ~ServiceRegistrationManager(); - - static ServiceRegistrationManager* GetSharedInstance(); - - // Registers a ServiceFactory to be provided to extensions with the - // |permission_name| API permission. - // - // TODO(sammc): Add support for service factories that take the Extension* - // (or extension ID) and BrowserContext to allow fine-grained service and - // permission customization. - // - // TODO(sammc): Support more flexible service filters than just API permission - // names. - template <typename Interface> - void AddServiceFactory( - const std::string& permission_name, - const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) { - factories_.push_back( - std::make_pair(permission_name, - linked_ptr<internal::ServiceFactoryBase>( - new internal::ServiceFactory<Interface>(factory)))); - } - - // Adds the service factories appropriate for |render_frame_host| to its - // ServiceRegistry. - void AddServicesToRenderFrame(content::RenderFrameHost* render_frame_host); - - protected: - virtual void AddServiceToServiceRegistry( - content::ServiceRegistry* service_registry, - internal::ServiceFactoryBase* service_factory); - - private: - friend class TestServiceRegistrationManager; - - static void SetServiceRegistrationManagerForTest( - ServiceRegistrationManager* service_registration_manager); - - // All factories and their corresponding API permissions. - std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>> - factories_; - - DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager); -}; - -} // namespace extensions - -#endif // EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_ |