OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/shared_impl/callback_tracker.h" | 5 #include "ppapi/shared_impl/callback_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
| 14 #include "ppapi/shared_impl/proxy_lock.h" |
14 #include "ppapi/shared_impl/tracked_callback.h" | 15 #include "ppapi/shared_impl/tracked_callback.h" |
15 | 16 |
16 namespace ppapi { | 17 namespace ppapi { |
17 | 18 |
18 // CallbackTracker ------------------------------------------------------------- | 19 // CallbackTracker ------------------------------------------------------------- |
19 | 20 |
20 CallbackTracker::CallbackTracker() {} | 21 CallbackTracker::CallbackTracker() : abort_all_called_(false) { |
| 22 } |
21 | 23 |
22 void CallbackTracker::AbortAll() { | 24 void CallbackTracker::AbortAll() { |
23 // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). | 25 // Iterate over a copy: |
24 // TODO(viettrungluu): This obviously isn't so efficient. | 26 // 1) because |Abort()| calls |Remove()| (indirectly). |
25 CallbackSetMap pending_callbacks_copy = pending_callbacks_; | 27 // 2) So we can drop the lock before calling in to TrackedCallback. |
| 28 CallbackSetMap pending_callbacks_copy; |
| 29 { |
| 30 base::AutoLock acquire(lock_); |
| 31 pending_callbacks_copy = pending_callbacks_; |
| 32 abort_all_called_ = true; |
| 33 } |
26 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); | 34 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); |
27 it1 != pending_callbacks_copy.end(); | 35 it1 != pending_callbacks_copy.end(); |
28 ++it1) { | 36 ++it1) { |
29 for (CallbackSet::iterator it2 = it1->second.begin(); | 37 for (CallbackSet::iterator it2 = it1->second.begin(); |
30 it2 != it1->second.end(); | 38 it2 != it1->second.end(); |
31 ++it2) { | 39 ++it2) { |
32 (*it2)->Abort(); | 40 (*it2)->Abort(); |
33 } | 41 } |
34 } | 42 } |
35 } | 43 } |
36 | 44 |
37 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { | 45 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { |
38 CHECK(resource_id != 0); | 46 // Only TrackedCallbacks with a valid resource should appear in the tracker. |
39 CallbackSetMap::iterator it1 = pending_callbacks_.find(resource_id); | 47 DCHECK_NE(resource_id, 0); |
40 if (it1 == pending_callbacks_.end()) | 48 CallbackSet callbacks_for_resource; |
41 return; | 49 { |
42 for (CallbackSet::iterator it2 = it1->second.begin(); | 50 base::AutoLock acquire(lock_); |
43 it2 != it1->second.end(); | 51 CallbackSetMap::iterator iter = pending_callbacks_.find(resource_id); |
44 ++it2) { | 52 // The resource may have no callbacks, so it won't be found, and we're done. |
45 // Post the abort. | 53 if (iter == pending_callbacks_.end()) |
46 (*it2)->PostAbort(); | 54 return; |
| 55 // Copy the set so we can drop the lock before calling in to |
| 56 // TrackedCallback. |
| 57 callbacks_for_resource = iter->second; |
| 58 } |
| 59 for (const auto& iter : callbacks_for_resource) { |
| 60 iter->PostAbort(); |
47 } | 61 } |
48 } | 62 } |
49 | 63 |
50 CallbackTracker::~CallbackTracker() { | 64 CallbackTracker::~CallbackTracker() { |
51 // All callbacks must be aborted before destruction. | 65 // All callbacks must be aborted before destruction. |
52 CHECK_EQ(0u, pending_callbacks_.size()); | 66 CHECK_EQ(0u, pending_callbacks_.size()); |
53 } | 67 } |
54 | 68 |
55 void CallbackTracker::Add( | 69 void CallbackTracker::Add( |
56 const scoped_refptr<TrackedCallback>& tracked_callback) { | 70 const scoped_refptr<TrackedCallback>& tracked_callback) { |
| 71 base::AutoLock acquire(lock_); |
| 72 DCHECK(!abort_all_called_); |
57 PP_Resource resource_id = tracked_callback->resource_id(); | 73 PP_Resource resource_id = tracked_callback->resource_id(); |
| 74 // Only TrackedCallbacks with a valid resource should appear in the tracker. |
| 75 DCHECK_NE(resource_id, 0); |
58 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == | 76 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == |
59 pending_callbacks_[resource_id].end()); | 77 pending_callbacks_[resource_id].end()); |
60 pending_callbacks_[resource_id].insert(tracked_callback); | 78 pending_callbacks_[resource_id].insert(tracked_callback); |
61 } | 79 } |
62 | 80 |
63 void CallbackTracker::Remove( | 81 void CallbackTracker::Remove( |
64 const scoped_refptr<TrackedCallback>& tracked_callback) { | 82 const scoped_refptr<TrackedCallback>& tracked_callback) { |
| 83 base::AutoLock acquire(lock_); |
65 CallbackSetMap::iterator map_it = | 84 CallbackSetMap::iterator map_it = |
66 pending_callbacks_.find(tracked_callback->resource_id()); | 85 pending_callbacks_.find(tracked_callback->resource_id()); |
67 DCHECK(map_it != pending_callbacks_.end()); | 86 DCHECK(map_it != pending_callbacks_.end()); |
68 CallbackSet::iterator it = map_it->second.find(tracked_callback); | 87 CallbackSet::iterator it = map_it->second.find(tracked_callback); |
69 DCHECK(it != map_it->second.end()); | 88 DCHECK(it != map_it->second.end()); |
70 map_it->second.erase(it); | 89 map_it->second.erase(it); |
71 | 90 |
72 // If there are no pending callbacks left for this ID, get rid of the entry. | 91 // If there are no pending callbacks left for this ID, get rid of the entry. |
73 if (map_it->second.empty()) | 92 if (map_it->second.empty()) |
74 pending_callbacks_.erase(map_it); | 93 pending_callbacks_.erase(map_it); |
75 } | 94 } |
76 | 95 |
77 } // namespace ppapi | 96 } // namespace ppapi |
OLD | NEW |