Remove temporary logging added for debugging a racing.
[chromium-blink-merge.git] / mojo / shell / application_manager.h
blob324761f31382bbd428afd6edbf3e0469c5fa8172
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/services/updater/updater.mojom.h"
20 #include "mojo/shell/application_loader.h"
21 #include "mojo/shell/capability_filter.h"
22 #include "mojo/shell/fetcher.h"
23 #include "mojo/shell/identity.h"
24 #include "mojo/shell/native_runner.h"
25 #include "url/gurl.h"
27 namespace base {
28 class FilePath;
29 class SequencedWorkerPool;
32 namespace mojo {
33 namespace shell {
35 class ApplicationInstance;
36 class ContentHandlerConnection;
38 class ApplicationManager {
39 public:
40 class Delegate {
41 public:
42 // Gives the delegate a chance to apply any mappings for the specified url.
43 // This should not resolve 'mojo' urls, that is done by ResolveMojoURL().
44 virtual GURL ResolveMappings(const GURL& url) = 0;
46 // Used to map a url with the scheme 'mojo' to the appropriate url. Return
47 // |url| if the scheme is not 'mojo'.
48 virtual GURL ResolveMojoURL(const GURL& url) = 0;
50 // Asks the delegate to create a Fetcher for the specified url. Return
51 // true on success, false if the default fetcher should be created.
52 virtual bool CreateFetcher(
53 const GURL& url,
54 const Fetcher::FetchCallback& loader_callback) = 0;
56 protected:
57 virtual ~Delegate() {}
60 // API for testing.
61 class TestAPI {
62 public:
63 explicit TestAPI(ApplicationManager* manager);
64 ~TestAPI();
66 // Returns true if the shared instance has been created.
67 static bool HasCreatedInstance();
68 // Returns true if there is a ApplicationInstance for this URL.
69 bool HasRunningInstanceForURL(const GURL& url) const;
71 private:
72 ApplicationManager* manager_;
74 DISALLOW_COPY_AND_ASSIGN(TestAPI);
77 explicit ApplicationManager(Delegate* delegate);
78 ~ApplicationManager();
80 // Loads a service if necessary and establishes a new client connection.
81 // |originator| can be NULL (e.g. for the first application or in tests), but
82 // typically is non-NULL and identifies the instance initiating the
83 // connection.
84 void ConnectToApplication(ApplicationInstance* originator,
85 URLRequestPtr requested_url,
86 const std::string& qualifier,
87 const GURL& requestor_url,
88 InterfaceRequest<ServiceProvider> services,
89 ServiceProviderPtr exposed_services,
90 const CapabilityFilter& capability_filter,
91 const base::Closure& on_application_end);
93 // Must only be used by shell internals and test code as it does not forward
94 // capability filters.
95 template <typename Interface>
96 inline void ConnectToService(const GURL& application_url,
97 InterfacePtr<Interface>* ptr) {
98 ScopedMessagePipeHandle service_handle =
99 ConnectToServiceByName(application_url, Interface::Name_);
100 ptr->Bind(InterfacePtrInfo<Interface>(service_handle.Pass(), 0u));
103 void RegisterContentHandler(const std::string& mime_type,
104 const GURL& content_handler_url);
106 // Registers a package alias. When attempting to load |alias|, it will
107 // instead redirect to |content_handler_package|, which is a content handler
108 // which will be passed the |alias| as the URLResponse::url. Different values
109 // of |alias| with the same |qualifier| that are in the same
110 // |content_handler_package| will run in the same process in multi-process
111 // mode.
112 void RegisterApplicationPackageAlias(const GURL& alias,
113 const GURL& content_handler_package,
114 const std::string& qualifier);
116 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
117 // or SetLoaderForScheme().
118 void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
119 default_loader_ = loader.Pass();
121 void set_native_runner_factory(
122 scoped_ptr<NativeRunnerFactory> runner_factory) {
123 native_runner_factory_ = runner_factory.Pass();
125 void set_blocking_pool(base::SequencedWorkerPool* blocking_pool) {
126 blocking_pool_ = blocking_pool;
128 void set_disable_cache(bool disable_cache) { disable_cache_ = disable_cache; }
129 // Sets a Loader to be used for a specific url.
130 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
131 // Sets a Loader to be used for a specific url scheme.
132 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
133 const std::string& scheme);
134 // These options will be used in running any native application at |url|
135 // (which shouldn't contain a query string). (|url| will be mapped and
136 // resolved, and any application whose base resolved URL matches it will have
137 // |options| applied.)
138 // TODO(vtl): This may not do what's desired if the resolved URL results in an
139 // HTTP redirect. Really, we want options to be identified with a particular
140 // implementation, maybe via a signed manifest or something like that.
141 void SetNativeOptionsForURL(const NativeRunnerFactory::Options& options,
142 const GURL& url);
144 // Destroys all Shell-ends of connections established with Applications.
145 // Applications connected by this ApplicationManager will observe pipe errors
146 // and have a chance to shutdown.
147 void TerminateShellConnections();
149 // Removes a ApplicationInstance when it encounters an error.
150 void OnApplicationInstanceError(ApplicationInstance* instance);
152 // Removes a ContentHandler when its connection is closed.
153 void OnContentHandlerConnectionClosed(
154 ContentHandlerConnection* content_handler);
156 ApplicationInstance* GetApplicationInstance(const Identity& identity) const;
158 private:
159 using ApplicationPackagedAlias = std::map<GURL, std::pair<GURL, std::string>>;
160 using IdentityToApplicationInstanceMap =
161 std::map<Identity, ApplicationInstance*>;
162 using MimeTypeToURLMap = std::map<std::string, GURL>;
163 using SchemeToLoaderMap = std::map<std::string, ApplicationLoader*>;
164 using URLToContentHandlerMap =
165 std::map<std::pair<GURL, std::string>, ContentHandlerConnection*>;
166 using URLToLoaderMap = std::map<GURL, ApplicationLoader*>;
167 using URLToNativeOptionsMap = std::map<GURL, NativeRunnerFactory::Options>;
169 bool ConnectToRunningApplication(ApplicationInstance* originator,
170 const GURL& resolved_url,
171 const std::string& qualifier,
172 const GURL& requestor_url,
173 InterfaceRequest<ServiceProvider>* services,
174 ServiceProviderPtr* exposed_services,
175 const CapabilityFilter& filter);
177 bool ConnectToApplicationWithLoader(
178 ApplicationInstance* originator,
179 const GURL& requested_url,
180 const std::string& qualifier,
181 const GURL& resolved_url,
182 const GURL& requestor_url,
183 InterfaceRequest<ServiceProvider>* services,
184 ServiceProviderPtr* exposed_services,
185 const CapabilityFilter& filter,
186 const base::Closure& on_application_end,
187 ApplicationLoader* loader);
189 InterfaceRequest<Application> RegisterInstance(
190 ApplicationInstance* originator,
191 const GURL& app_url,
192 const std::string& qualifier,
193 const GURL& requestor_url,
194 InterfaceRequest<ServiceProvider> services,
195 ServiceProviderPtr exposed_services,
196 const CapabilityFilter& filter,
197 const base::Closure& on_application_end);
199 // Called once |fetcher| has found app. |requested_url| is the url of the
200 // requested application before any mappings/resolution have been applied.
201 void HandleFetchCallback(ApplicationInstance* originator,
202 const GURL& requested_url,
203 const std::string& qualifier,
204 const GURL& requestor_url,
205 InterfaceRequest<ServiceProvider> services,
206 ServiceProviderPtr exposed_services,
207 const CapabilityFilter& filter,
208 const base::Closure& on_application_end,
209 NativeApplicationCleanup cleanup,
210 scoped_ptr<Fetcher> fetcher);
212 void RunNativeApplication(InterfaceRequest<Application> application_request,
213 bool start_sandboxed,
214 const NativeRunnerFactory::Options& options,
215 NativeApplicationCleanup cleanup,
216 scoped_ptr<Fetcher> fetcher,
217 const base::FilePath& file_path,
218 bool path_exists);
220 void LoadWithContentHandler(ApplicationInstance* originator,
221 const GURL& content_handler_url,
222 const GURL& requestor_url,
223 const std::string& qualifier,
224 const CapabilityFilter& filter,
225 InterfaceRequest<Application> application_request,
226 URLResponsePtr url_response);
228 // Returns the appropriate loader for |url|, or null if there is no loader
229 // configured for the URL.
230 ApplicationLoader* GetLoaderForURL(const GURL& url);
232 void CleanupRunner(NativeRunner* runner);
234 ScopedMessagePipeHandle ConnectToServiceByName(
235 const GURL& application_url,
236 const std::string& interface_name);
238 Delegate* const delegate_;
239 // Loader management.
240 // Loaders are chosen in the order they are listed here.
241 URLToLoaderMap url_to_loader_;
242 SchemeToLoaderMap scheme_to_loader_;
243 scoped_ptr<ApplicationLoader> default_loader_;
244 scoped_ptr<NativeRunnerFactory> native_runner_factory_;
246 ApplicationPackagedAlias application_package_alias_;
247 IdentityToApplicationInstanceMap identity_to_instance_;
248 URLToContentHandlerMap url_to_content_handler_;
249 // Note: The keys are URLs after mapping and resolving.
250 URLToNativeOptionsMap url_to_native_options_;
252 base::SequencedWorkerPool* blocking_pool_;
253 URLLoaderFactoryPtr url_loader_factory_;
254 updater::UpdaterPtr updater_;
255 MimeTypeToURLMap mime_type_to_url_;
256 ScopedVector<NativeRunner> native_runners_;
257 bool disable_cache_;
258 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
260 DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
263 } // namespace shell
264 } // namespace mojo
266 #endif // SHELL_APPLICATION_MANAGER_APPLICATION_MANAGER_H_