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 void RegisterExternalApplication(const GURL
& application_url
,
87 const std::vector
<std::string
>& args
,
88 ApplicationPtr application
);
90 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
91 // or SetLoaderForScheme().
92 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
93 default_loader_
= loader
.Pass();
95 void set_native_runner_factory(
96 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
97 native_runner_factory_
= runner_factory
.Pass();
99 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
100 blocking_pool_
= blocking_pool
;
102 void set_disable_cache(bool disable_cache
) { disable_cache_
= disable_cache
; }
103 // Sets a Loader to be used for a specific url.
104 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
105 // Sets a Loader to be used for a specific url scheme.
106 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
107 const std::string
& scheme
);
108 // These strings will be passed to the Initialize() method when an Application
110 // TODO(vtl): Maybe we should store/compare resolved URLs, like
111 // SetNativeOptionsForURL() below?
112 void SetArgsForURL(const std::vector
<std::string
>& args
, const GURL
& url
);
113 // These options will be used in running any native application at |url|
114 // (which shouldn't contain a query string). (|url| will be mapped and
115 // resolved, and any application whose base resolved URL matches it will have
116 // |options| applied.)
117 // TODO(vtl): This may not do what's desired if the resolved URL results in an
118 // HTTP redirect. Really, we want options to be identified with a particular
119 // implementation, maybe via a signed manifest or something like that.
120 void SetNativeOptionsForURL(const NativeRunnerFactory::Options
& options
,
123 // Destroys all Shell-ends of connections established with Applications.
124 // Applications connected by this ApplicationManager will observe pipe errors
125 // and have a chance to shutdown.
126 void TerminateShellConnections();
128 // Removes a ShellImpl when it encounters an error.
129 void OnShellImplError(ShellImpl
* shell_impl
);
132 class ContentHandlerConnection
;
134 typedef std::map
<std::string
, ApplicationLoader
*> SchemeToLoaderMap
;
135 typedef std::map
<GURL
, ApplicationLoader
*> URLToLoaderMap
;
136 typedef std::map
<Identity
, ShellImpl
*> IdentityToShellImplMap
;
137 typedef std::map
<GURL
, ContentHandlerConnection
*> URLToContentHandlerMap
;
138 typedef std::map
<GURL
, std::vector
<std::string
>> URLToArgsMap
;
139 typedef std::map
<std::string
, GURL
> MimeTypeToURLMap
;
140 typedef std::map
<GURL
, NativeRunnerFactory::Options
> URLToNativeOptionsMap
;
142 void ConnectToApplicationWithParameters(
143 const GURL
& application_url
,
144 const GURL
& requestor_url
,
145 InterfaceRequest
<ServiceProvider
> services
,
146 ServiceProviderPtr exposed_services
,
147 const base::Closure
& on_application_end
,
148 const std::vector
<std::string
>& pre_redirect_parameters
);
150 bool ConnectToRunningApplication(const GURL
& resolved_url
,
151 const GURL
& requestor_url
,
152 InterfaceRequest
<ServiceProvider
>* services
,
153 ServiceProviderPtr
* exposed_services
);
155 bool ConnectToApplicationWithLoader(
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
,
162 ApplicationLoader
* loader
);
164 InterfaceRequest
<Application
> RegisterShell(
165 // The URL after resolution and redirects, including the querystring.
166 const GURL
& resolved_url
,
167 const GURL
& requestor_url
,
168 InterfaceRequest
<ServiceProvider
> services
,
169 ServiceProviderPtr exposed_services
,
170 const base::Closure
& on_application_end
,
171 const std::vector
<std::string
>& parameters
);
173 ShellImpl
* GetShellImpl(const GURL
& url
);
175 void ConnectToClient(ShellImpl
* shell_impl
,
176 const GURL
& resolved_url
,
177 const GURL
& requestor_url
,
178 InterfaceRequest
<ServiceProvider
> services
,
179 ServiceProviderPtr exposed_services
);
181 void HandleFetchCallback(const GURL
& requestor_url
,
182 InterfaceRequest
<ServiceProvider
> services
,
183 ServiceProviderPtr exposed_services
,
184 const base::Closure
& on_application_end
,
185 const std::vector
<std::string
>& parameters
,
186 NativeApplicationCleanup cleanup
,
187 scoped_ptr
<Fetcher
> fetcher
);
189 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
190 const NativeRunnerFactory::Options
& options
,
191 NativeApplicationCleanup cleanup
,
192 scoped_ptr
<Fetcher
> fetcher
,
193 const base::FilePath
& file_path
,
196 void LoadWithContentHandler(const GURL
& content_handler_url
,
197 InterfaceRequest
<Application
> application_request
,
198 URLResponsePtr url_response
);
200 // Returns the appropriate loader for |url|, or null if there is no loader
201 // configured for the URL.
202 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
204 // Removes a ContentHandler when it encounters an error.
205 void OnContentHandlerError(ContentHandlerConnection
* content_handler
);
207 // Returns the arguments for the given url.
208 std::vector
<std::string
> GetArgsForURL(const GURL
& url
);
210 void CleanupRunner(NativeRunner
* runner
);
212 Delegate
* const delegate_
;
213 // Loader management.
214 // Loaders are chosen in the order they are listed here.
215 URLToLoaderMap url_to_loader_
;
216 SchemeToLoaderMap scheme_to_loader_
;
217 scoped_ptr
<ApplicationLoader
> default_loader_
;
218 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
220 IdentityToShellImplMap identity_to_shell_impl_
;
221 URLToContentHandlerMap url_to_content_handler_
;
222 URLToArgsMap url_to_args_
;
223 // Note: The keys are URLs after mapping and resolving.
224 URLToNativeOptionsMap url_to_native_options_
;
226 base::SequencedWorkerPool
* blocking_pool_
;
227 NetworkServicePtr network_service_
;
228 MimeTypeToURLMap mime_type_to_url_
;
229 ScopedVector
<NativeRunner
> native_runners_
;
231 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
233 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
239 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_