| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ | 5 #ifndef PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ |
| 6 #define PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ | 6 #define PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 const PP_CompletionCallback& callback); | 56 const PP_CompletionCallback& callback); |
| 57 | 57 |
| 58 // These run the callback in an abortive manner, or post a task to do so (but | 58 // These run the callback in an abortive manner, or post a task to do so (but |
| 59 // immediately marking the callback as to be aborted). | 59 // immediately marking the callback as to be aborted). |
| 60 void Abort(); | 60 void Abort(); |
| 61 void PostAbort(); | 61 void PostAbort(); |
| 62 | 62 |
| 63 // Run the callback with the given result. If the callback had previously been | 63 // Run the callback with the given result. If the callback had previously been |
| 64 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and | 64 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and |
| 65 // the callback will be run with result |PP_ERROR_ABORTED|. | 65 // the callback will be run with result |PP_ERROR_ABORTED|. |
| 66 // |
| 67 // See also ClearAndRun(). |
| 66 void Run(int32_t result); | 68 void Run(int32_t result); |
| 67 | 69 |
| 68 // Returns the ID of the resource which "owns" the callback, or 0 if the | 70 // Returns the ID of the resource which "owns" the callback, or 0 if the |
| 69 // callback is not associated with any resource. | 71 // callback is not associated with any resource. |
| 70 PP_Resource resource_id() const { return resource_id_; } | 72 PP_Resource resource_id() const { return resource_id_; } |
| 71 | 73 |
| 72 // Returns true if the callback was completed (possibly aborted). | 74 // Returns true if the callback was completed (possibly aborted). |
| 73 bool completed() const { return completed_; } | 75 bool completed() const { return completed_; } |
| 74 | 76 |
| 75 // Returns true if the callback was or should be aborted; this will be the | 77 // Returns true if the callback was or should be aborted; this will be the |
| 76 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive | 78 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive |
| 77 // completion. | 79 // completion. |
| 78 bool aborted() const { return aborted_; } | 80 bool aborted() const { return aborted_; } |
| 79 | 81 |
| 82 // Helper to determine if the given callback is set and not yet completed. |
| 83 // The normal pattern is to use a scoped_refptr to hold a callback. This |
| 84 // function tells you if the operation is currently in progress by checking |
| 85 // both the null-ness of the scoped_refptr, as well as the completion state |
| 86 // of the callback (which may still be out-standing via a PostAbort). |
| 87 static bool IsPending(const scoped_refptr<TrackedCallback>& callback); |
| 88 |
| 89 // Runs the given callback, clearing the given scoped_refptr before execution. |
| 90 // This is useful for cases where there can be only one pending callback, and |
| 91 // the presence of the callback indicates is one is pending. Such code would |
| 92 // normally want to clear it before execution so the plugin can issue a new |
| 93 // request. |
| 94 static void ClearAndRun(scoped_refptr<TrackedCallback>* callback, |
| 95 int32_t result); |
| 96 |
| 97 // Same as ClearAndRun except it calls Abort(). |
| 98 static void ClearAndAbort(scoped_refptr<TrackedCallback>* callback); |
| 99 |
| 80 private: | 100 private: |
| 81 // This class is ref counted. | 101 // This class is ref counted. |
| 82 friend class base::RefCountedThreadSafe<TrackedCallback>; | 102 friend class base::RefCountedThreadSafe<TrackedCallback>; |
| 83 virtual ~TrackedCallback(); | 103 virtual ~TrackedCallback(); |
| 84 | 104 |
| 85 // Mark this object as complete and remove it from the tracker. This must only | 105 // Mark this object as complete and remove it from the tracker. This must only |
| 86 // be called once. Note that running this may result in this object being | 106 // be called once. Note that running this may result in this object being |
| 87 // deleted (so keep a reference if it'll still be needed). | 107 // deleted (so keep a reference if it'll still be needed). |
| 88 void MarkAsCompleted(); | 108 void MarkAsCompleted(); |
| 89 | 109 |
| 90 // Factory used by |PostAbort()|. Note that it's safe to cancel any pending | 110 // Factory used by |PostAbort()|. Note that it's safe to cancel any pending |
| 91 // posted aborts on destruction -- before it's destroyed, the "owning" | 111 // posted aborts on destruction -- before it's destroyed, the "owning" |
| 92 // |CallbackTracker| must have gone through and done (synchronous) |Abort()|s. | 112 // |CallbackTracker| must have gone through and done (synchronous) |Abort()|s. |
| 93 base::WeakPtrFactory<TrackedCallback> abort_impl_factory_; | 113 base::WeakPtrFactory<TrackedCallback> abort_impl_factory_; |
| 94 | 114 |
| 95 scoped_refptr<CallbackTracker> tracker_; | 115 scoped_refptr<CallbackTracker> tracker_; |
| 96 PP_Resource resource_id_; | 116 PP_Resource resource_id_; |
| 97 bool completed_; | 117 bool completed_; |
| 98 bool aborted_; | 118 bool aborted_; |
| 99 PP_CompletionCallback callback_; | 119 PP_CompletionCallback callback_; |
| 100 | 120 |
| 101 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); | 121 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); |
| 102 }; | 122 }; |
| 103 | 123 |
| 104 } // namespace ppapi | 124 } // namespace ppapi |
| 105 | 125 |
| 106 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ | 126 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ |
| OLD | NEW |