OLD | NEW |
| (Empty) |
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/edk/test/scoped_ipc_support.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/synchronization/waitable_event.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "mojo/edk/embedder/embedder.h" | |
12 | |
13 namespace mojo { | |
14 namespace edk { | |
15 namespace test { | |
16 | |
17 namespace { | |
18 base::TaskRunner* g_io_task_runner = nullptr; | |
19 } | |
20 | |
21 base::TaskRunner* GetIoTaskRunner() { | |
22 return g_io_task_runner; | |
23 } | |
24 | |
25 ScopedIPCSupport::ScopedIPCSupport( | |
26 scoped_refptr<base::TaskRunner> io_thread_task_runner) | |
27 : shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, | |
28 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
29 g_io_task_runner = io_thread_task_runner.get(); | |
30 InitIPCSupport(this, io_thread_task_runner); | |
31 } | |
32 | |
33 ScopedIPCSupport::~ScopedIPCSupport() { | |
34 // ShutdownIPCSupport always runs OnShutdownComplete on the current | |
35 // ThreadTaskRunnerHandle if set. Otherwise it's run on the IPC thread. We | |
36 // account for both possibilities here to avoid unnecessarily starting a new | |
37 // MessageLoop or blocking the existing one. | |
38 // | |
39 // TODO(rockot): Clean this up. ShutdownIPCSupport should probably always call | |
40 // call OnShutdownComplete from the IPC thread. | |
41 ShutdownIPCSupport(); | |
42 if (base::ThreadTaskRunnerHandle::IsSet()) { | |
43 base::RunLoop run_loop; | |
44 shutdown_closure_ = base::Bind(IgnoreResult(&base::TaskRunner::PostTask), | |
45 base::ThreadTaskRunnerHandle::Get(), | |
46 FROM_HERE, run_loop.QuitClosure()); | |
47 run_loop.Run(); | |
48 } else { | |
49 shutdown_event_.Wait(); | |
50 } | |
51 } | |
52 | |
53 void ScopedIPCSupport::OnShutdownComplete() { | |
54 if (!shutdown_closure_.is_null()) | |
55 shutdown_closure_.Run(); | |
56 else | |
57 shutdown_event_.Signal(); | |
58 } | |
59 | |
60 } // namespace test | |
61 } // namespace edk | |
62 } // namespace mojo | |
OLD | NEW |