blob: 9e3da2d97c769a945fdaa7683ddec717a2434401 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -08005#ifndef SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
6#define SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
James Robinson646469d2014-10-03 15:33:28 -07007
8#include <map>
9
10#include "base/basictypes.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080011#include "base/callback.h"
James Robinson646469d2014-10-03 15:33:28 -070012#include "base/gtest_prod_util.h"
13#include "base/memory/scoped_ptr.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080014#include "base/memory/scoped_vector.h"
James Robinson646469d2014-10-03 15:33:28 -070015#include "base/memory/weak_ptr.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080016#include "mojo/public/cpp/bindings/interface_request.h"
James Robinson646469d2014-10-03 15:33:28 -070017#include "mojo/public/interfaces/application/service_provider.mojom.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080018#include "mojo/services/network/public/interfaces/network_service.mojom.h"
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -080019#include "shell/application_manager/application_loader.h"
20#include "shell/application_manager/application_manager_export.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080021#include "shell/application_manager/fetcher.h"
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -080022#include "shell/application_manager/shell_impl.h"
James Robinson646469d2014-10-03 15:33:28 -070023#include "url/gurl.h"
24
Aaron Boodman5e7f9042015-02-24 17:37:34 -080025namespace base {
26class FilePath;
27class SequencedWorkerPool;
28}
29
James Robinson646469d2014-10-03 15:33:28 -070030namespace mojo {
31
Aaron Boodman5e7f9042015-02-24 17:37:34 -080032// ApplicationManager requires implementations of NativeRunner and
33// NativeRunnerFactory to run native applications.
34class MOJO_APPLICATION_MANAGER_EXPORT NativeRunner {
35 public:
36 // Parameter for |Start| to specify its cleanup behavior.
37 enum CleanupBehavior { DeleteAppPath, DontDeleteAppPath };
38 virtual ~NativeRunner() {}
39
40 // Loads the app in the file at |app_path| and runs it on some other
41 // thread/process. If |cleanup_behavior| is |true|, takes ownership of the
42 // file. |app_completed_callback| is posted (to the thread on which |Start()|
43 // was called) after |MojoMain()| completes.
44 virtual void Start(const base::FilePath& app_path,
45 CleanupBehavior cleanup_behavior,
46 InterfaceRequest<Application> application_request,
47 const base::Closure& app_completed_callback) = 0;
48};
49
50class MOJO_APPLICATION_MANAGER_EXPORT NativeRunnerFactory {
51 public:
52 virtual ~NativeRunnerFactory() {}
53 virtual scoped_ptr<NativeRunner> Create() = 0;
54};
55
James Robinson646469d2014-10-03 15:33:28 -070056class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
57 public:
58 class MOJO_APPLICATION_MANAGER_EXPORT Delegate {
59 public:
60 virtual ~Delegate();
61 // Send when the Application holding the handle on the other end of the
62 // Shell pipe goes away.
Aaron Boodmancd7b9112014-11-06 21:44:49 -080063 virtual void OnApplicationError(const GURL& url);
64 virtual GURL ResolveURL(const GURL& url);
Benjamin Lerman75aef1d2015-01-21 12:04:39 +010065 virtual GURL ResolveMappings(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -070066 };
67
68 // API for testing.
69 class MOJO_APPLICATION_MANAGER_EXPORT TestAPI {
70 public:
71 explicit TestAPI(ApplicationManager* manager);
72 ~TestAPI();
73
74 // Returns true if the shared instance has been created.
75 static bool HasCreatedInstance();
76 // Returns true if there is a ShellImpl for this URL.
77 bool HasFactoryForURL(const GURL& url) const;
78
79 private:
80 ApplicationManager* manager_;
81
82 DISALLOW_COPY_AND_ASSIGN(TestAPI);
83 };
84
James Robinson3217bf32015-01-14 14:33:07 -080085 explicit ApplicationManager(Delegate* delegate);
James Robinson646469d2014-10-03 15:33:28 -070086 ~ApplicationManager();
87
James Robinson646469d2014-10-03 15:33:28 -070088 // Loads a service if necessary and establishes a new client connection.
89 void ConnectToApplication(const GURL& application_url,
90 const GURL& requestor_url,
James Robinson3217bf32015-01-14 14:33:07 -080091 InterfaceRequest<ServiceProvider> services,
92 ServiceProviderPtr exposed_services);
James Robinson646469d2014-10-03 15:33:28 -070093
94 template <typename Interface>
95 inline void ConnectToService(const GURL& application_url,
96 InterfacePtr<Interface>* ptr) {
97 ScopedMessagePipeHandle service_handle =
98 ConnectToServiceByName(application_url, Interface::Name_);
99 ptr->Bind(service_handle.Pass());
100 }
101
102 ScopedMessagePipeHandle ConnectToServiceByName(
103 const GURL& application_url,
104 const std::string& interface_name);
105
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800106 void RegisterContentHandler(const std::string& mime_type,
107 const GURL& content_handler_url);
108
James Robinson646469d2014-10-03 15:33:28 -0700109 void RegisterExternalApplication(const GURL& application_url,
Dave Mooref2efe552015-01-26 14:31:45 -0800110 const std::vector<std::string>& args,
James Robinsone5ae9e42015-01-26 17:53:08 -0800111 ApplicationPtr application);
James Robinson646469d2014-10-03 15:33:28 -0700112
James Robinson646469d2014-10-03 15:33:28 -0700113 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
114 // or SetLoaderForScheme().
115 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
116 default_loader_ = loader.Pass();
117 }
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800118 void set_native_runner_factory(
119 scoped_ptr<NativeRunnerFactory> runner_factory) {
120 native_runner_factory_ = runner_factory.Pass();
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800121 }
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800122 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
123 blocking_pool_ = blocking_pool;
124 }
125 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
James Robinson646469d2014-10-03 15:33:28 -0700126 // Sets a Loader to be used for a specific url.
127 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
128 // Sets a Loader to be used for a specific url scheme.
129 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
130 const std::string& scheme);
131 // These strings will be passed to the Initialize() method when an
132 // Application is instantiated.
133 void SetArgsForURL(const std::vector<std::string>& args, const GURL& url);
134
James Robinson646469d2014-10-03 15:33:28 -0700135 // Destroys all Shell-ends of connections established with Applications.
136 // Applications connected by this ApplicationManager will observe pipe errors
137 // and have a chance to shutdown.
138 void TerminateShellConnections();
139
James Robinsonf5713182015-01-13 17:41:34 -0800140 // Removes a ShellImpl when it encounters an error.
141 void OnShellImplError(ShellImpl* shell_impl);
142
James Robinson646469d2014-10-03 15:33:28 -0700143 private:
Benjamin Lerman7c057142014-10-31 18:23:10 +0100144 class ContentHandlerConnection;
James Robinson646469d2014-10-03 15:33:28 -0700145
146 typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap;
147 typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap;
148 typedef std::map<GURL, ShellImpl*> URLToShellImplMap;
149 typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap;
Viet-Trung Luu32952272015-02-11 16:32:03 -0800150 typedef std::map<GURL, std::vector<std::string>> URLToArgsMap;
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800151 typedef std::map<std::string, GURL> MimeTypeToURLMap;
James Robinson646469d2014-10-03 15:33:28 -0700152
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800153 bool ConnectToRunningApplication(const GURL& application_url,
154 const GURL& requestor_url,
155 InterfaceRequest<ServiceProvider>* services,
156 ServiceProviderPtr* exposed_services);
157
158 bool ConnectToApplicationWithLoader(
159 const GURL& requested_url,
160 const GURL& resolved_url,
161 const GURL& requestor_url,
162 InterfaceRequest<ServiceProvider>* services,
163 ServiceProviderPtr* exposed_services,
164 ApplicationLoader* loader);
165
166 InterfaceRequest<Application> RegisterShell(
167 const GURL& requested_url,
168 const GURL& resolved_url,
169 const GURL& requestor_url,
170 InterfaceRequest<ServiceProvider> services,
171 ServiceProviderPtr exposed_services);
Aaron Boodmancd7b9112014-11-06 21:44:49 -0800172
Dave Moore0504d062015-01-23 15:33:29 -0800173 ShellImpl* GetShellImpl(const GURL& url);
174
James Robinson646469d2014-10-03 15:33:28 -0700175 void ConnectToClient(ShellImpl* shell_impl,
176 const GURL& url,
177 const GURL& requestor_url,
James Robinson3217bf32015-01-14 14:33:07 -0800178 InterfaceRequest<ServiceProvider> services,
179 ServiceProviderPtr exposed_services);
James Robinson646469d2014-10-03 15:33:28 -0700180
Aaron Boodmandf23d132015-02-25 13:43:19 -0800181 void HandleFetchCallback(const GURL& requested_url,
182 const GURL& requestor_url,
183 InterfaceRequest<ServiceProvider> services,
184 ServiceProviderPtr exposed_services,
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800185 NativeRunner::CleanupBehavior cleanup_behavior,
186 scoped_ptr<Fetcher> fetcher);
187
188 void RunNativeApplication(InterfaceRequest<Application> application_request,
189 NativeRunner::CleanupBehavior cleanup_behavior,
190 scoped_ptr<Fetcher> fetcher,
191 const base::FilePath& file_path,
192 bool path_exists);
193
Benjamin Lerman51701292014-11-21 10:53:37 +0100194 void LoadWithContentHandler(const GURL& content_handler_url,
James Robinsone5ae9e42015-01-26 17:53:08 -0800195 InterfaceRequest<Application> application_request,
Benjamin Lerman51701292014-11-21 10:53:37 +0100196 URLResponsePtr url_response);
James Robinson646469d2014-10-03 15:33:28 -0700197
Aaron Boodmancd7b9112014-11-06 21:44:49 -0800198 // Return the appropriate loader for |url|. This can return NULL if there is
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800199 // no loader configured for the URL.
200 ApplicationLoader* GetLoaderForURL(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -0700201
Benjamin Lerman7c057142014-10-31 18:23:10 +0100202 // Removes a ContentHandler when it encounters an error.
203 void OnContentHandlerError(ContentHandlerConnection* content_handler);
204
205 // Returns the arguments for the given url.
206 Array<String> GetArgsForURL(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -0700207
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800208 void CleanupRunner(NativeRunner* runner);
209
James Robinson646469d2014-10-03 15:33:28 -0700210 Delegate* delegate_;
211 // Loader management.
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800212 // Loaders are chosen in the order they are listed here.
James Robinson646469d2014-10-03 15:33:28 -0700213 URLToLoaderMap url_to_loader_;
214 SchemeToLoaderMap scheme_to_loader_;
215 scoped_ptr<ApplicationLoader> default_loader_;
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800216 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
James Robinson646469d2014-10-03 15:33:28 -0700217
218 URLToShellImplMap url_to_shell_impl_;
219 URLToContentHandlerMap url_to_content_handler_;
220 URLToArgsMap url_to_args_;
James Robinson646469d2014-10-03 15:33:28 -0700221
222 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
223
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800224 base::SequencedWorkerPool* blocking_pool_;
225 NetworkServicePtr network_service_;
226 MimeTypeToURLMap mime_type_to_url_;
227 ScopedVector<NativeRunner> native_runners_;
228 bool disable_cache_;
229
James Robinson646469d2014-10-03 15:33:28 -0700230 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
231};
232
233} // namespace mojo
234
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -0800235#endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_