OLD | NEW |
| (Empty) |
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 | |
5 #ifndef MOJO_SHELL_APPLICATION_MANAGER_H_ | |
6 #define MOJO_SHELL_APPLICATION_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "mojo/public/cpp/bindings/binding_set.h" | |
15 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | |
16 #include "mojo/services/package_manager/package_manager.h" | |
17 #include "mojo/services/package_manager/public/interfaces/shell_resolver.mojom.h
" | |
18 #include "mojo/shell/connect_params.h" | |
19 #include "mojo/shell/identity.h" | |
20 #include "mojo/shell/loader.h" | |
21 #include "mojo/shell/native_runner.h" | |
22 #include "mojo/shell/public/cpp/interface_factory.h" | |
23 #include "mojo/shell/public/cpp/shell_client.h" | |
24 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | |
25 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | |
26 #include "mojo/shell/public/interfaces/shell.mojom.h" | |
27 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | |
28 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" | |
29 | |
30 namespace base { | |
31 class FilePath; | |
32 class SequencedWorkerPool; | |
33 } | |
34 | |
35 namespace mojo { | |
36 class ShellConnection; | |
37 namespace shell { | |
38 | |
39 class ApplicationManager : public ShellClient { | |
40 public: | |
41 // API for testing. | |
42 class TestAPI { | |
43 public: | |
44 explicit TestAPI(ApplicationManager* manager); | |
45 ~TestAPI(); | |
46 | |
47 // Returns true if there is a Instance for this name. | |
48 bool HasRunningInstanceForName(const std::string& name) const; | |
49 private: | |
50 ApplicationManager* manager_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(TestAPI); | |
53 }; | |
54 | |
55 // |native_runner_factory| is an instance of an object capable of vending | |
56 // implementations of NativeRunner, e.g. for in or out-of-process execution. | |
57 // See native_runner.h and RunNativeApplication(). | |
58 // |file_task_runner| provides access to a thread to perform file copy | |
59 // operations on. This may be null only in testing environments where | |
60 // applications are loaded via Loader implementations. | |
61 ApplicationManager( | |
62 scoped_ptr<NativeRunnerFactory> native_runner_factory, | |
63 base::TaskRunner* file_task_runner, | |
64 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); | |
65 ~ApplicationManager() override; | |
66 | |
67 // Provide a callback to be notified whenever an instance is destroyed. | |
68 // Typically the creator of the ApplicationManager will use this to determine | |
69 // when some set of instances it created are destroyed, so it can shut down. | |
70 void SetInstanceQuitCallback(base::Callback<void(const Identity&)> callback); | |
71 | |
72 // Completes a connection between a source and target application as defined | |
73 // by |params|, exchanging InterfaceProviders between them. If no existing | |
74 // instance of the target application is running, one will be loaded. | |
75 void Connect(scoped_ptr<ConnectParams> params); | |
76 | |
77 // Creates a new Instance identified as |name|. This is intended for use by | |
78 // the ApplicationManager's embedder to register itself with the shell. The | |
79 // name is never resolved and there must not be an existing instance | |
80 // associated with it. This must only be called once. | |
81 mojom::ShellClientRequest InitInstanceForEmbedder(const std::string& name); | |
82 | |
83 // Sets the default Loader to be used if not overridden by SetLoaderForName(). | |
84 void set_default_loader(scoped_ptr<Loader> loader) { | |
85 default_loader_ = std::move(loader); | |
86 } | |
87 | |
88 // Sets a Loader to be used for a specific name. | |
89 void SetLoaderForName(scoped_ptr<Loader> loader, const std::string& name); | |
90 | |
91 private: | |
92 class Instance; | |
93 | |
94 using IdentityToInstanceMap = std::map<Identity, Instance*>; | |
95 using NameToLoaderMap = std::map<std::string, Loader*>; | |
96 using IdentityToShellClientFactoryMap = | |
97 std::map<Identity, mojom::ShellClientFactoryPtr>; | |
98 | |
99 // ShellClient: | |
100 bool AcceptConnection(Connection* connection) override; | |
101 | |
102 void InitPackageManager( | |
103 scoped_ptr<package_manager::ApplicationCatalogStore> app_catalog); | |
104 | |
105 // Destroys all Shell-ends of connections established with Applications. | |
106 // Applications connected by this ApplicationManager will observe pipe errors | |
107 // and have a chance to shutdown. | |
108 void TerminateShellConnections(); | |
109 | |
110 // Removes a Instance when it encounters an error. | |
111 void OnInstanceError(Instance* instance); | |
112 | |
113 Instance* GetExistingInstance(const Identity& identity) const; | |
114 | |
115 void NotifyPIDAvailable(uint32_t id, base::ProcessId pid); | |
116 | |
117 // Attempt to complete the connection requested by |params| by connecting to | |
118 // an existing instance. If there is an existing instance, |params| is taken, | |
119 // and this function returns true. | |
120 bool ConnectToExistingInstance(scoped_ptr<ConnectParams>* params); | |
121 | |
122 Instance* CreateInstance(const Identity& target_id, | |
123 mojom::ShellClientRequest* request); | |
124 | |
125 // Called from the instance implementing mojom::ApplicationManager. |user_id| | |
126 // must be resolved by the instance (i.e. must not be | |
127 // mojom::Connector::kUserInherit). | |
128 void CreateInstanceForFactory( | |
129 mojom::ShellClientFactoryPtr factory, | |
130 const String& name, | |
131 uint32_t user_id, | |
132 mojom::CapabilityFilterPtr filter, | |
133 mojom::PIDReceiverRequest pid_receiver); | |
134 // Called from the instance implementing mojom::ApplicationManager. | |
135 void AddInstanceListener(mojom::InstanceListenerPtr listener); | |
136 | |
137 void CreateShellClient(const Identity& source, | |
138 const Identity& shell_client_factory, | |
139 const std::string& name, | |
140 mojom::ShellClientRequest request); | |
141 // Returns a running ShellClientFactory for |shell_client_factory_identity|, | |
142 // if there is not one running one is started for |source_identity|. | |
143 mojom::ShellClientFactory* GetShellClientFactory( | |
144 const Identity& shell_client_factory_identity, | |
145 const Identity& source_identity); | |
146 void OnShellClientFactoryLost(const Identity& which);; | |
147 | |
148 // Callback when remote PackageManager resolves mojo:foo to mojo:bar. | |
149 // |params| are the params passed to Connect(). | |
150 // |resolved_name| is the mojo: name identifying the physical package | |
151 // application. | |
152 // |file_url| is the resolved file:// URL of the physical package. | |
153 // |base_filter| is the CapabilityFilter the requested application should be | |
154 // run with, from its manifest. | |
155 void OnGotResolvedName(scoped_ptr<ConnectParams> params, | |
156 const String& resolved_name, | |
157 const String& resolved_qualifier, | |
158 mojom::CapabilityFilterPtr base_filter, | |
159 const String& file_url); | |
160 | |
161 // Tries to load |target| with an Loader. Returns true if one was registered | |
162 // and it was loaded, in which case |request| is taken. | |
163 bool LoadWithLoader(const Identity& target, | |
164 mojom::ShellClientRequest* request); | |
165 | |
166 // Returns the appropriate loader for |name|, or the default loader if there | |
167 // is no loader configured for the name. | |
168 Loader* GetLoaderForName(const std::string& name); | |
169 | |
170 void CleanupRunner(NativeRunner* runner); | |
171 | |
172 package_manager::mojom::ShellResolverPtr shell_resolver_; | |
173 | |
174 // Loader management. | |
175 // Loaders are chosen in the order they are listed here. | |
176 NameToLoaderMap name_to_loader_; | |
177 scoped_ptr<Loader> default_loader_; | |
178 | |
179 IdentityToInstanceMap identity_to_instance_; | |
180 | |
181 IdentityToShellClientFactoryMap shell_client_factories_; | |
182 // Counter used to assign ids to content handlers. | |
183 uint32_t shell_client_factory_id_counter_; | |
184 | |
185 // The Instance created by the shell embedder, if any. | |
186 Instance* embedder_instance_ = nullptr; | |
187 | |
188 InterfacePtrSet<mojom::InstanceListener> instance_listeners_; | |
189 | |
190 base::Callback<void(const Identity&)> instance_quit_callback_; | |
191 base::TaskRunner* file_task_runner_; | |
192 scoped_ptr<NativeRunnerFactory> native_runner_factory_; | |
193 std::vector<scoped_ptr<NativeRunner>> native_runners_; | |
194 scoped_ptr<ShellConnection> shell_connection_; | |
195 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; | |
196 | |
197 DISALLOW_COPY_AND_ASSIGN(ApplicationManager); | |
198 }; | |
199 | |
200 mojom::Connector::ConnectCallback EmptyConnectCallback(); | |
201 | |
202 } // namespace shell | |
203 } // namespace mojo | |
204 | |
205 #endif // MOJO_SHELL_APPLICATION_MANAGER_H_ | |
OLD | NEW |