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 true if the shared instance has been created.
29 static bool HasCreatedInstance();
30 // Returns true if there is a ShellImpl for this URL.
31 bool HasFactoryForURL(const GURL
& url
) const;
34 ServiceManager
* manager_
;
36 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
39 // Interface class for debugging only.
42 virtual ~Interceptor() {}
43 // Called when ServiceManager::Connect is called.
44 virtual ServiceProviderPtr
OnConnectToClient(
45 const GURL
& url
, ServiceProviderPtr service_provider
) = 0;
51 // Returns a shared instance, creating it if necessary.
52 static ServiceManager
* GetInstance();
54 // Loads a service if necessary and establishes a new client connection.
55 void ConnectToApplication(const GURL
& application_url
,
56 const GURL
& requestor_url
,
57 ServiceProviderPtr service_provider
);
59 template <typename Interface
>
60 inline void ConnectToService(const GURL
& application_url
,
61 InterfacePtr
<Interface
>* ptr
) {
62 ScopedMessagePipeHandle service_handle
=
63 ConnectToServiceByName(application_url
, Interface::Name_
);
64 ptr
->Bind(service_handle
.Pass());
67 ScopedMessagePipeHandle
ConnectToServiceByName(
68 const GURL
& application_url
,
69 const std::string
& interface_name
);
71 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
72 // or SetLoaderForScheme().
73 void set_default_loader(scoped_ptr
<ServiceLoader
> loader
) {
74 default_loader_
= loader
.Pass();
76 // Sets a Loader to be used for a specific url.
77 void SetLoaderForURL(scoped_ptr
<ServiceLoader
> loader
, const GURL
& url
);
78 // Sets a Loader to be used for a specific url scheme.
79 void SetLoaderForScheme(scoped_ptr
<ServiceLoader
> loader
,
80 const std::string
& scheme
);
81 // Allows to interpose a debugger to service connections.
82 void SetInterceptor(Interceptor
* interceptor
);
84 // Destroys all Shell-ends of connections established with Applications.
85 // Applications connected by this ServiceManager will observe pipe errors
86 // and have a chance to shutdown.
87 void TerminateShellConnections();
91 typedef std::map
<std::string
, ServiceLoader
*> SchemeToLoaderMap
;
92 typedef std::map
<GURL
, ServiceLoader
*> URLToLoaderMap
;
93 typedef std::map
<GURL
, ShellImpl
*> URLToShellImplMap
;
95 // Returns the Loader to use for a url (using default if not overridden.)
96 // The preference is to use a loader that's been specified for an url first,
97 // then one that's been specified for a scheme, then the default.
98 ServiceLoader
* GetLoaderForURL(const GURL
& url
);
100 // Removes a ShellImpl when it encounters an error.
101 void OnShellImplError(ShellImpl
* shell_impl
);
103 // Loader management.
104 URLToLoaderMap url_to_loader_
;
105 SchemeToLoaderMap scheme_to_loader_
;
106 scoped_ptr
<ServiceLoader
> default_loader_
;
107 Interceptor
* interceptor_
;
109 URLToShellImplMap url_to_shell_impl_
;
111 DISALLOW_COPY_AND_ASSIGN(ServiceManager
);
116 #endif // MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_