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/application/public/interfaces/application.mojom.h"
15 #include "mojo/application/public/interfaces/service_provider.mojom.h"
16 #include "mojo/application/public/interfaces/shell.mojom.h"
17 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
18 #include "mojo/public/cpp/bindings/interface_request.h"
19 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
20 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
21 #include "mojo/services/updater/updater.mojom.h"
22 #include "mojo/shell/application_loader.h"
23 #include "mojo/shell/capability_filter.h"
24 #include "mojo/shell/fetcher.h"
25 #include "mojo/shell/identity.h"
26 #include "mojo/shell/native_runner.h"
31 class SequencedWorkerPool
;
37 class ApplicationInstance
;
38 class ContentHandlerConnection
;
40 class ApplicationManager
{
44 // Gives the delegate a chance to apply any mappings for the specified url.
45 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
46 virtual GURL
ResolveMappings(const GURL
& url
) = 0;
48 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
49 // |url| if the scheme is not 'mojo'.
50 virtual GURL
ResolveMojoURL(const GURL
& url
) = 0;
52 // Asks the delegate to create a Fetcher for the specified url. Return
53 // true on success, false if the default fetcher should be created.
54 virtual bool CreateFetcher(
56 const Fetcher::FetchCallback
& loader_callback
) = 0;
59 virtual ~Delegate() {}
65 explicit TestAPI(ApplicationManager
* manager
);
68 // Returns true if the shared instance has been created.
69 static bool HasCreatedInstance();
70 // Returns true if there is a ApplicationInstance for this URL.
71 bool HasRunningInstanceForURL(const GURL
& url
) const;
74 ApplicationManager
* manager_
;
76 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
79 explicit ApplicationManager(Delegate
* delegate
);
80 ~ApplicationManager();
82 // Loads a service if necessary and establishes a new client connection.
83 // |originator| can be NULL (e.g. for the first application or in tests), but
84 // typically is non-NULL and identifies the instance initiating the
86 void ConnectToApplication(
87 ApplicationInstance
* originator
,
88 URLRequestPtr requested_url
,
89 const std::string
& qualifier
,
90 const GURL
& requestor_url
,
91 InterfaceRequest
<ServiceProvider
> services
,
92 ServiceProviderPtr exposed_services
,
93 const CapabilityFilter
& capability_filter
,
94 const base::Closure
& on_application_end
,
95 const Shell::ConnectToApplicationCallback
& connect_callback
);
97 // Must only be used by shell internals and test code as it does not forward
98 // capability filters.
99 template <typename Interface
>
100 inline void ConnectToService(const GURL
& application_url
,
101 InterfacePtr
<Interface
>* ptr
) {
102 ScopedMessagePipeHandle service_handle
=
103 ConnectToServiceByName(application_url
, Interface::Name_
);
104 ptr
->Bind(InterfacePtrInfo
<Interface
>(service_handle
.Pass(), 0u));
107 void RegisterContentHandler(const std::string
& mime_type
,
108 const GURL
& content_handler_url
);
110 // Registers a package alias. When attempting to load |alias|, it will
111 // instead redirect to |content_handler_package|, which is a content handler
112 // which will be passed the |alias| as the URLResponse::url. Different values
113 // of |alias| with the same |qualifier| that are in the same
114 // |content_handler_package| will run in the same process in multi-process
116 void RegisterApplicationPackageAlias(const GURL
& alias
,
117 const GURL
& content_handler_package
,
118 const std::string
& qualifier
);
120 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
121 // or SetLoaderForScheme().
122 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
123 default_loader_
= loader
.Pass();
125 void set_native_runner_factory(
126 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
127 native_runner_factory_
= runner_factory
.Pass();
129 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
130 blocking_pool_
= blocking_pool
;
132 void set_disable_cache(bool disable_cache
) { disable_cache_
= disable_cache
; }
133 // Sets a Loader to be used for a specific url.
134 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
135 // Sets a Loader to be used for a specific url scheme.
136 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
137 const std::string
& scheme
);
138 // These options will be used in running any native application at |url|
139 // (which shouldn't contain a query string). (|url| will be mapped and
140 // resolved, and any application whose base resolved URL matches it will have
141 // |options| applied.)
142 // TODO(vtl): This may not do what's desired if the resolved URL results in an
143 // HTTP redirect. Really, we want options to be identified with a particular
144 // implementation, maybe via a signed manifest or something like that.
145 void SetNativeOptionsForURL(const NativeRunnerFactory::Options
& options
,
148 // Destroys all Shell-ends of connections established with Applications.
149 // Applications connected by this ApplicationManager will observe pipe errors
150 // and have a chance to shutdown.
151 void TerminateShellConnections();
153 // Removes a ApplicationInstance when it encounters an error.
154 void OnApplicationInstanceError(ApplicationInstance
* instance
);
156 // Removes a ContentHandler when its connection is closed.
157 void OnContentHandlerConnectionClosed(
158 ContentHandlerConnection
* content_handler
);
160 ApplicationInstance
* GetApplicationInstance(const Identity
& identity
) const;
163 using ApplicationPackagedAlias
= std::map
<GURL
, std::pair
<GURL
, std::string
>>;
164 using IdentityToApplicationInstanceMap
=
165 std::map
<Identity
, ApplicationInstance
*>;
166 using MimeTypeToURLMap
= std::map
<std::string
, GURL
>;
167 using SchemeToLoaderMap
= std::map
<std::string
, ApplicationLoader
*>;
168 using URLToContentHandlerMap
=
169 std::map
<std::pair
<GURL
, std::string
>, ContentHandlerConnection
*>;
170 using URLToLoaderMap
= std::map
<GURL
, ApplicationLoader
*>;
171 using URLToNativeOptionsMap
= std::map
<GURL
, NativeRunnerFactory::Options
>;
173 bool ConnectToRunningApplication(
174 ApplicationInstance
* originator
,
175 const GURL
& resolved_url
,
176 const std::string
& qualifier
,
177 const GURL
& requestor_url
,
178 InterfaceRequest
<ServiceProvider
>* services
,
179 ServiceProviderPtr
* exposed_services
,
180 const CapabilityFilter
& filter
,
181 const Shell::ConnectToApplicationCallback
& connect_callback
);
183 bool ConnectToApplicationWithLoader(
184 ApplicationInstance
* originator
,
185 const GURL
& requested_url
,
186 const std::string
& qualifier
,
187 const GURL
& resolved_url
,
188 const GURL
& requestor_url
,
189 InterfaceRequest
<ServiceProvider
>* services
,
190 ServiceProviderPtr
* exposed_services
,
191 const CapabilityFilter
& filter
,
192 const base::Closure
& on_application_end
,
193 const Shell::ConnectToApplicationCallback
& connect_callback
,
194 ApplicationLoader
* loader
);
196 InterfaceRequest
<Application
> RegisterInstance(
197 ApplicationInstance
* originator
,
199 const std::string
& qualifier
,
200 const GURL
& requestor_url
,
201 InterfaceRequest
<ServiceProvider
> services
,
202 ServiceProviderPtr exposed_services
,
203 const CapabilityFilter
& filter
,
204 const base::Closure
& on_application_end
,
205 const Shell::ConnectToApplicationCallback
& connect_callback
,
206 ApplicationInstance
** resulting_instance
);
208 // Called once |fetcher| has found app. |requested_url| is the url of the
209 // requested application before any mappings/resolution have been applied.
210 void HandleFetchCallback(
211 ApplicationInstance
* originator
,
212 const GURL
& requested_url
,
213 const std::string
& qualifier
,
214 const GURL
& requestor_url
,
215 InterfaceRequest
<ServiceProvider
> services
,
216 ServiceProviderPtr exposed_services
,
217 const CapabilityFilter
& filter
,
218 const base::Closure
& on_application_end
,
219 const Shell::ConnectToApplicationCallback
& connect_callback
,
220 NativeApplicationCleanup cleanup
,
221 scoped_ptr
<Fetcher
> fetcher
);
223 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
224 bool start_sandboxed
,
225 const NativeRunnerFactory::Options
& options
,
226 NativeApplicationCleanup cleanup
,
227 scoped_ptr
<Fetcher
> fetcher
,
228 const base::FilePath
& file_path
,
231 void LoadWithContentHandler(
232 ApplicationInstance
* originator
,
233 const GURL
& content_handler_url
,
234 const GURL
& requestor_url
,
235 const std::string
& qualifier
,
236 const CapabilityFilter
& filter
,
237 const Shell::ConnectToApplicationCallback
& connect_callback
,
238 ApplicationInstance
* app
,
239 InterfaceRequest
<Application
> application_request
,
240 URLResponsePtr url_response
);
242 // Returns the appropriate loader for |url|, or null if there is no loader
243 // configured for the URL.
244 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
246 void CleanupRunner(NativeRunner
* runner
);
248 ScopedMessagePipeHandle
ConnectToServiceByName(
249 const GURL
& application_url
,
250 const std::string
& interface_name
);
252 Delegate
* const delegate_
;
253 // Loader management.
254 // Loaders are chosen in the order they are listed here.
255 URLToLoaderMap url_to_loader_
;
256 SchemeToLoaderMap scheme_to_loader_
;
257 scoped_ptr
<ApplicationLoader
> default_loader_
;
258 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
260 ApplicationPackagedAlias application_package_alias_
;
261 IdentityToApplicationInstanceMap identity_to_instance_
;
262 URLToContentHandlerMap url_to_content_handler_
;
263 // Note: The keys are URLs after mapping and resolving.
264 URLToNativeOptionsMap url_to_native_options_
;
266 base::SequencedWorkerPool
* blocking_pool_
;
267 NetworkServicePtr network_service_
;
268 URLLoaderFactoryPtr url_loader_factory_
;
269 updater::UpdaterPtr updater_
;
270 MimeTypeToURLMap mime_type_to_url_
;
271 ScopedVector
<NativeRunner
> native_runners_
;
273 // Counter used to assign ids to content_handlers.
274 uint32_t content_handler_id_counter_
;
275 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
277 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
280 Shell::ConnectToApplicationCallback
EmptyConnectCallback();
285 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_