| 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/ui_service_loader_android.h" | 5 #include "mojo/shell/ui_service_loader_android.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "mojo/service_manager/service_manager.h" | 8 #include "mojo/service_manager/service_manager.h" |
| 9 #include "mojo/shell/context.h" | 9 #include "mojo/shell/context.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 | 12 |
| 13 class UIServiceLoader::UILoader { | 13 class UIServiceLoader::UILoader { |
| 14 public: | 14 public: |
| 15 explicit UILoader(ServiceLoader* loader) : loader_(loader) {} | 15 explicit UILoader(ServiceLoader* loader) : loader_(loader) {} |
| 16 ~UILoader() {} | 16 ~UILoader() {} |
| 17 | 17 |
| 18 void LoadService(ServiceManager* manager, | 18 void LoadService(ServiceManager* manager, |
| 19 const GURL& url, | 19 const GURL& url, |
| 20 ScopedMessagePipeHandle shell_handle) { | 20 ScopedMessagePipeHandle shell_handle) { |
| 21 loader_->LoadService(manager, url, shell_handle.Pass()); | 21 scoped_refptr<LoadServiceCallbacks> callbacks( |
| 22 new ServiceLoader::SimpleLoadServiceCallbacks(shell_handle.Pass())); |
| 23 loader_->LoadService(manager, url, callbacks); |
| 22 } | 24 } |
| 23 | 25 |
| 24 void OnServiceError(ServiceManager* manager, const GURL& url) { | 26 void OnServiceError(ServiceManager* manager, const GURL& url) { |
| 25 loader_->OnServiceError(manager, url); | 27 loader_->OnServiceError(manager, url); |
| 26 } | 28 } |
| 27 | 29 |
| 28 private: | 30 private: |
| 29 ServiceLoader* loader_; // Owned by UIServiceLoader | 31 ServiceLoader* loader_; // Owned by UIServiceLoader |
| 30 | 32 |
| 31 DISALLOW_COPY_AND_ASSIGN(UILoader); | 33 DISALLOW_COPY_AND_ASSIGN(UILoader); |
| 32 }; | 34 }; |
| 33 | 35 |
| 34 UIServiceLoader::UIServiceLoader(scoped_ptr<ServiceLoader> real_loader, | 36 UIServiceLoader::UIServiceLoader(scoped_ptr<ServiceLoader> real_loader, |
| 35 shell::Context* context) | 37 shell::Context* context) |
| 36 : loader_(real_loader.Pass()), context_(context) { | 38 : loader_(real_loader.Pass()), context_(context) { |
| 37 } | 39 } |
| 38 | 40 |
| 39 UIServiceLoader::~UIServiceLoader() { | 41 UIServiceLoader::~UIServiceLoader() { |
| 40 context_->ui_loop()->PostTask( | 42 context_->ui_loop()->PostTask( |
| 41 FROM_HERE, | 43 FROM_HERE, |
| 42 base::Bind(&UIServiceLoader::ShutdownOnUIThread, base::Unretained(this))); | 44 base::Bind(&UIServiceLoader::ShutdownOnUIThread, base::Unretained(this))); |
| 43 } | 45 } |
| 44 | 46 |
| 45 void UIServiceLoader::LoadService(ServiceManager* manager, | 47 void UIServiceLoader::LoadService( |
| 46 const GURL& url, | 48 ServiceManager* manager, |
| 47 ScopedMessagePipeHandle shell_handle) { | 49 const GURL& url, |
| 50 scoped_refptr<LoadServiceCallbacks> callbacks) { |
| 51 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 52 if (!shell_handle.is_valid()) { |
| 53 return; |
| 54 } |
| 48 context_->ui_loop()->PostTask( | 55 context_->ui_loop()->PostTask( |
| 49 FROM_HERE, | 56 FROM_HERE, |
| 50 base::Bind( | 57 base::Bind( |
| 51 &UIServiceLoader::LoadServiceOnUIThread, | 58 &UIServiceLoader::LoadServiceOnUIThread, |
| 52 base::Unretained(this), | 59 base::Unretained(this), |
| 53 manager, | 60 manager, |
| 54 url, | 61 url, |
| 55 base::Owned(new ScopedMessagePipeHandle(shell_handle.Pass())))); | 62 shell_handle.Pass())); |
| 56 } | 63 } |
| 57 | 64 |
| 58 void UIServiceLoader::OnServiceError(ServiceManager* manager, const GURL& url) { | 65 void UIServiceLoader::OnServiceError(ServiceManager* manager, const GURL& url) { |
| 59 context_->ui_loop()->PostTask( | 66 context_->ui_loop()->PostTask( |
| 60 FROM_HERE, | 67 FROM_HERE, |
| 61 base::Bind(&UIServiceLoader::OnServiceErrorOnUIThread, | 68 base::Bind(&UIServiceLoader::OnServiceErrorOnUIThread, |
| 62 base::Unretained(this), | 69 base::Unretained(this), |
| 63 manager, | 70 manager, |
| 64 url)); | 71 url)); |
| 65 } | 72 } |
| 66 | 73 |
| 67 void UIServiceLoader::LoadServiceOnUIThread( | 74 void UIServiceLoader::LoadServiceOnUIThread( |
| 68 ServiceManager* manager, | 75 ServiceManager* manager, |
| 69 const GURL& url, | 76 const GURL& url, |
| 70 ScopedMessagePipeHandle* shell_handle) { | 77 ScopedMessagePipeHandle shell_handle) { |
| 71 if (!ui_loader_) | 78 if (!ui_loader_) |
| 72 ui_loader_ = new UILoader(loader_.get()); | 79 ui_loader_ = new UILoader(loader_.get()); |
| 73 ui_loader_->LoadService(manager, url, shell_handle->Pass()); | 80 ui_loader_->LoadService(manager, url, shell_handle.Pass()); |
| 74 } | 81 } |
| 75 | 82 |
| 76 void UIServiceLoader::OnServiceErrorOnUIThread(ServiceManager* manager, | 83 void UIServiceLoader::OnServiceErrorOnUIThread(ServiceManager* manager, |
| 77 const GURL& url) { | 84 const GURL& url) { |
| 78 if (!ui_loader_) | 85 if (!ui_loader_) |
| 79 ui_loader_ = new UILoader(loader_.get()); | 86 ui_loader_ = new UILoader(loader_.get()); |
| 80 ui_loader_->OnServiceError(manager, url); | 87 ui_loader_->OnServiceError(manager, url); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void UIServiceLoader::ShutdownOnUIThread() { | 90 void UIServiceLoader::ShutdownOnUIThread() { |
| 84 delete ui_loader_; | 91 delete ui_loader_; |
| 85 // Destroy |loader_| on the thread it's actually used on. | 92 // Destroy |loader_| on the thread it's actually used on. |
| 86 loader_.reset(); | 93 loader_.reset(); |
| 87 } | 94 } |
| 88 | 95 |
| 89 } // namespace mojo | 96 } // namespace mojo |
| OLD | NEW |