Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / shell / application_manager.h
blobfa7cbd85a9b93d53fda7f55a9f63382239f73f47
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/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"
24 #include "url/gurl.h"
26 namespace base {
27 class FilePath;
28 class SequencedWorkerPool;
31 namespace mojo {
32 namespace shell {
34 class ApplicationInstance;
35 class ContentHandlerConnection;
37 class ApplicationManager {
38 public:
39 class Delegate {
40 public:
41 // Gives the delegate a chance to apply any mappings for the specified url.
42 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
43 virtual GURL ResolveMappings(const GURL& url) = 0;
45 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
46 // |url| if the scheme is not 'mojo'.
47 virtual GURL ResolveMojoURL(const GURL& url) = 0;
49 // Asks the delegate to create a Fetcher for the specified url. Return
50 // true on success, false if the default fetcher should be created.
51 virtual bool CreateFetcher(
52 const GURL& url,
53 const Fetcher::FetchCallback& loader_callback) = 0;
55 protected:
56 virtual ~Delegate() {}
59 // API for testing.
60 class TestAPI {
61 public:
62 explicit TestAPI(ApplicationManager* manager);
63 ~TestAPI();
65 // Returns true if the shared instance has been created.
66 static bool HasCreatedInstance();
67 // Returns true if there is a ApplicationInstance for this URL.
68 bool HasRunningInstanceForURL(const GURL& url) const;
70 private:
71 ApplicationManager* manager_;
73 DISALLOW_COPY_AND_ASSIGN(TestAPI);
76 explicit ApplicationManager(Delegate* delegate);
77 ~ApplicationManager();
79 // Loads a service if necessary and establishes a new client connection.
80 // |originator| can be NULL (e.g. for the first application or in tests), but
81 // typically is non-NULL and identifies the instance initiating the
82 // connection.
83 void ConnectToApplication(
84 ApplicationInstance* originator,
85 URLRequestPtr requested_url,
86 const std::string& qualifier,
87 const GURL& requestor_url,
88 InterfaceRequest<ServiceProvider> services,
89 ServiceProviderPtr exposed_services,
90 CapabilityFilterPtr filter,
91 const base::Closure& on_application_end);
93 // Must only be used by shell internals and test code as it does not forward
94 // capability filters.
95 template <typename Interface>
96 inline void ConnectToService(const GURL& application_url,
97 InterfacePtr<Interface>* ptr) {
98 ScopedMessagePipeHandle service_handle =
99 ConnectToServiceByName(application_url, Interface::Name_);
100 ptr->Bind(InterfacePtrInfo<Interface>(service_handle.Pass(), 0u));
103 void RegisterContentHandler(const std::string& mime_type,
104 const GURL& content_handler_url);
106 // Registers a package alias. When attempting to load |alias|, it will
107 // instead redirect to |content_handler_package|, which is a content handler
108 // which will be passed the |alias| as the URLResponse::url. Different values
109 // of |alias| with the same |qualifier| that are in the same
110 // |content_handler_package| will run in the same process in multi-process
111 // mode.
112 void RegisterApplicationPackageAlias(const GURL& alias,
113 const GURL& content_handler_package,
114 const std::string& qualifier);
116 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
117 // or SetLoaderForScheme().
118 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
119 default_loader_ = loader.Pass();
121 void set_native_runner_factory(
122 scoped_ptr<NativeRunnerFactory> runner_factory) {
123 native_runner_factory_ = runner_factory.Pass();
125 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
126 blocking_pool_ = blocking_pool;
128 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
129 // Sets a Loader to be used for a specific url.
130 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
131 // Sets a Loader to be used for a specific url scheme.
132 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
133 const std::string& scheme);
134 // These options will be used in running any native application at |url|
135 // (which shouldn't contain a query string). (|url| will be mapped and
136 // resolved, and any application whose base resolved URL matches it will have
137 // |options| applied.)
138 // TODO(vtl): This may not do what's desired if the resolved URL results in an
139 // HTTP redirect. Really, we want options to be identified with a particular
140 // implementation, maybe via a signed manifest or something like that.
141 void SetNativeOptionsForURL(const NativeRunnerFactory::Options& options,
142 const GURL& url);
144 // Destroys all Shell-ends of connections established with Applications.
145 // Applications connected by this ApplicationManager will observe pipe errors
146 // and have a chance to shutdown.
147 void TerminateShellConnections();
149 // Removes a ApplicationInstance when it encounters an error.
150 void OnApplicationInstanceError(ApplicationInstance* instance);
152 // Removes a ContentHandler when its connection is closed.
153 void OnContentHandlerConnectionClosed(
154 ContentHandlerConnection* content_handler);
156 private:
157 using ApplicationPackagedAlias = std::map<GURL, std::pair<GURL, std::string>>;
158 using IdentityToApplicationInstanceMap =
159 std::map<Identity, ApplicationInstance*>;
160 using MimeTypeToURLMap = std::map<std::string, GURL>;
161 using SchemeToLoaderMap = std::map<std::string, ApplicationLoader*>;
162 using URLToContentHandlerMap =
163 std::map<std::pair<GURL, std::string>, ContentHandlerConnection*>;
164 using URLToLoaderMap = std::map<GURL, ApplicationLoader*>;
165 using URLToNativeOptionsMap = std::map<GURL, NativeRunnerFactory::Options>;
167 bool ConnectToRunningApplication(ApplicationInstance* originator,
168 const GURL& resolved_url,
169 const std::string& qualifier,
170 const GURL& requestor_url,
171 InterfaceRequest<ServiceProvider>* services,
172 ServiceProviderPtr* exposed_services,
173 CapabilityFilterPtr* filter);
175 bool ConnectToApplicationWithLoader(
176 ApplicationInstance* originator,
177 const GURL& requested_url,
178 const std::string& qualifier,
179 const GURL& resolved_url,
180 const GURL& requestor_url,
181 InterfaceRequest<ServiceProvider>* services,
182 ServiceProviderPtr* exposed_services,
183 CapabilityFilterPtr* filter,
184 const base::Closure& on_application_end,
185 ApplicationLoader* loader);
187 InterfaceRequest<Application> RegisterInstance(
188 ApplicationInstance* originator,
189 const GURL& app_url,
190 const std::string& qualifier,
191 const GURL& requestor_url,
192 InterfaceRequest<ServiceProvider> services,
193 ServiceProviderPtr exposed_services,
194 CapabilityFilterPtr filter,
195 const base::Closure& on_application_end);
197 ApplicationInstance* GetApplicationInstance(const GURL& url,
198 const std::string& qualifier);
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 CapabilityFilterPtr filter,
209 const base::Closure& on_application_end,
210 NativeApplicationCleanup cleanup,
211 scoped_ptr<Fetcher> fetcher);
213 void RunNativeApplication(InterfaceRequest<Application> application_request,
214 const NativeRunnerFactory::Options& options,
215 NativeApplicationCleanup cleanup,
216 scoped_ptr<Fetcher> fetcher,
217 const base::FilePath& file_path,
218 bool path_exists);
220 void LoadWithContentHandler(const GURL& content_handler_url,
221 const GURL& requestor_url,
222 const std::string& qualifier,
223 InterfaceRequest<Application> application_request,
224 URLResponsePtr url_response);
226 // Returns the appropriate loader for |url|, or null if there is no loader
227 // configured for the URL.
228 ApplicationLoader* GetLoaderForURL(const GURL& url);
230 void CleanupRunner(NativeRunner* runner);
232 ScopedMessagePipeHandle ConnectToServiceByName(
233 const GURL& application_url,
234 const std::string& interface_name);
236 Delegate* const delegate_;
237 // Loader management.
238 // Loaders are chosen in the order they are listed here.
239 URLToLoaderMap url_to_loader_;
240 SchemeToLoaderMap scheme_to_loader_;
241 scoped_ptr<ApplicationLoader> default_loader_;
242 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
244 ApplicationPackagedAlias application_package_alias_;
245 IdentityToApplicationInstanceMap identity_to_instance_;
246 URLToContentHandlerMap url_to_content_handler_;
247 // Note: The keys are URLs after mapping and resolving.
248 URLToNativeOptionsMap url_to_native_options_;
250 base::SequencedWorkerPool* blocking_pool_;
251 URLLoaderFactoryPtr url_loader_factory_;
252 updater::UpdaterPtr updater_;
253 MimeTypeToURLMap mime_type_to_url_;
254 ScopedVector<NativeRunner> native_runners_;
255 bool disable_cache_;
256 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
258 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
261 } // namespace shell
262 } // namespace mojo
264 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_