| 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 "shell/android/background_application_loader.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "shell/application_manager/application_manager.h" | |
| 10 | |
| 11 namespace shell { | |
| 12 | |
| 13 BackgroundApplicationLoader::BackgroundApplicationLoader( | |
| 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 BackgroundApplicationLoader::~BackgroundApplicationLoader() { | |
| 24 if (thread_) | |
| 25 thread_->Join(); | |
| 26 } | |
| 27 | |
| 28 void BackgroundApplicationLoader::Load( | |
| 29 const GURL& url, | |
| 30 mojo::InterfaceRequest<mojo::Application> application_request) { | |
| 31 DCHECK(application_request.is_pending()); | |
| 32 if (!thread_) { | |
| 33 // TODO(tim): It'd be nice if we could just have each Load call | |
| 34 // result in a new thread like DynamicService{Loader, Runner}. But some | |
| 35 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) | |
| 36 // sharing a delegate (etc). So we have to keep it single threaded, wait | |
| 37 // for the thread to initialize, and post to the TaskRunner for subsequent | |
| 38 // Load calls for now. | |
| 39 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); | |
| 40 thread_->Start(); | |
| 41 message_loop_created_.Wait(); | |
| 42 DCHECK(task_runner_.get()); | |
| 43 } | |
| 44 | |
| 45 task_runner_->PostTask( | |
| 46 FROM_HERE, | |
| 47 base::Bind(&BackgroundApplicationLoader::LoadOnBackgroundThread, | |
| 48 base::Unretained(this), url, | |
| 49 base::Passed(&application_request))); | |
| 50 } | |
| 51 | |
| 52 void BackgroundApplicationLoader::Run() { | |
| 53 base::MessageLoop message_loop(message_loop_type_); | |
| 54 base::RunLoop loop; | |
| 55 task_runner_ = message_loop.task_runner(); | |
| 56 quit_closure_ = loop.QuitClosure(); | |
| 57 message_loop_created_.Signal(); | |
| 58 loop.Run(); | |
| 59 | |
| 60 // Destroy |loader_| on the thread it's actually used on. | |
| 61 loader_.reset(); | |
| 62 } | |
| 63 | |
| 64 void BackgroundApplicationLoader::LoadOnBackgroundThread( | |
| 65 const GURL& url, | |
| 66 mojo::InterfaceRequest<mojo::Application> application_request) { | |
| 67 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 68 loader_->Load(url, application_request.Pass()); | |
| 69 } | |
| 70 | |
| 71 } // namespace shell | |
| OLD | NEW |