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 MOJO_SHELL_APPLICATION_MANAGER_H_
6 #define MOJO_SHELL_APPLICATION_MANAGER_H_
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/application/public/interfaces/shell.mojom.h"
17 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
18 #include "mojo/public/cpp/bindings/interface_request.h"
19 #include "mojo/shell/application_loader.h"
20 #include "mojo/shell/capability_filter.h"
21 #include "mojo/shell/connect_to_application_params.h"
22 #include "mojo/shell/fetcher.h"
23 #include "mojo/shell/identity.h"
24 #include "mojo/shell/native_runner.h"
29 class SequencedWorkerPool
;
36 class ApplicationInstance
;
37 class ContentHandlerConnection
;
39 class ApplicationManager
{
44 explicit TestAPI(ApplicationManager
* manager
);
47 // Returns true if the shared instance has been created.
48 static bool HasCreatedInstance();
49 // Returns true if there is a ApplicationInstance for this URL.
50 bool HasRunningInstanceForURL(const GURL
& url
) const;
52 ApplicationManager
* manager_
;
54 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
57 explicit ApplicationManager(scoped_ptr
<PackageManager
> package_manager
);
58 ~ApplicationManager();
60 // Loads a service if necessary and establishes a new client connection.
61 // Please see the comments in connect_to_application_params.h for more details
62 // about the parameters.
63 void ConnectToApplication(scoped_ptr
<ConnectToApplicationParams
> params
);
65 // Sets the default Loader to be used if not overridden by SetLoaderForURL().
66 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
67 default_loader_
= loader
.Pass();
69 void set_native_runner_factory(
70 scoped_ptr
<NativeRunnerFactory
> runner_factory
) {
71 native_runner_factory_
= runner_factory
.Pass();
73 void set_blocking_pool(base::SequencedWorkerPool
* blocking_pool
) {
74 blocking_pool_
= blocking_pool
;
76 // Sets a Loader to be used for a specific url.
77 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
79 // Destroys all Shell-ends of connections established with Applications.
80 // Applications connected by this ApplicationManager will observe pipe errors
81 // and have a chance to shutdown.
82 void TerminateShellConnections();
84 // Removes a ApplicationInstance when it encounters an error.
85 void OnApplicationInstanceError(ApplicationInstance
* instance
);
87 // Removes a ContentHandler when its connection is closed.
88 void OnContentHandlerConnectionClosed(
89 ContentHandlerConnection
* content_handler
);
91 ApplicationInstance
* GetApplicationInstance(const Identity
& identity
) const;
94 using IdentityToInstanceMap
= std::map
<Identity
, ApplicationInstance
*>;
95 using IdentityToContentHandlerMap
=
96 std::map
<Identity
, ContentHandlerConnection
*>;
97 using URLToLoaderMap
= std::map
<GURL
, ApplicationLoader
*>;
99 // Takes the contents of |params| only when it returns true.
100 bool ConnectToRunningApplication(
101 scoped_ptr
<ConnectToApplicationParams
>* params
);
103 InterfaceRequest
<Application
> CreateInstance(
104 scoped_ptr
<ConnectToApplicationParams
> params
,
105 ApplicationInstance
** instance
);
107 // Called once |fetcher| has found app. |params->app_url()| is the url of
108 // the requested application before any mappings/resolution have been applied.
109 // The corresponding URLRequest struct in |params| has been taken.
110 void HandleFetchCallback(scoped_ptr
<ConnectToApplicationParams
> params
,
111 scoped_ptr
<Fetcher
> fetcher
);
113 void RunNativeApplication(InterfaceRequest
<Application
> application_request
,
114 bool start_sandboxed
,
115 scoped_ptr
<Fetcher
> fetcher
,
116 const base::FilePath
& file_path
,
119 void LoadWithContentHandler(
120 const Identity
& originator_identity
,
121 const CapabilityFilter
& originator_filter
,
122 const GURL
& content_handler_url
,
123 const std::string
& qualifier
,
124 const CapabilityFilter
& filter
,
125 const Shell::ConnectToApplicationCallback
& connect_callback
,
126 ApplicationInstance
* app
,
127 InterfaceRequest
<Application
> application_request
,
128 URLResponsePtr url_response
);
130 // Returns the appropriate loader for |url|, or the default loader if there is
131 // no loader configured for the URL.
132 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
134 void CleanupRunner(NativeRunner
* runner
);
136 scoped_ptr
<PackageManager
> const package_manager_
;
137 // Loader management.
138 // Loaders are chosen in the order they are listed here.
139 URLToLoaderMap url_to_loader_
;
140 scoped_ptr
<ApplicationLoader
> default_loader_
;
141 scoped_ptr
<NativeRunnerFactory
> native_runner_factory_
;
143 IdentityToInstanceMap identity_to_instance_
;
144 IdentityToContentHandlerMap identity_to_content_handler_
;
146 base::SequencedWorkerPool
* blocking_pool_
;
147 ScopedVector
<NativeRunner
> native_runners_
;
148 // Counter used to assign ids to content_handlers.
149 uint32_t content_handler_id_counter_
;
150 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
152 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
155 Shell::ConnectToApplicationCallback
EmptyConnectCallback();
160 #endif // MOJO_SHELL_APPLICATION_MANAGER_H_