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
3 // found in the LICENSE file.
5 #ifndef MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_
6 #define MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
14 #include "mojo/service_manager/service_loader.h"
15 #include "mojo/service_manager/service_manager_export.h"
20 class MOJO_SERVICE_MANAGER_EXPORT ServiceManager
{
23 class MOJO_SERVICE_MANAGER_EXPORT TestAPI
{
25 explicit TestAPI(ServiceManager
* manager
);
28 // Returns a handle to a unique ServiceProvider instance.
29 ScopedMessagePipeHandle
GetServiceProviderHandle();
31 // Returns true if the shared instance has been created.
32 static bool HasCreatedInstance();
33 // Returns true if there is a ServiceFactory for this URL.
34 bool HasFactoryForURL(const GURL
& url
) const;
37 class TestServiceProviderConnection
;
39 ServiceManager
* manager_
;
40 scoped_ptr
<TestServiceProviderConnection
> service_provider_
;
42 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
45 // Interface class for debugging only.
48 virtual ~Interceptor() {}
49 // Called when ServiceManager::Connect is called.
50 virtual ScopedMessagePipeHandle
OnConnectToClient(
51 const GURL
& url
, ScopedMessagePipeHandle handle
) = 0;
57 // Returns a shared instance, creating it if necessary.
58 static ServiceManager
* GetInstance();
60 // Loads a service if necessary and establishes a new client connection.
61 void ConnectToService(const GURL
& service_url
,
62 const std::string
& service_name
,
63 ScopedMessagePipeHandle client_handle
,
64 const GURL
& requestor_url
);
66 template <typename Interface
>
67 void ConnectTo(const GURL
& service_url
,
68 InterfacePtr
<Interface
>* ptr
,
69 const GURL
& requestor_url
) {
71 ptr
->Bind(pipe
.handle0
.Pass());
72 ConnectToService(service_url
,
78 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
79 // or SetLoaderForScheme().
80 void set_default_loader(scoped_ptr
<ServiceLoader
> loader
) {
81 default_loader_
= loader
.Pass();
83 // Sets a Loader to be used for a specific url.
84 void SetLoaderForURL(scoped_ptr
<ServiceLoader
> loader
, const GURL
& url
);
85 // Sets a Loader to be used for a specific url scheme.
86 void SetLoaderForScheme(scoped_ptr
<ServiceLoader
> loader
,
87 const std::string
& scheme
);
88 // Allows to interpose a debugger to service connections.
89 void SetInterceptor(Interceptor
* interceptor
);
93 typedef std::map
<std::string
, ServiceLoader
*> SchemeToLoaderMap
;
94 typedef std::map
<GURL
, ServiceLoader
*> URLToLoaderMap
;
95 typedef std::map
<GURL
, ServiceFactory
*> URLToServiceFactoryMap
;
97 // Returns the Loader to use for a url (using default if not overridden.)
98 // The preference is to use a loader that's been specified for an url first,
99 // then one that's been specified for a scheme, then the default.
100 ServiceLoader
* GetLoaderForURL(const GURL
& url
);
102 // Removes a ServiceFactory when it encounters an error.
103 void OnServiceFactoryError(ServiceFactory
* service_factory
);
105 // Loader management.
106 URLToLoaderMap url_to_loader_
;
107 SchemeToLoaderMap scheme_to_loader_
;
108 scoped_ptr
<ServiceLoader
> default_loader_
;
109 Interceptor
* interceptor_
;
111 URLToServiceFactoryMap url_to_service_factory_
;
112 DISALLOW_COPY_AND_ASSIGN(ServiceManager
);
117 #endif // MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_