| 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/background_shell_application_loader.h" | 5 #include "mojo/application_manager/background_shell_application_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "mojo/application_manager/application_manager.h" | 9 #include "mojo/application_manager/application_manager.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 | 12 |
| 13 class BackgroundShellApplicationLoader::BackgroundLoader { | |
| 14 public: | |
| 15 explicit BackgroundLoader(ApplicationLoader* loader) : loader_(loader) {} | |
| 16 ~BackgroundLoader() {} | |
| 17 | |
| 18 void Load(ApplicationManager* manager, | |
| 19 const GURL& url, | |
| 20 ScopedMessagePipeHandle shell_handle) { | |
| 21 scoped_refptr<LoadCallbacks> callbacks( | |
| 22 new ApplicationLoader::SimpleLoadCallbacks(shell_handle.Pass())); | |
| 23 loader_->Load(manager, url, callbacks); | |
| 24 } | |
| 25 | |
| 26 void OnApplicationError(ApplicationManager* manager, const GURL& url) { | |
| 27 loader_->OnApplicationError(manager, url); | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 ApplicationLoader* loader_; // Owned by BackgroundShellApplicationLoader | |
| 32 | |
| 33 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader); | |
| 34 }; | |
| 35 | |
| 36 BackgroundShellApplicationLoader::BackgroundShellApplicationLoader( | 13 BackgroundShellApplicationLoader::BackgroundShellApplicationLoader( |
| 37 scoped_ptr<ApplicationLoader> real_loader, | 14 scoped_ptr<ApplicationLoader> real_loader, |
| 38 const std::string& thread_name, | 15 const std::string& thread_name, |
| 39 base::MessageLoop::Type message_loop_type) | 16 base::MessageLoop::Type message_loop_type) |
| 40 : loader_(real_loader.Pass()), | 17 : loader_(real_loader.Pass()), |
| 41 message_loop_type_(message_loop_type), | 18 message_loop_type_(message_loop_type), |
| 42 thread_name_(thread_name), | 19 thread_name_(thread_name), |
| 43 message_loop_created_(true, false), | 20 message_loop_created_(true, false) { |
| 44 background_loader_(NULL) { | |
| 45 } | 21 } |
| 46 | 22 |
| 47 BackgroundShellApplicationLoader::~BackgroundShellApplicationLoader() { | 23 BackgroundShellApplicationLoader::~BackgroundShellApplicationLoader() { |
| 48 if (thread_) | 24 if (thread_) |
| 49 thread_->Join(); | 25 thread_->Join(); |
| 50 } | 26 } |
| 51 | 27 |
| 52 void BackgroundShellApplicationLoader::Load( | 28 void BackgroundShellApplicationLoader::Load( |
| 53 ApplicationManager* manager, | 29 ApplicationManager* manager, |
| 54 const GURL& url, | 30 const GURL& url, |
| 55 scoped_refptr<LoadCallbacks> callbacks) { | 31 ScopedMessagePipeHandle shell_handle, |
| 56 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); | 32 LoadCallback callback) { |
| 57 if (!shell_handle.is_valid()) | 33 DCHECK(shell_handle.is_valid()); |
| 58 return; | |
| 59 | |
| 60 if (!thread_) { | 34 if (!thread_) { |
| 61 // TODO(tim): It'd be nice if we could just have each Load call | 35 // TODO(tim): It'd be nice if we could just have each Load call |
| 62 // result in a new thread like DynamicService{Loader, Runner}. But some | 36 // result in a new thread like DynamicService{Loader, Runner}. But some |
| 63 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) | 37 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) |
| 64 // sharing a delegate (etc). So we have to keep it single threaded, wait | 38 // sharing a delegate (etc). So we have to keep it single threaded, wait |
| 65 // for the thread to initialize, and post to the TaskRunner for subsequent | 39 // for the thread to initialize, and post to the TaskRunner for subsequent |
| 66 // Load calls for now. | 40 // Load calls for now. |
| 67 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); | 41 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); |
| 68 thread_->Start(); | 42 thread_->Start(); |
| 69 message_loop_created_.Wait(); | 43 message_loop_created_.Wait(); |
| 70 DCHECK(task_runner_.get()); | 44 DCHECK(task_runner_.get()); |
| 71 } | 45 } |
| 72 | 46 |
| 73 task_runner_->PostTask( | 47 task_runner_->PostTask( |
| 74 FROM_HERE, | 48 FROM_HERE, |
| 75 base::Bind( | 49 base::Bind(&BackgroundShellApplicationLoader::LoadOnBackgroundThread, |
| 76 &BackgroundShellApplicationLoader::LoadOnBackgroundThread, | 50 base::Unretained(this), manager, url, |
| 77 base::Unretained(this), | 51 base::Passed(&shell_handle))); |
| 78 manager, | |
| 79 url, | |
| 80 base::Owned(new ScopedMessagePipeHandle(shell_handle.Pass())))); | |
| 81 } | 52 } |
| 82 | 53 |
| 83 void BackgroundShellApplicationLoader::OnApplicationError( | 54 void BackgroundShellApplicationLoader::OnApplicationError( |
| 84 ApplicationManager* manager, | 55 ApplicationManager* manager, |
| 85 const GURL& url) { | 56 const GURL& url) { |
| 86 task_runner_->PostTask(FROM_HERE, | 57 task_runner_->PostTask(FROM_HERE, |
| 87 base::Bind(&BackgroundShellApplicationLoader:: | 58 base::Bind(&BackgroundShellApplicationLoader:: |
| 88 OnApplicationErrorOnBackgroundThread, | 59 OnApplicationErrorOnBackgroundThread, |
| 89 base::Unretained(this), | 60 base::Unretained(this), |
| 90 manager, | 61 manager, |
| 91 url)); | 62 url)); |
| 92 } | 63 } |
| 93 | 64 |
| 94 void BackgroundShellApplicationLoader::Run() { | 65 void BackgroundShellApplicationLoader::Run() { |
| 95 base::MessageLoop message_loop(message_loop_type_); | 66 base::MessageLoop message_loop(message_loop_type_); |
| 96 base::RunLoop loop; | 67 base::RunLoop loop; |
| 97 task_runner_ = message_loop.task_runner(); | 68 task_runner_ = message_loop.task_runner(); |
| 98 quit_closure_ = loop.QuitClosure(); | 69 quit_closure_ = loop.QuitClosure(); |
| 99 message_loop_created_.Signal(); | 70 message_loop_created_.Signal(); |
| 100 loop.Run(); | 71 loop.Run(); |
| 101 | 72 |
| 102 delete background_loader_; | |
| 103 background_loader_ = NULL; | |
| 104 // Destroy |loader_| on the thread it's actually used on. | 73 // Destroy |loader_| on the thread it's actually used on. |
| 105 loader_.reset(); | 74 loader_.reset(); |
| 106 } | 75 } |
| 107 | 76 |
| 108 void BackgroundShellApplicationLoader::LoadOnBackgroundThread( | 77 void BackgroundShellApplicationLoader::LoadOnBackgroundThread( |
| 109 ApplicationManager* manager, | 78 ApplicationManager* manager, |
| 110 const GURL& url, | 79 const GURL& url, |
| 111 ScopedMessagePipeHandle* shell_handle) { | 80 ScopedMessagePipeHandle shell_handle) { |
| 112 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 81 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 113 if (!background_loader_) | 82 loader_->Load(manager, url, shell_handle.Pass(), SimpleLoadCallback()); |
| 114 background_loader_ = new BackgroundLoader(loader_.get()); | |
| 115 background_loader_->Load(manager, url, shell_handle->Pass()); | |
| 116 } | 83 } |
| 117 | 84 |
| 118 void BackgroundShellApplicationLoader::OnApplicationErrorOnBackgroundThread( | 85 void BackgroundShellApplicationLoader::OnApplicationErrorOnBackgroundThread( |
| 119 ApplicationManager* manager, | 86 ApplicationManager* manager, |
| 120 const GURL& url) { | 87 const GURL& url) { |
| 121 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 88 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 122 if (!background_loader_) | 89 loader_->OnApplicationError(manager, url); |
| 123 background_loader_ = new BackgroundLoader(loader_.get()); | |
| 124 background_loader_->OnApplicationError(manager, url); | |
| 125 } | 90 } |
| 126 | 91 |
| 127 } // namespace mojo | 92 } // namespace mojo |
| OLD | NEW |