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 #include "mojo/shell/application_manager.h" | 5 #include "mojo/shell/application_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 InterfaceRequest<mojom::PIDReceiver> pid_receiver) { | 133 InterfaceRequest<mojom::PIDReceiver> pid_receiver) { |
134 // Instances created by others are considered unique, and thus have no | 134 // Instances created by others are considered unique, and thus have no |
135 // identity. As such they cannot be connected to by anyone else, and so we | 135 // identity. As such they cannot be connected to by anyone else, and so we |
136 // never call ConnectToClient(). | 136 // never call ConnectToClient(). |
137 // TODO(beng): GetPermissiveCapabilityFilter() here obviously cannot make it | 137 // TODO(beng): GetPermissiveCapabilityFilter() here obviously cannot make it |
138 // to production. See note in application_manager.mojom. | 138 // to production. See note in application_manager.mojom. |
139 // http://crbug.com/555392 | 139 // http://crbug.com/555392 |
140 CapabilityFilter local_filter = filter->filter.To<CapabilityFilter>(); | 140 CapabilityFilter local_filter = filter->filter.To<CapabilityFilter>(); |
141 Identity target_id(url, std::string(), local_filter); | 141 Identity target_id(url, std::string(), local_filter); |
142 ApplicationInstance* instance = nullptr; | 142 ApplicationInstance* instance = nullptr; |
143 InterfaceRequest<mojom::Application> application_request = | 143 InterfaceRequest<mojom::ShellClient> request = |
144 CreateInstance(target_id, base::Closure(), &instance); | 144 CreateInstance(target_id, base::Closure(), &instance); |
145 instance->BindPIDReceiver(std::move(pid_receiver)); | 145 instance->BindPIDReceiver(std::move(pid_receiver)); |
146 scoped_ptr<NativeRunner> runner = | 146 scoped_ptr<NativeRunner> runner = |
147 native_runner_factory_->Create(base::FilePath()); | 147 native_runner_factory_->Create(base::FilePath()); |
148 runner->InitHost(std::move(channel), std::move(application_request)); | 148 runner->InitHost(std::move(channel), std::move(request)); |
149 instance->SetNativeRunner(runner.get()); | 149 instance->SetNativeRunner(runner.get()); |
150 native_runners_.push_back(std::move(runner)); | 150 native_runners_.push_back(std::move(runner)); |
151 } | 151 } |
152 | 152 |
153 void ApplicationManager::AddListener( | 153 void ApplicationManager::AddListener( |
154 mojom::ApplicationManagerListenerPtr listener) { | 154 mojom::ApplicationManagerListenerPtr listener) { |
155 Array<mojom::ApplicationInfoPtr> applications; | 155 Array<mojom::ApplicationInfoPtr> applications; |
156 for (auto& entry : identity_to_instance_) | 156 for (auto& entry : identity_to_instance_) |
157 applications.push_back(CreateApplicationInfoForInstance(entry.second)); | 157 applications.push_back(CreateApplicationInfoForInstance(entry.second)); |
158 listener->SetRunningApplications(std::move(applications)); | 158 listener->SetRunningApplications(std::move(applications)); |
159 | 159 |
160 listeners_.AddInterfacePtr(std::move(listener)); | 160 listeners_.AddInterfacePtr(std::move(listener)); |
161 } | 161 } |
162 | 162 |
163 void ApplicationManager::ApplicationPIDAvailable( | 163 void ApplicationManager::ApplicationPIDAvailable( |
164 uint32_t id, | 164 uint32_t id, |
165 base::ProcessId pid) { | 165 base::ProcessId pid) { |
166 for (auto& instance : identity_to_instance_) { | 166 for (auto& instance : identity_to_instance_) { |
167 if (instance.second->id() == id) { | 167 if (instance.second->id() == id) { |
168 instance.second->set_pid(pid); | 168 instance.second->set_pid(pid); |
169 break; | 169 break; |
170 } | 170 } |
171 } | 171 } |
172 listeners_.ForAllPtrs( | 172 listeners_.ForAllPtrs( |
173 [this, id, pid](mojom::ApplicationManagerListener* listener) { | 173 [this, id, pid](mojom::ApplicationManagerListener* listener) { |
174 listener->ApplicationPIDAvailable(id, pid); | 174 listener->ApplicationPIDAvailable(id, pid); |
175 }); | 175 }); |
176 } | 176 } |
177 | 177 |
178 InterfaceRequest<mojom::Application> | 178 InterfaceRequest<mojom::ShellClient> |
179 ApplicationManager::CreateAndConnectToInstance( | 179 ApplicationManager::CreateAndConnectToInstance( |
180 scoped_ptr<ConnectToApplicationParams> params, | 180 scoped_ptr<ConnectToApplicationParams> params, |
181 ApplicationInstance** resulting_instance) { | 181 ApplicationInstance** resulting_instance) { |
182 ApplicationInstance* instance = nullptr; | 182 ApplicationInstance* instance = nullptr; |
183 InterfaceRequest<mojom::Application> application_request = | 183 InterfaceRequest<mojom::ShellClient> request = |
184 CreateInstance(params->target(), params->on_application_end(), &instance); | 184 CreateInstance(params->target(), params->on_application_end(), &instance); |
185 instance->ConnectToClient(std::move(params)); | 185 instance->ConnectToClient(std::move(params)); |
186 if (resulting_instance) | 186 if (resulting_instance) |
187 *resulting_instance = instance; | 187 *resulting_instance = instance; |
188 return application_request; | 188 return request; |
189 } | 189 } |
190 | 190 |
191 InterfaceRequest<mojom::Application> ApplicationManager::CreateInstance( | 191 InterfaceRequest<mojom::ShellClient> ApplicationManager::CreateInstance( |
192 const Identity& target_id, | 192 const Identity& target_id, |
193 const base::Closure& on_application_end, | 193 const base::Closure& on_application_end, |
194 ApplicationInstance** resulting_instance) { | 194 ApplicationInstance** resulting_instance) { |
195 mojom::ApplicationPtr application; | 195 mojom::ShellClientPtr shell_client; |
196 InterfaceRequest<mojom::Application> application_request = | 196 InterfaceRequest<mojom::ShellClient> request = GetProxy(&shell_client); |
197 GetProxy(&application); | |
198 ApplicationInstance* instance = new ApplicationInstance( | 197 ApplicationInstance* instance = new ApplicationInstance( |
199 std::move(application), this, target_id, | 198 std::move(shell_client), this, target_id, |
200 mojom::Shell::kInvalidApplicationID, on_application_end); | 199 mojom::Shell::kInvalidApplicationID, on_application_end); |
201 DCHECK(identity_to_instance_.find(target_id) == | 200 DCHECK(identity_to_instance_.find(target_id) == |
202 identity_to_instance_.end()); | 201 identity_to_instance_.end()); |
203 identity_to_instance_[target_id] = instance; | 202 identity_to_instance_[target_id] = instance; |
204 mojom::ApplicationInfoPtr application_info = | 203 mojom::ApplicationInfoPtr application_info = |
205 CreateApplicationInfoForInstance(instance); | 204 CreateApplicationInfoForInstance(instance); |
206 listeners_.ForAllPtrs( | 205 listeners_.ForAllPtrs( |
207 [this, &application_info](mojom::ApplicationManagerListener* listener) { | 206 [this, &application_info](mojom::ApplicationManagerListener* listener) { |
208 listener->ApplicationInstanceCreated(application_info.Clone()); | 207 listener->ApplicationInstanceCreated(application_info.Clone()); |
209 }); | 208 }); |
210 instance->InitializeApplication(); | 209 instance->InitializeApplication(); |
211 if (resulting_instance) | 210 if (resulting_instance) |
212 *resulting_instance = instance; | 211 *resulting_instance = instance; |
213 return application_request; | 212 return request; |
214 } | 213 } |
215 | 214 |
216 void ApplicationManager::HandleFetchCallback( | 215 void ApplicationManager::HandleFetchCallback( |
217 scoped_ptr<ConnectToApplicationParams> params, | 216 scoped_ptr<ConnectToApplicationParams> params, |
218 scoped_ptr<Fetcher> fetcher) { | 217 scoped_ptr<Fetcher> fetcher) { |
219 if (!fetcher) { | 218 if (!fetcher) { |
220 // Network error. Drop |params| to tell the requestor. | 219 // Network error. Drop |params| to tell the requestor. |
221 params->connect_callback().Run(mojom::Shell::kInvalidApplicationID, | 220 params->connect_callback().Run(mojom::Shell::kInvalidApplicationID, |
222 mojom::Shell::kInvalidApplicationID); | 221 mojom::Shell::kInvalidApplicationID); |
223 return; | 222 return; |
(...skipping 19 matching lines...) Expand all Loading... |
243 // have two copies of the app running, so check again. | 242 // have two copies of the app running, so check again. |
244 if (ConnectToRunningApplication(¶ms)) | 243 if (ConnectToRunningApplication(¶ms)) |
245 return; | 244 return; |
246 | 245 |
247 Identity source = params->source(); | 246 Identity source = params->source(); |
248 Identity target = params->target(); | 247 Identity target = params->target(); |
249 mojom::Shell::ConnectToApplicationCallback connect_callback = | 248 mojom::Shell::ConnectToApplicationCallback connect_callback = |
250 params->connect_callback(); | 249 params->connect_callback(); |
251 params->set_connect_callback(EmptyConnectCallback()); | 250 params->set_connect_callback(EmptyConnectCallback()); |
252 ApplicationInstance* app = nullptr; | 251 ApplicationInstance* app = nullptr; |
253 InterfaceRequest<mojom::Application> request( | 252 InterfaceRequest<mojom::ShellClient> request( |
254 CreateAndConnectToInstance(std::move(params), &app)); | 253 CreateAndConnectToInstance(std::move(params), &app)); |
255 | 254 |
256 uint32_t content_handler_id = package_manager_->HandleWithContentHandler( | 255 uint32_t content_handler_id = package_manager_->HandleWithContentHandler( |
257 fetcher.get(), source, target.url(), target.filter(), &request); | 256 fetcher.get(), source, target.url(), target.filter(), &request); |
258 if (content_handler_id != mojom::Shell::kInvalidApplicationID) { | 257 if (content_handler_id != mojom::Shell::kInvalidApplicationID) { |
259 app->set_requesting_content_handler_id(content_handler_id); | 258 app->set_requesting_content_handler_id(content_handler_id); |
260 connect_callback.Run(app->id(), content_handler_id); | 259 connect_callback.Run(app->id(), content_handler_id); |
261 return; | 260 return; |
262 } | 261 } |
263 | 262 |
(...skipping 11 matching lines...) Expand all Loading... |
275 | 274 |
276 fetcher->AsPath( | 275 fetcher->AsPath( |
277 task_runner_, | 276 task_runner_, |
278 base::Bind(&ApplicationManager::RunNativeApplication, | 277 base::Bind(&ApplicationManager::RunNativeApplication, |
279 weak_ptr_factory_.GetWeakPtr(), | 278 weak_ptr_factory_.GetWeakPtr(), |
280 base::Passed(std::move(request)), start_sandboxed, | 279 base::Passed(std::move(request)), start_sandboxed, |
281 base::Passed(std::move(fetcher)), base::Unretained(app))); | 280 base::Passed(std::move(fetcher)), base::Unretained(app))); |
282 } | 281 } |
283 | 282 |
284 void ApplicationManager::RunNativeApplication( | 283 void ApplicationManager::RunNativeApplication( |
285 InterfaceRequest<mojom::Application> application_request, | 284 InterfaceRequest<mojom::ShellClient> request, |
286 bool start_sandboxed, | 285 bool start_sandboxed, |
287 scoped_ptr<Fetcher> fetcher, | 286 scoped_ptr<Fetcher> fetcher, |
288 ApplicationInstance* instance, | 287 ApplicationInstance* instance, |
289 const base::FilePath& path, | 288 const base::FilePath& path, |
290 bool path_exists) { | 289 bool path_exists) { |
291 // We only passed fetcher to keep it alive. Done with it now. | 290 // We only passed fetcher to keep it alive. Done with it now. |
292 fetcher.reset(); | 291 fetcher.reset(); |
293 | 292 |
294 DCHECK(application_request.is_pending()); | 293 DCHECK(request.is_pending()); |
295 | 294 |
296 if (!path_exists) { | 295 if (!path_exists) { |
297 LOG(ERROR) << "Library not started because library path '" << path.value() | 296 LOG(ERROR) << "Library not started because library path '" << path.value() |
298 << "' does not exist."; | 297 << "' does not exist."; |
299 return; | 298 return; |
300 } | 299 } |
301 | 300 |
302 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", | 301 TRACE_EVENT1("mojo_shell", "ApplicationManager::RunNativeApplication", "path", |
303 path.AsUTF8Unsafe()); | 302 path.AsUTF8Unsafe()); |
304 scoped_ptr<NativeRunner> runner = native_runner_factory_->Create(path); | 303 scoped_ptr<NativeRunner> runner = native_runner_factory_->Create(path); |
305 runner->Start(path, start_sandboxed, std::move(application_request), | 304 runner->Start(path, start_sandboxed, std::move(request), |
306 base::Bind(&ApplicationManager::ApplicationPIDAvailable, | 305 base::Bind(&ApplicationManager::ApplicationPIDAvailable, |
307 weak_ptr_factory_.GetWeakPtr(), instance->id()), | 306 weak_ptr_factory_.GetWeakPtr(), instance->id()), |
308 base::Bind(&ApplicationManager::CleanupRunner, | 307 base::Bind(&ApplicationManager::CleanupRunner, |
309 weak_ptr_factory_.GetWeakPtr(), runner.get())); | 308 weak_ptr_factory_.GetWeakPtr(), runner.get())); |
310 instance->SetNativeRunner(runner.get()); | 309 instance->SetNativeRunner(runner.get()); |
311 native_runners_.push_back(std::move(runner)); | 310 native_runners_.push_back(std::move(runner)); |
312 } | 311 } |
313 | 312 |
314 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, | 313 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, |
315 const GURL& url) { | 314 const GURL& url) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 } | 367 } |
369 } | 368 } |
370 } | 369 } |
371 | 370 |
372 mojom::Shell::ConnectToApplicationCallback EmptyConnectCallback() { | 371 mojom::Shell::ConnectToApplicationCallback EmptyConnectCallback() { |
373 return base::Bind(&OnEmptyOnConnectCallback); | 372 return base::Bind(&OnEmptyOnConnectCallback); |
374 } | 373 } |
375 | 374 |
376 } // namespace shell | 375 } // namespace shell |
377 } // namespace mojo | 376 } // namespace mojo |
OLD | NEW |