Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Unified Diff: extensions/browser/service_registration_manager.h

Issue 652793002: Add service registration for apps APIs implemented as mojo services. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/browser/service_registration_manager.h
diff --git a/extensions/browser/service_registration_manager.h b/extensions/browser/service_registration_manager.h
new file mode 100644
index 0000000000000000000000000000000000000000..cec84a0b9dc7b0cbbbe9514d95ccea19fe3e68af
--- /dev/null
+++ b/extensions/browser/service_registration_manager.h
@@ -0,0 +1,94 @@
+// 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_SERVICE_REGISTRATION_MANAGER_H_
+#define EXTENSIONS_BROWSER_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 {
+
+class ServiceFactoryBase {
raymes 2014/10/15 20:22:50 Maybe add a comment explaining what this is for an
Sam McNally 2014/10/20 00:22:07 Done.
+ 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) {}
+ virtual ~ServiceFactory() {}
+
+ virtual 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.
+ 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 AddToRenderFrame(content::RenderFrameHost* render_frame_host);
raymes 2014/10/15 20:22:50 nit: AddServicesToRenderFrame might be more descri
Sam McNally 2014/10/20 00:22:07 Done.
+
+ 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_SERVICE_REGISTRATION_MANAGER_H_

Powered by Google App Engine
This is Rietveld 408576698