OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/synchronization/condition_variable.h" | 14 #include "base/synchronization/condition_variable.h" |
16 #include "ppapi/c/pp_completion_callback.h" | 15 #include "ppapi/c/pp_completion_callback.h" |
17 #include "ppapi/c/pp_instance.h" | 16 #include "ppapi/c/pp_instance.h" |
18 #include "ppapi/c/pp_resource.h" | 17 #include "ppapi/c/pp_resource.h" |
19 #include "ppapi/shared_impl/ppapi_shared_export.h" | 18 #include "ppapi/shared_impl/ppapi_shared_export.h" |
20 | 19 |
21 namespace ppapi { | 20 namespace ppapi { |
22 | 21 |
23 class CallbackTracker; | 22 class CallbackTracker; |
24 class Resource; | 23 class Resource; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 66 |
68 // These run the callback in an abortive manner, or post a task to do so (but | 67 // These run the callback in an abortive manner, or post a task to do so (but |
69 // immediately marking the callback as to be aborted). | 68 // immediately marking the callback as to be aborted). |
70 void Abort(); | 69 void Abort(); |
71 void PostAbort(); | 70 void PostAbort(); |
72 | 71 |
73 // Run the callback with the given result. If the callback had previously been | 72 // Run the callback with the given result. If the callback had previously been |
74 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and | 73 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and |
75 // the callback will be run with result |PP_ERROR_ABORTED|. | 74 // the callback will be run with result |PP_ERROR_ABORTED|. |
76 // | 75 // |
77 // See also ClearAndRun(). | 76 // Run() will invoke the call immediately, if invoked from the target thread |
| 77 // (as determined by target_loop_). If invoked on a different thread, the |
| 78 // callback will be scheduled to run later on target_loop_. |
| 79 // TODO(dmichael): Make the above part about different threads actually true. |
78 void Run(int32_t result); | 80 void Run(int32_t result); |
| 81 // PostRun is like Run(), except it guarantees that the callback will be run |
| 82 // later. In particular, if you invoke PostRun on the same thread on which the |
| 83 // callback is targeted to run, it will *not* be run immediately. |
79 void PostRun(int32_t result); | 84 void PostRun(int32_t result); |
80 | 85 |
81 void BlockUntilRun(); | 86 void BlockUntilRun(); |
82 | 87 |
83 // Returns the ID of the resource which "owns" the callback, or 0 if the | 88 // Returns the ID of the resource which "owns" the callback, or 0 if the |
84 // callback is not associated with any resource. | 89 // callback is not associated with any resource. |
85 PP_Resource resource_id() const { return resource_id_; } | 90 PP_Resource resource_id() const { return resource_id_; } |
86 | 91 |
87 // Returns true if the callback was completed (possibly aborted). | 92 // Returns true if the callback was completed (possibly aborted). |
88 bool completed() const { return completed_; } | 93 bool completed() const { return completed_; } |
89 | 94 |
90 // Returns true if the callback was or should be aborted; this will be the | 95 // Returns true if the callback was or should be aborted; this will be the |
91 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive | 96 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive |
92 // completion. | 97 // completion. |
93 bool aborted() const { return aborted_; } | 98 bool aborted() const { return aborted_; } |
94 | 99 |
95 // Helper to determine if the given callback is set and not yet completed. | 100 // Helper to determine if the given callback is set and not yet completed. |
96 // The normal pattern is to use a scoped_refptr to hold a callback. This | 101 // The normal pattern is to use a scoped_refptr to hold a callback. This |
97 // function tells you if the operation is currently in progress by checking | 102 // function tells you if the operation is currently in progress by checking |
98 // both the null-ness of the scoped_refptr, as well as the completion state | 103 // both the null-ness of the scoped_refptr, as well as the completion state |
99 // of the callback (which may still be out-standing via a PostAbort). | 104 // of the callback (which may still be out-standing via a PostAbort). |
100 static bool IsPending(const scoped_refptr<TrackedCallback>& callback); | 105 static bool IsPending(const scoped_refptr<TrackedCallback>& callback); |
101 | 106 |
102 // Runs the given callback, clearing the given scoped_refptr before execution. | |
103 // This is useful for cases where there can be only one pending callback, and | |
104 // the presence of the callback indicates one is pending. Such code would | |
105 // normally want to clear it before execution so the plugin can issue a new | |
106 // request. | |
107 static void ClearAndRun(scoped_refptr<TrackedCallback>* callback, | |
108 int32_t result); | |
109 | |
110 // Same as ClearAndRun except it calls Abort(). | |
111 static void ClearAndAbort(scoped_refptr<TrackedCallback>* callback); | |
112 | |
113 protected: | 107 protected: |
114 bool is_blocking() { | 108 bool is_blocking() { |
115 return !callback_.func; | 109 return !callback_.func; |
116 } | 110 } |
117 bool is_required() { | 111 bool is_required() { |
118 return (callback_.func && | 112 return (callback_.func && |
119 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)); | 113 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)); |
120 } | 114 } |
121 bool is_optional() { | 115 bool is_optional() { |
122 return (callback_.func && | 116 return (callback_.func && |
(...skipping 12 matching lines...) Expand all Loading... |
135 | 129 |
136 // Mark this object as complete and remove it from the tracker. This must only | 130 // Mark this object as complete and remove it from the tracker. This must only |
137 // be called once. Note that running this may result in this object being | 131 // be called once. Note that running this may result in this object being |
138 // deleted (so keep a reference if it'll still be needed). | 132 // deleted (so keep a reference if it'll still be needed). |
139 void MarkAsCompleted(); | 133 void MarkAsCompleted(); |
140 | 134 |
141 // This class is ref counted. | 135 // This class is ref counted. |
142 friend class base::RefCountedThreadSafe<TrackedCallback>; | 136 friend class base::RefCountedThreadSafe<TrackedCallback>; |
143 virtual ~TrackedCallback(); | 137 virtual ~TrackedCallback(); |
144 | 138 |
145 // Factory used by |PostAbort()| and |PostRun()|. Note that it's safe to | 139 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule |
146 // cancel any pending posted tasks on destruction -- before it's destroyed, | 140 // the callback more than once. |
147 // the "owning" |CallbackTracker| must have gone through and done | 141 bool is_scheduled_; |
148 // (synchronous) |Abort()|s. | |
149 base::WeakPtrFactory<TrackedCallback> weak_ptr_factory_; | |
150 | 142 |
151 scoped_refptr<CallbackTracker> tracker_; | 143 scoped_refptr<CallbackTracker> tracker_; |
152 PP_Resource resource_id_; | 144 PP_Resource resource_id_; |
153 bool completed_; | 145 bool completed_; |
154 bool aborted_; | 146 bool aborted_; |
155 PP_CompletionCallback callback_; | 147 PP_CompletionCallback callback_; |
156 | 148 |
157 int32_t result_for_blocked_callback_; | 149 int32_t result_for_blocked_callback_; |
158 // Used for pausing/waking the blocked thread if this is a blocking completion | 150 // Used for pausing/waking the blocked thread if this is a blocking completion |
159 // callback. Note that in-process, there is no lock, blocking callbacks are | 151 // callback. Note that in-process, there is no lock, blocking callbacks are |
160 // not allowed, and therefore this pointer will be NULL. | 152 // not allowed, and therefore this pointer will be NULL. |
161 scoped_ptr<base::ConditionVariable> operation_completed_condvar_; | 153 scoped_ptr<base::ConditionVariable> operation_completed_condvar_; |
162 | 154 |
163 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); | 155 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); |
164 }; | 156 }; |
165 | 157 |
166 } // namespace ppapi | 158 } // namespace ppapi |
167 | 159 |
168 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ | 160 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ |
OLD | NEW |