| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ | 5 #ifndef MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ |
| 6 #define MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ | 6 #define MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" |
| 12 #include "mojo/public/shell/shell.mojom.h" | 13 #include "mojo/public/shell/shell.mojom.h" |
| 14 #include "mojo/service_manager/service_manager_export.h" |
| 13 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 14 | 16 |
| 17 namespace content { |
| 18 class MojoTest; |
| 19 } |
| 20 |
| 15 namespace mojo { | 21 namespace mojo { |
| 16 | 22 |
| 17 class ServiceLoader; | 23 class ServiceLoader; |
| 18 | 24 |
| 19 class ServiceManager { | 25 class MOJO_SERVICE_MANAGER_EXPORT ServiceManager { |
| 20 public: | 26 public: |
| 21 // API for testing. | 27 // API for testing. |
| 22 class TestAPI { | 28 class MOJO_SERVICE_MANAGER_EXPORT TestAPI { |
| 23 private: | 29 private: |
| 24 friend class ServiceManagerTest; | 30 friend class ServiceManagerTest; |
| 31 friend class content::MojoTest; |
| 32 |
| 25 explicit TestAPI(ServiceManager* manager) : manager_(manager) {} | 33 explicit TestAPI(ServiceManager* manager) : manager_(manager) {} |
| 34 // Returns true if the shared instance has been created. |
| 35 static bool HasCreatedInstance(); |
| 26 // Returns true if there is a ServiceFactory for this URL. | 36 // Returns true if there is a ServiceFactory for this URL. |
| 27 bool HasFactoryForURL(const GURL& url) const; | 37 bool HasFactoryForURL(const GURL& url) const; |
| 28 | 38 |
| 29 ServiceManager* manager_; | 39 ServiceManager* manager_; |
| 30 }; | 40 }; |
| 31 | 41 |
| 32 ServiceManager(); | 42 ServiceManager(); |
| 33 ~ServiceManager(); | 43 ~ServiceManager(); |
| 34 | 44 |
| 35 // Returns a shared instance, creating it if necessary. | 45 // Returns a shared instance, creating it if necessary. |
| 36 static ServiceManager* GetInstance(); | 46 static ServiceManager* GetInstance(); |
| 37 | 47 |
| 38 // Sets the default Loader to be used if not overridden by SetLoaderForURL(). | 48 // Sets the default Loader to be used if not overridden by SetLoaderForURL(). |
| 39 // Does not take ownership of |loader|. | 49 // Does not take ownership of |loader|. |
| 40 void set_default_loader(ServiceLoader* loader) { default_loader_ = loader; } | 50 void set_default_loader(ServiceLoader* loader) { default_loader_ = loader; } |
| 41 // Sets a Loader to be used for a specific url. | 51 // Sets a Loader to be used for a specific url. |
| 42 // Does not take ownership of |loader|. | 52 // Does not take ownership of |loader|. |
| 43 void SetLoaderForURL(ServiceLoader* loader, const GURL& gurl); | 53 void SetLoaderForURL(ServiceLoader* loader, const GURL& gurl); |
| 44 // Returns the Loader to use for a url (using default if not overridden.) | 54 // Returns the Loader to use for a url (using default if not overridden.) |
| 45 ServiceLoader* GetLoaderForURL(const GURL& gurl); | 55 ServiceLoader* GetLoaderForURL(const GURL& gurl); |
| 46 // Loads a service if necessary and establishes a new client connection. | 56 // Loads a service if necessary and establishes a new client connection. |
| 47 void Connect(const GURL& url, ScopedMessagePipeHandle client_handle); | 57 void Connect(const GURL& url, ScopedMessagePipeHandle client_handle); |
| 48 | 58 |
| 49 private: | 59 private: |
| 50 class ServiceFactory; | 60 class ServiceFactory; |
| 51 | 61 |
| 52 // Removes a ServiceFactory when it no longer has any connections. | 62 // Removes a ServiceFactory when it no longer has any connections. |
| 53 void RemoveServiceFactory(ServiceFactory* service_factory); | 63 void OnServiceFactoryError(ServiceFactory* service_factory); |
| 54 | 64 |
| 55 ServiceLoader* default_loader_; | 65 ServiceLoader* default_loader_; |
| 56 typedef std::map<GURL, ServiceFactory*> ServiceFactoryMap; | 66 typedef std::map<GURL, ServiceFactory*> ServiceFactoryMap; |
| 57 ServiceFactoryMap url_to_service_factory_; | 67 ServiceFactoryMap url_to_service_factory_; |
| 58 typedef std::map<GURL, ServiceLoader*> LoaderMap; | 68 typedef std::map<GURL, ServiceLoader*> LoaderMap; |
| 59 LoaderMap url_to_loader_; | 69 LoaderMap url_to_loader_; |
| 60 DISALLOW_COPY_AND_ASSIGN(ServiceManager); | 70 DISALLOW_COPY_AND_ASSIGN(ServiceManager); |
| 61 }; | 71 }; |
| 62 | 72 |
| 63 } // namespace mojo | 73 } // namespace mojo |
| 64 | 74 |
| 65 #endif // MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ | 75 #endif // MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_ |
| OLD | NEW |