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/edk/system/watcher.h" | 5 #include "mojo/edk/system/watcher.h" |
6 | 6 |
7 #include "mojo/edk/system/handle_signals_state.h" | 7 #include "mojo/edk/system/handle_signals_state.h" |
8 #include "mojo/edk/system/request_context.h" | 8 #include "mojo/edk/system/request_context.h" |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
11 namespace edk { | 11 namespace edk { |
12 | 12 |
13 Watcher::Watcher(MojoHandleSignals signals, const WatchCallback& callback) | 13 Watcher::Watcher(MojoHandleSignals signals, const WatchCallback& callback) |
14 : signals_(signals), callback_(callback) { | 14 : signals_(signals), callback_(callback) { |
15 } | 15 } |
16 | 16 |
17 void Watcher::MaybeInvokeCallback(MojoResult result, | 17 void Watcher::MaybeInvokeCallback(MojoResult result, |
18 const HandleSignalsState& state, | 18 const HandleSignalsState& state, |
19 MojoWatchNotificationFlags flags) { | 19 MojoWatchNotificationFlags flags) { |
20 base::AutoLock lock(lock_); | 20 base::AutoLock lock(notification_lock_); |
21 if (is_cancelled_) | 21 if (is_cancelled_) |
22 return; | 22 return; |
23 | 23 |
24 callback_.Run(result, state, flags); | 24 callback_.Run(result, state, flags); |
25 } | 25 } |
26 | 26 |
27 void Watcher::NotifyForStateChange(const HandleSignalsState& signals_state) { | 27 void Watcher::NotifyState(const HandleSignalsState& signals_state) { |
28 RequestContext* request_context = RequestContext::current(); | 28 RequestContext* request_context = RequestContext::current(); |
29 if (signals_state.satisfies(signals_)) { | 29 if (signals_state.satisfies(signals_) && is_armed_.Get()) { |
| 30 is_armed_.Set(false); |
30 request_context->AddWatchNotifyFinalizer( | 31 request_context->AddWatchNotifyFinalizer( |
31 make_scoped_refptr(this), MOJO_RESULT_OK, signals_state); | 32 make_scoped_refptr(this), MOJO_RESULT_OK, signals_state); |
32 } else if (!signals_state.can_satisfy(signals_)) { | 33 } else if (!signals_state.can_satisfy(signals_) && is_armed_.Get()) { |
| 34 is_armed_.Set(false); |
33 request_context->AddWatchNotifyFinalizer( | 35 request_context->AddWatchNotifyFinalizer( |
34 make_scoped_refptr(this), MOJO_RESULT_FAILED_PRECONDITION, | 36 make_scoped_refptr(this), MOJO_RESULT_FAILED_PRECONDITION, |
35 signals_state); | 37 signals_state); |
36 } | 38 } |
37 } | 39 } |
38 | 40 |
39 void Watcher::NotifyClosed() { | 41 void Watcher::NotifyClosed() { |
40 static const HandleSignalsState closed_state = {0, 0}; | 42 static const HandleSignalsState closed_state = {0, 0}; |
41 RequestContext::current()->AddWatchNotifyFinalizer( | 43 RequestContext::current()->AddWatchNotifyFinalizer( |
42 make_scoped_refptr(this), MOJO_RESULT_CANCELLED, closed_state); | 44 make_scoped_refptr(this), MOJO_RESULT_CANCELLED, closed_state); |
43 } | 45 } |
44 | 46 |
| 47 MojoResult Watcher::Arm(const HandleSignalsState& current_state) { |
| 48 if (is_armed_.Get()) |
| 49 return MOJO_RESULT_OK; |
| 50 |
| 51 if (current_state.satisfies(signals_)) |
| 52 return MOJO_RESULT_ALREADY_EXISTS; |
| 53 else if (!current_state.can_satisfy(signals_)) |
| 54 return MOJO_RESULT_FAILED_PRECONDITION; |
| 55 |
| 56 is_armed_.Set(true); |
| 57 return MOJO_RESULT_OK; |
| 58 } |
| 59 |
45 void Watcher::Cancel() { | 60 void Watcher::Cancel() { |
46 base::AutoLock lock(lock_); | 61 base::AutoLock lock(notification_lock_); |
47 is_cancelled_ = true; | 62 is_cancelled_ = true; |
48 } | 63 } |
49 | 64 |
50 Watcher::~Watcher() {} | 65 Watcher::~Watcher() {} |
51 | 66 |
52 } // namespace edk | 67 } // namespace edk |
53 } // namespace mojo | 68 } // namespace mojo |
OLD | NEW |