Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: content/common/mojo/embedded_application_runner.cc

Issue 2398783002: Rename a bunch of Mojo Application stuff to reference Services. (Closed)
Patch Set: . Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/common/mojo/embedded_application_runner.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "services/shell/public/cpp/service_context.h"
17
18 namespace content {
19
20 class EmbeddedApplicationRunner::Instance
21 : public base::RefCountedThreadSafe<Instance> {
22 public:
23 Instance(const base::StringPiece& name,
24 const MojoApplicationInfo& info,
25 const base::Closure& quit_closure)
26 : name_(name.as_string()),
27 factory_callback_(info.application_factory),
28 use_own_thread_(!info.application_task_runner && info.use_own_thread),
29 quit_closure_(quit_closure),
30 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 application_task_runner_(info.application_task_runner) {
32 if (!use_own_thread_ && !application_task_runner_)
33 application_task_runner_ = base::ThreadTaskRunnerHandle::Get();
34 }
35
36 void BindServiceRequest(shell::mojom::ServiceRequest request) {
37 DCHECK(runner_thread_checker_.CalledOnValidThread());
38
39 if (use_own_thread_ && !thread_) {
40 // Start a new thread if necessary.
41 thread_.reset(new base::Thread(name_));
42 thread_->Start();
43 application_task_runner_ = thread_->task_runner();
44 }
45
46 DCHECK(application_task_runner_);
47 application_task_runner_->PostTask(
48 FROM_HERE,
49 base::Bind(&Instance::BindServiceRequestOnApplicationThread, this,
50 base::Passed(&request)));
51 }
52
53 void ShutDown() {
54 DCHECK(runner_thread_checker_.CalledOnValidThread());
55 if (!application_task_runner_)
56 return;
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));
63 }
64 }
65
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
75 void BindServiceRequestOnApplicationThread(
76 shell::mojom::ServiceRequest request) {
77 DCHECK(application_task_runner_->BelongsToCurrentThread());
78
79 if (!service_) {
80 service_ = factory_callback_.Run(
81 base::Bind(&Instance::Quit, base::Unretained(this)));
82 }
83
84 shell::ServiceContext* new_connection =
85 new shell::ServiceContext(service_.get(), std::move(request));
86 shell_connections_.push_back(base::WrapUnique(new_connection));
87 new_connection->SetConnectionLostClosure(
88 base::Bind(&Instance::OnStop, base::Unretained(this),
89 new_connection));
90 }
91
92 void OnStop(shell::ServiceContext* connection) {
93 DCHECK(application_task_runner_->BelongsToCurrentThread());
94
95 for (auto it = shell_connections_.begin(); it != shell_connections_.end();
96 ++it) {
97 if (it->get() == connection) {
98 shell_connections_.erase(it);
99 break;
100 }
101 }
102 }
103
104 void Quit() {
105 DCHECK(application_task_runner_->BelongsToCurrentThread());
106
107 shell_connections_.clear();
108 service_.reset();
109 if (quit_task_runner_->BelongsToCurrentThread()) {
110 QuitOnRunnerThread();
111 } else {
112 quit_task_runner_->PostTask(
113 FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this));
114 }
115 }
116
117 void QuitOnRunnerThread() {
118 DCHECK(runner_thread_checker_.CalledOnValidThread());
119 if (thread_) {
120 thread_.reset();
121 application_task_runner_ = nullptr;
122 }
123 quit_closure_.Run();
124 }
125
126 const std::string name_;
127 const MojoApplicationInfo::ApplicationFactory factory_callback_;
128 const bool use_own_thread_;
129 const base::Closure quit_closure_;
130 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_;
131
132 // Thread checker used to ensure certain operations happen only on the
133 // runner's (i.e. our owner's) thread.
134 base::ThreadChecker runner_thread_checker_;
135
136 // These fields must only be accessed from the runner's thread.
137 std::unique_ptr<base::Thread> thread_;
138 scoped_refptr<base::SingleThreadTaskRunner> application_task_runner_;
139
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
142 // thread.
143 std::unique_ptr<shell::Service> service_;
144 std::vector<std::unique_ptr<shell::ServiceContext>> shell_connections_;
145
146 DISALLOW_COPY_AND_ASSIGN(Instance);
147 };
148
149 EmbeddedApplicationRunner::EmbeddedApplicationRunner(
150 const base::StringPiece& name,
151 const MojoApplicationInfo& info)
152 : weak_factory_(this) {
153 instance_ = new Instance(name, info,
154 base::Bind(&EmbeddedApplicationRunner::OnQuit,
155 weak_factory_.GetWeakPtr()));
156 }
157
158 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() {
159 instance_->ShutDown();
160 }
161
162 void EmbeddedApplicationRunner::BindServiceRequest(
163 shell::mojom::ServiceRequest request) {
164 instance_->BindServiceRequest(std::move(request));
165 }
166
167 void EmbeddedApplicationRunner::SetQuitClosure(
168 const base::Closure& quit_closure) {
169 quit_closure_ = quit_closure;
170 }
171
172 void EmbeddedApplicationRunner::OnQuit() {
173 if (!quit_closure_.is_null())
174 quit_closure_.Run();
175 }
176
177 } // namespace content
OLDNEW
« no previous file with comments | « content/common/mojo/embedded_application_runner.h ('k') | content/common/mojo/mojo_child_connection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698