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/service_manager/embedded_service_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" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "services/shell/public/cpp/service_context.h" | 16 #include "services/shell/public/cpp/service_context.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 class EmbeddedApplicationRunner::Instance | 20 class EmbeddedServiceRunner::Instance |
21 : public base::RefCountedThreadSafe<Instance> { | 21 : public base::RefCountedThreadSafe<Instance> { |
22 public: | 22 public: |
23 Instance(const base::StringPiece& name, | 23 Instance(const base::StringPiece& name, |
24 const MojoApplicationInfo& info, | 24 const ServiceInfo& info, |
25 const base::Closure& quit_closure) | 25 const base::Closure& quit_closure) |
26 : name_(name.as_string()), | 26 : name_(name.as_string()), |
27 factory_callback_(info.application_factory), | 27 factory_callback_(info.factory), |
28 use_own_thread_(!info.application_task_runner && info.use_own_thread), | 28 use_own_thread_(!info.task_runner && info.use_own_thread), |
29 quit_closure_(quit_closure), | 29 quit_closure_(quit_closure), |
30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
31 application_task_runner_(info.application_task_runner) { | 31 task_runner_(info.task_runner) { |
32 if (!use_own_thread_ && !application_task_runner_) | 32 if (!use_own_thread_ && !task_runner_) |
33 application_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 33 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
34 } | 34 } |
35 | 35 |
36 void BindServiceRequest(shell::mojom::ServiceRequest request) { | 36 void BindServiceRequest(shell::mojom::ServiceRequest request) { |
37 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 37 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
38 | 38 |
39 if (use_own_thread_ && !thread_) { | 39 if (use_own_thread_ && !thread_) { |
40 // Start a new thread if necessary. | 40 // Start a new thread if necessary. |
41 thread_.reset(new base::Thread(name_)); | 41 thread_.reset(new base::Thread(name_)); |
42 thread_->Start(); | 42 thread_->Start(); |
43 application_task_runner_ = thread_->task_runner(); | 43 task_runner_ = thread_->task_runner(); |
44 } | 44 } |
45 | 45 |
46 DCHECK(application_task_runner_); | 46 DCHECK(task_runner_); |
47 application_task_runner_->PostTask( | 47 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 (!application_task_runner_) | 55 if (!task_runner_) |
56 return; | 56 return; |
57 // Any extant ServiceContexts must be destroyed on the application thread. | 57 // Any extant ServiceContexts must be destroyed on the application thread. |
58 if (application_task_runner_->BelongsToCurrentThread()) { | 58 if (task_runner_->BelongsToCurrentThread()) { |
59 Quit(); | 59 Quit(); |
60 } else { | 60 } else { |
61 application_task_runner_->PostTask(FROM_HERE, | 61 task_runner_->PostTask(FROM_HERE, |
62 base::Bind(&Instance::Quit, this)); | 62 base::Bind(&Instance::Quit, this)); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 private: | 66 private: |
67 friend class base::RefCountedThreadSafe<Instance>; | 67 friend class base::RefCountedThreadSafe<Instance>; |
68 | 68 |
69 ~Instance() { | 69 ~Instance() { |
70 // If this instance had its own thread, it MUST be explicitly destroyed by | 70 // If this instance had its own thread, it MUST be explicitly destroyed by |
71 // QuitOnRunnerThread() by the time this destructor is run. | 71 // QuitOnRunnerThread() by the time this destructor is run. |
72 DCHECK(!thread_); | 72 DCHECK(!thread_); |
73 } | 73 } |
74 | 74 |
75 void BindServiceRequestOnApplicationThread( | 75 void BindServiceRequestOnApplicationThread( |
76 shell::mojom::ServiceRequest request) { | 76 shell::mojom::ServiceRequest request) { |
77 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 77 DCHECK(task_runner_->BelongsToCurrentThread()); |
78 | 78 |
79 if (!service_) { | 79 if (!service_) { |
80 service_ = factory_callback_.Run( | 80 service_ = factory_callback_.Run( |
81 base::Bind(&Instance::Quit, base::Unretained(this))); | 81 base::Bind(&Instance::Quit, base::Unretained(this))); |
82 } | 82 } |
83 | 83 |
84 shell::ServiceContext* new_connection = | 84 shell::ServiceContext* new_connection = |
85 new shell::ServiceContext(service_.get(), std::move(request)); | 85 new shell::ServiceContext(service_.get(), std::move(request)); |
86 shell_connections_.push_back(base::WrapUnique(new_connection)); | 86 shell_connections_.push_back(base::WrapUnique(new_connection)); |
87 new_connection->SetConnectionLostClosure( | 87 new_connection->SetConnectionLostClosure( |
88 base::Bind(&Instance::OnStop, base::Unretained(this), | 88 base::Bind(&Instance::OnStop, base::Unretained(this), |
89 new_connection)); | 89 new_connection)); |
90 } | 90 } |
91 | 91 |
92 void OnStop(shell::ServiceContext* connection) { | 92 void OnStop(shell::ServiceContext* connection) { |
93 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 93 DCHECK(task_runner_->BelongsToCurrentThread()); |
94 | 94 |
95 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); | 95 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
96 ++it) { | 96 ++it) { |
97 if (it->get() == connection) { | 97 if (it->get() == connection) { |
98 shell_connections_.erase(it); | 98 shell_connections_.erase(it); |
99 break; | 99 break; |
100 } | 100 } |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 void Quit() { | 104 void Quit() { |
105 DCHECK(application_task_runner_->BelongsToCurrentThread()); | 105 DCHECK(task_runner_->BelongsToCurrentThread()); |
106 | 106 |
107 shell_connections_.clear(); | 107 shell_connections_.clear(); |
108 service_.reset(); | 108 service_.reset(); |
109 if (quit_task_runner_->BelongsToCurrentThread()) { | 109 if (quit_task_runner_->BelongsToCurrentThread()) { |
110 QuitOnRunnerThread(); | 110 QuitOnRunnerThread(); |
111 } else { | 111 } else { |
112 quit_task_runner_->PostTask( | 112 quit_task_runner_->PostTask( |
113 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); | 113 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this)); |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 void QuitOnRunnerThread() { | 117 void QuitOnRunnerThread() { |
118 DCHECK(runner_thread_checker_.CalledOnValidThread()); | 118 DCHECK(runner_thread_checker_.CalledOnValidThread()); |
119 if (thread_) { | 119 if (thread_) { |
120 thread_.reset(); | 120 thread_.reset(); |
121 application_task_runner_ = nullptr; | 121 task_runner_ = nullptr; |
122 } | 122 } |
123 quit_closure_.Run(); | 123 quit_closure_.Run(); |
124 } | 124 } |
125 | 125 |
126 const std::string name_; | 126 const std::string name_; |
127 const MojoApplicationInfo::ApplicationFactory factory_callback_; | 127 const ServiceInfo::ServiceFactory factory_callback_; |
128 const bool use_own_thread_; | 128 const bool use_own_thread_; |
129 const base::Closure quit_closure_; | 129 const base::Closure quit_closure_; |
130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; | 130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
131 | 131 |
132 // Thread checker used to ensure certain operations happen only on the | 132 // Thread checker used to ensure certain operations happen only on the |
133 // runner's (i.e. our owner's) thread. | 133 // runner's (i.e. our owner's) thread. |
134 base::ThreadChecker runner_thread_checker_; | 134 base::ThreadChecker runner_thread_checker_; |
135 | 135 |
136 // These fields must only be accessed from the runner's thread. | 136 // These fields must only be accessed from the runner's thread. |
137 std::unique_ptr<base::Thread> thread_; | 137 std::unique_ptr<base::Thread> thread_; |
138 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_; | 138 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
139 | 139 |
140 // These fields must only be accessed from the application thread, except in | 140 // These fields must only be accessed from the application thread, except in |
141 // the destructor which may run on either the runner thread or the application | 141 // the destructor which may run on either the runner thread or the application |
142 // thread. | 142 // thread. |
143 std::unique_ptr<shell::Service> service_; | 143 std::unique_ptr<shell::Service> service_; |
144 std::vector<std::unique_ptr<shell::ServiceContext>> shell_connections_; | 144 std::vector<std::unique_ptr<shell::ServiceContext>> shell_connections_; |
145 | 145 |
146 DISALLOW_COPY_AND_ASSIGN(Instance); | 146 DISALLOW_COPY_AND_ASSIGN(Instance); |
147 }; | 147 }; |
148 | 148 |
149 EmbeddedApplicationRunner::EmbeddedApplicationRunner( | 149 EmbeddedServiceRunner::EmbeddedServiceRunner(const base::StringPiece& name, |
150 const base::StringPiece& name, | 150 const ServiceInfo& info) |
151 const MojoApplicationInfo& info) | |
152 : weak_factory_(this) { | 151 : weak_factory_(this) { |
153 instance_ = new Instance(name, info, | 152 instance_ = new Instance(name, info, |
154 base::Bind(&EmbeddedApplicationRunner::OnQuit, | 153 base::Bind(&EmbeddedServiceRunner::OnQuit, |
155 weak_factory_.GetWeakPtr())); | 154 weak_factory_.GetWeakPtr())); |
156 } | 155 } |
157 | 156 |
158 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { | 157 EmbeddedServiceRunner::~EmbeddedServiceRunner() { |
159 instance_->ShutDown(); | 158 instance_->ShutDown(); |
160 } | 159 } |
161 | 160 |
162 void EmbeddedApplicationRunner::BindServiceRequest( | 161 void EmbeddedServiceRunner::BindServiceRequest( |
163 shell::mojom::ServiceRequest request) { | 162 shell::mojom::ServiceRequest request) { |
164 instance_->BindServiceRequest(std::move(request)); | 163 instance_->BindServiceRequest(std::move(request)); |
165 } | 164 } |
166 | 165 |
167 void EmbeddedApplicationRunner::SetQuitClosure( | 166 void EmbeddedServiceRunner::SetQuitClosure( |
168 const base::Closure& quit_closure) { | 167 const base::Closure& quit_closure) { |
169 quit_closure_ = quit_closure; | 168 quit_closure_ = quit_closure; |
170 } | 169 } |
171 | 170 |
172 void EmbeddedApplicationRunner::OnQuit() { | 171 void EmbeddedServiceRunner::OnQuit() { |
173 if (!quit_closure_.is_null()) | 172 if (!quit_closure_.is_null()) |
174 quit_closure_.Run(); | 173 quit_closure_.Run(); |
175 } | 174 } |
176 | 175 |
177 } // namespace content | 176 } // namespace content |
OLD | NEW |