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_ |