blob: edfb46ec90ac0d118d10e94e5c7ced7aff64a22e [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
Viet-Trung Luu3f2970c2015-03-05 10:21:56 -080010#include "base/macros.h"
James Robinson646469d2014-10-03 15:33:28 -070011#include "base/memory/scoped_ptr.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080012#include "base/memory/scoped_vector.h"
James Robinson646469d2014-10-03 15:33:28 -070013#include "base/memory/weak_ptr.h"
Aaron Boodman5e7f9042015-02-24 17:37:34 -080014#include "mojo/public/cpp/bindings/interface_request.h"
Viet-Trung Luu3f2970c2015-03-05 10:21:56 -080015#include "mojo/public/interfaces/application/application.mojom.h"
James Robinson646469d2014-10-03 15:33:28 -070016#include "mojo/public/interfaces/application/service_provider.mojom.h"
Viet-Trung Luu2e11a3f2015-10-13 13:20:30 -070017#include "mojo/services/network/interfaces/network_service.mojom.h"
Viet-Trung Luu0f4f3ba2015-10-10 01:08:40 -070018#include "mojo/services/url_response_disk_cache/interfaces/url_response_disk_cache.mojom.h"
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -080019#include "shell/application_manager/application_loader.h"
Benjamin Lerman71f56072015-03-12 15:04:50 +010020#include "shell/application_manager/identity.h"
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -070021#include "shell/application_manager/native_application_options.h"
Viet-Trung Luu3f2970c2015-03-05 10:21:56 -080022#include "shell/application_manager/native_runner.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070023#include "shell/native_application_support.h"
James Robinson646469d2014-10-03 15:33:28 -070024#include "url/gurl.h"
25
Aaron Boodman5e7f9042015-02-24 17:37:34 -080026namespace base {
27class FilePath;
28class SequencedWorkerPool;
29}
30
Viet-Trung Luu36faa4d2015-03-04 18:08:18 -080031namespace shell {
James Robinson646469d2014-10-03 15:33:28 -070032
Viet-Trung Luu3f2970c2015-03-05 10:21:56 -080033class Fetcher;
34class ShellImpl;
Aaron Boodman5e7f9042015-02-24 17:37:34 -080035
Viet-Trung Luu16492012015-03-03 15:18:32 -080036class ApplicationManager {
James Robinson646469d2014-10-03 15:33:28 -070037 public:
Viet-Trung Luu988b4be2015-05-15 13:00:15 -070038 struct Options {
Benjamin Lerman9a258502015-09-17 16:53:28 +020039 Options() : disable_cache(false), force_offline_by_default(false) {}
Viet-Trung Luu988b4be2015-05-15 13:00:15 -070040
41 bool disable_cache;
Benjamin Lerman9a258502015-09-17 16:53:28 +020042 bool force_offline_by_default;
Viet-Trung Luu988b4be2015-05-15 13:00:15 -070043 };
44
Viet-Trung Luu16492012015-03-03 15:18:32 -080045 class Delegate {
James Robinson646469d2014-10-03 15:33:28 -070046 public:
Benjamin Lerman605969e2015-04-30 13:51:04 +020047 // Gives the delegate a chance to apply any mappings for the specified url.
48 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
49 virtual GURL ResolveMappings(const GURL& url) = 0;
50
51 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
52 // |url| if the scheme is not 'mojo'.
53 virtual GURL ResolveMojoURL(const GURL& url) = 0;
54
55 protected:
56 virtual ~Delegate() {}
James Robinson646469d2014-10-03 15:33:28 -070057 };
58
59 // API for testing.
Viet-Trung Luu16492012015-03-03 15:18:32 -080060 class TestAPI {
James Robinson646469d2014-10-03 15:33:28 -070061 public:
62 explicit TestAPI(ApplicationManager* manager);
63 ~TestAPI();
64
65 // Returns true if the shared instance has been created.
66 static bool HasCreatedInstance();
67 // Returns true if there is a ShellImpl for this URL.
68 bool HasFactoryForURL(const GURL& url) const;
69
70 private:
71 ApplicationManager* manager_;
72
73 DISALLOW_COPY_AND_ASSIGN(TestAPI);
74 };
75
Viet-Trung Luu988b4be2015-05-15 13:00:15 -070076 ApplicationManager(const Options& options, Delegate* delegate);
James Robinson646469d2014-10-03 15:33:28 -070077 ~ApplicationManager();
78
James Robinson646469d2014-10-03 15:33:28 -070079 // Loads a service if necessary and establishes a new client connection.
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070080 void ConnectToApplication(
81 const GURL& application_url,
82 const GURL& requestor_url,
83 mojo::InterfaceRequest<mojo::ServiceProvider> services,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070084 const base::Closure& on_application_end);
James Robinson646469d2014-10-03 15:33:28 -070085
86 template <typename Interface>
87 inline void ConnectToService(const GURL& application_url,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070088 mojo::InterfacePtr<Interface>* ptr) {
89 mojo::ScopedMessagePipeHandle service_handle =
James Robinson646469d2014-10-03 15:33:28 -070090 ConnectToServiceByName(application_url, Interface::Name_);
Vardhan Mudunurudac048a2016-02-03 17:16:52 -080091 ptr->Bind(mojo::InterfaceHandle<Interface>(service_handle.Pass(), 0u));
James Robinson646469d2014-10-03 15:33:28 -070092 }
93
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070094 mojo::ScopedMessagePipeHandle ConnectToServiceByName(
James Robinson646469d2014-10-03 15:33:28 -070095 const GURL& application_url,
96 const std::string& interface_name);
97
Aaron Boodman5e7f9042015-02-24 17:37:34 -080098 void RegisterContentHandler(const std::string& mime_type,
99 const GURL& content_handler_url);
100
James Robinson646469d2014-10-03 15:33:28 -0700101 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
102 // or SetLoaderForScheme().
103 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
104 default_loader_ = loader.Pass();
105 }
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800106 void set_native_runner_factory(
107 scoped_ptr<NativeRunnerFactory> runner_factory) {
108 native_runner_factory_ = runner_factory.Pass();
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800109 }
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800110 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
111 blocking_pool_ = blocking_pool;
112 }
James Robinson646469d2014-10-03 15:33:28 -0700113 // Sets a Loader to be used for a specific url.
114 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
115 // Sets a Loader to be used for a specific url scheme.
116 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
117 const std::string& scheme);
Viet-Trung Luufbf0a4e2015-03-04 14:37:27 -0800118 // These strings will be passed to the Initialize() method when an Application
119 // is instantiated.
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700120 // TODO(vtl): Maybe we should store/compare resolved URLs?
James Robinson646469d2014-10-03 15:33:28 -0700121 void SetArgsForURL(const std::vector<std::string>& args, const GURL& url);
Viet-Trung Luu915cdc52015-03-09 09:55:30 -0700122 // These options will be used in running any native application at |url|
123 // (which shouldn't contain a query string). (|url| will be mapped and
124 // resolved, and any application whose base resolved URL matches it will have
125 // |options| applied.)
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700126 // Note: Calling this for a URL will add (default) options for that URL if
127 // necessary.
Viet-Trung Luufbf0a4e2015-03-04 14:37:27 -0800128 // TODO(vtl): This may not do what's desired if the resolved URL results in an
129 // HTTP redirect. Really, we want options to be identified with a particular
130 // implementation, maybe via a signed manifest or something like that.
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700131 NativeApplicationOptions* GetNativeApplicationOptionsForURL(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -0700132
James Robinson646469d2014-10-03 15:33:28 -0700133 // Destroys all Shell-ends of connections established with Applications.
134 // Applications connected by this ApplicationManager will observe pipe errors
135 // and have a chance to shutdown.
136 void TerminateShellConnections();
137
James Robinsonf5713182015-01-13 17:41:34 -0800138 // Removes a ShellImpl when it encounters an error.
139 void OnShellImplError(ShellImpl* shell_impl);
140
James Robinson646469d2014-10-03 15:33:28 -0700141 private:
Benjamin Lerman7c057142014-10-31 18:23:10 +0100142 class ContentHandlerConnection;
James Robinson646469d2014-10-03 15:33:28 -0700143
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700144 using URLToLoaderMap = std::map<GURL, scoped_ptr<ApplicationLoader>>;
145 using SchemeToLoaderMap =
146 std::map<std::string, scoped_ptr<ApplicationLoader>>;
147 using IdentityToShellImplMap = std::map<Identity, scoped_ptr<ShellImpl>>;
Sean Klein78eb28d2015-11-17 10:15:09 -0800148 using IdentityToContentHandlerMap =
149 std::map<Identity, scoped_ptr<ContentHandlerConnection>>;
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700150 using URLToArgsMap = std::map<GURL, std::vector<std::string>>;
151 using MimeTypeToURLMap = std::map<std::string, GURL>;
152 using URLToNativeOptionsMap = std::map<GURL, NativeApplicationOptions>;
James Robinson646469d2014-10-03 15:33:28 -0700153
Benjamin Lermanacf32fb2015-03-24 10:54:37 +0100154 void ConnectToApplicationWithParameters(
155 const GURL& application_url,
156 const GURL& requestor_url,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700157 mojo::InterfaceRequest<mojo::ServiceProvider> services,
Benjamin Lerman993869e2015-03-24 13:35:28 +0100158 const base::Closure& on_application_end,
Benjamin Lerman128ca7d2015-03-24 12:02:49 +0100159 const std::vector<std::string>& pre_redirect_parameters);
Benjamin Lermanacf32fb2015-03-24 10:54:37 +0100160
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700161 bool ConnectToRunningApplication(
162 const GURL& resolved_url,
163 const GURL& requestor_url,
Viet-Trung Luud588c942016-05-09 16:49:45 -0700164 mojo::InterfaceRequest<mojo::ServiceProvider>* services);
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800165
166 bool ConnectToApplicationWithLoader(
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800167 const GURL& resolved_url,
168 const GURL& requestor_url,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700169 mojo::InterfaceRequest<mojo::ServiceProvider>* services,
Benjamin Lerman993869e2015-03-24 13:35:28 +0100170 const base::Closure& on_application_end,
Benjamin Lermanacf32fb2015-03-24 10:54:37 +0100171 const std::vector<std::string>& parameters,
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800172 ApplicationLoader* loader);
173
Sean Klein78eb28d2015-11-17 10:15:09 -0800174 // Creates an Identity for the service identified by |resolved_url|.
175 // If |new_process_per_connection| is true for the URL's options, then the
176 // identity is unique. Otherwise, repeated invocations with the same
Benjamin Lermane58454c2015-11-24 14:59:06 +0100177 // |resolved_url| will result in an equivalent Identity. If |strip_query| is
178 // true, the query is stripped before creating the Identity.
179 Identity MakeApplicationIdentity(const GURL& resolved_url,
180 bool strip_query = true);
Sean Klein78eb28d2015-11-17 10:15:09 -0800181
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700182 mojo::InterfaceRequest<mojo::Application> RegisterShell(
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800183 // The URL after resolution and redirects, including the querystring.
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800184 const GURL& resolved_url,
185 const GURL& requestor_url,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700186 mojo::InterfaceRequest<mojo::ServiceProvider> services,
Benjamin Lerman993869e2015-03-24 13:35:28 +0100187 const base::Closure& on_application_end,
Benjamin Lermanacf32fb2015-03-24 10:54:37 +0100188 const std::vector<std::string>& parameters);
Aaron Boodmancd7b9112014-11-06 21:44:49 -0800189
Dave Moore0504d062015-01-23 15:33:29 -0800190 ShellImpl* GetShellImpl(const GURL& url);
191
Viet-Trung Luud588c942016-05-09 16:49:45 -0700192 void ConnectToClient(ShellImpl* shell_impl,
193 const GURL& resolved_url,
194 const GURL& requestor_url,
195 mojo::InterfaceRequest<mojo::ServiceProvider> services);
James Robinson646469d2014-10-03 15:33:28 -0700196
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700197 void HandleFetchCallback(
198 const GURL& requestor_url,
199 mojo::InterfaceRequest<mojo::ServiceProvider> services,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700200 const base::Closure& on_application_end,
201 const std::vector<std::string>& parameters,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700202 scoped_ptr<Fetcher> fetcher);
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800203
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700204 void RunNativeApplication(
205 mojo::InterfaceRequest<mojo::Application> application_request,
Viet-Trung Luu64bb17c2015-10-02 17:02:04 -0700206 const NativeApplicationOptions& options,
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700207 scoped_ptr<Fetcher> fetcher,
208 const base::FilePath& file_path,
209 bool path_exists);
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800210
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700211 void LoadWithContentHandler(
212 const GURL& content_handler_url,
213 mojo::InterfaceRequest<mojo::Application> application_request,
214 mojo::URLResponsePtr url_response);
James Robinson646469d2014-10-03 15:33:28 -0700215
Viet-Trung Luua417b142015-03-31 11:24:44 -0700216 // Returns the appropriate loader for |url|, or null if there is no loader
217 // configured for the URL.
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800218 ApplicationLoader* GetLoaderForURL(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -0700219
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700220 // Removes a mojo::ContentHandler when it encounters an error.
Benjamin Lerman7c057142014-10-31 18:23:10 +0100221 void OnContentHandlerError(ContentHandlerConnection* content_handler);
222
223 // Returns the arguments for the given url.
Benjamin Lermanacf32fb2015-03-24 10:54:37 +0100224 std::vector<std::string> GetArgsForURL(const GURL& url);
James Robinson646469d2014-10-03 15:33:28 -0700225
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800226 void CleanupRunner(NativeRunner* runner);
227
Viet-Trung Luu988b4be2015-05-15 13:00:15 -0700228 const Options options_;
Viet-Trung Luufbf0a4e2015-03-04 14:37:27 -0800229 Delegate* const delegate_;
James Robinson646469d2014-10-03 15:33:28 -0700230 // Loader management.
Aaron Boodmanbfc11182015-02-19 12:42:19 -0800231 // Loaders are chosen in the order they are listed here.
James Robinson646469d2014-10-03 15:33:28 -0700232 URLToLoaderMap url_to_loader_;
233 SchemeToLoaderMap scheme_to_loader_;
234 scoped_ptr<ApplicationLoader> default_loader_;
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800235 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
James Robinson646469d2014-10-03 15:33:28 -0700236
Benjamin Lerman71f56072015-03-12 15:04:50 +0100237 IdentityToShellImplMap identity_to_shell_impl_;
Sean Klein78eb28d2015-11-17 10:15:09 -0800238 IdentityToContentHandlerMap identity_to_content_handler_;
James Robinson646469d2014-10-03 15:33:28 -0700239 URLToArgsMap url_to_args_;
Viet-Trung Luufbf0a4e2015-03-04 14:37:27 -0800240 // Note: The keys are URLs after mapping and resolving.
241 URLToNativeOptionsMap url_to_native_options_;
James Robinson646469d2014-10-03 15:33:28 -0700242
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800243 base::SequencedWorkerPool* blocking_pool_;
Benjamin Lerman5d429aa2015-05-07 16:21:00 +0200244 mojo::URLResponseDiskCachePtr url_response_disk_cache_;
Colin Blundell8ca4d652015-06-05 16:41:30 +0200245 mojo::NetworkServicePtr network_service_;
Benjamin Lermand15e3f42015-09-17 11:26:18 +0200246 mojo::NetworkServicePtr authenticating_network_service_;
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800247 MimeTypeToURLMap mime_type_to_url_;
248 ScopedVector<NativeRunner> native_runners_;
Colin Blundell8ca4d652015-06-05 16:41:30 +0200249 bool initialized_authentication_interceptor_;
Dave Moorecc0e4f92015-03-10 15:23:04 -0700250 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
Aaron Boodman5e7f9042015-02-24 17:37:34 -0800251
James Robinson646469d2014-10-03 15:33:28 -0700252 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
253};
254
Viet-Trung Luu36faa4d2015-03-04 18:08:18 -0800255} // namespace shell
James Robinson646469d2014-10-03 15:33:28 -0700256
Przemysław Pietrzkiewicz6384d092015-01-15 16:03:17 -0800257#endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_