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/shell/app_child_process.h" | 5 #include "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/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
20 #include "mojo/common/message_pump_mojo.h" | 20 #include "mojo/common/message_pump_mojo.h" |
21 #include "mojo/edk/embedder/embedder.h" | 21 #include "mojo/edk/embedder/embedder.h" |
22 #include "mojo/edk/embedder/simple_platform_support.h" | 22 #include "mojo/edk/embedder/simple_platform_support.h" |
23 #include "mojo/public/cpp/system/core.h" | 23 #include "mojo/public/cpp/system/core.h" |
24 #include "mojo/shell/app_child_process.mojom.h" | 24 #include "shell/app_child_process.mojom.h" |
25 #include "mojo/shell/dynamic_service_runner.h" | 25 #include "shell/dynamic_service_runner.h" |
26 | 26 |
27 namespace mojo { | 27 namespace mojo { |
28 namespace shell { | 28 namespace shell { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // Blocker --------------------------------------------------------------------- | 32 // Blocker --------------------------------------------------------------------- |
33 | 33 |
34 // Blocks a thread until another thread unblocks it, at which point it unblocks | 34 // Blocks a thread until another thread unblocks it, at which point it unblocks |
35 // and runs a closure provided by that thread. | 35 // and runs a closure provided by that thread. |
36 class Blocker { | 36 class Blocker { |
37 public: | 37 public: |
38 class Unblocker { | 38 class Unblocker { |
39 public: | 39 public: |
40 ~Unblocker() {} | 40 ~Unblocker() {} |
41 | 41 |
42 void Unblock(base::Closure run_after) { | 42 void Unblock(base::Closure run_after) { |
43 DCHECK(blocker_); | 43 DCHECK(blocker_); |
44 DCHECK(blocker_->run_after_.is_null()); | 44 DCHECK(blocker_->run_after_.is_null()); |
45 blocker_->run_after_ = run_after; | 45 blocker_->run_after_ = run_after; |
46 blocker_->event_.Signal(); | 46 blocker_->event_.Signal(); |
47 blocker_ = NULL; | 47 blocker_ = NULL; |
48 } | 48 } |
49 | 49 |
50 private: | 50 private: |
51 friend class Blocker; | 51 friend class Blocker; |
52 Unblocker(Blocker* blocker) : blocker_(blocker) { | 52 Unblocker(Blocker* blocker) : blocker_(blocker) { DCHECK(blocker_); } |
53 DCHECK(blocker_); | |
54 } | |
55 | 53 |
56 Blocker* blocker_; | 54 Blocker* blocker_; |
57 | 55 |
58 // Copy and assign allowed. | 56 // Copy and assign allowed. |
59 }; | 57 }; |
60 | 58 |
61 Blocker() : event_(true, false) {} | 59 Blocker() : event_(true, false) {} |
62 ~Blocker() {} | 60 ~Blocker() {} |
63 | 61 |
64 void Block() { | 62 void Block() { |
65 DCHECK(run_after_.is_null()); | 63 DCHECK(run_after_.is_null()); |
66 event_.Wait(); | 64 event_.Wait(); |
67 run_after_.Run(); | 65 run_after_.Run(); |
68 } | 66 } |
69 | 67 |
70 Unblocker GetUnblocker() { | 68 Unblocker GetUnblocker() { return Unblocker(this); } |
71 return Unblocker(this); | |
72 } | |
73 | 69 |
74 private: | 70 private: |
75 base::WaitableEvent event_; | 71 base::WaitableEvent event_; |
76 base::Closure run_after_; | 72 base::Closure run_after_; |
77 | 73 |
78 DISALLOW_COPY_AND_ASSIGN(Blocker); | 74 DISALLOW_COPY_AND_ASSIGN(Blocker); |
79 }; | 75 }; |
80 | 76 |
81 // AppContext ------------------------------------------------------------------ | 77 // AppContext ------------------------------------------------------------------ |
82 | 78 |
83 class AppChildControllerImpl; | 79 class AppChildControllerImpl; |
84 | 80 |
85 static void DestroyController(scoped_ptr<AppChildControllerImpl> controller) { | 81 static void DestroyController(scoped_ptr<AppChildControllerImpl> controller) { |
86 } | 82 } |
87 | 83 |
88 // Should be created and initialized on the main thread. | 84 // Should be created and initialized on the main thread. |
89 class AppContext { | 85 class AppContext { |
90 public: | 86 public: |
91 AppContext() | 87 AppContext() |
92 : io_thread_("io_thread"), | 88 : io_thread_("io_thread"), controller_thread_("controller_thread") {} |
93 controller_thread_("controller_thread") {} | |
94 ~AppContext() {} | 89 ~AppContext() {} |
95 | 90 |
96 void Init() { | 91 void Init() { |
97 // Initialize Mojo before starting any threads. | 92 // Initialize Mojo before starting any threads. |
98 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( | 93 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( |
99 new mojo::embedder::SimplePlatformSupport())); | 94 new mojo::embedder::SimplePlatformSupport())); |
100 | 95 |
101 // Create and start our I/O thread. | 96 // Create and start our I/O thread. |
102 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 97 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); |
103 CHECK(io_thread_.StartWithOptions(io_thread_options)); | 98 CHECK(io_thread_.StartWithOptions(io_thread_options)); |
104 io_runner_ = io_thread_.message_loop_proxy().get(); | 99 io_runner_ = io_thread_.message_loop_proxy().get(); |
105 CHECK(io_runner_.get()); | 100 CHECK(io_runner_.get()); |
106 | 101 |
107 // Create and start our controller thread. | 102 // Create and start our controller thread. |
108 base::Thread::Options controller_thread_options; | 103 base::Thread::Options controller_thread_options; |
109 controller_thread_options.message_loop_type = | 104 controller_thread_options.message_loop_type = |
110 base::MessageLoop::TYPE_CUSTOM; | 105 base::MessageLoop::TYPE_CUSTOM; |
111 controller_thread_options.message_pump_factory = | 106 controller_thread_options.message_pump_factory = |
112 base::Bind(&common::MessagePumpMojo::Create); | 107 base::Bind(&common::MessagePumpMojo::Create); |
113 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 108 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
114 controller_runner_ = controller_thread_.message_loop_proxy().get(); | 109 controller_runner_ = controller_thread_.message_loop_proxy().get(); |
115 CHECK(controller_runner_.get()); | 110 CHECK(controller_runner_.get()); |
116 } | 111 } |
117 | 112 |
118 void Shutdown() { | 113 void Shutdown() { |
119 controller_runner_->PostTask( | 114 controller_runner_->PostTask( |
120 FROM_HERE, | 115 FROM_HERE, base::Bind(&DestroyController, base::Passed(&controller_))); |
121 base::Bind(&DestroyController, base::Passed(&controller_))); | |
122 } | 116 } |
123 | 117 |
124 base::SingleThreadTaskRunner* io_runner() const { | 118 base::SingleThreadTaskRunner* io_runner() const { return io_runner_.get(); } |
125 return io_runner_.get(); | |
126 } | |
127 | 119 |
128 base::SingleThreadTaskRunner* controller_runner() const { | 120 base::SingleThreadTaskRunner* controller_runner() const { |
129 return controller_runner_.get(); | 121 return controller_runner_.get(); |
130 } | 122 } |
131 | 123 |
132 AppChildControllerImpl* controller() const { | 124 AppChildControllerImpl* controller() const { return controller_.get(); } |
133 return controller_.get(); | |
134 } | |
135 | 125 |
136 void set_controller(scoped_ptr<AppChildControllerImpl> controller) { | 126 void set_controller(scoped_ptr<AppChildControllerImpl> controller) { |
137 controller_ = controller.Pass(); | 127 controller_ = controller.Pass(); |
138 } | 128 } |
139 | 129 |
140 private: | 130 private: |
141 // Accessed only on the controller thread. | 131 // Accessed only on the controller thread. |
142 // IMPORTANT: This must be BEFORE |controller_thread_|, so that the controller | 132 // IMPORTANT: This must be BEFORE |controller_thread_|, so that the controller |
143 // thread gets joined (and thus |controller_| reset) before |controller_| is | 133 // thread gets joined (and thus |controller_| reset) before |controller_| is |
144 // destroyed. | 134 // destroyed. |
(...skipping 14 matching lines...) Expand all Loading... |
159 public: | 149 public: |
160 ~AppChildControllerImpl() override { | 150 ~AppChildControllerImpl() override { |
161 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
162 | 152 |
163 // TODO(vtl): Pass in the result from |MainMain()|. | 153 // TODO(vtl): Pass in the result from |MainMain()|. |
164 client()->AppCompleted(MOJO_RESULT_UNIMPLEMENTED); | 154 client()->AppCompleted(MOJO_RESULT_UNIMPLEMENTED); |
165 } | 155 } |
166 | 156 |
167 // To be executed on the controller thread. Creates the |AppChildController|, | 157 // To be executed on the controller thread. Creates the |AppChildController|, |
168 // etc. | 158 // etc. |
169 static void Init( | 159 static void Init(AppContext* app_context, |
170 AppContext* app_context, | 160 embedder::ScopedPlatformHandle platform_channel, |
171 embedder::ScopedPlatformHandle platform_channel, | 161 const Blocker::Unblocker& unblocker) { |
172 const Blocker::Unblocker& unblocker) { | |
173 DCHECK(app_context); | 162 DCHECK(app_context); |
174 DCHECK(platform_channel.is_valid()); | 163 DCHECK(platform_channel.is_valid()); |
175 | 164 |
176 DCHECK(!app_context->controller()); | 165 DCHECK(!app_context->controller()); |
177 | 166 |
178 scoped_ptr<AppChildControllerImpl> impl( | 167 scoped_ptr<AppChildControllerImpl> impl( |
179 new AppChildControllerImpl(app_context, unblocker)); | 168 new AppChildControllerImpl(app_context, unblocker)); |
180 | 169 |
181 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( | 170 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( |
182 platform_channel.Pass(), | 171 platform_channel.Pass(), app_context->io_runner(), |
183 app_context->io_runner(), | |
184 base::Bind(&AppChildControllerImpl::DidCreateChannel, | 172 base::Bind(&AppChildControllerImpl::DidCreateChannel, |
185 base::Unretained(impl.get())), | 173 base::Unretained(impl.get())), |
186 base::MessageLoopProxy::current())); | 174 base::MessageLoopProxy::current())); |
187 | 175 |
188 BindToPipe(impl.get(), host_message_pipe.Pass()); | 176 BindToPipe(impl.get(), host_message_pipe.Pass()); |
189 | 177 |
190 app_context->set_controller(impl.Pass()); | 178 app_context->set_controller(impl.Pass()); |
191 } | 179 } |
192 | 180 |
193 void OnConnectionError() override { | 181 void OnConnectionError() override { |
194 // TODO(darin): How should we handle a connection error here? | 182 // TODO(darin): How should we handle a connection error here? |
195 } | 183 } |
196 | 184 |
197 // |AppChildController| methods: | 185 // |AppChildController| methods: |
198 void StartApp(const String& app_path, | 186 void StartApp(const String& app_path, |
199 ScopedMessagePipeHandle service) override { | 187 ScopedMessagePipeHandle service) override { |
200 DVLOG(2) << "AppChildControllerImpl::StartApp(" << app_path << ", ...)"; | 188 DVLOG(2) << "AppChildControllerImpl::StartApp(" << app_path << ", ...)"; |
201 DCHECK(thread_checker_.CalledOnValidThread()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
202 | 190 |
203 unblocker_.Unblock(base::Bind(&AppChildControllerImpl::StartAppOnMainThread, | 191 unblocker_.Unblock(base::Bind(&AppChildControllerImpl::StartAppOnMainThread, |
204 base::FilePath::FromUTF8Unsafe(app_path), | 192 base::FilePath::FromUTF8Unsafe(app_path), |
205 base::Passed(&service))); | 193 base::Passed(&service))); |
206 } | 194 } |
207 | 195 |
208 private: | 196 private: |
209 AppChildControllerImpl(AppContext* app_context, | 197 AppChildControllerImpl(AppContext* app_context, |
210 const Blocker::Unblocker& unblocker) | 198 const Blocker::Unblocker& unblocker) |
211 : app_context_(app_context), | 199 : app_context_(app_context), unblocker_(unblocker), channel_info_(NULL) {} |
212 unblocker_(unblocker), | |
213 channel_info_(NULL) { | |
214 } | |
215 | 200 |
216 // Callback for |embedder::CreateChannel()|. | 201 // Callback for |embedder::CreateChannel()|. |
217 void DidCreateChannel(embedder::ChannelInfo* channel_info) { | 202 void DidCreateChannel(embedder::ChannelInfo* channel_info) { |
218 DVLOG(2) << "AppChildControllerImpl::DidCreateChannel()"; | 203 DVLOG(2) << "AppChildControllerImpl::DidCreateChannel()"; |
219 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
220 channel_info_ = channel_info; | 205 channel_info_ = channel_info; |
221 } | 206 } |
222 | 207 |
223 static void StartAppOnMainThread(const base::FilePath& app_path, | 208 static void StartAppOnMainThread(const base::FilePath& app_path, |
224 ScopedMessagePipeHandle service) { | 209 ScopedMessagePipeHandle service) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 base::Bind(&AppChildControllerImpl::Init, base::Unretained(&app_context), | 247 base::Bind(&AppChildControllerImpl::Init, base::Unretained(&app_context), |
263 base::Passed(platform_channel()), blocker.GetUnblocker())); | 248 base::Passed(platform_channel()), blocker.GetUnblocker())); |
264 // This will block, then run whatever the controller wants. | 249 // This will block, then run whatever the controller wants. |
265 blocker.Block(); | 250 blocker.Block(); |
266 | 251 |
267 app_context.Shutdown(); | 252 app_context.Shutdown(); |
268 } | 253 } |
269 | 254 |
270 } // namespace shell | 255 } // namespace shell |
271 } // namespace mojo | 256 } // namespace mojo |
OLD | NEW |