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_set.h" 6 7 #include "mojo/edk/system/request_context.h" 8 #include "mojo/public/c/system/types.h" 9 10 namespace mojo { 11 namespace edk { 12 WatcherSet()13WatcherSet::WatcherSet() {} 14 ~WatcherSet()15WatcherSet::~WatcherSet() {} 16 NotifyForStateChange(const HandleSignalsState & state)17void WatcherSet::NotifyForStateChange(const HandleSignalsState& state) { 18 for (const auto& entry : watchers_) 19 entry.second->NotifyForStateChange(state); 20 } 21 NotifyClosed()22void WatcherSet::NotifyClosed() { 23 for (const auto& entry : watchers_) 24 entry.second->NotifyClosed(); 25 } 26 Add(MojoHandleSignals signals,const Watcher::WatchCallback & callback,uintptr_t context,const HandleSignalsState & current_state)27MojoResult WatcherSet::Add(MojoHandleSignals signals, 28 const Watcher::WatchCallback& callback, 29 uintptr_t context, 30 const HandleSignalsState& current_state) { 31 auto it = watchers_.find(context); 32 if (it != watchers_.end()) 33 return MOJO_RESULT_ALREADY_EXISTS; 34 35 if (!current_state.can_satisfy(signals)) 36 return MOJO_RESULT_FAILED_PRECONDITION; 37 38 scoped_refptr<Watcher> watcher(new Watcher(signals, callback)); 39 watchers_.insert(std::make_pair(context, watcher)); 40 41 watcher->NotifyForStateChange(current_state); 42 43 return MOJO_RESULT_OK; 44 } 45 Remove(uintptr_t context)46MojoResult WatcherSet::Remove(uintptr_t context) { 47 auto it = watchers_.find(context); 48 if (it == watchers_.end()) 49 return MOJO_RESULT_INVALID_ARGUMENT; 50 51 RequestContext::current()->AddWatchCancelFinalizer(it->second); 52 watchers_.erase(it); 53 return MOJO_RESULT_OK; 54 } 55 56 } // namespace edk 57 } // namespace mojo 58