| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/public/cpp/system/watcher.h" | 5 #include "mojo/public/cpp/system/watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 49 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 50 weak_factory_(this) { | 50 weak_factory_(this) { |
| 51 weak_self_ = weak_factory_.GetWeakPtr(); | 51 weak_self_ = weak_factory_.GetWeakPtr(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 Watcher::~Watcher() { | 54 Watcher::~Watcher() { |
| 55 if(IsWatching()) | 55 if(IsWatching()) |
| 56 Cancel(); | 56 Cancel(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void Watcher::SetAllowSyncDispatch(bool allowed) { |
| 60 allow_sync_dispatch_ = allowed; |
| 61 } |
| 62 |
| 59 bool Watcher::IsWatching() const { | 63 bool Watcher::IsWatching() const { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 return handle_.is_valid(); | 65 return handle_.is_valid(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 MojoResult Watcher::Start(Handle handle, | 68 MojoResult Watcher::Start(Handle handle, |
| 65 MojoHandleSignals signals, | 69 MojoHandleSignals signals, |
| 66 const ReadyCallback& callback) { | 70 const ReadyCallback& callback) { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 DCHECK(!IsWatching()); | 72 DCHECK(!IsWatching()); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // static | 125 // static |
| 122 void Watcher::CallOnHandleReady(uintptr_t context, | 126 void Watcher::CallOnHandleReady(uintptr_t context, |
| 123 MojoResult result, | 127 MojoResult result, |
| 124 MojoHandleSignalsState signals_state) { | 128 MojoHandleSignalsState signals_state) { |
| 125 // NOTE: It is safe to assume the Watcher still exists because this callback | 129 // NOTE: It is safe to assume the Watcher still exists because this callback |
| 126 // will never be run after the Watcher's destructor. | 130 // will never be run after the Watcher's destructor. |
| 127 // | 131 // |
| 128 // TODO: Maybe we should also expose |signals_state| throught he Watcher API. | 132 // TODO: Maybe we should also expose |signals_state| throught he Watcher API. |
| 129 // Current HandleWatcher users have no need for it, so it's omitted here. | 133 // Current HandleWatcher users have no need for it, so it's omitted here. |
| 130 Watcher* watcher = reinterpret_cast<Watcher*>(context); | 134 Watcher* watcher = reinterpret_cast<Watcher*>(context); |
| 131 watcher->task_runner_->PostTask( | 135 |
| 132 FROM_HERE, | 136 if (watcher->task_runner_->RunsTasksOnCurrentThread() && |
| 133 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); | 137 watcher->allow_sync_dispatch_) { |
| 138 watcher->OnHandleReady(result); |
| 139 } else { |
| 140 watcher->task_runner_->PostTask( |
| 141 FROM_HERE, |
| 142 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); |
| 143 } |
| 134 } | 144 } |
| 135 | 145 |
| 136 } // namespace mojo | 146 } // namespace mojo |
| OLD | NEW |