Chromium Code Reviews| Index: mojo/edk/system/watcher_set.cc |
| diff --git a/mojo/edk/system/watcher_set.cc b/mojo/edk/system/watcher_set.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a9c0b3fea17f2053e832712ffa327502f26d581 |
| --- /dev/null |
| +++ b/mojo/edk/system/watcher_set.cc |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/edk/system/watcher_set.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/containers/stack_container.h" |
|
Anand Mistry (off Chromium)
2016/02/29 12:03:35
Unused?
Ken Rockot(use gerrit already)
2016/02/29 20:59:06
Removed
|
| + |
| +namespace mojo { |
| +namespace edk { |
| + |
| +struct HandleSignalsState; |
|
Anand Mistry (off Chromium)
2016/02/29 12:03:35
Remove and #include "mojo/edk/system/handle_signal
Ken Rockot(use gerrit already)
2016/02/29 20:59:06
Done
|
| + |
| +WatcherSet::Watcher::Watcher(MojoHandleSignals signals, |
| + const Dispatcher::WatchCallback& callback) |
| + : signals(signals), callback(callback) {} |
| + |
| +WatcherSet::Watcher::~Watcher() {} |
| + |
| +WatcherSet::WatcherSet() {} |
| + |
| +WatcherSet::~WatcherSet() {} |
| + |
| +void WatcherSet::NotifyOfStateChange( |
| + const HandleSignalsState& state, |
| + Dispatcher::RequestContext* request_context) { |
| + MojoHandleSignalsState signals_state; |
|
Anand Mistry (off Chromium)
2016/02/29 12:03:35
Given that HandleSignalsSate inherits from MojoHan
Ken Rockot(use gerrit already)
2016/02/29 20:59:06
Ah right... static_cast :) Done
|
| + signals_state.satisfied_signals = state.satisfied_signals; |
| + signals_state.satisfiable_signals = state.satisfiable_signals; |
| + for (const auto& entry : watchers_) |
| + MaybeNotifyWatcher(entry.second, signals_state, request_context); |
| +} |
| + |
| +void WatcherSet::CancelAll(Dispatcher::RequestContext* request_context) { |
| + MojoHandleSignalsState signals_state = {0, 0}; |
| + for (const auto& entry : watchers_) { |
| + request_context->AddFinalizer(base::Bind( |
| + entry.second.callback, MOJO_RESULT_CANCELLED, signals_state)); |
| + } |
| + watchers_.clear(); |
| +} |
| + |
| +MojoResult WatcherSet::Add(MojoHandleSignals signals, |
| + const Dispatcher::WatchCallback& callback, |
| + uintptr_t context, |
| + const HandleSignalsState& current_state, |
| + Dispatcher::RequestContext* request_context) { |
| + auto it = watchers_.find(context); |
| + if (it != watchers_.end()) |
| + return MOJO_RESULT_ALREADY_EXISTS; |
| + |
| + if (!current_state.can_satisfy(signals)) |
| + return MOJO_RESULT_FAILED_PRECONDITION; |
| + |
| + Watcher watcher(signals, callback); |
| + watchers_.insert(std::make_pair(context, watcher)); |
| + |
| + MojoHandleSignalsState signals_state; |
| + signals_state.satisfied_signals = current_state.satisfied_signals; |
| + signals_state.satisfiable_signals = current_state.satisfiable_signals; |
| + MaybeNotifyWatcher(watcher, signals_state, request_context); |
| + |
| + return MOJO_RESULT_OK; |
| +} |
| + |
| +MojoResult WatcherSet::Remove(uintptr_t context) { |
| + auto it = watchers_.find(context); |
| + if (it == watchers_.end()) |
| + return MOJO_RESULT_INVALID_ARGUMENT; |
| + watchers_.erase(it); |
| + return MOJO_RESULT_OK; |
| +} |
| + |
| +void WatcherSet::MaybeNotifyWatcher( |
| + const Watcher& watcher, |
| + const MojoHandleSignalsState& current_state, |
| + Dispatcher::RequestContext* request_context) { |
| + if (current_state.satisfied_signals & watcher.signals) { |
| + request_context->AddFinalizer( |
| + base::Bind(watcher.callback, MOJO_RESULT_OK, current_state)); |
| + } else if (!(current_state.satisfiable_signals & watcher.signals)) { |
| + request_context->AddFinalizer(base::Bind( |
| + watcher.callback, MOJO_RESULT_FAILED_PRECONDITION, current_state)); |
| + } |
| +} |
| + |
| +} // namespace edk |
| +} // namespace mojo |