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