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.
4 
5 #include "mojo/core/embedder/scoped_ipc_support.h"
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "mojo/core/core.h"
12 
13 namespace mojo {
14 namespace core {
15 
16 namespace {
17 
ShutdownIPCSupport(const base::Closure & callback)18 void ShutdownIPCSupport(const base::Closure& callback) {
19   Core::Get()->RequestShutdown(callback);
20 }
21 
22 }  // namespace
23 
ScopedIPCSupport(scoped_refptr<base::TaskRunner> io_thread_task_runner,ShutdownPolicy shutdown_policy)24 ScopedIPCSupport::ScopedIPCSupport(
25     scoped_refptr<base::TaskRunner> io_thread_task_runner,
26     ShutdownPolicy shutdown_policy)
27     : shutdown_policy_(shutdown_policy) {
28   Core::Get()->SetIOTaskRunner(io_thread_task_runner);
29 }
30 
~ScopedIPCSupport()31 ScopedIPCSupport::~ScopedIPCSupport() {
32   if (shutdown_policy_ == ShutdownPolicy::FAST) {
33     ShutdownIPCSupport(base::DoNothing());
34     return;
35   }
36 
37   base::WaitableEvent shutdown_event(
38       base::WaitableEvent::ResetPolicy::MANUAL,
39       base::WaitableEvent::InitialState::NOT_SIGNALED);
40   ShutdownIPCSupport(base::Bind(&base::WaitableEvent::Signal,
41                                 base::Unretained(&shutdown_event)));
42 
43   base::ScopedAllowBaseSyncPrimitives allow_io;
44   shutdown_event.Wait();
45 }
46 
47 }  // namespace core
48 }  // namespace mojo
49