diff options
author | sammc <sammc@chromium.org> | 2014-10-26 19:54:39 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-10-27 02:54:58 +0000 |
commit | d92097b170dd8bf44cbef31fa5fae5d523d1c651 (patch) | |
tree | 4d086b4ddf56d4a7e5ec4d9d37232bbae7e17d79 /extensions/browser/mojo | |
parent | 26df1b6ef65f42d581c2d8d5e0e2807d67602f2b (diff) | |
download | chromium_src-d92097b170dd8bf44cbef31fa5fae5d523d1c651.zip chromium_src-d92097b170dd8bf44cbef31fa5fae5d523d1c651.tar.gz chromium_src-d92097b170dd8bf44cbef31fa5fae5d523d1c651.tar.bz2 |
Add service registration for apps APIs implemented as mojo services.
This adds a ServiceRegistrationManager that acts as a global repository
of extensions API services to be added to RenderFrameHosts along with
the extensions API permissions that gate access to them.
BUG=389016
Review URL: https://codereview.chromium.org/652793002
Cr-Commit-Position: refs/heads/master@{#301310}
Diffstat (limited to 'extensions/browser/mojo')
-rw-r--r-- | extensions/browser/mojo/service_registration_manager.cc | 80 | ||||
-rw-r--r-- | extensions/browser/mojo/service_registration_manager.h | 97 |
2 files changed, 177 insertions, 0 deletions
diff --git a/extensions/browser/mojo/service_registration_manager.cc b/extensions/browser/mojo/service_registration_manager.cc new file mode 100644 index 0000000..97f3698 --- /dev/null +++ b/extensions/browser/mojo/service_registration_manager.cc @@ -0,0 +1,80 @@ +// 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; + +} // 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() { + 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()) + factory.second->Register(render_frame_host->GetServiceRegistry()); + } +} + +} // namespace extensions diff --git a/extensions/browser/mojo/service_registration_manager.h b/extensions/browser/mojo/service_registration_manager.h new file mode 100644 index 0000000..c7d14c6 --- /dev/null +++ b/extensions/browser/mojo/service_registration_manager.h @@ -0,0 +1,97 @@ +// 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 "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; +}; + +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_); + } + + 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(); + ~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); + + private: + // 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_ |