OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/common/mojo/embedded_application_runner.h" | 5 #include "content/common/mojo/embedded_application_runner.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 DCHECK(application_task_runner_); | 46 DCHECK(application_task_runner_); |
47 application_task_runner_->PostTask( | 47 application_task_runner_->PostTask( |
48 FROM_HERE, | 48 FROM_HERE, |
49 base::Bind(&Instance::BindServiceRequestOnApplicationThread, this, | 49 base::Bind(&Instance::BindServiceRequestOnApplicationThread, this, |
50 base::Passed(&request))); | 50 base::Passed(&request))); |
51 } | 51 } |
52 | 52 |
53 void ShutDown() { | 53 void ShutDown() { |
54 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 54 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
55 if (thread_) { | 55 if (!application_task_runner_) |
56 thread_.reset(); | 56 return; |
57 application_task_runner_ = nullptr; | 57 // Any extant ServiceContexts must be destroyed on the application thread. |
| 58 if (application_task_runner_->BelongsToCurrentThread()) { |
| 59 Quit(); |
| 60 } else { |
| 61 application_task_runner_->PostTask(FROM_HERE, |
| 62 base::Bind(&Instance::Quit, this)); |
58 } | 63 } |
59 } | 64 } |
60 | 65 |
61 private: | 66 private: |
| 67 friend class base::RefCountedThreadSafe<Instance>; |
| 68 |
| 69 ~Instance() { |
| 70 // If this instance had its own thread, it MUST be explicitly destroyed by |
| 71 // QuitOnRunnerThread() by the time this destructor is run. |
| 72 DCHECK(!thread_); |
| 73 } |
| 74 |
62 void BindServiceRequestOnApplicationThread( | 75 void BindServiceRequestOnApplicationThread( |
63 shell::mojom::ServiceRequest request) { | 76 shell::mojom::ServiceRequest request) { |
64 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 77 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
65 | 78 |
66 if (!service_) { | 79 if (!service_) { |
67 service_ = factory_callback_.Run( | 80 service_ = factory_callback_.Run( |
68 base::Bind(&Instance::Quit, base::Unretained(this))); | 81 base::Bind(&Instance::Quit, base::Unretained(this))); |
69 } | 82 } |
70 | 83 |
71 shell::ServiceContext* new_connection = | 84 shell::ServiceContext* new_connection = |
72 new shell::ServiceContext(service_.get(), std::move(request)); | 85 new shell::ServiceContext(service_.get(), std::move(request)); |
73 shell_connections_.push_back(base::WrapUnique(new_connection)); | 86 shell_connections_.push_back(base::WrapUnique(new_connection)); |
74 new_connection->SetConnectionLostClosure( | 87 new_connection->SetConnectionLostClosure( |
75 base::Bind(&Instance::OnStop, base::Unretained(this), | 88 base::Bind(&Instance::OnStop, base::Unretained(this), |
76 new_connection)); | 89 new_connection)); |
77 } | 90 } |
78 | 91 |
79 private: | |
80 friend class base::RefCountedThreadSafe<Instance>; | |
81 | |
82 ~Instance() { | |
83 // If this instance had its own thread, it MUST be explicitly destroyed by | |
84 // ShutDown() on the runner's thread by the time this destructor is run. | |
85 DCHECK(!thread_); | |
86 } | |
87 | |
88 void OnStop(shell::ServiceContext* connection) { | 92 void OnStop(shell::ServiceContext* connection) { |
89 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 93 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
90 | 94 |
91 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); | 95 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
92 ++it) { | 96 ++it) { |
93 if (it->get() == connection) { | 97 if (it->get() == connection) { |
94 shell_connections_.erase(it); | 98 shell_connections_.erase(it); |
95 break; | 99 break; |
96 } | 100 } |
97 } | 101 } |
98 } | 102 } |
99 | 103 |
100 void Quit() { | 104 void Quit() { |
101 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 105 DCHECK(application_task_runner_->BelongsToCurrentThread()); |
102 | 106 |
103 shell_connections_.clear(); | 107 shell_connections_.clear(); |
104 service_.reset(); | 108 service_.reset(); |
105 quit_task_runner_->PostTask( | 109 if (quit_task_runner_->BelongsToCurrentThread()) { |
106 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); | 110 QuitOnRunnerThread(); |
| 111 } else { |
| 112 quit_task_runner_->PostTask( |
| 113 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); |
| 114 } |
107 } | 115 } |
108 | 116 |
109 void QuitOnRunnerThread() { | 117 void QuitOnRunnerThread() { |
110 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 118 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
111 ShutDown(); | 119 if (thread_) { |
| 120 thread_.reset(); |
| 121 application_task_runner_ = nullptr; |
| 122 } |
112 quit_closure_.Run(); | 123 quit_closure_.Run(); |
113 } | 124 } |
114 | 125 |
115 const std::string name_; | 126 const std::string name_; |
116 const MojoApplicationInfo::ApplicationFactory factory_callback_; | 127 const MojoApplicationInfo::ApplicationFactory factory_callback_; |
117 const bool use_own_thread_; | 128 const bool use_own_thread_; |
118 const base::Closure quit_closure_; | 129 const base::Closure quit_closure_; |
119 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
120 | 131 |
121 // Thread checker used to ensure certain operations happen only on the | 132 // Thread checker used to ensure certain operations happen only on the |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 const base::Closure& quit_closure) { | 168 const base::Closure& quit_closure) { |
158 quit_closure_ = quit_closure; | 169 quit_closure_ = quit_closure; |
159 } | 170 } |
160 | 171 |
161 void EmbeddedApplicationRunner::OnQuit() { | 172 void EmbeddedApplicationRunner::OnQuit() { |
162 if (!quit_closure_.is_null()) | 173 if (!quit_closure_.is_null()) |
163 quit_closure_.Run(); | 174 quit_closure_.Run(); |
164 } | 175 } |
165 | 176 |
166 } // namespace content | 177 } // namespace content |
OLD | NEW |