OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/system/watcher.h" |
| 6 |
| 7 #include "mojo/edk/system/handle_signals_state.h" |
| 8 #include "mojo/edk/system/request_context.h" |
| 9 |
| 10 namespace mojo { |
| 11 namespace edk { |
| 12 |
| 13 Watcher::Watcher(MojoHandleSignals signals, const WatchCallback& callback) |
| 14 : signals_(signals), callback_(callback) { |
| 15 } |
| 16 |
| 17 void Watcher::MaybeInvokeCallback(MojoResult result, |
| 18 const HandleSignalsState& state) { |
| 19 base::AutoLock lock(lock_); |
| 20 if (is_cancelled_) |
| 21 return; |
| 22 |
| 23 callback_.Run(result, state); |
| 24 } |
| 25 |
| 26 void Watcher::NotifyForStateChange(const HandleSignalsState& signals_state) { |
| 27 RequestContext* request_context = RequestContext::current(); |
| 28 if (signals_state.satisfies(signals_)) { |
| 29 request_context->AddWatchNotifyFinalizer( |
| 30 make_scoped_refptr(this), MOJO_RESULT_OK, signals_state); |
| 31 } else if (!signals_state.can_satisfy(signals_)) { |
| 32 request_context->AddWatchNotifyFinalizer(make_scoped_refptr(this), |
| 33 MOJO_RESULT_FAILED_PRECONDITION, |
| 34 signals_state); |
| 35 } |
| 36 } |
| 37 |
| 38 void Watcher::NotifyClosed() { |
| 39 static const HandleSignalsState closed_state = {0, 0}; |
| 40 RequestContext::current()->AddWatchNotifyFinalizer( |
| 41 make_scoped_refptr(this), MOJO_RESULT_CANCELLED, closed_state); |
| 42 } |
| 43 |
| 44 void Watcher::Cancel() { |
| 45 base::AutoLock lock(lock_); |
| 46 is_cancelled_ = true; |
| 47 } |
| 48 |
| 49 Watcher::~Watcher() {} |
| 50 |
| 51 } // namespace edk |
| 52 } // namespace mojo |
OLD | NEW |