Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / ipc / mojo / scoped_ipc_support.cc
blobfafc9c2614b8a7dc51d03d7ee6bbb51f1d62c2c5
1 // Copyright 2015 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.
5 #include "ipc/mojo/scoped_ipc_support.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/synchronization/condition_variable.h"
12 #include "base/synchronization/lock.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
15 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h"
17 namespace IPC {
19 namespace {
21 class IPCSupportInitializer : public mojo::embedder::ProcessDelegate {
22 public:
23 IPCSupportInitializer()
24 : init_count_(0),
25 shutting_down_(false) {
28 ~IPCSupportInitializer() override {}
30 void Init(scoped_refptr<base::TaskRunner> io_thread_task_runner) {
31 base::AutoLock locker(lock_);
32 DCHECK((init_count_ == 0 && !io_thread_task_runner_) ||
33 io_thread_task_runner_ == io_thread_task_runner);
35 if (shutting_down_) {
36 // If reinitialized before a pending shutdown task is executed, we
37 // effectively cancel the shutdown task.
38 DCHECK(init_count_ == 1);
39 shutting_down_ = false;
40 return;
43 init_count_++;
44 if (init_count_ == 1) {
45 io_thread_task_runner_ = io_thread_task_runner;
46 mojo::embedder::InitIPCSupport(mojo::embedder::ProcessType::NONE,
47 io_thread_task_runner_,
48 this, io_thread_task_runner_,
49 mojo::embedder::ScopedPlatformHandle());
53 void ShutDown() {
54 base::AutoLock locker(lock_);
55 DCHECK(init_count_ > 0);
56 DCHECK(!shutting_down_);
58 if (init_count_ > 1) {
59 init_count_--;
60 return;
63 shutting_down_ = true;
64 if (base::MessageLoop::current() &&
65 base::MessageLoop::current()->task_runner() == io_thread_task_runner_) {
66 base::AutoUnlock unlocker_(lock_);
67 ShutDownOnIOThread();
68 } else {
69 io_thread_task_runner_->PostTask(
70 FROM_HERE,
71 base::Bind(&IPCSupportInitializer::ShutDownOnIOThread,
72 base::Unretained(this)));
76 private:
77 void ShutDownOnIOThread() {
78 base::AutoLock locker(lock_);
79 if (shutting_down_) {
80 DCHECK(init_count_ == 1);
81 mojo::embedder::ShutdownIPCSupportOnIOThread();
82 init_count_ = 0;
83 shutting_down_ = false;
84 io_thread_task_runner_ = nullptr;
88 void OnShutdownComplete() override {}
90 base::Lock lock_;
91 size_t init_count_;
92 bool shutting_down_;
94 scoped_refptr<base::TaskRunner> io_thread_task_runner_;
96 DISALLOW_COPY_AND_ASSIGN(IPCSupportInitializer);
99 base::LazyInstance<IPCSupportInitializer>::Leaky ipc_support_initializer;
101 } // namespace
103 ScopedIPCSupport::ScopedIPCSupport(
104 scoped_refptr<base::TaskRunner> io_thread_task_runner) {
105 ipc_support_initializer.Get().Init(io_thread_task_runner);
108 ScopedIPCSupport::~ScopedIPCSupport() {
109 ipc_support_initializer.Get().ShutDown();
112 } // namespace IPC