Shutdown RenderWidgetHost owned by RenderFrameHost when destroying the RenderFrameHost.
[chromium-blink-merge.git] / mojo / shell / application_manager.h
blob8e517f83ae4e5fdfa4cedb0c3f541619109058f3
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/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/runner/native_application_support.h"
18 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
19 #include "mojo/shell/application_loader.h"
20 #include "mojo/shell/identity.h"
21 #include "mojo/shell/native_runner.h"
22 #include "url/gurl.h"
24 namespace base {
25 class FilePath;
26 class SequencedWorkerPool;
29 namespace mojo {
30 namespace shell {
32 class Fetcher;
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 protected:
48 virtual ~Delegate() {}
51 // API for testing.
52 class TestAPI {
53 public:
54 explicit TestAPI(ApplicationManager* manager);
55 ~TestAPI();
57 // Returns true if the shared instance has been created.
58 static bool HasCreatedInstance();
59 // Returns true if there is a ShellImpl for this URL.
60 bool HasFactoryForURL(const GURL& url) const;
62 private:
63 ApplicationManager* manager_;
65 DISALLOW_COPY_AND_ASSIGN(TestAPI);
68 explicit ApplicationManager(Delegate* delegate);
69 ~ApplicationManager();
71 // Loads a service if necessary and establishes a new client connection.
72 void ConnectToApplication(const GURL& application_url,
73 const GURL& requestor_url,
74 InterfaceRequest<ServiceProvider> services,
75 ServiceProviderPtr exposed_services,
76 const base::Closure& on_application_end);
78 template <typename Interface>
79 inline void ConnectToService(const GURL& application_url,
80 InterfacePtr<Interface>* ptr) {
81 ScopedMessagePipeHandle service_handle =
82 ConnectToServiceByName(application_url, Interface::Name_);
83 ptr->Bind(service_handle.Pass());
86 ScopedMessagePipeHandle ConnectToServiceByName(
87 const GURL& application_url,
88 const std::string& interface_name);
90 void RegisterContentHandler(const std::string& mime_type,
91 const GURL& content_handler_url);
93 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
94 // or SetLoaderForScheme().
95 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
96 default_loader_ = loader.Pass();
98 void set_native_runner_factory(
99 scoped_ptr<NativeRunnerFactory> runner_factory) {
100 native_runner_factory_ = runner_factory.Pass();
102 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
103 blocking_pool_ = blocking_pool;
105 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
106 // Sets a Loader to be used for a specific url.
107 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
108 // Sets a Loader to be used for a specific url scheme.
109 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
110 const std::string& scheme);
111 // These options will be used in running any native application at |url|
112 // (which shouldn't contain a query string). (|url| will be mapped and
113 // resolved, and any application whose base resolved URL matches it will have
114 // |options| applied.)
115 // TODO(vtl): This may not do what's desired if the resolved URL results in an
116 // HTTP redirect. Really, we want options to be identified with a particular
117 // implementation, maybe via a signed manifest or something like that.
118 void SetNativeOptionsForURL(const NativeRunnerFactory::Options& options,
119 const GURL& url);
121 // Destroys all Shell-ends of connections established with Applications.
122 // Applications connected by this ApplicationManager will observe pipe errors
123 // and have a chance to shutdown.
124 void TerminateShellConnections();
126 // Removes a ShellImpl when it encounters an error.
127 void OnShellImplError(ShellImpl* shell_impl);
129 private:
130 class ContentHandlerConnection;
132 typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap;
133 typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap;
134 typedef std::map<Identity, ShellImpl*> IdentityToShellImplMap;
135 typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap;
136 typedef std::map<std::string, GURL> MimeTypeToURLMap;
137 typedef std::map<GURL, NativeRunnerFactory::Options> URLToNativeOptionsMap;
139 void ConnectToApplicationWithParameters(
140 const GURL& application_url,
141 const GURL& requestor_url,
142 InterfaceRequest<ServiceProvider> services,
143 ServiceProviderPtr exposed_services,
144 const base::Closure& on_application_end,
145 const std::vector<std::string>& pre_redirect_parameters);
147 bool ConnectToRunningApplication(const GURL& resolved_url,
148 const GURL& requestor_url,
149 InterfaceRequest<ServiceProvider>* services,
150 ServiceProviderPtr* exposed_services);
152 bool ConnectToApplicationWithLoader(
153 const GURL& resolved_url,
154 const GURL& requestor_url,
155 InterfaceRequest<ServiceProvider>* services,
156 ServiceProviderPtr* exposed_services,
157 const base::Closure& on_application_end,
158 const std::vector<std::string>& parameters,
159 ApplicationLoader* loader);
161 InterfaceRequest<Application> RegisterShell(
162 // The URL after resolution and redirects, including the querystring.
163 const GURL& resolved_url,
164 const GURL& requestor_url,
165 InterfaceRequest<ServiceProvider> services,
166 ServiceProviderPtr exposed_services,
167 const base::Closure& on_application_end,
168 const std::vector<std::string>& parameters);
170 ShellImpl* GetShellImpl(const GURL& url);
172 void ConnectToClient(ShellImpl* shell_impl,
173 const GURL& resolved_url,
174 const GURL& requestor_url,
175 InterfaceRequest<ServiceProvider> services,
176 ServiceProviderPtr exposed_services);
178 void HandleFetchCallback(const GURL& requestor_url,
179 InterfaceRequest<ServiceProvider> services,
180 ServiceProviderPtr exposed_services,
181 const base::Closure& on_application_end,
182 const std::vector<std::string>& parameters,
183 NativeApplicationCleanup cleanup,
184 scoped_ptr<Fetcher> fetcher);
186 void RunNativeApplication(InterfaceRequest<Application> application_request,
187 const NativeRunnerFactory::Options& options,
188 NativeApplicationCleanup cleanup,
189 scoped_ptr<Fetcher> fetcher,
190 const base::FilePath& file_path,
191 bool path_exists);
193 void LoadWithContentHandler(const GURL& content_handler_url,
194 InterfaceRequest<Application> application_request,
195 URLResponsePtr url_response);
197 // Returns the appropriate loader for |url|, or null if there is no loader
198 // configured for the URL.
199 ApplicationLoader* GetLoaderForURL(const GURL& url);
201 // Removes a ContentHandler when it encounters an error.
202 void OnContentHandlerError(ContentHandlerConnection* content_handler);
204 void CleanupRunner(NativeRunner* runner);
206 Delegate* const delegate_;
207 // Loader management.
208 // Loaders are chosen in the order they are listed here.
209 URLToLoaderMap url_to_loader_;
210 SchemeToLoaderMap scheme_to_loader_;
211 scoped_ptr<ApplicationLoader> default_loader_;
212 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
214 IdentityToShellImplMap identity_to_shell_impl_;
215 URLToContentHandlerMap url_to_content_handler_;
216 // Note: The keys are URLs after mapping and resolving.
217 URLToNativeOptionsMap url_to_native_options_;
219 base::SequencedWorkerPool* blocking_pool_;
220 NetworkServicePtr network_service_;
221 MimeTypeToURLMap mime_type_to_url_;
222 ScopedVector<NativeRunner> native_runners_;
223 bool disable_cache_;
224 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
226 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
229 } // namespace shell
230 } // namespace mojo
232 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_