1// Copyright (c) 2010 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 "ppapi/shared_impl/callback_tracker.h" 6 7#include <algorithm> 8 9#include "base/bind.h" 10#include "base/compiler_specific.h" 11#include "base/logging.h" 12#include "base/message_loop/message_loop.h" 13#include "ppapi/c/pp_completion_callback.h" 14#include "ppapi/shared_impl/tracked_callback.h" 15 16namespace ppapi { 17 18// CallbackTracker ------------------------------------------------------------- 19 20CallbackTracker::CallbackTracker() {} 21 22void CallbackTracker::AbortAll() { 23 // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). 24 // TODO(viettrungluu): This obviously isn't so efficient. 25 CallbackSetMap pending_callbacks_copy = pending_callbacks_; 26 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); 27 it1 != pending_callbacks_copy.end(); 28 ++it1) { 29 for (CallbackSet::iterator it2 = it1->second.begin(); 30 it2 != it1->second.end(); 31 ++it2) { 32 (*it2)->Abort(); 33 } 34 } 35} 36 37void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { 38 CHECK(resource_id != 0); 39 CallbackSetMap::iterator it1 = pending_callbacks_.find(resource_id); 40 if (it1 == pending_callbacks_.end()) 41 return; 42 for (CallbackSet::iterator it2 = it1->second.begin(); 43 it2 != it1->second.end(); 44 ++it2) { 45 // Post the abort. 46 (*it2)->PostAbort(); 47 } 48} 49 50CallbackTracker::~CallbackTracker() { 51 // All callbacks must be aborted before destruction. 52 CHECK_EQ(0u, pending_callbacks_.size()); 53} 54 55void CallbackTracker::Add( 56 const scoped_refptr<TrackedCallback>& tracked_callback) { 57 PP_Resource resource_id = tracked_callback->resource_id(); 58 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == 59 pending_callbacks_[resource_id].end()); 60 pending_callbacks_[resource_id].insert(tracked_callback); 61} 62 63void CallbackTracker::Remove( 64 const scoped_refptr<TrackedCallback>& tracked_callback) { 65 CallbackSetMap::iterator map_it = 66 pending_callbacks_.find(tracked_callback->resource_id()); 67 DCHECK(map_it != pending_callbacks_.end()); 68 CallbackSet::iterator it = map_it->second.find(tracked_callback); 69 DCHECK(it != map_it->second.end()); 70 map_it->second.erase(it); 71 72 // If there are no pending callbacks left for this ID, get rid of the entry. 73 if (map_it->second.empty()) 74 pending_callbacks_.erase(map_it); 75} 76 77} // namespace ppapi 78