Chromium Code Reviews| Index: ppapi/shared_impl/tracked_callback.cc |
| =================================================================== |
| --- ppapi/shared_impl/tracked_callback.cc (revision 115831) |
| +++ ppapi/shared_impl/tracked_callback.cc (working copy) |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "webkit/plugins/ppapi/callbacks.h" |
| +#include "ppapi/shared_impl/tracked_callback.h" |
| #include <algorithm> |
|
viettrungluu
2011/12/29 03:45:27
Self-nit #2: I don't think this is actually needed
|
| @@ -12,83 +12,35 @@ |
| #include "base/message_loop.h" |
| #include "ppapi/c/pp_completion_callback.h" |
| #include "ppapi/c/pp_errors.h" |
| +#include "ppapi/shared_impl/callback_tracker.h" |
| +#include "ppapi/shared_impl/ppapi_globals.h" |
| +#include "ppapi/shared_impl/resource.h" |
| -namespace webkit { |
| namespace ppapi { |
| -// CallbackTracker ------------------------------------------------------------- |
| - |
| -CallbackTracker::CallbackTracker() { |
| -} |
| - |
| -void CallbackTracker::AbortAll() { |
| - // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). |
| - // TODO(viettrungluu): This obviously isn't so efficient. |
| - CallbackSetMap pending_callbacks_copy = pending_callbacks_; |
| - for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); |
| - it1 != pending_callbacks_copy.end(); ++it1) { |
| - for (CallbackSet::iterator it2 = it1->second.begin(); |
| - it2 != it1->second.end(); ++it2) { |
| - (*it2)->Abort(); |
| - } |
| - } |
| -} |
| - |
| -void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { |
| - CHECK(resource_id != 0); |
| - CallbackSetMap::iterator it1 = pending_callbacks_.find(resource_id); |
| - if (it1 == pending_callbacks_.end()) |
| - return; |
| - for (CallbackSet::iterator it2 = it1->second.begin(); |
| - it2 != it1->second.end(); ++it2) { |
| - // Post the abort. |
| - (*it2)->PostAbort(); |
| - } |
| -} |
| - |
| -CallbackTracker::~CallbackTracker() { |
| - // All callbacks must be aborted before destruction. |
| - CHECK_EQ(0u, pending_callbacks_.size()); |
| -} |
| - |
| -void CallbackTracker::Add( |
| - const scoped_refptr<TrackedCallback>& tracked_callback) { |
| - PP_Resource resource_id = tracked_callback->resource_id(); |
| - DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == |
| - pending_callbacks_[resource_id].end()); |
| - pending_callbacks_[resource_id].insert(tracked_callback); |
| -} |
| - |
| -void CallbackTracker::Remove( |
| - const scoped_refptr<TrackedCallback>& tracked_callback) { |
| - CallbackSetMap::iterator map_it = |
| - pending_callbacks_.find(tracked_callback->resource_id()); |
| - DCHECK(map_it != pending_callbacks_.end()); |
| - CallbackSet::iterator it = map_it->second.find(tracked_callback); |
| - DCHECK(it != map_it->second.end()); |
| - map_it->second.erase(it); |
| - |
| - // If there are no pending callbacks left for this ID, get rid of the entry. |
| - if (map_it->second.empty()) |
| - pending_callbacks_.erase(map_it); |
| -} |
| - |
| // TrackedCallback ------------------------------------------------------------- |
| -TrackedCallback::TrackedCallback(const scoped_refptr<CallbackTracker>& tracker, |
| - PP_Resource resource_id) |
| +// Note: don't keep a Resource* since it may go out of scope before us. |
| +TrackedCallback::TrackedCallback( |
| + Resource* resource, |
| + const PP_CompletionCallback& callback) |
| : ALLOW_THIS_IN_INITIALIZER_LIST(abort_impl_factory_(this)), |
| - tracker_(tracker), |
| - resource_id_(resource_id), |
| + resource_id_(resource->pp_resource()), |
| completed_(false), |
| - aborted_(false) { |
| + aborted_(false), |
| + callback_(callback) { |
| + tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance( |
| + resource->pp_instance()), |
| tracker_->Add(make_scoped_refptr(this)); |
| } |
| +TrackedCallback::~TrackedCallback() { |
| +} |
| + |
| void TrackedCallback::Abort() { |
| if (!completed()) { |
| aborted_ = true; |
| - AbortImpl(); |
| + Run(PP_ERROR_ABORTED); |
| } |
| } |
| @@ -99,39 +51,13 @@ |
| if (!abort_impl_factory_.HasWeakPtrs()) { |
| MessageLoop::current()->PostTask( |
| FROM_HERE, |
| - base::Bind(&TrackedCallback::AbortImpl, |
| + base::Bind(&TrackedCallback::Abort, |
| abort_impl_factory_.GetWeakPtr())); |
| } |
| } |
| } |
| -TrackedCallback::~TrackedCallback() { |
| - // The tracker must ensure that the callback is completed (maybe abortively). |
| - DCHECK(completed_); |
| -} |
| - |
| -void TrackedCallback::MarkAsCompleted() { |
| - DCHECK(!completed()); |
| - |
| - // We will be removed; maintain a reference to ensure we won't be deleted |
| - // until we're done. |
| - scoped_refptr<TrackedCallback> thiz = this; |
| - completed_ = true; |
| - tracker_->Remove(thiz); |
| - tracker_ = NULL; |
| -} |
| - |
| -// TrackedCompletionCallback --------------------------------------------------- |
| - |
| -TrackedCompletionCallback::TrackedCompletionCallback( |
| - const scoped_refptr<CallbackTracker>& tracker, |
| - PP_Resource resource_id, |
| - const PP_CompletionCallback& callback) |
| - : TrackedCallback(tracker, resource_id), |
| - callback_(callback) { |
| -} |
| - |
| -void TrackedCompletionCallback::Run(int32_t result) { |
| +void TrackedCallback::Run(int32_t result) { |
| if (!completed()) { |
| // Cancel any pending calls. |
| abort_impl_factory_.InvalidateWeakPtrs(); |
| @@ -149,9 +75,15 @@ |
| } |
| } |
| -void TrackedCompletionCallback::AbortImpl() { |
| - Run(PP_ERROR_ABORTED); |
| +void TrackedCallback::MarkAsCompleted() { |
| + DCHECK(!completed()); |
| + |
| + // We will be removed; maintain a reference to ensure we won't be deleted |
| + // until we're done. |
| + scoped_refptr<TrackedCallback> thiz = this; |
| + completed_ = true; |
| + tracker_->Remove(thiz); |
| + tracker_ = NULL; |
| } |
| } // namespace ppapi |
| -} // namespace webkit |