OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/runner/host/child_process_base.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/synchronization/waitable_event.h" | |
13 #include "base/threading/thread.h" | |
14 #include "mojo/edk/embedder/embedder.h" | |
15 #include "mojo/edk/embedder/process_delegate.h" | |
16 #include "mojo/shell/runner/common/client_util.h" | |
17 | |
18 namespace mojo { | |
19 namespace shell { | |
20 | |
21 namespace { | |
22 | |
23 // Should be created and initialized on the main thread and kept alive as long | |
24 // a Mojo application is running in the current process. | |
25 class ScopedAppContext : public edk::ProcessDelegate { | |
26 public: | |
27 ScopedAppContext() | |
28 : io_thread_("io_thread"), wait_for_shutdown_event_(true, false) { | |
29 // Initialize Mojo before starting any threads. | |
30 edk::Init(); | |
31 | |
32 // Create and start our I/O thread. | |
33 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | |
34 CHECK(io_thread_.StartWithOptions(io_thread_options)); | |
35 io_runner_ = io_thread_.task_runner().get(); | |
36 CHECK(io_runner_.get()); | |
37 | |
38 edk::InitIPCSupport(this, io_runner_); | |
39 edk::SetParentPipeHandleFromCommandLine(); | |
40 } | |
41 | |
42 ~ScopedAppContext() override { | |
43 edk::ShutdownIPCSupport(); | |
44 wait_for_shutdown_event_.Wait(); | |
45 } | |
46 | |
47 private: | |
48 // ProcessDelegate implementation. | |
49 void OnShutdownComplete() override { | |
50 wait_for_shutdown_event_.Signal(); | |
51 } | |
52 | |
53 base::Thread io_thread_; | |
54 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | |
55 | |
56 // Used to unblock the main thread on shutdown. | |
57 base::WaitableEvent wait_for_shutdown_event_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(ScopedAppContext); | |
60 }; | |
61 | |
62 } // namespace | |
63 | |
64 void ChildProcessMain(const RunCallback& callback) { | |
65 DCHECK(!base::MessageLoop::current()); | |
66 | |
67 ScopedAppContext app_context; | |
68 callback.Run(GetShellClientRequestFromCommandLine()); | |
69 } | |
70 | |
71 } // namespace shell | |
72 } // namespace mojo | |
OLD | NEW |