summaryrefslogtreecommitdiffstats
path: root/mojo/shell/runner/host/child_process_base.cc
blob: e6b9a2529fcc2942c7d9f7ee7b85a1538c725b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/shell/runner/host/child_process_base.h"

#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/process_delegate.h"
#include "mojo/shell/runner/common/client_util.h"

namespace mojo {
namespace shell {

namespace {

// Should be created and initialized on the main thread and kept alive as long
// a Mojo application is running in the current process.
class ScopedAppContext : public edk::ProcessDelegate {
 public:
  ScopedAppContext()
      : io_thread_("io_thread"), wait_for_shutdown_event_(true, false) {
    // Initialize Mojo before starting any threads.
    edk::Init();

    // Create and start our I/O thread.
    base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0);
    CHECK(io_thread_.StartWithOptions(io_thread_options));
    io_runner_ = io_thread_.task_runner().get();
    CHECK(io_runner_.get());

    edk::InitIPCSupport(this, io_runner_);
    edk::SetParentPipeHandleFromCommandLine();
  }

  ~ScopedAppContext() override {
    edk::ShutdownIPCSupport();
    wait_for_shutdown_event_.Wait();
  }

 private:
  // ProcessDelegate implementation.
  void OnShutdownComplete() override {
    wait_for_shutdown_event_.Signal();
  }

  base::Thread io_thread_;
  scoped_refptr<base::SingleThreadTaskRunner> io_runner_;

  // Used to unblock the main thread on shutdown.
  base::WaitableEvent wait_for_shutdown_event_;

  DISALLOW_COPY_AND_ASSIGN(ScopedAppContext);
};

}  // namespace

void ChildProcessMain(const RunCallback& callback) {
  DCHECK(!base::MessageLoop::current());

  ScopedAppContext app_context;
  callback.Run(GetShellClientRequestFromCommandLine());
}

}  // namespace shell
}  // namespace mojo