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 "mojo/application_manager/background_shell_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 "mojo/application_manager/application_manager.h" | 9 #include "mojo/application_manager/application_manager.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 12 |
13 BackgroundShellApplicationLoader::BackgroundShellApplicationLoader( | 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 BackgroundShellApplicationLoader::~BackgroundShellApplicationLoader() { | 23 BackgroundApplicationLoader::~BackgroundApplicationLoader() { |
24 if (thread_) | 24 if (thread_) |
25 thread_->Join(); | 25 thread_->Join(); |
26 } | 26 } |
27 | 27 |
28 void BackgroundShellApplicationLoader::Load( | 28 void BackgroundApplicationLoader::Load(ApplicationManager* manager, |
29 ApplicationManager* manager, | 29 const GURL& url, |
30 const GURL& url, | 30 ScopedMessagePipeHandle shell_handle, |
31 ScopedMessagePipeHandle shell_handle, | 31 LoadCallback callback) { |
32 LoadCallback callback) { | |
33 DCHECK(shell_handle.is_valid()); | 32 DCHECK(shell_handle.is_valid()); |
34 if (!thread_) { | 33 if (!thread_) { |
35 // 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 |
36 // result in a new thread like DynamicService{Loader, Runner}. But some | 35 // result in a new thread like DynamicService{Loader, Runner}. But some |
37 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) | 36 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) |
38 // 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 |
39 // 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 |
40 // Load calls for now. | 39 // Load calls for now. |
41 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); | 40 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); |
42 thread_->Start(); | 41 thread_->Start(); |
43 message_loop_created_.Wait(); | 42 message_loop_created_.Wait(); |
44 DCHECK(task_runner_.get()); | 43 DCHECK(task_runner_.get()); |
45 } | 44 } |
46 | 45 |
47 task_runner_->PostTask( | 46 task_runner_->PostTask( |
48 FROM_HERE, | 47 FROM_HERE, |
49 base::Bind(&BackgroundShellApplicationLoader::LoadOnBackgroundThread, | 48 base::Bind(&BackgroundApplicationLoader::LoadOnBackgroundThread, |
50 base::Unretained(this), manager, url, | 49 base::Unretained(this), manager, url, |
51 base::Passed(&shell_handle))); | 50 base::Passed(&shell_handle))); |
52 } | 51 } |
53 | 52 |
54 void BackgroundShellApplicationLoader::OnApplicationError( | 53 void BackgroundApplicationLoader::OnApplicationError( |
55 ApplicationManager* manager, | 54 ApplicationManager* manager, |
56 const GURL& url) { | 55 const GURL& url) { |
57 task_runner_->PostTask(FROM_HERE, | 56 task_runner_->PostTask( |
58 base::Bind(&BackgroundShellApplicationLoader:: | 57 FROM_HERE, |
59 OnApplicationErrorOnBackgroundThread, | 58 base::Bind( |
60 base::Unretained(this), | 59 &BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread, |
61 manager, | 60 base::Unretained(this), manager, url)); |
62 url)); | |
63 } | 61 } |
64 | 62 |
65 void BackgroundShellApplicationLoader::Run() { | 63 void BackgroundApplicationLoader::Run() { |
66 base::MessageLoop message_loop(message_loop_type_); | 64 base::MessageLoop message_loop(message_loop_type_); |
67 base::RunLoop loop; | 65 base::RunLoop loop; |
68 task_runner_ = message_loop.task_runner(); | 66 task_runner_ = message_loop.task_runner(); |
69 quit_closure_ = loop.QuitClosure(); | 67 quit_closure_ = loop.QuitClosure(); |
70 message_loop_created_.Signal(); | 68 message_loop_created_.Signal(); |
71 loop.Run(); | 69 loop.Run(); |
72 | 70 |
73 // Destroy |loader_| on the thread it's actually used on. | 71 // Destroy |loader_| on the thread it's actually used on. |
74 loader_.reset(); | 72 loader_.reset(); |
75 } | 73 } |
76 | 74 |
77 void BackgroundShellApplicationLoader::LoadOnBackgroundThread( | 75 void BackgroundApplicationLoader::LoadOnBackgroundThread( |
78 ApplicationManager* manager, | 76 ApplicationManager* manager, |
79 const GURL& url, | 77 const GURL& url, |
80 ScopedMessagePipeHandle shell_handle) { | 78 ScopedMessagePipeHandle shell_handle) { |
81 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 79 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
82 loader_->Load(manager, url, shell_handle.Pass(), SimpleLoadCallback()); | 80 loader_->Load(manager, url, shell_handle.Pass(), SimpleLoadCallback()); |
83 } | 81 } |
84 | 82 |
85 void BackgroundShellApplicationLoader::OnApplicationErrorOnBackgroundThread( | 83 void BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread( |
86 ApplicationManager* manager, | 84 ApplicationManager* manager, |
87 const GURL& url) { | 85 const GURL& url) { |
88 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 86 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
89 loader_->OnApplicationError(manager, url); | 87 loader_->OnApplicationError(manager, url); |
90 } | 88 } |
91 | 89 |
92 } // namespace mojo | 90 } // namespace mojo |
OLD | NEW |