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"
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"
21 class IPCSupportInitializer
: public mojo::embedder::ProcessDelegate
{
23 IPCSupportInitializer()
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
);
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;
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());
54 base::AutoLock
locker(lock_
);
55 DCHECK(init_count_
> 0);
56 DCHECK(!shutting_down_
);
58 if (init_count_
> 1) {
63 shutting_down_
= true;
64 if (base::MessageLoop::current() &&
65 base::MessageLoop::current()->task_runner() == io_thread_task_runner_
) {
66 base::AutoUnlock
unlocker_(lock_
);
69 io_thread_task_runner_
->PostTask(
71 base::Bind(&IPCSupportInitializer::ShutDownOnIOThread
,
72 base::Unretained(this)));
77 void ShutDownOnIOThread() {
78 base::AutoLock
locker(lock_
);
80 DCHECK(init_count_
== 1);
81 mojo::embedder::ShutdownIPCSupportOnIOThread();
83 shutting_down_
= false;
84 io_thread_task_runner_
= nullptr;
88 void OnShutdownComplete() override
{}
94 scoped_refptr
<base::TaskRunner
> io_thread_task_runner_
;
96 DISALLOW_COPY_AND_ASSIGN(IPCSupportInitializer
);
99 base::LazyInstance
<IPCSupportInitializer
>::Leaky ipc_support_initializer
;
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();