Update V8 to version 4.6.54.
[chromium-blink-merge.git] / mojo / shell / application_manager.h
blob76b5482be3dc9d1507f6d54f38bf13ce457b6264
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_
8 #include <map>
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/network_service.mojom.h"
19 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
20 #include "mojo/services/updater/updater.mojom.h"
21 #include "mojo/shell/application_loader.h"
22 #include "mojo/shell/capability_filter.h"
23 #include "mojo/shell/fetcher.h"
24 #include "mojo/shell/identity.h"
25 #include "mojo/shell/native_runner.h"
26 #include "url/gurl.h"
28 namespace base {
29 class FilePath;
30 class SequencedWorkerPool;
33 namespace mojo {
34 namespace shell {
36 class ApplicationInstance;
37 class ContentHandlerConnection;
39 class ApplicationManager {
40 public:
41 class Delegate {
42 public:
43 // Gives the delegate a chance to apply any mappings for the specified url.
44 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
45 virtual GURL ResolveMappings(const GURL& url) = 0;
47 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
48 // |url| if the scheme is not 'mojo'.
49 virtual GURL ResolveMojoURL(const GURL& url) = 0;
51 // Asks the delegate to create a Fetcher for the specified url. Return
52 // true on success, false if the default fetcher should be created.
53 virtual bool CreateFetcher(
54 const GURL& url,
55 const Fetcher::FetchCallback& loader_callback) = 0;
57 protected:
58 virtual ~Delegate() {}
61 // API for testing.
62 class TestAPI {
63 public:
64 explicit TestAPI(ApplicationManager* manager);
65 ~TestAPI();
67 // Returns true if the shared instance has been created.
68 static bool HasCreatedInstance();
69 // Returns true if there is a ApplicationInstance for this URL.
70 bool HasRunningInstanceForURL(const GURL& url) const;
72 private:
73 ApplicationManager* manager_;
75 DISALLOW_COPY_AND_ASSIGN(TestAPI);
78 explicit ApplicationManager(Delegate* delegate);
79 ~ApplicationManager();
81 // Loads a service if necessary and establishes a new client connection.
82 // |originator| can be NULL (e.g. for the first application or in tests), but
83 // typically is non-NULL and identifies the instance initiating the
84 // connection.
85 void ConnectToApplication(ApplicationInstance* originator,
86 URLRequestPtr requested_url,
87 const std::string& qualifier,
88 const GURL& requestor_url,
89 InterfaceRequest<ServiceProvider> services,
90 ServiceProviderPtr exposed_services,
91 const CapabilityFilter& capability_filter,
92 const base::Closure& on_application_end);
94 // Must only be used by shell internals and test code as it does not forward
95 // capability filters.
96 template <typename Interface>
97 inline void ConnectToService(const GURL& application_url,
98 InterfacePtr<Interface>* ptr) {
99 ScopedMessagePipeHandle service_handle =
100 ConnectToServiceByName(application_url, Interface::Name_);
101 ptr->Bind(InterfacePtrInfo<Interface>(service_handle.Pass(), 0u));
104 void RegisterContentHandler(const std::string& mime_type,
105 const GURL& content_handler_url);
107 // Registers a package alias. When attempting to load |alias|, it will
108 // instead redirect to |content_handler_package|, which is a content handler
109 // which will be passed the |alias| as the URLResponse::url. Different values
110 // of |alias| with the same |qualifier| that are in the same
111 // |content_handler_package| will run in the same process in multi-process
112 // mode.
113 void RegisterApplicationPackageAlias(const GURL& alias,
114 const GURL& content_handler_package,
115 const std::string& qualifier);
117 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
118 // or SetLoaderForScheme().
119 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
120 default_loader_ = loader.Pass();
122 void set_native_runner_factory(
123 scoped_ptr<NativeRunnerFactory> runner_factory) {
124 native_runner_factory_ = runner_factory.Pass();
126 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
127 blocking_pool_ = blocking_pool;
129 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
130 // Sets a Loader to be used for a specific url.
131 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
132 // Sets a Loader to be used for a specific url scheme.
133 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
134 const std::string& scheme);
135 // These options will be used in running any native application at |url|
136 // (which shouldn't contain a query string). (|url| will be mapped and
137 // resolved, and any application whose base resolved URL matches it will have
138 // |options| applied.)
139 // TODO(vtl): This may not do what's desired if the resolved URL results in an
140 // HTTP redirect. Really, we want options to be identified with a particular
141 // implementation, maybe via a signed manifest or something like that.
142 void SetNativeOptionsForURL(const NativeRunnerFactory::Options& options,
143 const GURL& url);
145 // Destroys all Shell-ends of connections established with Applications.
146 // Applications connected by this ApplicationManager will observe pipe errors
147 // and have a chance to shutdown.
148 void TerminateShellConnections();
150 // Removes a ApplicationInstance when it encounters an error.
151 void OnApplicationInstanceError(ApplicationInstance* instance);
153 // Removes a ContentHandler when its connection is closed.
154 void OnContentHandlerConnectionClosed(
155 ContentHandlerConnection* content_handler);
157 ApplicationInstance* GetApplicationInstance(const Identity& identity) const;
159 private:
160 using ApplicationPackagedAlias = std::map<GURL, std::pair<GURL, std::string>>;
161 using IdentityToApplicationInstanceMap =
162 std::map<Identity, ApplicationInstance*>;
163 using MimeTypeToURLMap = std::map<std::string, GURL>;
164 using SchemeToLoaderMap = std::map<std::string, ApplicationLoader*>;
165 using URLToContentHandlerMap =
166 std::map<std::pair<GURL, std::string>, ContentHandlerConnection*>;
167 using URLToLoaderMap = std::map<GURL, ApplicationLoader*>;
168 using URLToNativeOptionsMap = std::map<GURL, NativeRunnerFactory::Options>;
170 bool ConnectToRunningApplication(ApplicationInstance* originator,
171 const GURL& resolved_url,
172 const std::string& qualifier,
173 const GURL& requestor_url,
174 InterfaceRequest<ServiceProvider>* services,
175 ServiceProviderPtr* exposed_services,
176 const CapabilityFilter& filter);
178 bool ConnectToApplicationWithLoader(
179 ApplicationInstance* originator,
180 const GURL& requested_url,
181 const std::string& qualifier,
182 const GURL& resolved_url,
183 const GURL& requestor_url,
184 InterfaceRequest<ServiceProvider>* services,
185 ServiceProviderPtr* exposed_services,
186 const CapabilityFilter& filter,
187 const base::Closure& on_application_end,
188 ApplicationLoader* loader);
190 InterfaceRequest<Application> RegisterInstance(
191 ApplicationInstance* originator,
192 const GURL& app_url,
193 const std::string& qualifier,
194 const GURL& requestor_url,
195 InterfaceRequest<ServiceProvider> services,
196 ServiceProviderPtr exposed_services,
197 const CapabilityFilter& filter,
198 const base::Closure& on_application_end);
200 // Called once |fetcher| has found app. |requested_url| is the url of the
201 // requested application before any mappings/resolution have been applied.
202 void HandleFetchCallback(ApplicationInstance* originator,
203 const GURL& requested_url,
204 const std::string& qualifier,
205 const GURL& requestor_url,
206 InterfaceRequest<ServiceProvider> services,
207 ServiceProviderPtr exposed_services,
208 const CapabilityFilter& filter,
209 const base::Closure& on_application_end,
210 NativeApplicationCleanup cleanup,
211 scoped_ptr<Fetcher> fetcher);
213 void RunNativeApplication(InterfaceRequest<Application> application_request,
214 bool start_sandboxed,
215 const NativeRunnerFactory::Options& options,
216 NativeApplicationCleanup cleanup,
217 scoped_ptr<Fetcher> fetcher,
218 const base::FilePath& file_path,
219 bool path_exists);
221 void LoadWithContentHandler(ApplicationInstance* originator,
222 const GURL& content_handler_url,
223 const GURL& requestor_url,
224 const std::string& qualifier,
225 const CapabilityFilter& filter,
226 InterfaceRequest<Application> application_request,
227 URLResponsePtr url_response);
229 // Returns the appropriate loader for |url|, or null if there is no loader
230 // configured for the URL.
231 ApplicationLoader* GetLoaderForURL(const GURL& url);
233 void CleanupRunner(NativeRunner* runner);
235 ScopedMessagePipeHandle ConnectToServiceByName(
236 const GURL& application_url,
237 const std::string& interface_name);
239 Delegate* const delegate_;
240 // Loader management.
241 // Loaders are chosen in the order they are listed here.
242 URLToLoaderMap url_to_loader_;
243 SchemeToLoaderMap scheme_to_loader_;
244 scoped_ptr<ApplicationLoader> default_loader_;
245 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
247 ApplicationPackagedAlias application_package_alias_;
248 IdentityToApplicationInstanceMap identity_to_instance_;
249 URLToContentHandlerMap url_to_content_handler_;
250 // Note: The keys are URLs after mapping and resolving.
251 URLToNativeOptionsMap url_to_native_options_;
253 base::SequencedWorkerPool* blocking_pool_;
254 NetworkServicePtr network_service_;
255 URLLoaderFactoryPtr url_loader_factory_;
256 updater::UpdaterPtr updater_;
257 MimeTypeToURLMap mime_type_to_url_;
258 ScopedVector<NativeRunner> native_runners_;
259 bool disable_cache_;
260 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
262 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
265 } // namespace shell
266 } // namespace mojo
268 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_