1 // Copyright 2014 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_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
6 #define MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/application_manager/application_loader.h"
15 #include "mojo/application_manager/application_manager_export.h"
16 #include "mojo/public/interfaces/application/service_provider.mojom.h"
21 class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager
{
23 class MOJO_APPLICATION_MANAGER_EXPORT Delegate
{
26 // Send when the Application holding the handle on the other end of the
27 // Shell pipe goes away.
28 virtual void OnApplicationError(const GURL
& url
) = 0;
32 class MOJO_APPLICATION_MANAGER_EXPORT TestAPI
{
34 explicit TestAPI(ApplicationManager
* manager
);
37 // Returns true if the shared instance has been created.
38 static bool HasCreatedInstance();
39 // Returns true if there is a ShellImpl for this URL.
40 bool HasFactoryForURL(const GURL
& url
) const;
43 ApplicationManager
* manager_
;
45 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
48 // Interface class for debugging only.
51 virtual ~Interceptor() {}
52 // Called when ApplicationManager::Connect is called.
53 virtual ServiceProviderPtr
OnConnectToClient(
55 ServiceProviderPtr service_provider
) = 0;
59 ~ApplicationManager();
61 // Returns a shared instance, creating it if necessary.
62 static ApplicationManager
* GetInstance();
64 void SetDelegate(Delegate
* delegate
) { delegate_
= delegate
; }
66 // Loads a service if necessary and establishes a new client connection.
67 void ConnectToApplication(const GURL
& application_url
,
68 const GURL
& requestor_url
,
69 ServiceProviderPtr service_provider
);
71 template <typename Interface
>
72 inline void ConnectToService(const GURL
& application_url
,
73 InterfacePtr
<Interface
>* ptr
) {
74 ScopedMessagePipeHandle service_handle
=
75 ConnectToServiceByName(application_url
, Interface::Name_
);
76 ptr
->Bind(service_handle
.Pass());
79 ScopedMessagePipeHandle
ConnectToServiceByName(
80 const GURL
& application_url
,
81 const std::string
& interface_name
);
83 void RegisterExternalApplication(const GURL
& application_url
,
84 ScopedMessagePipeHandle shell
);
86 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
88 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
89 // or SetLoaderForScheme().
90 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
91 default_loader_
= loader
.Pass();
93 // Sets a Loader to be used for a specific url.
94 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
95 // Sets a Loader to be used for a specific url scheme.
96 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
97 const std::string
& scheme
);
98 // These strings will be passed to the Initialize() method when an
99 // Application is instantiated.
100 void SetArgsForURL(const std::vector
<std::string
>& args
, const GURL
& url
);
102 // Allows to interpose a debugger to service connections.
103 void SetInterceptor(Interceptor
* interceptor
);
105 // Destroys all Shell-ends of connections established with Applications.
106 // Applications connected by this ApplicationManager will observe pipe errors
107 // and have a chance to shutdown.
108 void TerminateShellConnections();
111 struct ContentHandlerConnection
;
112 class LoadCallbacksImpl
;
115 typedef std::map
<std::string
, ApplicationLoader
*> SchemeToLoaderMap
;
116 typedef std::map
<GURL
, ApplicationLoader
*> URLToLoaderMap
;
117 typedef std::map
<GURL
, ShellImpl
*> URLToShellImplMap
;
118 typedef std::map
<GURL
, ContentHandlerConnection
*> URLToContentHandlerMap
;
119 typedef std::map
<GURL
, std::vector
<std::string
> > URLToArgsMap
;
121 void ConnectToClient(ShellImpl
* shell_impl
,
123 const GURL
& requestor_url
,
124 ServiceProviderPtr service_provider
);
126 void RegisterLoadedApplication(const GURL
& service_url
,
127 const GURL
& requestor_url
,
128 ServiceProviderPtr service_provider
,
129 ScopedMessagePipeHandle
* shell_handle
);
131 void LoadWithContentHandler(const GURL
& content_url
,
132 const GURL
& requestor_url
,
133 const GURL
& content_handler_url
,
134 URLResponsePtr url_response
,
135 ServiceProviderPtr service_provider
);
137 // Returns the Loader to use for a url (using default if not overridden.)
138 // The preference is to use a loader that's been specified for an url first,
139 // then one that's been specified for a scheme, then the default.
140 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
142 // Removes a ShellImpl when it encounters an error.
143 void OnShellImplError(ShellImpl
* shell_impl
);
146 // Loader management.
147 URLToLoaderMap url_to_loader_
;
148 SchemeToLoaderMap scheme_to_loader_
;
149 scoped_ptr
<ApplicationLoader
> default_loader_
;
150 Interceptor
* interceptor_
;
152 URLToShellImplMap url_to_shell_impl_
;
153 URLToContentHandlerMap url_to_content_handler_
;
154 URLToArgsMap url_to_args_
;
156 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
163 #endif // MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_