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 SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
6 #define SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/public/cpp/bindings/interface_request.h"
15 #include "mojo/public/interfaces/application/application.mojom.h"
16 #include "mojo/public/interfaces/application/service_provider.mojom.h"
17 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
18 #include "mojo/shell/application_manager/application_loader.h"
19 #include "mojo/shell/application_manager/identity.h"
20 #include "mojo/shell/application_manager/native_runner.h"
21 #include "mojo/shell/native_application_support.h"
26 class SequencedWorkerPool
;
35 class ApplicationManager
{
40 virtual GURL
ResolveURL(const GURL
& url
);
41 virtual GURL
ResolveMappings(const GURL
& url
);
47 explicit TestAPI(ApplicationManager
* manager
);
50 // Returns true if the shared instance has been created.
51 static bool HasCreatedInstance();
52 // Returns true if there is a ShellImpl for this URL.
53 bool HasFactoryForURL(const GURL
& url
) const;
56 ApplicationManager
* manager_
;
58 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
61 explicit ApplicationManager(Delegate
* delegate
);
62 ~ApplicationManager();
64 // Loads a service if necessary and establishes a new client connection.
65 void ConnectToApplication(const GURL
& application_url
,
66 const GURL
& requestor_url
,
67 InterfaceRequest
<ServiceProvider
> services
,
68 ServiceProviderPtr exposed_services
,
69 const base::Closure
& on_application_end
);
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 RegisterContentHandler(const std::string
& mime_type
,
84 const GURL
& content_handler_url
);
86 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
87 // or SetLoaderForScheme().
88 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
89 default_loader_
= loader
.Pass();
91 void set_native_runner_factory(
92 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
93 native_runner_factory_
= runner_factory
.Pass();
95 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
96 blocking_pool_
= blocking_pool
;
98 void set_disable_cache(bool disable_cache
) { disable_cache_
= disable_cache
; }
99 // Sets a Loader to be used for a specific url.
100 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
101 // Sets a Loader to be used for a specific url scheme.
102 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
103 const std::string
& scheme
);
104 // These options will be used in running any native application at |url|
105 // (which shouldn't contain a query string). (|url| will be mapped and
106 // resolved, and any application whose base resolved URL matches it will have
107 // |options| applied.)
108 // TODO(vtl): This may not do what's desired if the resolved URL results in an
109 // HTTP redirect. Really, we want options to be identified with a particular
110 // implementation, maybe via a signed manifest or something like that.
111 void SetNativeOptionsForURL(const NativeRunnerFactory::Options
& options
,
114 // Destroys all Shell-ends of connections established with Applications.
115 // Applications connected by this ApplicationManager will observe pipe errors
116 // and have a chance to shutdown.
117 void TerminateShellConnections();
119 // Removes a ShellImpl when it encounters an error.
120 void OnShellImplError(ShellImpl
* shell_impl
);
123 class ContentHandlerConnection
;
125 typedef std::map
<std::string
, ApplicationLoader
*> SchemeToLoaderMap
;
126 typedef std::map
<GURL
, ApplicationLoader
*> URLToLoaderMap
;
127 typedef std::map
<Identity
, ShellImpl
*> IdentityToShellImplMap
;
128 typedef std::map
<GURL
, ContentHandlerConnection
*> URLToContentHandlerMap
;
129 typedef std::map
<std::string
, GURL
> MimeTypeToURLMap
;
130 typedef std::map
<GURL
, NativeRunnerFactory::Options
> URLToNativeOptionsMap
;
132 void ConnectToApplicationWithParameters(
133 const GURL
& application_url
,
134 const GURL
& requestor_url
,
135 InterfaceRequest
<ServiceProvider
> services
,
136 ServiceProviderPtr exposed_services
,
137 const base::Closure
& on_application_end
,
138 const std::vector
<std::string
>& pre_redirect_parameters
);
140 bool ConnectToRunningApplication(const GURL
& resolved_url
,
141 const GURL
& requestor_url
,
142 InterfaceRequest
<ServiceProvider
>* services
,
143 ServiceProviderPtr
* exposed_services
);
145 bool ConnectToApplicationWithLoader(
146 const GURL
& resolved_url
,
147 const GURL
& requestor_url
,
148 InterfaceRequest
<ServiceProvider
>* services
,
149 ServiceProviderPtr
* exposed_services
,
150 const base::Closure
& on_application_end
,
151 const std::vector
<std::string
>& parameters
,
152 ApplicationLoader
* loader
);
154 InterfaceRequest
<Application
> RegisterShell(
155 // The URL after resolution and redirects, including the querystring.
156 const GURL
& resolved_url
,
157 const GURL
& requestor_url
,
158 InterfaceRequest
<ServiceProvider
> services
,
159 ServiceProviderPtr exposed_services
,
160 const base::Closure
& on_application_end
,
161 const std::vector
<std::string
>& parameters
);
163 ShellImpl
* GetShellImpl(const GURL
& url
);
165 void ConnectToClient(ShellImpl
* shell_impl
,
166 const GURL
& resolved_url
,
167 const GURL
& requestor_url
,
168 InterfaceRequest
<ServiceProvider
> services
,
169 ServiceProviderPtr exposed_services
);
171 void HandleFetchCallback(const GURL
& requestor_url
,
172 InterfaceRequest
<ServiceProvider
> services
,
173 ServiceProviderPtr exposed_services
,
174 const base::Closure
& on_application_end
,
175 const std::vector
<std::string
>& parameters
,
176 NativeApplicationCleanup cleanup
,
177 scoped_ptr
<Fetcher
> fetcher
);
179 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
180 const NativeRunnerFactory::Options
& options
,
181 NativeApplicationCleanup cleanup
,
182 scoped_ptr
<Fetcher
> fetcher
,
183 const base::FilePath
& file_path
,
186 void LoadWithContentHandler(const GURL
& content_handler_url
,
187 InterfaceRequest
<Application
> application_request
,
188 URLResponsePtr url_response
);
190 // Returns the appropriate loader for |url|, or null if there is no loader
191 // configured for the URL.
192 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
194 // Removes a ContentHandler when it encounters an error.
195 void OnContentHandlerError(ContentHandlerConnection
* content_handler
);
197 void CleanupRunner(NativeRunner
* runner
);
199 Delegate
* const delegate_
;
200 // Loader management.
201 // Loaders are chosen in the order they are listed here.
202 URLToLoaderMap url_to_loader_
;
203 SchemeToLoaderMap scheme_to_loader_
;
204 scoped_ptr
<ApplicationLoader
> default_loader_
;
205 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
207 IdentityToShellImplMap identity_to_shell_impl_
;
208 URLToContentHandlerMap url_to_content_handler_
;
209 // Note: The keys are URLs after mapping and resolving.
210 URLToNativeOptionsMap url_to_native_options_
;
212 base::SequencedWorkerPool
* blocking_pool_
;
213 NetworkServicePtr network_service_
;
214 MimeTypeToURLMap mime_type_to_url_
;
215 ScopedVector
<NativeRunner
> native_runners_
;
217 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
219 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
225 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_