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