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