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"
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"
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();
29 for (CallbackSet::iterator it2
= it1
->second
.begin();
30 it2
!= it1
->second
.end();
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())
42 for (CallbackSet::iterator it2
= it1
->second
.begin();
43 it2
!= it1
->second
.end();
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
);