| 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/application_manager/application_manager.h" | 5 #include "mojo/application_manager/application_manager.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 void ApplicationManager::Delegate::OnApplicationError(const GURL& url) { | 44 void ApplicationManager::Delegate::OnApplicationError(const GURL& url) { |
| 45 LOG(ERROR) << "Communication error with application: " << url.spec(); | 45 LOG(ERROR) << "Communication error with application: " << url.spec(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 GURL ApplicationManager::Delegate::ResolveURL(const GURL& url) { | 48 GURL ApplicationManager::Delegate::ResolveURL(const GURL& url) { |
| 49 return url; | 49 return url; |
| 50 } | 50 } |
| 51 | 51 |
| 52 | 52 |
| 53 class ApplicationManager::LoadCallbacksImpl | |
| 54 : public ApplicationLoader::LoadCallbacks { | |
| 55 public: | |
| 56 LoadCallbacksImpl(base::WeakPtr<ApplicationManager> manager, | |
| 57 const GURL& requested_url, | |
| 58 const GURL& resolved_url, | |
| 59 const GURL& requestor_url, | |
| 60 ServiceProviderPtr service_provider) | |
| 61 : manager_(manager), | |
| 62 requested_url_(requested_url), | |
| 63 resolved_url_(resolved_url), | |
| 64 requestor_url_(requestor_url), | |
| 65 service_provider_(service_provider.Pass()) {} | |
| 66 | |
| 67 private: | |
| 68 ~LoadCallbacksImpl() override {} | |
| 69 | |
| 70 // LoadCallbacks implementation | |
| 71 ScopedMessagePipeHandle RegisterApplication() override { | |
| 72 ScopedMessagePipeHandle shell_handle; | |
| 73 if (manager_) { | |
| 74 manager_->RegisterLoadedApplication(requested_url_, | |
| 75 resolved_url_, | |
| 76 requestor_url_, | |
| 77 service_provider_.Pass(), | |
| 78 &shell_handle); | |
| 79 } | |
| 80 return shell_handle.Pass(); | |
| 81 } | |
| 82 | |
| 83 void LoadWithContentHandler(const GURL& content_handler_url, | |
| 84 URLResponsePtr url_response) override { | |
| 85 if (manager_) { | |
| 86 manager_->LoadWithContentHandler(requested_url_, | |
| 87 resolved_url_, | |
| 88 requestor_url_, | |
| 89 content_handler_url, | |
| 90 url_response.Pass(), | |
| 91 service_provider_.Pass()); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 base::WeakPtr<ApplicationManager> manager_; | |
| 96 GURL requested_url_; | |
| 97 GURL resolved_url_; | |
| 98 GURL requestor_url_; | |
| 99 ServiceProviderPtr service_provider_; | |
| 100 }; | |
| 101 | |
| 102 class ApplicationManager::ShellImpl : public Shell, public ErrorHandler { | 53 class ApplicationManager::ShellImpl : public Shell, public ErrorHandler { |
| 103 public: | 54 public: |
| 104 ShellImpl(ScopedMessagePipeHandle handle, | 55 ShellImpl(ScopedMessagePipeHandle handle, |
| 105 ApplicationManager* manager, | 56 ApplicationManager* manager, |
| 106 const GURL& requested_url, | 57 const GURL& requested_url, |
| 107 const GURL& url) | 58 const GURL& url) |
| 108 : ShellImpl(manager, requested_url, url) { | 59 : ShellImpl(manager, requested_url, url) { |
| 109 binding_.Bind(handle.Pass()); | 60 binding_.Bind(handle.Pass()); |
| 110 } | 61 } |
| 111 | 62 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 ConnectToApplicationImpl(requested_url, resolved_url, requestor_url, | 194 ConnectToApplicationImpl(requested_url, resolved_url, requestor_url, |
| 244 service_provider.Pass(), loader); | 195 service_provider.Pass(), loader); |
| 245 return; | 196 return; |
| 246 } | 197 } |
| 247 | 198 |
| 248 LOG(WARNING) << "Could not find loader to load application: " | 199 LOG(WARNING) << "Could not find loader to load application: " |
| 249 << requested_url.spec(); | 200 << requested_url.spec(); |
| 250 } | 201 } |
| 251 | 202 |
| 252 void ApplicationManager::ConnectToApplicationImpl( | 203 void ApplicationManager::ConnectToApplicationImpl( |
| 253 const GURL& requested_url, const GURL& resolved_url, | 204 const GURL& requested_url, |
| 254 const GURL& requestor_url, ServiceProviderPtr service_provider, | 205 const GURL& resolved_url, |
| 206 const GURL& requestor_url, |
| 207 ServiceProviderPtr service_provider, |
| 255 ApplicationLoader* loader) { | 208 ApplicationLoader* loader) { |
| 209 ShellImpl* shell = nullptr; |
| 256 URLToShellImplMap::const_iterator shell_it = | 210 URLToShellImplMap::const_iterator shell_it = |
| 257 url_to_shell_impl_.find(resolved_url); | 211 url_to_shell_impl_.find(resolved_url); |
| 258 if (shell_it != url_to_shell_impl_.end()) { | 212 if (shell_it != url_to_shell_impl_.end()) { |
| 259 ConnectToClient(shell_it->second, resolved_url, requestor_url, | 213 shell = shell_it->second; |
| 260 service_provider.Pass()); | 214 } else { |
| 261 return; | 215 MessagePipe pipe; |
| 216 shell = |
| 217 new ShellImpl(pipe.handle0.Pass(), this, requested_url, resolved_url); |
| 218 url_to_shell_impl_[resolved_url] = shell; |
| 219 shell->client()->Initialize(GetArgsForURL(requested_url)); |
| 220 |
| 221 loader->Load(this, resolved_url, pipe.handle1.Pass(), |
| 222 base::Bind(&ApplicationManager::LoadWithContentHandler, |
| 223 weak_ptr_factory_.GetWeakPtr())); |
| 262 } | 224 } |
| 263 | 225 ConnectToClient(shell, resolved_url, requestor_url, service_provider.Pass()); |
| 264 scoped_refptr<LoadCallbacksImpl> callbacks( | |
| 265 new LoadCallbacksImpl(weak_ptr_factory_.GetWeakPtr(), | |
| 266 requested_url, | |
| 267 resolved_url, | |
| 268 requestor_url, | |
| 269 service_provider.Pass())); | |
| 270 loader->Load(this, resolved_url, callbacks); | |
| 271 } | 226 } |
| 272 | 227 |
| 273 void ApplicationManager::ConnectToClient(ShellImpl* shell_impl, | 228 void ApplicationManager::ConnectToClient(ShellImpl* shell_impl, |
| 274 const GURL& url, | 229 const GURL& url, |
| 275 const GURL& requestor_url, | 230 const GURL& requestor_url, |
| 276 ServiceProviderPtr service_provider) { | 231 ServiceProviderPtr service_provider) { |
| 277 if (interceptor_) { | 232 if (interceptor_) { |
| 278 shell_impl->ConnectToClient( | 233 shell_impl->ConnectToClient( |
| 279 requestor_url, | 234 requestor_url, |
| 280 interceptor_->OnConnectToClient(url, service_provider.Pass())); | 235 interceptor_->OnConnectToClient(url, service_provider.Pass())); |
| 281 } else { | 236 } else { |
| 282 shell_impl->ConnectToClient(requestor_url, service_provider.Pass()); | 237 shell_impl->ConnectToClient(requestor_url, service_provider.Pass()); |
| 283 } | 238 } |
| 284 } | 239 } |
| 285 | 240 |
| 286 void ApplicationManager::RegisterExternalApplication( | 241 void ApplicationManager::RegisterExternalApplication( |
| 287 const GURL& url, | 242 const GURL& url, |
| 288 ScopedMessagePipeHandle shell_handle) { | 243 ScopedMessagePipeHandle shell_handle) { |
| 289 ShellImpl* shell_impl = new ShellImpl(shell_handle.Pass(), this, url, url); | 244 ShellImpl* shell_impl = new ShellImpl(shell_handle.Pass(), this, url, url); |
| 290 url_to_shell_impl_[url] = shell_impl; | 245 url_to_shell_impl_[url] = shell_impl; |
| 291 | 246 |
| 292 URLToArgsMap::const_iterator args_it = url_to_args_.find(url); | 247 URLToArgsMap::const_iterator args_it = url_to_args_.find(url); |
| 293 Array<String> args; | 248 Array<String> args; |
| 294 if (args_it != url_to_args_.end()) | 249 if (args_it != url_to_args_.end()) |
| 295 args = Array<String>::From(args_it->second); | 250 args = Array<String>::From(args_it->second); |
| 296 shell_impl->client()->Initialize(args.Pass()); | 251 shell_impl->client()->Initialize(args.Pass()); |
| 297 } | 252 } |
| 298 | 253 |
| 299 void ApplicationManager::RegisterLoadedApplication( | |
| 300 const GURL& requested_url, | |
| 301 const GURL& resolved_url, | |
| 302 const GURL& requestor_url, | |
| 303 ServiceProviderPtr service_provider, | |
| 304 ScopedMessagePipeHandle* shell_handle) { | |
| 305 ShellImpl* shell_impl = NULL; | |
| 306 URLToShellImplMap::iterator iter = url_to_shell_impl_.find(resolved_url); | |
| 307 if (iter != url_to_shell_impl_.end()) { | |
| 308 // This can happen because services are loaded asynchronously. So if we get | |
| 309 // two requests for the same service close to each other, we might get here | |
| 310 // and find that we already have it. | |
| 311 shell_impl = iter->second; | |
| 312 } else { | |
| 313 MessagePipe pipe; | |
| 314 shell_impl = | |
| 315 new ShellImpl(pipe.handle1.Pass(), this, requested_url, resolved_url); | |
| 316 url_to_shell_impl_[resolved_url] = shell_impl; | |
| 317 *shell_handle = pipe.handle0.Pass(); | |
| 318 shell_impl->client()->Initialize(GetArgsForURL(requested_url)); | |
| 319 } | |
| 320 | |
| 321 ConnectToClient(shell_impl, resolved_url, requestor_url, | |
| 322 service_provider.Pass()); | |
| 323 } | |
| 324 | |
| 325 void ApplicationManager::LoadWithContentHandler( | 254 void ApplicationManager::LoadWithContentHandler( |
| 326 const GURL& requested_url, | |
| 327 const GURL& resolved_url, | |
| 328 const GURL& requestor_url, | |
| 329 const GURL& content_handler_url, | 255 const GURL& content_handler_url, |
| 330 URLResponsePtr url_response, | 256 ScopedMessagePipeHandle shell_handle, |
| 331 ServiceProviderPtr service_provider) { | 257 URLResponsePtr url_response) { |
| 332 ContentHandlerConnection* connection = NULL; | 258 ContentHandlerConnection* connection = NULL; |
| 333 URLToContentHandlerMap::iterator iter = | 259 URLToContentHandlerMap::iterator iter = |
| 334 url_to_content_handler_.find(content_handler_url); | 260 url_to_content_handler_.find(content_handler_url); |
| 335 if (iter != url_to_content_handler_.end()) { | 261 if (iter != url_to_content_handler_.end()) { |
| 336 connection = iter->second; | 262 connection = iter->second; |
| 337 } else { | 263 } else { |
| 338 connection = new ContentHandlerConnection(this, content_handler_url); | 264 connection = new ContentHandlerConnection(this, content_handler_url); |
| 339 url_to_content_handler_[content_handler_url] = connection; | 265 url_to_content_handler_[content_handler_url] = connection; |
| 340 } | 266 } |
| 341 | 267 |
| 342 ShellPtr shell_proxy; | 268 connection->content_handler()->StartApplication( |
| 343 ShellImpl* shell_impl = | 269 MakeProxy<Shell>(shell_handle.Pass()), url_response.Pass()); |
| 344 new ShellImpl(&shell_proxy, this, requested_url, resolved_url); | |
| 345 content_shell_impls_.insert(shell_impl); | |
| 346 shell_impl->client()->Initialize(GetArgsForURL(requested_url)); | |
| 347 | |
| 348 connection->content_handler()->StartApplication(shell_proxy.Pass(), | |
| 349 url_response.Pass()); | |
| 350 ConnectToClient( | |
| 351 shell_impl, resolved_url, requestor_url, service_provider.Pass()); | |
| 352 } | 270 } |
| 353 | 271 |
| 354 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, | 272 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, |
| 355 const GURL& url) { | 273 const GURL& url) { |
| 356 URLToLoaderMap::iterator it = url_to_loader_.find(url); | 274 URLToLoaderMap::iterator it = url_to_loader_.find(url); |
| 357 if (it != url_to_loader_.end()) | 275 if (it != url_to_loader_.end()) |
| 358 delete it->second; | 276 delete it->second; |
| 359 url_to_loader_[url] = loader.release(); | 277 url_to_loader_[url] = loader.release(); |
| 360 } | 278 } |
| 361 | 279 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 return pipe.handle0.Pass(); | 353 return pipe.handle0.Pass(); |
| 436 } | 354 } |
| 437 | 355 |
| 438 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) { | 356 Array<String> ApplicationManager::GetArgsForURL(const GURL& url) { |
| 439 URLToArgsMap::const_iterator args_it = url_to_args_.find(url); | 357 URLToArgsMap::const_iterator args_it = url_to_args_.find(url); |
| 440 if (args_it != url_to_args_.end()) | 358 if (args_it != url_to_args_.end()) |
| 441 return Array<String>::From(args_it->second); | 359 return Array<String>::From(args_it->second); |
| 442 return Array<String>(); | 360 return Array<String>(); |
| 443 } | 361 } |
| 444 } // namespace mojo | 362 } // namespace mojo |
| OLD | NEW |