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_SHELL_APPLICATION_MANAGER_H_
6 #define MOJO_SHELL_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/connect_to_application_params.h"
25 #include "mojo/shell/fetcher.h"
26 #include "mojo/shell/identity.h"
27 #include "mojo/shell/native_runner.h"
32 class SequencedWorkerPool
;
38 class ApplicationInstance
;
39 class ContentHandlerConnection
;
41 class ApplicationManager
{
45 // Gives the delegate a chance to apply any mappings for the specified url.
46 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
47 virtual GURL
ResolveMappings(const GURL
& url
) = 0;
49 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
50 // |url| if the scheme is not 'mojo'.
51 virtual GURL
ResolveMojoURL(const GURL
& url
) = 0;
53 // Asks the delegate to create a Fetcher for the specified url. Return
54 // true on success, false if the default fetcher should be created.
55 virtual bool CreateFetcher(
57 const Fetcher::FetchCallback
& loader_callback
) = 0;
60 virtual ~Delegate() {}
66 explicit TestAPI(ApplicationManager
* manager
);
69 // Returns true if the shared instance has been created.
70 static bool HasCreatedInstance();
71 // Returns true if there is a ApplicationInstance for this URL.
72 bool HasRunningInstanceForURL(const GURL
& url
) const;
75 ApplicationManager
* manager_
;
77 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
80 explicit ApplicationManager(Delegate
* delegate
);
81 ~ApplicationManager();
83 // Loads a service if necessary and establishes a new client connection.
84 // |originator| can be NULL (e.g. for the first application or in tests), but
85 // typically is non-NULL and identifies the instance initiating the
87 // Please see the comments in connect_to_application_params.h for more details
88 // about the parameters.
89 void ConnectToApplication(
90 ApplicationInstance
* originator
,
91 URLRequestPtr app_url_request
,
92 const std::string
& qualifier
,
93 InterfaceRequest
<ServiceProvider
> services
,
94 ServiceProviderPtr exposed_services
,
95 const CapabilityFilter
& filter
,
96 const base::Closure
& on_application_end
,
97 const Shell::ConnectToApplicationCallback
& connect_callback
);
99 void ConnectToApplication(scoped_ptr
<ConnectToApplicationParams
> params
);
101 // Must only be used by shell internals and test code as it does not forward
102 // capability filters.
103 template <typename Interface
>
104 inline void ConnectToService(const GURL
& application_url
,
105 InterfacePtr
<Interface
>* ptr
) {
106 ScopedMessagePipeHandle service_handle
=
107 ConnectToServiceByName(application_url
, Interface::Name_
);
108 ptr
->Bind(InterfacePtrInfo
<Interface
>(service_handle
.Pass(), 0u));
111 void RegisterContentHandler(const std::string
& mime_type
,
112 const GURL
& content_handler_url
);
114 // Registers a package alias. When attempting to load |alias|, it will
115 // instead redirect to |content_handler_package|, which is a content handler
116 // which will be passed the |alias| as the URLResponse::url. Different values
117 // of |alias| with the same |qualifier| that are in the same
118 // |content_handler_package| will run in the same process in multi-process
120 void RegisterApplicationPackageAlias(const GURL
& alias
,
121 const GURL
& content_handler_package
,
122 const std::string
& qualifier
);
124 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
125 // or SetLoaderForScheme().
126 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
127 default_loader_
= loader
.Pass();
129 void set_native_runner_factory(
130 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
131 native_runner_factory_
= runner_factory
.Pass();
133 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
134 blocking_pool_
= blocking_pool
;
136 void set_disable_cache(bool disable_cache
) { disable_cache_
= disable_cache
; }
137 // Sets a Loader to be used for a specific url.
138 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
139 // Sets a Loader to be used for a specific url scheme.
140 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
141 const std::string
& scheme
);
142 // These options will be used in running any native application at |url|
143 // (which shouldn't contain a query string). (|url| will be mapped and
144 // resolved, and any application whose base resolved URL matches it will have
145 // |options| applied.)
146 // TODO(vtl): This may not do what's desired if the resolved URL results in an
147 // HTTP redirect. Really, we want options to be identified with a particular
148 // implementation, maybe via a signed manifest or something like that.
149 void SetNativeOptionsForURL(const NativeRunnerFactory::Options
& options
,
152 // Destroys all Shell-ends of connections established with Applications.
153 // Applications connected by this ApplicationManager will observe pipe errors
154 // and have a chance to shutdown.
155 void TerminateShellConnections();
157 // Removes a ApplicationInstance when it encounters an error.
158 void OnApplicationInstanceError(ApplicationInstance
* instance
);
160 // Removes a ContentHandler when its connection is closed.
161 void OnContentHandlerConnectionClosed(
162 ContentHandlerConnection
* content_handler
);
164 ApplicationInstance
* GetApplicationInstance(const Identity
& identity
) const;
167 using ApplicationPackagedAlias
= std::map
<GURL
, std::pair
<GURL
, std::string
>>;
168 using IdentityToApplicationInstanceMap
=
169 std::map
<Identity
, ApplicationInstance
*>;
170 using MimeTypeToURLMap
= std::map
<std::string
, GURL
>;
171 using SchemeToLoaderMap
= std::map
<std::string
, ApplicationLoader
*>;
172 using URLToContentHandlerMap
=
173 std::map
<std::pair
<GURL
, std::string
>, ContentHandlerConnection
*>;
174 using URLToLoaderMap
= std::map
<GURL
, ApplicationLoader
*>;
175 using URLToNativeOptionsMap
= std::map
<GURL
, NativeRunnerFactory::Options
>;
177 // Takes the contents of |params| only when it returns true.
178 bool ConnectToRunningApplication(
179 scoped_ptr
<ConnectToApplicationParams
>* params
);
180 // |resolved_url| is the URL to load by |loader| (if loader is not null). It
181 // may be different from |(*params)->app_url()| because of mappings and
183 // Takes the contents of |params| only when it returns true.
184 bool ConnectToApplicationWithLoader(
185 scoped_ptr
<ConnectToApplicationParams
>* params
,
186 const GURL
& resolved_url
,
187 ApplicationLoader
* loader
);
189 InterfaceRequest
<Application
> RegisterInstance(
190 scoped_ptr
<ConnectToApplicationParams
> params
,
191 ApplicationInstance
** resulting_instance
);
193 // Called once |fetcher| has found app. |params->app_url()| is the url of
194 // the requested application before any mappings/resolution have been applied.
195 // The corresponding URLRequest struct in |params| has been taken.
196 void HandleFetchCallback(scoped_ptr
<ConnectToApplicationParams
> params
,
197 NativeApplicationCleanup cleanup
,
198 scoped_ptr
<Fetcher
> fetcher
);
200 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
201 bool start_sandboxed
,
202 const NativeRunnerFactory::Options
& options
,
203 NativeApplicationCleanup cleanup
,
204 scoped_ptr
<Fetcher
> fetcher
,
205 const base::FilePath
& file_path
,
208 void LoadWithContentHandler(
209 const Identity
& originator_identity
,
210 const CapabilityFilter
& originator_filter
,
211 const GURL
& content_handler_url
,
212 const std::string
& qualifier
,
213 const CapabilityFilter
& filter
,
214 const Shell::ConnectToApplicationCallback
& connect_callback
,
215 ApplicationInstance
* app
,
216 InterfaceRequest
<Application
> application_request
,
217 URLResponsePtr url_response
);
219 // Returns the appropriate loader for |url|, or null if there is no loader
220 // configured for the URL.
221 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
223 void CleanupRunner(NativeRunner
* runner
);
225 ScopedMessagePipeHandle
ConnectToServiceByName(
226 const GURL
& application_url
,
227 const std::string
& interface_name
);
229 Delegate
* const delegate_
;
230 // Loader management.
231 // Loaders are chosen in the order they are listed here.
232 URLToLoaderMap url_to_loader_
;
233 SchemeToLoaderMap scheme_to_loader_
;
234 scoped_ptr
<ApplicationLoader
> default_loader_
;
235 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
237 ApplicationPackagedAlias application_package_alias_
;
238 IdentityToApplicationInstanceMap identity_to_instance_
;
239 URLToContentHandlerMap url_to_content_handler_
;
240 // Note: The keys are URLs after mapping and resolving.
241 URLToNativeOptionsMap url_to_native_options_
;
243 base::SequencedWorkerPool
* blocking_pool_
;
244 NetworkServicePtr network_service_
;
245 URLLoaderFactoryPtr url_loader_factory_
;
246 updater::UpdaterPtr updater_
;
247 MimeTypeToURLMap mime_type_to_url_
;
248 ScopedVector
<NativeRunner
> native_runners_
;
250 // Counter used to assign ids to content_handlers.
251 uint32_t content_handler_id_counter_
;
252 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
254 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
257 Shell::ConnectToApplicationCallback
EmptyConnectCallback();
262 #endif // MOJO_SHELL_APPLICATION_MANAGER_H_