Remove ExtensionPrefs::SetDidExtensionEscalatePermissions.
[chromium-blink-merge.git] / mojo / shell / application_manager.h
blob9581ac5eaeea5df535700f112cda8a661e094b07
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/shell/application_loader.h"
20 #include "mojo/shell/fetcher.h"
21 #include "mojo/shell/identity.h"
22 #include "mojo/shell/native_runner.h"
23 #include "url/gurl.h"
25 namespace base {
26 class FilePath;
27 class SequencedWorkerPool;
30 namespace mojo {
31 namespace shell {
33 class ShellImpl;
35 class ApplicationManager {
36 public:
37 class Delegate {
38 public:
39 // Gives the delegate a chance to apply any mappings for the specified url.
40 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
41 virtual GURL ResolveMappings(const GURL& url) = 0;
43 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
44 // |url| if the scheme is not 'mojo'.
45 virtual GURL ResolveMojoURL(const GURL& url) = 0;
47 // Asks the delegate to create a Fetcher for the specified url. Return
48 // true on success, false if the default fetcher should be created.
49 virtual bool CreateFetcher(
50 const GURL& url,
51 const Fetcher::FetchCallback& loader_callback) = 0;
53 protected:
54 virtual ~Delegate() {}
57 // API for testing.
58 class TestAPI {
59 public:
60 explicit TestAPI(ApplicationManager* manager);
61 ~TestAPI();
63 // Returns true if the shared instance has been created.
64 static bool HasCreatedInstance();
65 // Returns true if there is a ShellImpl for this URL.
66 bool HasFactoryForURL(const GURL& url) const;
68 private:
69 ApplicationManager* manager_;
71 DISALLOW_COPY_AND_ASSIGN(TestAPI);
74 explicit ApplicationManager(Delegate* delegate);
75 ~ApplicationManager();
77 // Loads a service if necessary and establishes a new client connection.
78 void ConnectToApplication(mojo::URLRequestPtr application_url,
79 const GURL& requestor_url,
80 InterfaceRequest<ServiceProvider> services,
81 ServiceProviderPtr exposed_services,
82 const base::Closure& on_application_end);
84 template <typename Interface>
85 inline void ConnectToService(const GURL& application_url,
86 InterfacePtr<Interface>* ptr) {
87 ScopedMessagePipeHandle service_handle =
88 ConnectToServiceByName(application_url, Interface::Name_);
89 ptr->Bind(InterfacePtrInfo<Interface>(service_handle.Pass(), 0u));
92 ScopedMessagePipeHandle ConnectToServiceByName(
93 const GURL& application_url,
94 const std::string& interface_name);
96 void RegisterContentHandler(const std::string& mime_type,
97 const GURL& content_handler_url);
99 // Registers a package alias. When attempting to load |alias|, it will
100 // instead redirect to |content_handler_package|, which is a content handler
101 // which will be passed the |alias| as the URLResponse::url. Different values
102 // of |alias| with the same |qualifier| that are in the same
103 // |content_handler_package| will run in the same process in multi-process
104 // mode.
105 void RegisterApplicationPackageAlias(const GURL& alias,
106 const GURL& content_handler_package,
107 const std::string& qualifier);
109 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
110 // or SetLoaderForScheme().
111 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
112 default_loader_ = loader.Pass();
114 void set_native_runner_factory(
115 scoped_ptr<NativeRunnerFactory> runner_factory) {
116 native_runner_factory_ = runner_factory.Pass();
118 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
119 blocking_pool_ = blocking_pool;
121 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
122 // Sets a Loader to be used for a specific url.
123 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
124 // Sets a Loader to be used for a specific url scheme.
125 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
126 const std::string& scheme);
127 // These options will be used in running any native application at |url|
128 // (which shouldn't contain a query string). (|url| will be mapped and
129 // resolved, and any application whose base resolved URL matches it will have
130 // |options| applied.)
131 // TODO(vtl): This may not do what's desired if the resolved URL results in an
132 // HTTP redirect. Really, we want options to be identified with a particular
133 // implementation, maybe via a signed manifest or something like that.
134 void SetNativeOptionsForURL(const NativeRunnerFactory::Options& options,
135 const GURL& url);
137 // Destroys all Shell-ends of connections established with Applications.
138 // Applications connected by this ApplicationManager will observe pipe errors
139 // and have a chance to shutdown.
140 void TerminateShellConnections();
142 // Removes a ShellImpl when it encounters an error.
143 void OnShellImplError(ShellImpl* shell_impl);
145 private:
146 class ContentHandlerConnection;
148 using ApplicationPackagedAlias = std::map<GURL, std::pair<GURL, std::string>>;
149 using IdentityToShellImplMap = std::map<Identity, ShellImpl*>;
150 using MimeTypeToURLMap = std::map<std::string, GURL>;
151 using SchemeToLoaderMap = std::map<std::string, ApplicationLoader*>;
152 using URLToContentHandlerMap =
153 std::map<std::pair<GURL, std::string>, ContentHandlerConnection*>;
154 using URLToLoaderMap = std::map<GURL, ApplicationLoader*>;
155 using URLToNativeOptionsMap = std::map<GURL, NativeRunnerFactory::Options>;
157 void ConnectToApplicationInternal(
158 mojo::URLRequestPtr requested_url,
159 const std::string& qualifier,
160 const GURL& requestor_url,
161 InterfaceRequest<ServiceProvider> services,
162 ServiceProviderPtr exposed_services,
163 const base::Closure& on_application_end);
165 bool ConnectToRunningApplication(const GURL& resolved_url,
166 const std::string& qualifier,
167 const GURL& requestor_url,
168 InterfaceRequest<ServiceProvider>* services,
169 ServiceProviderPtr* exposed_services);
171 bool ConnectToApplicationWithLoader(
172 const GURL& requested_url,
173 const std::string& qualifier,
174 const GURL& resolved_url,
175 const GURL& requestor_url,
176 InterfaceRequest<ServiceProvider>* services,
177 ServiceProviderPtr* exposed_services,
178 const base::Closure& on_application_end,
179 ApplicationLoader* loader);
181 InterfaceRequest<Application> RegisterShell(
182 const GURL& app_url,
183 const std::string& qualifier,
184 const GURL& requestor_url,
185 InterfaceRequest<ServiceProvider> services,
186 ServiceProviderPtr exposed_services,
187 const base::Closure& on_application_end);
189 ShellImpl* GetShellImpl(const GURL& url, const std::string& qualifier);
191 void ConnectToClient(ShellImpl* shell_impl,
192 const GURL& resolved_url,
193 const GURL& requestor_url,
194 InterfaceRequest<ServiceProvider> services,
195 ServiceProviderPtr exposed_services);
197 // Called once |fetcher| has found app. |requested_url| is the url of the
198 // requested application before any mappings/resolution have been applied.
199 void HandleFetchCallback(const GURL& requested_url,
200 const std::string& qualifier,
201 const GURL& requestor_url,
202 InterfaceRequest<ServiceProvider> services,
203 ServiceProviderPtr exposed_services,
204 const base::Closure& on_application_end,
205 NativeApplicationCleanup cleanup,
206 scoped_ptr<Fetcher> fetcher);
208 void RunNativeApplication(InterfaceRequest<Application> application_request,
209 const NativeRunnerFactory::Options& options,
210 NativeApplicationCleanup cleanup,
211 scoped_ptr<Fetcher> fetcher,
212 const base::FilePath& file_path,
213 bool path_exists);
215 void LoadWithContentHandler(const GURL& content_handler_url,
216 const GURL& requestor_url,
217 const std::string& qualifier,
218 InterfaceRequest<Application> application_request,
219 URLResponsePtr url_response);
221 // Returns the appropriate loader for |url|, or null if there is no loader
222 // configured for the URL.
223 ApplicationLoader* GetLoaderForURL(const GURL& url);
225 // Removes a ContentHandler when it encounters an error.
226 void OnContentHandlerError(ContentHandlerConnection* content_handler);
228 void CleanupRunner(NativeRunner* runner);
230 Delegate* const delegate_;
231 // Loader management.
232 // Loaders are chosen in the order they are listed here.
233 URLToLoaderMap url_to_loader_;
234 SchemeToLoaderMap scheme_to_loader_;
235 scoped_ptr<ApplicationLoader> default_loader_;
236 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
238 ApplicationPackagedAlias application_package_alias_;
239 IdentityToShellImplMap identity_to_shell_impl_;
240 URLToContentHandlerMap url_to_content_handler_;
241 // Note: The keys are URLs after mapping and resolving.
242 URLToNativeOptionsMap url_to_native_options_;
244 base::SequencedWorkerPool* blocking_pool_;
245 URLLoaderFactoryPtr url_loader_factory_;
246 MimeTypeToURLMap mime_type_to_url_;
247 ScopedVector<NativeRunner> native_runners_;
248 bool disable_cache_;
249 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
251 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
254 } // namespace shell
255 } // namespace mojo
257 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_