Commit d92097b1 authored by sammc's avatar sammc Committed by Commit bot

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}
parent 26df1b6e
......@@ -406,6 +406,8 @@ source_set("browser") {
"management_policy.h",
"mojo/keep_alive_impl.cc",
"mojo/keep_alive_impl.h",
"mojo/service_registration_manager.cc",
"mojo/service_registration_manager.h",
"mojo/stash_backend.cc",
"mojo/stash_backend.h",
"pref_names.cc",
......
......@@ -7,6 +7,7 @@ include_rules = [
"+components/web_modal",
"+content/public/browser",
"+device/bluetooth",
"+device/serial",
"+device/usb",
"+google_apis/gaia",
"+grit/extensions_strings.h",
......
......@@ -12,6 +12,7 @@
#include "content/public/common/url_constants.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/mojo/service_registration_manager.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h"
......@@ -31,6 +32,11 @@ void ExtensionWebContentsObserver::RenderViewCreated(
content::RenderViewHost* render_view_host) {
NotifyRenderViewType(render_view_host);
// TODO(sammc): Call AddServicesToRenderFrame() for frames that aren't main
// frames.
ServiceRegistrationManager::GetSharedInstance()->AddServicesToRenderFrame(
render_view_host->GetMainFrame());
const Extension* extension = GetExtension(render_view_host);
if (!extension)
return;
......
// 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
// 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_
......@@ -50,6 +50,9 @@ const char kEnableExtensionActionRedesign[] =
// kEnableScriptsRequireAction.
const char kEnableMimeHandlerView[] = "enable-mime-handler-view";
// Enables the mojo implementation of the serial API.
const char kEnableMojoSerialService[] = "enable-mojo-serial-service";
// Enables extensions to hide bookmarks UI elements.
const char kEnableOverrideBookmarksUI[] = "enable-override-bookmarks-ui";
......
......@@ -21,6 +21,7 @@ extern const char kEnableEmbeddedExtensionOptions[];
extern const char kEnableExperimentalExtensionApis[];
extern const char kEnableExtensionActionRedesign[];
extern const char kEnableMimeHandlerView[];
extern const char kEnableMojoSerialService[];
extern const char kEnableOverrideBookmarksUI[];
extern const char kEnableRemoteAssistance[];
extern const char kErrorConsole[];
......
......@@ -709,6 +709,8 @@
'browser/management_policy.h',
'browser/mojo/keep_alive_impl.cc',
'browser/mojo/keep_alive_impl.h',
'browser/mojo/service_registration_manager.cc',
'browser/mojo/service_registration_manager.h',
'browser/mojo/stash_backend.cc',
'browser/mojo/stash_backend.h',
'browser/notification_types.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment