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 "ipc/mojo/scoped_ipc_support.h" |
| 6 |
| 7 #include "base/bind.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" |
| 16 |
| 17 namespace IPC { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class IPCSupportInitializer : public mojo::embedder::ProcessDelegate { |
| 22 public: |
| 23 IPCSupportInitializer() |
| 24 : init_count_(0), |
| 25 shutting_down_(false) { |
| 26 } |
| 27 |
| 28 ~IPCSupportInitializer() override {} |
| 29 |
| 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); |
| 34 |
| 35 if (shutting_down_) { |
| 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; |
| 40 return; |
| 41 } |
| 42 |
| 43 init_count_++; |
| 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()); |
| 50 } |
| 51 } |
| 52 |
| 53 void ShutDown() { |
| 54 base::AutoLock locker(lock_); |
| 55 DCHECK(init_count_ > 0); |
| 56 DCHECK(!shutting_down_); |
| 57 |
| 58 if (init_count_ > 1) { |
| 59 init_count_--; |
| 60 return; |
| 61 } |
| 62 |
| 63 shutting_down_ = true; |
| 64 if (base::MessageLoop::current() && |
| 65 base::MessageLoop::current()->task_runner() == io_thread_task_runner_) { |
| 66 base::AutoUnlock unlocker_(lock_); |
| 67 ShutDownOnIOThread(); |
| 68 } else { |
| 69 io_thread_task_runner_->PostTask( |
| 70 FROM_HERE, |
| 71 base::Bind(&IPCSupportInitializer::ShutDownOnIOThread, |
| 72 base::Unretained(this))); |
| 73 } |
| 74 } |
| 75 |
| 76 private: |
| 77 void ShutDownOnIOThread() { |
| 78 base::AutoLock locker(lock_); |
| 79 if (shutting_down_) { |
| 80 DCHECK(init_count_ == 1); |
| 81 mojo::embedder::ShutdownIPCSupportOnIOThread(); |
| 82 init_count_ = 0; |
| 83 shutting_down_ = false; |
| 84 io_thread_task_runner_ = nullptr; |
| 85 } |
| 86 } |
| 87 |
| 88 void OnShutdownComplete() override {} |
| 89 |
| 90 base::Lock lock_; |
| 91 size_t init_count_; |
| 92 bool shutting_down_; |
| 93 |
| 94 scoped_refptr<base::TaskRunner> io_thread_task_runner_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(IPCSupportInitializer); |
| 97 }; |
| 98 |
| 99 base::LazyInstance<IPCSupportInitializer>::Leaky ipc_support_initializer; |
| 100 |
| 101 } // namespace |
| 102 |
| 103 ScopedIPCSupport::ScopedIPCSupport( |
| 104 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
| 105 ipc_support_initializer.Get().Init(io_thread_task_runner); |
| 106 } |
| 107 |
| 108 ScopedIPCSupport::~ScopedIPCSupport() { |
| 109 ipc_support_initializer.Get().ShutDown(); |
| 110 } |
| 111 |
| 112 } // namespace IPC |
OLD | NEW |