Chromium Code Reviews| 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 "webkit/plugins/ppapi/callbacks.h" | 5 #include "ppapi/shared_impl/tracked_callback.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
|
viettrungluu
2011/12/29 03:45:27
Self-nit #2: I don't think this is actually needed
| |
| 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.h" | 12 #include "base/message_loop.h" |
| 13 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
| 14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/shared_impl/callback_tracker.h" | |
| 16 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 17 #include "ppapi/shared_impl/resource.h" | |
| 15 | 18 |
| 16 namespace webkit { | |
| 17 namespace ppapi { | 19 namespace ppapi { |
| 18 | 20 |
| 19 // CallbackTracker ------------------------------------------------------------- | |
| 20 | |
| 21 CallbackTracker::CallbackTracker() { | |
| 22 } | |
| 23 | |
| 24 void CallbackTracker::AbortAll() { | |
| 25 // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). | |
| 26 // TODO(viettrungluu): This obviously isn't so efficient. | |
| 27 CallbackSetMap pending_callbacks_copy = pending_callbacks_; | |
| 28 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); | |
| 29 it1 != pending_callbacks_copy.end(); ++it1) { | |
| 30 for (CallbackSet::iterator it2 = it1->second.begin(); | |
| 31 it2 != it1->second.end(); ++it2) { | |
| 32 (*it2)->Abort(); | |
| 33 } | |
| 34 } | |
| 35 } | |
| 36 | |
| 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(); ++it2) { | |
| 44 // Post the abort. | |
| 45 (*it2)->PostAbort(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 CallbackTracker::~CallbackTracker() { | |
| 50 // All callbacks must be aborted before destruction. | |
| 51 CHECK_EQ(0u, pending_callbacks_.size()); | |
| 52 } | |
| 53 | |
| 54 void CallbackTracker::Add( | |
| 55 const scoped_refptr<TrackedCallback>& tracked_callback) { | |
| 56 PP_Resource resource_id = tracked_callback->resource_id(); | |
| 57 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == | |
| 58 pending_callbacks_[resource_id].end()); | |
| 59 pending_callbacks_[resource_id].insert(tracked_callback); | |
| 60 } | |
| 61 | |
| 62 void CallbackTracker::Remove( | |
| 63 const scoped_refptr<TrackedCallback>& tracked_callback) { | |
| 64 CallbackSetMap::iterator map_it = | |
| 65 pending_callbacks_.find(tracked_callback->resource_id()); | |
| 66 DCHECK(map_it != pending_callbacks_.end()); | |
| 67 CallbackSet::iterator it = map_it->second.find(tracked_callback); | |
| 68 DCHECK(it != map_it->second.end()); | |
| 69 map_it->second.erase(it); | |
| 70 | |
| 71 // If there are no pending callbacks left for this ID, get rid of the entry. | |
| 72 if (map_it->second.empty()) | |
| 73 pending_callbacks_.erase(map_it); | |
| 74 } | |
| 75 | |
| 76 // TrackedCallback ------------------------------------------------------------- | 21 // TrackedCallback ------------------------------------------------------------- |
| 77 | 22 |
| 78 TrackedCallback::TrackedCallback(const scoped_refptr<CallbackTracker>& tracker, | 23 // Note: don't keep a Resource* since it may go out of scope before us. |
| 79 PP_Resource resource_id) | 24 TrackedCallback::TrackedCallback( |
| 25 Resource* resource, | |
| 26 const PP_CompletionCallback& callback) | |
| 80 : ALLOW_THIS_IN_INITIALIZER_LIST(abort_impl_factory_(this)), | 27 : ALLOW_THIS_IN_INITIALIZER_LIST(abort_impl_factory_(this)), |
| 81 tracker_(tracker), | 28 resource_id_(resource->pp_resource()), |
| 82 resource_id_(resource_id), | |
| 83 completed_(false), | 29 completed_(false), |
| 84 aborted_(false) { | 30 aborted_(false), |
| 31 callback_(callback) { | |
| 32 tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance( | |
| 33 resource->pp_instance()), | |
| 85 tracker_->Add(make_scoped_refptr(this)); | 34 tracker_->Add(make_scoped_refptr(this)); |
| 86 } | 35 } |
| 87 | 36 |
| 37 TrackedCallback::~TrackedCallback() { | |
| 38 } | |
| 39 | |
| 88 void TrackedCallback::Abort() { | 40 void TrackedCallback::Abort() { |
| 89 if (!completed()) { | 41 if (!completed()) { |
| 90 aborted_ = true; | 42 aborted_ = true; |
| 91 AbortImpl(); | 43 Run(PP_ERROR_ABORTED); |
| 92 } | 44 } |
| 93 } | 45 } |
| 94 | 46 |
| 95 void TrackedCallback::PostAbort() { | 47 void TrackedCallback::PostAbort() { |
| 96 if (!completed()) { | 48 if (!completed()) { |
| 97 aborted_ = true; | 49 aborted_ = true; |
| 98 // Post a task for the abort (only if necessary). | 50 // Post a task for the abort (only if necessary). |
| 99 if (!abort_impl_factory_.HasWeakPtrs()) { | 51 if (!abort_impl_factory_.HasWeakPtrs()) { |
| 100 MessageLoop::current()->PostTask( | 52 MessageLoop::current()->PostTask( |
| 101 FROM_HERE, | 53 FROM_HERE, |
| 102 base::Bind(&TrackedCallback::AbortImpl, | 54 base::Bind(&TrackedCallback::Abort, |
| 103 abort_impl_factory_.GetWeakPtr())); | 55 abort_impl_factory_.GetWeakPtr())); |
| 104 } | 56 } |
| 105 } | 57 } |
| 106 } | 58 } |
| 107 | 59 |
| 108 TrackedCallback::~TrackedCallback() { | 60 void TrackedCallback::Run(int32_t result) { |
| 109 // The tracker must ensure that the callback is completed (maybe abortively). | |
| 110 DCHECK(completed_); | |
| 111 } | |
| 112 | |
| 113 void TrackedCallback::MarkAsCompleted() { | |
| 114 DCHECK(!completed()); | |
| 115 | |
| 116 // We will be removed; maintain a reference to ensure we won't be deleted | |
| 117 // until we're done. | |
| 118 scoped_refptr<TrackedCallback> thiz = this; | |
| 119 completed_ = true; | |
| 120 tracker_->Remove(thiz); | |
| 121 tracker_ = NULL; | |
| 122 } | |
| 123 | |
| 124 // TrackedCompletionCallback --------------------------------------------------- | |
| 125 | |
| 126 TrackedCompletionCallback::TrackedCompletionCallback( | |
| 127 const scoped_refptr<CallbackTracker>& tracker, | |
| 128 PP_Resource resource_id, | |
| 129 const PP_CompletionCallback& callback) | |
| 130 : TrackedCallback(tracker, resource_id), | |
| 131 callback_(callback) { | |
| 132 } | |
| 133 | |
| 134 void TrackedCompletionCallback::Run(int32_t result) { | |
| 135 if (!completed()) { | 61 if (!completed()) { |
| 136 // Cancel any pending calls. | 62 // Cancel any pending calls. |
| 137 abort_impl_factory_.InvalidateWeakPtrs(); | 63 abort_impl_factory_.InvalidateWeakPtrs(); |
| 138 | 64 |
| 139 // Copy |callback_| and look at |aborted()| now, since |MarkAsCompleted()| | 65 // Copy |callback_| and look at |aborted()| now, since |MarkAsCompleted()| |
| 140 // may delete us. | 66 // may delete us. |
| 141 PP_CompletionCallback callback = callback_; | 67 PP_CompletionCallback callback = callback_; |
| 142 if (aborted()) | 68 if (aborted()) |
| 143 result = PP_ERROR_ABORTED; | 69 result = PP_ERROR_ABORTED; |
| 144 | 70 |
| 145 // Do this before running the callback in case of reentrancy (which | 71 // Do this before running the callback in case of reentrancy (which |
| 146 // shouldn't happen, but avoid strange failures). | 72 // shouldn't happen, but avoid strange failures). |
| 147 MarkAsCompleted(); | 73 MarkAsCompleted(); |
| 148 PP_RunCompletionCallback(&callback, result); | 74 PP_RunCompletionCallback(&callback, result); |
| 149 } | 75 } |
| 150 } | 76 } |
| 151 | 77 |
| 152 void TrackedCompletionCallback::AbortImpl() { | 78 void TrackedCallback::MarkAsCompleted() { |
| 153 Run(PP_ERROR_ABORTED); | 79 DCHECK(!completed()); |
| 80 | |
| 81 // We will be removed; maintain a reference to ensure we won't be deleted | |
| 82 // until we're done. | |
| 83 scoped_refptr<TrackedCallback> thiz = this; | |
| 84 completed_ = true; | |
| 85 tracker_->Remove(thiz); | |
| 86 tracker_ = NULL; | |
| 154 } | 87 } |
| 155 | 88 |
| 156 } // namespace ppapi | 89 } // namespace ppapi |
| 157 } // namespace webkit | |
| OLD | NEW |