OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/test/scoped_ipc_support.h" | 5 #include "mojo/edk/test/scoped_ipc_support.h" |
6 | 6 |
7 #include <utility> | 7 #include "base/bind.h" |
8 | 8 #include "base/run_loop.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
10 #include "mojo/edk/embedder/embedder.h" | 11 #include "mojo/edk/embedder/embedder.h" |
11 | 12 |
12 namespace mojo { | 13 namespace mojo { |
13 namespace edk { | 14 namespace edk { |
14 namespace test { | 15 namespace test { |
15 | 16 |
16 namespace { | 17 namespace { |
17 base::TaskRunner* g_io_task_runner = nullptr; | 18 base::TaskRunner* g_io_task_runner = nullptr; |
18 } | 19 } |
19 | 20 |
20 base::TaskRunner* GetIoTaskRunner() { | 21 base::TaskRunner* GetIoTaskRunner() { |
21 return g_io_task_runner; | 22 return g_io_task_runner; |
22 } | 23 } |
23 | 24 |
24 namespace internal { | |
25 | |
26 ScopedIPCSupportHelper::ScopedIPCSupportHelper() { | |
27 } | |
28 | |
29 ScopedIPCSupportHelper::~ScopedIPCSupportHelper() { | |
30 ShutdownIPCSupport(); | |
31 run_loop_.Run(); | |
32 } | |
33 | |
34 void ScopedIPCSupportHelper::Init( | |
35 ProcessDelegate* process_delegate, | |
36 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
37 io_thread_task_runner_ = io_thread_task_runner; | |
38 InitIPCSupport(process_delegate, io_thread_task_runner_); | |
39 } | |
40 | |
41 void ScopedIPCSupportHelper::OnShutdownCompleteImpl() { | |
42 run_loop_.Quit(); | |
43 } | |
44 | |
45 } // namespace internal | |
46 | |
47 ScopedIPCSupport::ScopedIPCSupport( | 25 ScopedIPCSupport::ScopedIPCSupport( |
48 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 26 scoped_refptr<base::TaskRunner> io_thread_task_runner) |
| 27 : shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 28 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
49 g_io_task_runner = io_thread_task_runner.get(); | 29 g_io_task_runner = io_thread_task_runner.get(); |
50 helper_.Init(this, std::move(io_thread_task_runner)); | 30 InitIPCSupport(this, io_thread_task_runner); |
51 } | 31 } |
52 | 32 |
53 ScopedIPCSupport::~ScopedIPCSupport() { | 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 } |
54 } | 51 } |
55 | 52 |
56 void ScopedIPCSupport::OnShutdownComplete() { | 53 void ScopedIPCSupport::OnShutdownComplete() { |
57 helper_.OnShutdownCompleteImpl(); | 54 if (!shutdown_closure_.is_null()) |
| 55 shutdown_closure_.Run(); |
| 56 else |
| 57 shutdown_event_.Signal(); |
58 } | 58 } |
59 | 59 |
60 } // namespace test | 60 } // namespace test |
61 } // namespace edk | 61 } // namespace edk |
62 } // namespace mojo | 62 } // namespace mojo |
OLD | NEW |