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/runner/android/background_application_loader.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/run_loop.h" | |
11 #include "mojo/shell/application_manager.h" | |
12 | |
13 namespace mojo { | |
14 namespace runner { | |
15 | |
16 BackgroundApplicationLoader::BackgroundApplicationLoader( | |
17 scoped_ptr<ApplicationLoader> real_loader, | |
18 const std::string& thread_name, | |
19 base::MessageLoop::Type message_loop_type) | |
20 : loader_(std::move(real_loader)), | |
21 message_loop_type_(message_loop_type), | |
22 thread_name_(thread_name), | |
23 message_loop_created_(true, false) {} | |
24 | |
25 BackgroundApplicationLoader::~BackgroundApplicationLoader() { | |
26 if (thread_) | |
27 thread_->Join(); | |
28 } | |
29 | |
30 void BackgroundApplicationLoader::Load( | |
31 const GURL& url, | |
32 InterfaceRequest<Application> application_request) { | |
33 DCHECK(application_request.is_pending()); | |
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(&BackgroundApplicationLoader::LoadOnBackgroundThread, | |
50 base::Unretained(this), url, | |
51 base::Passed(&application_request))); | |
52 } | |
53 | |
54 void BackgroundApplicationLoader::Run() { | |
55 base::MessageLoop message_loop(message_loop_type_); | |
56 base::RunLoop loop; | |
57 task_runner_ = message_loop.task_runner(); | |
58 quit_closure_ = loop.QuitClosure(); | |
59 message_loop_created_.Signal(); | |
60 loop.Run(); | |
61 | |
62 // Destroy |loader_| on the thread it's actually used on. | |
63 loader_.reset(); | |
64 } | |
65 | |
66 void BackgroundApplicationLoader::LoadOnBackgroundThread( | |
67 const GURL& url, | |
68 InterfaceRequest<Application> application_request) { | |
69 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
70 loader_->Load(url, std::move(application_request)); | |
71 } | |
72 | |
73 } // namespace runner | |
74 } // namespace mojo | |
OLD | NEW |