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