James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 5 | #ifndef SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ |
| 6 | #define SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "base/basictypes.h" |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 11 | #include "base/callback.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 14 | #include "base/memory/scoped_vector.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/interface_request.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 17 | #include "mojo/public/interfaces/application/service_provider.mojom.h" |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 18 | #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 19 | #include "shell/application_manager/application_loader.h" |
| 20 | #include "shell/application_manager/application_manager_export.h" |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 21 | #include "shell/application_manager/fetcher.h" |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 22 | #include "shell/application_manager/shell_impl.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 23 | #include "url/gurl.h" |
| 24 | |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 25 | namespace base { |
| 26 | class FilePath; |
| 27 | class SequencedWorkerPool; |
| 28 | } |
| 29 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 30 | namespace mojo { |
| 31 | |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 32 | // ApplicationManager requires implementations of NativeRunner and |
| 33 | // NativeRunnerFactory to run native applications. |
| 34 | class 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 | |
| 50 | class MOJO_APPLICATION_MANAGER_EXPORT NativeRunnerFactory { |
| 51 | public: |
| 52 | virtual ~NativeRunnerFactory() {} |
| 53 | virtual scoped_ptr<NativeRunner> Create() = 0; |
| 54 | }; |
| 55 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 56 | class 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 Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 63 | virtual void OnApplicationError(const GURL& url); |
| 64 | virtual GURL ResolveURL(const GURL& url); |
Benjamin Lerman | 75aef1d | 2015-01-21 12:04:39 +0100 | [diff] [blame] | 65 | virtual GURL ResolveMappings(const GURL& url); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 66 | }; |
| 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 Robinson | 3217bf3 | 2015-01-14 14:33:07 -0800 | [diff] [blame] | 85 | explicit ApplicationManager(Delegate* delegate); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 86 | ~ApplicationManager(); |
| 87 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 88 | // Loads a service if necessary and establishes a new client connection. |
| 89 | void ConnectToApplication(const GURL& application_url, |
| 90 | const GURL& requestor_url, |
James Robinson | 3217bf3 | 2015-01-14 14:33:07 -0800 | [diff] [blame] | 91 | InterfaceRequest<ServiceProvider> services, |
| 92 | ServiceProviderPtr exposed_services); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 93 | |
| 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 Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 106 | void RegisterContentHandler(const std::string& mime_type, |
| 107 | const GURL& content_handler_url); |
| 108 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 109 | void RegisterExternalApplication(const GURL& application_url, |
Dave Moore | f2efe55 | 2015-01-26 14:31:45 -0800 | [diff] [blame] | 110 | const std::vector<std::string>& args, |
James Robinson | e5ae9e4 | 2015-01-26 17:53:08 -0800 | [diff] [blame] | 111 | ApplicationPtr application); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 112 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 113 | // 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 Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 118 | void set_native_runner_factory( |
| 119 | scoped_ptr<NativeRunnerFactory> runner_factory) { |
| 120 | native_runner_factory_ = runner_factory.Pass(); |
Aaron Boodman | bfc1118 | 2015-02-19 12:42:19 -0800 | [diff] [blame] | 121 | } |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 122 | 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 Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 126 | // 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 Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 135 | // 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 Robinson | f571318 | 2015-01-13 17:41:34 -0800 | [diff] [blame] | 140 | // Removes a ShellImpl when it encounters an error. |
| 141 | void OnShellImplError(ShellImpl* shell_impl); |
| 142 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 143 | private: |
Benjamin Lerman | 7c05714 | 2014-10-31 18:23:10 +0100 | [diff] [blame] | 144 | class ContentHandlerConnection; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 145 | |
| 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 Luu | 3295227 | 2015-02-11 16:32:03 -0800 | [diff] [blame] | 150 | typedef std::map<GURL, std::vector<std::string>> URLToArgsMap; |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 151 | typedef std::map<std::string, GURL> MimeTypeToURLMap; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 152 | |
Aaron Boodman | bfc1118 | 2015-02-19 12:42:19 -0800 | [diff] [blame] | 153 | 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 Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 172 | |
Dave Moore | 0504d06 | 2015-01-23 15:33:29 -0800 | [diff] [blame] | 173 | ShellImpl* GetShellImpl(const GURL& url); |
| 174 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 175 | void ConnectToClient(ShellImpl* shell_impl, |
| 176 | const GURL& url, |
| 177 | const GURL& requestor_url, |
James Robinson | 3217bf3 | 2015-01-14 14:33:07 -0800 | [diff] [blame] | 178 | InterfaceRequest<ServiceProvider> services, |
| 179 | ServiceProviderPtr exposed_services); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 180 | |
Aaron Boodman | df23d13 | 2015-02-25 13:43:19 -0800 | [diff] [blame] | 181 | void HandleFetchCallback(const GURL& requested_url, |
| 182 | const GURL& requestor_url, |
| 183 | InterfaceRequest<ServiceProvider> services, |
| 184 | ServiceProviderPtr exposed_services, |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 185 | 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 Lerman | 5170129 | 2014-11-21 10:53:37 +0100 | [diff] [blame] | 194 | void LoadWithContentHandler(const GURL& content_handler_url, |
James Robinson | e5ae9e4 | 2015-01-26 17:53:08 -0800 | [diff] [blame] | 195 | InterfaceRequest<Application> application_request, |
Benjamin Lerman | 5170129 | 2014-11-21 10:53:37 +0100 | [diff] [blame] | 196 | URLResponsePtr url_response); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 197 | |
Aaron Boodman | cd7b911 | 2014-11-06 21:44:49 -0800 | [diff] [blame] | 198 | // Return the appropriate loader for |url|. This can return NULL if there is |
Aaron Boodman | bfc1118 | 2015-02-19 12:42:19 -0800 | [diff] [blame] | 199 | // no loader configured for the URL. |
| 200 | ApplicationLoader* GetLoaderForURL(const GURL& url); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 201 | |
Benjamin Lerman | 7c05714 | 2014-10-31 18:23:10 +0100 | [diff] [blame] | 202 | // 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 Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 207 | |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 208 | void CleanupRunner(NativeRunner* runner); |
| 209 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 210 | Delegate* delegate_; |
| 211 | // Loader management. |
Aaron Boodman | bfc1118 | 2015-02-19 12:42:19 -0800 | [diff] [blame] | 212 | // Loaders are chosen in the order they are listed here. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 213 | URLToLoaderMap url_to_loader_; |
| 214 | SchemeToLoaderMap scheme_to_loader_; |
| 215 | scoped_ptr<ApplicationLoader> default_loader_; |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 216 | scoped_ptr<NativeRunnerFactory> native_runner_factory_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 217 | |
| 218 | URLToShellImplMap url_to_shell_impl_; |
| 219 | URLToContentHandlerMap url_to_content_handler_; |
| 220 | URLToArgsMap url_to_args_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 221 | |
| 222 | base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; |
| 223 | |
Aaron Boodman | 5e7f904 | 2015-02-24 17:37:34 -0800 | [diff] [blame] | 224 | 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 Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 230 | DISALLOW_COPY_AND_ASSIGN(ApplicationManager); |
| 231 | }; |
| 232 | |
| 233 | } // namespace mojo |
| 234 | |
Przemysław Pietrzkiewicz | 6384d09 | 2015-01-15 16:03:17 -0800 | [diff] [blame] | 235 | #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ |