| 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 "ipc/mojo/scoped_ipc_support.h" | 5 #include "ipc/mojo/scoped_ipc_support.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/synchronization/condition_variable.h" | 14 #include "base/synchronization/condition_variable.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | 18 #include "mojo/edk/embedder/embedder.h" |
| 19 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h" | 19 #include "mojo/edk/embedder/process_delegate.h" |
| 20 | 20 |
| 21 namespace IPC { | 21 namespace IPC { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 // TODO(use_chrome_edk) | 24 class IPCSupportInitializer : public mojo::edk::ProcessDelegate { |
| 25 //class IPCSupportInitializer : public mojo::edk::ProcessDelegate { | |
| 26 class IPCSupportInitializer : public mojo::embedder::ProcessDelegate { | |
| 27 public: | 25 public: |
| 28 IPCSupportInitializer() | 26 IPCSupportInitializer() |
| 29 : init_count_(0), | 27 : init_count_(0), |
| 30 shutting_down_(false), | 28 shutting_down_(false), |
| 31 was_shut_down_(false), | 29 was_shut_down_(false), |
| 32 observer_(nullptr) {} | 30 observer_(nullptr) {} |
| 33 | 31 |
| 34 ~IPCSupportInitializer() override { DCHECK(!observer_); } | 32 ~IPCSupportInitializer() override { DCHECK(!observer_); } |
| 35 | 33 |
| 36 void Init(scoped_refptr<base::TaskRunner> io_thread_task_runner); | 34 void Init(scoped_refptr<base::TaskRunner> io_thread_task_runner); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 55 initializer_->ShutDown(true); | 53 initializer_->ShutDown(true); |
| 56 } | 54 } |
| 57 | 55 |
| 58 IPCSupportInitializer* initializer_; | 56 IPCSupportInitializer* initializer_; |
| 59 | 57 |
| 60 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); | 58 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); |
| 61 }; | 59 }; |
| 62 | 60 |
| 63 void ShutDownOnIOThread(); | 61 void ShutDownOnIOThread(); |
| 64 | 62 |
| 65 // mojo::embedder::ProcessDelegate: | 63 // mojo::edk::ProcessDelegate: |
| 66 void OnShutdownComplete() override {} | 64 void OnShutdownComplete() override {} |
| 67 | 65 |
| 68 static void WatchMessageLoopOnIOThread(MessageLoopObserver* observer); | 66 static void WatchMessageLoopOnIOThread(MessageLoopObserver* observer); |
| 69 | 67 |
| 70 base::Lock lock_; | 68 base::Lock lock_; |
| 71 size_t init_count_; | 69 size_t init_count_; |
| 72 bool shutting_down_; | 70 bool shutting_down_; |
| 73 | 71 |
| 74 // This is used to track whether shutdown has occurred yet, since we can be | 72 // This is used to track whether shutdown has occurred yet, since we can be |
| 75 // shut down by either the scoper or IO MessageLoop destruction. | 73 // shut down by either the scoper or IO MessageLoop destruction. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 99 return; | 97 return; |
| 100 } | 98 } |
| 101 | 99 |
| 102 init_count_++; | 100 init_count_++; |
| 103 if (init_count_ == 1) { | 101 if (init_count_ == 1) { |
| 104 was_shut_down_ = false; | 102 was_shut_down_ = false; |
| 105 observer_ = new MessageLoopObserver(this); | 103 observer_ = new MessageLoopObserver(this); |
| 106 io_thread_task_runner_ = io_thread_task_runner; | 104 io_thread_task_runner_ = io_thread_task_runner; |
| 107 io_thread_task_runner_->PostTask( | 105 io_thread_task_runner_->PostTask( |
| 108 FROM_HERE, base::Bind(&WatchMessageLoopOnIOThread, observer_)); | 106 FROM_HERE, base::Bind(&WatchMessageLoopOnIOThread, observer_)); |
| 109 mojo::embedder::InitIPCSupport( | 107 mojo::edk::InitIPCSupport(this, io_thread_task_runner_); |
| 110 mojo::embedder::ProcessType::NONE, this, io_thread_task_runner_, | |
| 111 mojo::embedder::ScopedPlatformHandle()); | |
| 112 } | 108 } |
| 113 } | 109 } |
| 114 | 110 |
| 115 void IPCSupportInitializer::ShutDown(bool force) { | 111 void IPCSupportInitializer::ShutDown(bool force) { |
| 116 base::AutoLock locker(lock_); | 112 base::AutoLock locker(lock_); |
| 117 if (shutting_down_ || was_shut_down_) | 113 if (shutting_down_ || was_shut_down_) |
| 118 return; | 114 return; |
| 119 DCHECK(init_count_ > 0); | 115 DCHECK(init_count_ > 0); |
| 120 if (init_count_ > 1 && !force) { | 116 if (init_count_ > 1 && !force) { |
| 121 init_count_--; | 117 init_count_--; |
| 122 return; | 118 return; |
| 123 } | 119 } |
| 124 | 120 |
| 125 shutting_down_ = true; | 121 shutting_down_ = true; |
| 126 if (base::MessageLoop::current() && | 122 if (base::MessageLoop::current() && |
| 127 base::MessageLoop::current()->task_runner() == io_thread_task_runner_) { | 123 base::MessageLoop::current()->task_runner() == io_thread_task_runner_) { |
| 128 base::AutoUnlock unlocker_(lock_); | 124 base::AutoUnlock unlocker_(lock_); |
| 129 ShutDownOnIOThread(); | 125 ShutDownOnIOThread(); |
| 130 } else { | 126 } else { |
| 131 io_thread_task_runner_->PostTask( | 127 io_thread_task_runner_->PostTask( |
| 132 FROM_HERE, base::Bind(&IPCSupportInitializer::ShutDownOnIOThread, | 128 FROM_HERE, base::Bind(&IPCSupportInitializer::ShutDownOnIOThread, |
| 133 base::Unretained(this))); | 129 base::Unretained(this))); |
| 134 } | 130 } |
| 135 } | 131 } |
| 136 | 132 |
| 137 void IPCSupportInitializer::ShutDownOnIOThread() { | 133 void IPCSupportInitializer::ShutDownOnIOThread() { |
| 138 base::AutoLock locker(lock_); | 134 base::AutoLock locker(lock_); |
| 139 if (shutting_down_ && !was_shut_down_) { | 135 if (shutting_down_ && !was_shut_down_) { |
| 140 mojo::embedder::ShutdownIPCSupportOnIOThread(); | 136 mojo::edk::ShutdownIPCSupportOnIOThread(); |
| 141 init_count_ = 0; | 137 init_count_ = 0; |
| 142 shutting_down_ = false; | 138 shutting_down_ = false; |
| 143 io_thread_task_runner_ = nullptr; | 139 io_thread_task_runner_ = nullptr; |
| 144 was_shut_down_ = true; | 140 was_shut_down_ = true; |
| 145 if (observer_) { | 141 if (observer_) { |
| 146 delete observer_; | 142 delete observer_; |
| 147 observer_ = nullptr; | 143 observer_ = nullptr; |
| 148 } | 144 } |
| 149 } | 145 } |
| 150 } | 146 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 162 ScopedIPCSupport::ScopedIPCSupport( | 158 ScopedIPCSupport::ScopedIPCSupport( |
| 163 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 159 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
| 164 ipc_support_initializer.Get().Init(io_thread_task_runner); | 160 ipc_support_initializer.Get().Init(io_thread_task_runner); |
| 165 } | 161 } |
| 166 | 162 |
| 167 ScopedIPCSupport::~ScopedIPCSupport() { | 163 ScopedIPCSupport::~ScopedIPCSupport() { |
| 168 ipc_support_initializer.Get().ShutDown(false); | 164 ipc_support_initializer.Get().ShutDown(false); |
| 169 } | 165 } |
| 170 | 166 |
| 171 } // namespace IPC | 167 } // namespace IPC |
| OLD | NEW |