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/embedder/scoped_ipc_support.h" | 5 #include "mojo/edk/embedder/scoped_ipc_support.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/macros.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread_restrictions.h" |
10 #include "mojo/edk/embedder/embedder.h" | 11 #include "mojo/edk/embedder/embedder.h" |
11 #include "mojo/edk/embedder/process_delegate.h" | |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 namespace edk { | 14 namespace edk { |
15 | 15 |
16 namespace { | |
17 class IPCSupportInitializer : public mojo::edk::ProcessDelegate { | |
18 public: | |
19 IPCSupportInitializer() {} | |
20 ~IPCSupportInitializer() override {} | |
21 | |
22 void Init(scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
23 CHECK(!io_thread_task_runner_); | |
24 CHECK(io_thread_task_runner); | |
25 io_thread_task_runner_ = io_thread_task_runner; | |
26 | |
27 mojo::edk::InitIPCSupport(this, io_thread_task_runner_); | |
28 } | |
29 | |
30 void ShutDown() { | |
31 CHECK(io_thread_task_runner_); | |
32 mojo::edk::ShutdownIPCSupport(); | |
33 } | |
34 | |
35 private: | |
36 // mojo::edk::ProcessDelegate: | |
37 void OnShutdownComplete() override { | |
38 // TODO(rockot): We should ensure that IO runner shutdown is blocked until | |
39 // this is called. | |
40 } | |
41 | |
42 scoped_refptr<base::TaskRunner> io_thread_task_runner_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(IPCSupportInitializer); | |
45 }; | |
46 | |
47 base::LazyInstance<IPCSupportInitializer>::Leaky ipc_support_initializer; | |
48 | |
49 } // namespace | |
50 | |
51 ScopedIPCSupport::ScopedIPCSupport( | 16 ScopedIPCSupport::ScopedIPCSupport( |
52 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 17 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
53 ipc_support_initializer.Get().Init(io_thread_task_runner); | 18 ShutdownPolicy shutdown_policy) : shutdown_policy_(shutdown_policy) { |
| 19 InitIPCSupport(io_thread_task_runner); |
54 } | 20 } |
55 | 21 |
56 ScopedIPCSupport::~ScopedIPCSupport() { | 22 ScopedIPCSupport::~ScopedIPCSupport() { |
57 ipc_support_initializer.Get().ShutDown(); | 23 if (shutdown_policy_ == ShutdownPolicy::FAST) { |
| 24 ShutdownIPCSupport(base::Bind(&base::DoNothing)); |
| 25 return; |
| 26 } |
| 27 |
| 28 base::WaitableEvent shutdown_event( |
| 29 base::WaitableEvent::ResetPolicy::MANUAL, |
| 30 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 31 ShutdownIPCSupport(base::Bind(&base::WaitableEvent::Signal, |
| 32 base::Unretained(&shutdown_event))); |
| 33 |
| 34 base::ThreadRestrictions::ScopedAllowWait allow_io; |
| 35 shutdown_event.Wait(); |
58 } | 36 } |
59 | 37 |
60 } // namespace edk | 38 } // namespace edk |
61 } // namespace mojo | 39 } // namespace mojo |
OLD | NEW |