| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/application_manager/background_shell_application_loader.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "mojo/application_manager/application_manager.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 | |
| 13 BackgroundShellApplicationLoader::BackgroundShellApplicationLoader( | |
| 14 scoped_ptr<ApplicationLoader> real_loader, | |
| 15 const std::string& thread_name, | |
| 16 base::MessageLoop::Type message_loop_type) | |
| 17 : loader_(real_loader.Pass()), | |
| 18 message_loop_type_(message_loop_type), | |
| 19 thread_name_(thread_name), | |
| 20 message_loop_created_(true, false) { | |
| 21 } | |
| 22 | |
| 23 BackgroundShellApplicationLoader::~BackgroundShellApplicationLoader() { | |
| 24 if (thread_) | |
| 25 thread_->Join(); | |
| 26 } | |
| 27 | |
| 28 void BackgroundShellApplicationLoader::Load( | |
| 29 ApplicationManager* manager, | |
| 30 const GURL& url, | |
| 31 ScopedMessagePipeHandle shell_handle, | |
| 32 LoadCallback callback) { | |
| 33 DCHECK(shell_handle.is_valid()); | |
| 34 if (!thread_) { | |
| 35 // TODO(tim): It'd be nice if we could just have each Load call | |
| 36 // result in a new thread like DynamicService{Loader, Runner}. But some | |
| 37 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) | |
| 38 // sharing a delegate (etc). So we have to keep it single threaded, wait | |
| 39 // for the thread to initialize, and post to the TaskRunner for subsequent | |
| 40 // Load calls for now. | |
| 41 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); | |
| 42 thread_->Start(); | |
| 43 message_loop_created_.Wait(); | |
| 44 DCHECK(task_runner_.get()); | |
| 45 } | |
| 46 | |
| 47 task_runner_->PostTask( | |
| 48 FROM_HERE, | |
| 49 base::Bind(&BackgroundShellApplicationLoader::LoadOnBackgroundThread, | |
| 50 base::Unretained(this), manager, url, | |
| 51 base::Passed(&shell_handle))); | |
| 52 } | |
| 53 | |
| 54 void BackgroundShellApplicationLoader::OnApplicationError( | |
| 55 ApplicationManager* manager, | |
| 56 const GURL& url) { | |
| 57 task_runner_->PostTask(FROM_HERE, | |
| 58 base::Bind(&BackgroundShellApplicationLoader:: | |
| 59 OnApplicationErrorOnBackgroundThread, | |
| 60 base::Unretained(this), | |
| 61 manager, | |
| 62 url)); | |
| 63 } | |
| 64 | |
| 65 void BackgroundShellApplicationLoader::Run() { | |
| 66 base::MessageLoop message_loop(message_loop_type_); | |
| 67 base::RunLoop loop; | |
| 68 task_runner_ = message_loop.task_runner(); | |
| 69 quit_closure_ = loop.QuitClosure(); | |
| 70 message_loop_created_.Signal(); | |
| 71 loop.Run(); | |
| 72 | |
| 73 // Destroy |loader_| on the thread it's actually used on. | |
| 74 loader_.reset(); | |
| 75 } | |
| 76 | |
| 77 void BackgroundShellApplicationLoader::LoadOnBackgroundThread( | |
| 78 ApplicationManager* manager, | |
| 79 const GURL& url, | |
| 80 ScopedMessagePipeHandle shell_handle) { | |
| 81 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 82 loader_->Load(manager, url, shell_handle.Pass(), SimpleLoadCallback()); | |
| 83 } | |
| 84 | |
| 85 void BackgroundShellApplicationLoader::OnApplicationErrorOnBackgroundThread( | |
| 86 ApplicationManager* manager, | |
| 87 const GURL& url) { | |
| 88 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 89 loader_->OnApplicationError(manager, url); | |
| 90 } | |
| 91 | |
| 92 } // namespace mojo | |
| OLD | NEW |