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/public/cpp/bindings/interface_ptr_info.h"
17 #include "mojo/public/cpp/bindings/interface_request.h"
18 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
19 #include "mojo/services/updater/updater.mojom.h"
20 #include "mojo/shell/application_loader.h"
21 #include "mojo/shell/fetcher.h"
22 #include "mojo/shell/identity.h"
23 #include "mojo/shell/native_runner.h"
28 class SequencedWorkerPool
;
36 class ApplicationManager
{
40 // Gives the delegate a chance to apply any mappings for the specified url.
41 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
42 virtual GURL
ResolveMappings(const GURL
& url
) = 0;
44 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
45 // |url| if the scheme is not 'mojo'.
46 virtual GURL
ResolveMojoURL(const GURL
& url
) = 0;
48 // Asks the delegate to create a Fetcher for the specified url. Return
49 // true on success, false if the default fetcher should be created.
50 virtual bool CreateFetcher(
52 const Fetcher::FetchCallback
& loader_callback
) = 0;
55 virtual ~Delegate() {}
61 explicit TestAPI(ApplicationManager
* manager
);
64 // Returns true if the shared instance has been created.
65 static bool HasCreatedInstance();
66 // Returns true if there is a ShellImpl for this URL.
67 bool HasFactoryForURL(const GURL
& url
) const;
70 ApplicationManager
* manager_
;
72 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
75 explicit ApplicationManager(Delegate
* delegate
);
76 ~ApplicationManager();
78 // Loads a service if necessary and establishes a new client connection.
79 void ConnectToApplication(mojo::URLRequestPtr application_url
,
80 const GURL
& requestor_url
,
81 InterfaceRequest
<ServiceProvider
> services
,
82 ServiceProviderPtr exposed_services
,
83 const base::Closure
& on_application_end
);
85 template <typename Interface
>
86 inline void ConnectToService(const GURL
& application_url
,
87 InterfacePtr
<Interface
>* ptr
) {
88 ScopedMessagePipeHandle service_handle
=
89 ConnectToServiceByName(application_url
, Interface::Name_
);
90 ptr
->Bind(InterfacePtrInfo
<Interface
>(service_handle
.Pass(), 0u));
93 ScopedMessagePipeHandle
ConnectToServiceByName(
94 const GURL
& application_url
,
95 const std::string
& interface_name
);
97 void RegisterContentHandler(const std::string
& mime_type
,
98 const GURL
& content_handler_url
);
100 // Registers a package alias. When attempting to load |alias|, it will
101 // instead redirect to |content_handler_package|, which is a content handler
102 // which will be passed the |alias| as the URLResponse::url. Different values
103 // of |alias| with the same |qualifier| that are in the same
104 // |content_handler_package| will run in the same process in multi-process
106 void RegisterApplicationPackageAlias(const GURL
& alias
,
107 const GURL
& content_handler_package
,
108 const std::string
& qualifier
);
110 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
111 // or SetLoaderForScheme().
112 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
113 default_loader_
= loader
.Pass();
115 void set_native_runner_factory(
116 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
117 native_runner_factory_
= runner_factory
.Pass();
119 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
120 blocking_pool_
= blocking_pool
;
122 void set_disable_cache(bool disable_cache
) { disable_cache_
= disable_cache
; }
123 // Sets a Loader to be used for a specific url.
124 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
125 // Sets a Loader to be used for a specific url scheme.
126 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
127 const std::string
& scheme
);
128 // These options will be used in running any native application at |url|
129 // (which shouldn't contain a query string). (|url| will be mapped and
130 // resolved, and any application whose base resolved URL matches it will have
131 // |options| applied.)
132 // TODO(vtl): This may not do what's desired if the resolved URL results in an
133 // HTTP redirect. Really, we want options to be identified with a particular
134 // implementation, maybe via a signed manifest or something like that.
135 void SetNativeOptionsForURL(const NativeRunnerFactory::Options
& options
,
138 // Destroys all Shell-ends of connections established with Applications.
139 // Applications connected by this ApplicationManager will observe pipe errors
140 // and have a chance to shutdown.
141 void TerminateShellConnections();
143 // Removes a ShellImpl when it encounters an error.
144 void OnShellImplError(ShellImpl
* shell_impl
);
147 class ContentHandlerConnection
;
149 using ApplicationPackagedAlias
= std::map
<GURL
, std::pair
<GURL
, std::string
>>;
150 using IdentityToShellImplMap
= std::map
<Identity
, ShellImpl
*>;
151 using MimeTypeToURLMap
= std::map
<std::string
, GURL
>;
152 using SchemeToLoaderMap
= std::map
<std::string
, ApplicationLoader
*>;
153 using URLToContentHandlerMap
=
154 std::map
<std::pair
<GURL
, std::string
>, ContentHandlerConnection
*>;
155 using URLToLoaderMap
= std::map
<GURL
, ApplicationLoader
*>;
156 using URLToNativeOptionsMap
= std::map
<GURL
, NativeRunnerFactory::Options
>;
158 void ConnectToApplicationInternal(
159 mojo::URLRequestPtr requested_url
,
160 const std::string
& qualifier
,
161 const GURL
& requestor_url
,
162 InterfaceRequest
<ServiceProvider
> services
,
163 ServiceProviderPtr exposed_services
,
164 const base::Closure
& on_application_end
);
166 bool ConnectToRunningApplication(const GURL
& resolved_url
,
167 const std::string
& qualifier
,
168 const GURL
& requestor_url
,
169 InterfaceRequest
<ServiceProvider
>* services
,
170 ServiceProviderPtr
* exposed_services
);
172 bool ConnectToApplicationWithLoader(
173 const GURL
& requested_url
,
174 const std::string
& qualifier
,
175 const GURL
& resolved_url
,
176 const GURL
& requestor_url
,
177 InterfaceRequest
<ServiceProvider
>* services
,
178 ServiceProviderPtr
* exposed_services
,
179 const base::Closure
& on_application_end
,
180 ApplicationLoader
* loader
);
182 InterfaceRequest
<Application
> RegisterShell(
184 const std::string
& qualifier
,
185 const GURL
& requestor_url
,
186 InterfaceRequest
<ServiceProvider
> services
,
187 ServiceProviderPtr exposed_services
,
188 const base::Closure
& on_application_end
);
190 ShellImpl
* GetShellImpl(const GURL
& url
, const std::string
& qualifier
);
192 void ConnectToClient(ShellImpl
* shell_impl
,
193 const GURL
& resolved_url
,
194 const GURL
& requestor_url
,
195 InterfaceRequest
<ServiceProvider
> services
,
196 ServiceProviderPtr exposed_services
);
198 // Called once |fetcher| has found app. |requested_url| is the url of the
199 // requested application before any mappings/resolution have been applied.
200 void HandleFetchCallback(const GURL
& requested_url
,
201 const std::string
& qualifier
,
202 const GURL
& requestor_url
,
203 InterfaceRequest
<ServiceProvider
> services
,
204 ServiceProviderPtr exposed_services
,
205 const base::Closure
& on_application_end
,
206 NativeApplicationCleanup cleanup
,
207 scoped_ptr
<Fetcher
> fetcher
);
209 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
210 const NativeRunnerFactory::Options
& options
,
211 NativeApplicationCleanup cleanup
,
212 scoped_ptr
<Fetcher
> fetcher
,
213 const base::FilePath
& file_path
,
216 void LoadWithContentHandler(const GURL
& content_handler_url
,
217 const GURL
& requestor_url
,
218 const std::string
& qualifier
,
219 InterfaceRequest
<Application
> application_request
,
220 URLResponsePtr url_response
);
222 // Returns the appropriate loader for |url|, or null if there is no loader
223 // configured for the URL.
224 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
226 // Removes a ContentHandler when it encounters an error.
227 void OnContentHandlerError(ContentHandlerConnection
* content_handler
);
229 void CleanupRunner(NativeRunner
* runner
);
231 Delegate
* const delegate_
;
232 // Loader management.
233 // Loaders are chosen in the order they are listed here.
234 URLToLoaderMap url_to_loader_
;
235 SchemeToLoaderMap scheme_to_loader_
;
236 scoped_ptr
<ApplicationLoader
> default_loader_
;
237 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
239 ApplicationPackagedAlias application_package_alias_
;
240 IdentityToShellImplMap identity_to_shell_impl_
;
241 URLToContentHandlerMap url_to_content_handler_
;
242 // Note: The keys are URLs after mapping and resolving.
243 URLToNativeOptionsMap url_to_native_options_
;
245 base::SequencedWorkerPool
* blocking_pool_
;
246 URLLoaderFactoryPtr url_loader_factory_
;
247 updater::UpdaterPtr updater_
;
248 MimeTypeToURLMap mime_type_to_url_
;
249 ScopedVector
<NativeRunner
> native_runners_
;
251 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
253 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
259 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_