Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ppapi / shared_impl / callback_tracker.cc
blobfeefb0e117d70aa862553838656da09763927824
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.
5 #include "ppapi/shared_impl/callback_tracker.h"
7 #include <algorithm>
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"
16 namespace ppapi {
18 // CallbackTracker -------------------------------------------------------------
20 CallbackTracker::CallbackTracker() {}
22 void 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();
37 void 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();
50 CallbackTracker::~CallbackTracker() {
51 // All callbacks must be aborted before destruction.
52 CHECK_EQ(0u, pending_callbacks_.size());
55 void 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);
63 void 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);
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);
77 } // namespace ppapi