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

Side by Side Diff: mojo/shell/app_child_process.cc

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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/shell/app_child_process.h" 5 #include "mojo/shell/app_child_process.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback_helpers.h" 8 #include "base/callback_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/scoped_native_library.h" 16 #include "base/scoped_native_library.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/waitable_event.h" 18 #include "base/synchronization/waitable_event.h"
19 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
20 #include "base/threading/thread_checker.h" 20 #include "base/threading/thread_checker.h"
21 #include "mojo/common/message_pump_mojo.h" 21 #include "mojo/common/message_pump_mojo.h"
22 #include "mojo/embedder/embedder.h" 22 #include "mojo/embedder/embedder.h"
23 #include "mojo/public/cpp/bindings/remote_ptr.h"
24 #include "mojo/public/cpp/system/core.h" 23 #include "mojo/public/cpp/system/core.h"
25 #include "mojo/shell/app_child_process.mojom.h" 24 #include "mojo/shell/app_child_process.mojom.h"
26 25
27 namespace mojo { 26 namespace mojo {
28 namespace shell { 27 namespace shell {
29 28
30 namespace { 29 namespace {
31 30
32 // Blocker --------------------------------------------------------------------- 31 // Blocker ---------------------------------------------------------------------
33 32
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 base::WaitableEvent event_; 74 base::WaitableEvent event_;
76 base::Closure run_after_; 75 base::Closure run_after_;
77 76
78 DISALLOW_COPY_AND_ASSIGN(Blocker); 77 DISALLOW_COPY_AND_ASSIGN(Blocker);
79 }; 78 };
80 79
81 // AppContext ------------------------------------------------------------------ 80 // AppContext ------------------------------------------------------------------
82 81
83 class AppChildControllerImpl; 82 class AppChildControllerImpl;
84 83
84 static void DestroyController(AppChildControllerPtr controller) {
85 }
86
85 // Should be created and initialized on the main thread. 87 // Should be created and initialized on the main thread.
86 class AppContext { 88 class AppContext {
87 public: 89 public:
88 AppContext() 90 AppContext()
89 : io_thread_("io_thread"), 91 : io_thread_("io_thread"),
90 controller_thread_("controller_thread") {} 92 controller_thread_("controller_thread") {}
91 ~AppContext() {} 93 ~AppContext() {}
92 94
93 void Init() { 95 void Init() {
94 // Initialize Mojo before starting any threads. 96 // Initialize Mojo before starting any threads.
95 embedder::Init(); 97 embedder::Init();
96 98
97 // Create and start our I/O thread. 99 // Create and start our I/O thread.
98 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); 100 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0);
99 CHECK(io_thread_.StartWithOptions(io_thread_options)); 101 CHECK(io_thread_.StartWithOptions(io_thread_options));
100 io_runner_ = io_thread_.message_loop_proxy().get(); 102 io_runner_ = io_thread_.message_loop_proxy().get();
101 CHECK(io_runner_); 103 CHECK(io_runner_);
102 104
103 // Create and start our controller thread. 105 // Create and start our controller thread.
104 base::Thread::Options controller_thread_options; 106 base::Thread::Options controller_thread_options;
105 controller_thread_options.message_loop_type = 107 controller_thread_options.message_loop_type =
106 base::MessageLoop::TYPE_CUSTOM; 108 base::MessageLoop::TYPE_CUSTOM;
107 controller_thread_options.message_pump_factory = 109 controller_thread_options.message_pump_factory =
108 base::Bind(&common::MessagePumpMojo::Create); 110 base::Bind(&common::MessagePumpMojo::Create);
109 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); 111 CHECK(controller_thread_.StartWithOptions(controller_thread_options));
110 controller_runner_ = controller_thread_.message_loop_proxy().get(); 112 controller_runner_ = controller_thread_.message_loop_proxy().get();
111 CHECK(controller_runner_); 113 CHECK(controller_runner_);
112 } 114 }
113 115
116 void Shutdown() {
117 controller_runner_->PostTask(
118 FROM_HERE,
119 base::Bind(&DestroyController, base::Passed(&controller_)));
120 }
121
114 base::SingleThreadTaskRunner* io_runner() const { 122 base::SingleThreadTaskRunner* io_runner() const {
115 return io_runner_.get(); 123 return io_runner_.get();
116 } 124 }
117 125
118 base::SingleThreadTaskRunner* controller_runner() const { 126 base::SingleThreadTaskRunner* controller_runner() const {
119 return controller_runner_.get(); 127 return controller_runner_.get();
120 } 128 }
121 129
122 AppChildControllerImpl* controller() const { 130 AppChildController* controller() const {
123 return controller_.get(); 131 return controller_.get();
124 } 132 }
125 133
126 void set_controller(scoped_ptr<AppChildControllerImpl> controller) { 134 void set_controller(AppChildControllerPtr controller) {
127 controller_ = controller.Pass(); 135 controller_ = controller.Pass();
128 } 136 }
129 137
130 private: 138 private:
131 // Accessed only on the controller thread. 139 // Accessed only on the controller thread.
132 // IMPORTANT: This must be BEFORE |controller_thread_|, so that the controller 140 // IMPORTANT: This must be BEFORE |controller_thread_|, so that the controller
133 // thread gets joined (and thus |controller_| reset) before |controller_| is 141 // thread gets joined (and thus |controller_| reset) before |controller_| is
134 // destroyed. 142 // destroyed.
135 scoped_ptr<AppChildControllerImpl> controller_; 143 AppChildControllerPtr controller_;
136 144
137 base::Thread io_thread_; 145 base::Thread io_thread_;
138 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; 146 scoped_refptr<base::SingleThreadTaskRunner> io_runner_;
139 147
140 base::Thread controller_thread_; 148 base::Thread controller_thread_;
141 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; 149 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_;
142 150
143 DISALLOW_COPY_AND_ASSIGN(AppContext); 151 DISALLOW_COPY_AND_ASSIGN(AppContext);
144 }; 152 };
145 153
146 // AppChildControllerImpl ------------------------------------------------------ 154 // AppChildControllerImpl ------------------------------------------------------
147 155
148 class AppChildControllerImpl : public mojo_shell::AppChildController { 156 class AppChildControllerImpl : public AppChildController {
149 public: 157 public:
150 virtual ~AppChildControllerImpl() { 158 virtual ~AppChildControllerImpl() {
151 DCHECK(thread_checker_.CalledOnValidThread()); 159 DCHECK(thread_checker_.CalledOnValidThread());
160
161 // TODO(vtl): Pass in the result from |MainMain()|.
162 if (controller_client_)
163 controller_client_->AppCompleted(MOJO_RESULT_UNIMPLEMENTED);
152 } 164 }
153 165
154 // To be executed on the controller thread. Creates the |AppChildController|, 166 // To be executed on the controller thread. Creates the |AppChildController|,
155 // etc. 167 // etc.
156 static void Init( 168 static void Init(
157 AppContext* app_context, 169 AppContext* app_context,
158 embedder::ScopedPlatformHandle platform_channel, 170 embedder::ScopedPlatformHandle platform_channel,
159 const Blocker::Unblocker& unblocker) { 171 const Blocker::Unblocker& unblocker) {
160 DCHECK(app_context); 172 DCHECK(app_context);
161 DCHECK(platform_channel.is_valid()); 173 DCHECK(platform_channel.is_valid());
162 174
163 DCHECK(!app_context->controller()); 175 DCHECK(!app_context->controller());
164 app_context->set_controller( 176
165 make_scoped_ptr(new AppChildControllerImpl(app_context, unblocker))); 177 AppChildControllerImpl* impl =
166 app_context->controller()->CreateChannel(platform_channel.Pass()); 178 new AppChildControllerImpl(app_context, unblocker);
179
180 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel(
181 platform_channel.Pass(),
182 app_context->io_runner(),
183 base::Bind(&AppChildControllerImpl::DidCreateChannel,
184 base::Unretained(impl)),
185 base::MessageLoopProxy::current()));
186
187 AppChildControllerPtr controller(impl);
188 controller.ConfigureStub(host_message_pipe.Pass());
189
190 app_context->set_controller(controller.Pass());
167 } 191 }
168 192
169 void Shutdown() { 193 // |AppChildController| methods:
170 DVLOG(2) << "AppChildControllerImpl::Shutdown()";
171 DCHECK(thread_checker_.CalledOnValidThread());
172 194
173 // TODO(vtl): Pass in the result from |MainMain()|. 195 virtual void SetClient(AppChildControllerClient* client) {
174 controller_client_->AppCompleted(MOJO_RESULT_UNIMPLEMENTED); 196 controller_client_ = client;
175
176 // TODO(vtl): Drain then destroy the channel (on the I/O thread).
177
178 // This will destroy this object.
179 app_context_->set_controller(scoped_ptr<AppChildControllerImpl>());
180 } 197 }
181 198
182 // |AppChildController| method:
183 virtual void StartApp(const String& app_path, 199 virtual void StartApp(const String& app_path,
184 ScopedMessagePipeHandle service) OVERRIDE { 200 ScopedMessagePipeHandle service) OVERRIDE {
185 DVLOG(2) << "AppChildControllerImpl::StartApp(" 201 DVLOG(2) << "AppChildControllerImpl::StartApp("
186 << app_path.To<std::string>() << ", ...)"; 202 << app_path.To<std::string>() << ", ...)";
187 DCHECK(thread_checker_.CalledOnValidThread()); 203 DCHECK(thread_checker_.CalledOnValidThread());
188 204
205 // TODO(darin): Add TypeConverter for FilePath <-> mojo::String.
189 unblocker_.Unblock(base::Bind(&AppChildControllerImpl::StartAppOnMainThread, 206 unblocker_.Unblock(base::Bind(&AppChildControllerImpl::StartAppOnMainThread,
190 base::FilePath::FromUTF8Unsafe( 207 base::FilePath::FromUTF8Unsafe(
191 app_path.To<std::string>()), 208 app_path.To<std::string>()),
192 base::Passed(&service))); 209 base::Passed(&service)));
193 } 210 }
194 211
195 private: 212 private:
196 AppChildControllerImpl(AppContext* app_context, 213 AppChildControllerImpl(AppContext* app_context,
197 const Blocker::Unblocker& unblocker) 214 const Blocker::Unblocker& unblocker)
198 : app_context_(app_context), 215 : app_context_(app_context),
199 unblocker_(unblocker), 216 unblocker_(unblocker),
217 controller_client_(NULL),
200 channel_info_(NULL) { 218 channel_info_(NULL) {
201 } 219 }
202 220
203 void CreateChannel(embedder::ScopedPlatformHandle platform_channel) {
204 DVLOG(2) << "AppChildControllerImpl::CreateChannel()";
205 DCHECK(thread_checker_.CalledOnValidThread());
206
207 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel(
208 platform_channel.Pass(),
209 app_context_->io_runner(),
210 base::Bind(&AppChildControllerImpl::DidCreateChannel,
211 base::Unretained(this)),
212 base::MessageLoopProxy::current()));
213 controller_client_.reset(
214 mojo_shell::ScopedAppChildControllerClientHandle(
215 mojo_shell::AppChildControllerClientHandle(
216 host_message_pipe.release().value())), this);
217 }
218
219 // Callback for |embedder::CreateChannel()|. 221 // Callback for |embedder::CreateChannel()|.
220 void DidCreateChannel(embedder::ChannelInfo* channel_info) { 222 void DidCreateChannel(embedder::ChannelInfo* channel_info) {
221 DVLOG(2) << "AppChildControllerImpl::DidCreateChannel()"; 223 DVLOG(2) << "AppChildControllerImpl::DidCreateChannel()";
222 DCHECK(thread_checker_.CalledOnValidThread()); 224 DCHECK(thread_checker_.CalledOnValidThread());
223 channel_info_ = channel_info; 225 channel_info_ = channel_info;
224 } 226 }
225 227
226 static void StartAppOnMainThread(const base::FilePath& app_path, 228 static void StartAppOnMainThread(const base::FilePath& app_path,
227 ScopedMessagePipeHandle service) { 229 ScopedMessagePipeHandle service) {
228 // TODO(vtl): This is copied from in_process_dynamic_service_runner.cc. 230 // TODO(vtl): This is copied from in_process_dynamic_service_runner.cc.
(...skipping 26 matching lines...) Expand all
255 MojoResult result = main_function(service.release().value()); 257 MojoResult result = main_function(service.release().value());
256 if (result < MOJO_RESULT_OK) 258 if (result < MOJO_RESULT_OK)
257 LOG(ERROR) << "MojoMain returned an error: " << result; 259 LOG(ERROR) << "MojoMain returned an error: " << result;
258 } while (false); 260 } while (false);
259 } 261 }
260 262
261 base::ThreadChecker thread_checker_; 263 base::ThreadChecker thread_checker_;
262 AppContext* const app_context_; 264 AppContext* const app_context_;
263 Blocker::Unblocker unblocker_; 265 Blocker::Unblocker unblocker_;
264 266
265 RemotePtr<mojo_shell::AppChildControllerClient> controller_client_; 267 AppChildControllerClient* controller_client_;
266 embedder::ChannelInfo* channel_info_; 268 embedder::ChannelInfo* channel_info_;
267 269
268 DISALLOW_COPY_AND_ASSIGN(AppChildControllerImpl); 270 DISALLOW_COPY_AND_ASSIGN(AppChildControllerImpl);
269 }; 271 };
270 272
271 } // namespace 273 } // namespace
272 274
273 // AppChildProcess ------------------------------------------------------------- 275 // AppChildProcess -------------------------------------------------------------
274 276
275 AppChildProcess::AppChildProcess() { 277 AppChildProcess::AppChildProcess() {
276 } 278 }
277 279
278 AppChildProcess::~AppChildProcess() { 280 AppChildProcess::~AppChildProcess() {
279 } 281 }
280 282
281 void AppChildProcess::Main() { 283 void AppChildProcess::Main() {
282 DVLOG(2) << "AppChildProcess::Main()"; 284 DVLOG(2) << "AppChildProcess::Main()";
283 285
284 AppContext app_context; 286 AppContext app_context;
285 app_context.Init(); 287 app_context.Init();
286 288
287 Blocker blocker; 289 Blocker blocker;
288 app_context.controller_runner()->PostTask( 290 app_context.controller_runner()->PostTask(
289 FROM_HERE, 291 FROM_HERE,
290 base::Bind(&AppChildControllerImpl::Init, base::Unretained(&app_context), 292 base::Bind(&AppChildControllerImpl::Init, base::Unretained(&app_context),
291 base::Passed(platform_channel()), blocker.GetUnblocker())); 293 base::Passed(platform_channel()), blocker.GetUnblocker()));
292 // This will block, then run whatever the controller wants. 294 // This will block, then run whatever the controller wants.
293 blocker.Block(); 295 blocker.Block();
294 296
295 app_context.controller_runner()->PostTask( 297 app_context.Shutdown();
296 FROM_HERE,
297 base::Bind(&AppChildControllerImpl::Shutdown,
298 base::Unretained(app_context.controller())));
299 } 298 }
300 299
301 } // namespace shell 300 } // namespace shell
302 } // namespace mojo 301 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698