| 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/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/synchronization/condition_variable.h" | 15 #include "base/synchronization/condition_variable.h" |
| 16 #include "base/synchronization/lock.h" |
| 16 #include "ppapi/c/pp_completion_callback.h" | 17 #include "ppapi/c/pp_completion_callback.h" |
| 17 #include "ppapi/c/pp_instance.h" | 18 #include "ppapi/c/pp_instance.h" |
| 18 #include "ppapi/c/pp_resource.h" | 19 #include "ppapi/c/pp_resource.h" |
| 19 #include "ppapi/shared_impl/ppapi_shared_export.h" | 20 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 20 #include "ppapi/shared_impl/ppb_message_loop_shared.h" | 21 #include "ppapi/shared_impl/ppb_message_loop_shared.h" |
| 21 | 22 |
| 22 namespace ppapi { | 23 namespace ppapi { |
| 23 | 24 |
| 24 class CallbackTracker; | 25 class CallbackTracker; |
| 25 class MessageLoopShared; | 26 class MessageLoopShared; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 // The ability to post aborts is needed in many situations to ensure that the | 46 // The ability to post aborts is needed in many situations to ensure that the |
| 46 // plugin is not re-entered into. (Note that posting a task to just run | 47 // plugin is not re-entered into. (Note that posting a task to just run |
| 47 // |Abort()| is different and not correct; calling |PostAbort()| additionally | 48 // |Abort()| is different and not correct; calling |PostAbort()| additionally |
| 48 // guarantees that all subsequent completions will be abortive.) | 49 // guarantees that all subsequent completions will be abortive.) |
| 49 // | 50 // |
| 50 // This class is reference counted so that different things can hang on to it, | 51 // This class is reference counted so that different things can hang on to it, |
| 51 // and not worry too much about ensuring Pepper callback semantics. Note that | 52 // and not worry too much about ensuring Pepper callback semantics. Note that |
| 52 // the "owning" |CallbackTracker| will keep a reference until the callback is | 53 // the "owning" |CallbackTracker| will keep a reference until the callback is |
| 53 // completed. | 54 // completed. |
| 54 // | 55 // |
| 55 // Subclasses must do several things: | 56 // A note on threading: |
| 56 // - They must ensure that the callback is executed at most once (by looking at | 57 // TrackedCallback is usable on any thread. It is *mostly* only used when |
| 57 // |completed()| before running the callback). | 58 // ppapi::ProxyLock is held. However, it's necessary that Run() can be called |
| 58 // - They must ensure that the callback is run abortively if it is marked as to | 59 // without the ProxyLock. This is used to allow running the callback from |
| 59 // be aborted (by looking at |aborted()| before running the callback). | 60 // the IO thread. In particular, blocking callbacks may not have a message loop |
| 60 // - They must call |MarkAsCompleted()| immediately before actually running the | 61 // to which we could post, so Run() must be able to signal the condition |
| 61 // callback; see the comment for |MarkAsCompleted()| for a caveat. | 62 // variable to wake up the thread that's waiting on the blocking callback, and |
| 63 // Run() must be able to do this while not holding the ProxyLock. |
| 62 class PPAPI_SHARED_EXPORT TrackedCallback | 64 class PPAPI_SHARED_EXPORT TrackedCallback |
| 63 : public base::RefCountedThreadSafe<TrackedCallback> { | 65 : public base::RefCountedThreadSafe<TrackedCallback> { |
| 64 public: | 66 public: |
| 65 // Create a tracked completion callback and register it with the tracker. The | 67 // Create a tracked completion callback and register it with the tracker. The |
| 66 // resource pointer is not stored. If |resource| is NULL, this callback will | 68 // resource pointer is not stored. If |resource| is NULL, this callback will |
| 67 // not be added to the callback tracker. | 69 // not be added to the callback tracker. |
| 68 TrackedCallback(Resource* resource, const PP_CompletionCallback& callback); | 70 TrackedCallback(Resource* resource, const PP_CompletionCallback& callback); |
| 69 | 71 |
| 70 // These run the callback in an abortive manner, or post a task to do so (but | 72 // These run the callback in an abortive manner, or post a task to do so (but |
| 71 // immediately marking the callback as to be aborted). | 73 // immediately marking the callback as to be aborted). |
| 72 void Abort(); | 74 void Abort(); |
| 73 void PostAbort(); | 75 void PostAbort(); |
| 74 | 76 |
| 75 // Run the callback with the given result. If the callback had previously been | 77 // Run the callback with the given result. If the callback had previously been |
| 76 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and | 78 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and |
| 77 // the callback will be run with result |PP_ERROR_ABORTED|. | 79 // the callback will be run with result |PP_ERROR_ABORTED|. |
| 78 // | 80 // |
| 79 // Run() will invoke the call immediately, if invoked from the target thread | 81 // Run() will invoke the call immediately, if invoked from the target thread |
| 80 // (as determined by target_loop_). If invoked on a different thread, the | 82 // (as determined by target_loop_). If invoked on a different thread, the |
| 81 // callback will be scheduled to run later on target_loop_. | 83 // callback will be scheduled to run later on target_loop_. |
| 82 void Run(int32_t result); | 84 void Run(int32_t result); |
| 85 void AcquireProxyLockAndRun(int32_t result); |
| 83 // PostRun is like Run(), except it guarantees that the callback will be run | 86 // PostRun is like Run(), except it guarantees that the callback will be run |
| 84 // later. In particular, if you invoke PostRun on the same thread on which the | 87 // later. In particular, if you invoke PostRun on the same thread on which the |
| 85 // callback is targeted to run, it will *not* be run immediately. | 88 // callback is targeted to run, it will *not* be run immediately. |
| 86 void PostRun(int32_t result); | 89 void PostRun(int32_t result); |
| 87 | 90 |
| 88 // A task to perform cleanup or write output parameters before the callback | 91 // A task to perform cleanup or write output parameters before the callback |
| 89 // returns a result to the plugin. The |result| parameter has the result so | 92 // returns a result to the plugin. The |result| parameter has the result so |
| 90 // far, e.g. whether the callback has been aborted. If the callback hasn't | 93 // far, e.g. whether the callback has been aborted. If the callback hasn't |
| 91 // been aborted the return value of the task will become the callback result. | 94 // been aborted the return value of the task will become the callback result. |
| 92 // The task is always called on the same thread as the callback to the plugin. | 95 // The task is always called on the same thread as the callback to the plugin. |
| 93 typedef base::Callback<int32_t(int32_t /* result */)> CompletionTask; | 96 typedef base::Callback<int32_t(int32_t /* result */)> CompletionTask; |
| 94 | 97 |
| 95 // Sets a task that is run just before calling back into the plugin. This | 98 // Sets a task that is run just before calling back into the plugin. This |
| 96 // should only be called once. | 99 // should only be called once. Note that the CompletionTask always runs while |
| 100 // holding the ppapi::ProxyLock. |
| 97 void set_completion_task(const CompletionTask& completion_task); | 101 void set_completion_task(const CompletionTask& completion_task); |
| 98 | 102 |
| 99 // Returns the ID of the resource which "owns" the callback, or 0 if the | 103 // Returns the ID of the resource which "owns" the callback, or 0 if the |
| 100 // callback is not associated with any resource. | 104 // callback is not associated with any resource. |
| 101 PP_Resource resource_id() const { return resource_id_; } | 105 PP_Resource resource_id() const { return resource_id_; } |
| 102 | 106 |
| 103 // Returns true if the callback was completed (possibly aborted). | 107 // Returns true if this is a blocking callback. |
| 104 bool completed() const { return completed_; } | 108 bool is_blocking() const { |
| 109 // This is set on construction and never changes after that, so there is |
| 110 // no need to lock. |
| 111 return !callback_.func; |
| 112 } |
| 105 | 113 |
| 106 // Returns true if the callback was or should be aborted; this will be the | 114 MessageLoopShared* target_loop() const { |
| 107 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive | 115 // This is set on construction and never changes after that, so there is |
| 108 // completion. | 116 // no need to lock. |
| 109 bool aborted() const { return aborted_; } | 117 return target_loop_.get(); |
| 110 | 118 } |
| 111 // Returns true if this is a blocking callback. | |
| 112 bool is_blocking() { return !callback_.func; } | |
| 113 | |
| 114 MessageLoopShared* target_loop() const { return target_loop_.get(); } | |
| 115 | 119 |
| 116 // Determines if the given callback is pending. A callback is pending if it | 120 // Determines if the given callback is pending. A callback is pending if it |
| 117 // has not completed and has not been aborted. When receiving a plugin call, | 121 // has not completed and has not been aborted. When receiving a plugin call, |
| 118 // use this to detect if |callback| represents an operation in progress. When | 122 // use this to detect if |callback| represents an operation in progress. When |
| 119 // finishing a plugin call, use this to determine whether to write 'out' | 123 // finishing a plugin call, use this to determine whether to write 'out' |
| 120 // params and Run |callback|. | 124 // params and Run |callback|. |
| 121 // NOTE: an aborted callback has not necessarily completed, so a false result | 125 // NOTE: an aborted callback has not necessarily completed, so a false result |
| 122 // doesn't imply that the callback has completed. | 126 // doesn't imply that the callback has completed. |
| 123 // As a convenience, if |callback| is null, this returns false. | 127 // As a convenience, if |callback| is null, this returns false. |
| 124 static bool IsPending(const scoped_refptr<TrackedCallback>& callback); | 128 static bool IsPending(const scoped_refptr<TrackedCallback>& callback); |
| 125 | 129 |
| 126 // Helper to determine if the given callback is scheduled to run on another | 130 // Helper to determine if the given callback is scheduled to run on another |
| 127 // message loop. | 131 // message loop. |
| 128 static bool IsScheduledToRun(const scoped_refptr<TrackedCallback>& callback); | 132 static bool IsScheduledToRun(const scoped_refptr<TrackedCallback>& callback); |
| 129 | 133 |
| 130 protected: | 134 private: |
| 131 bool is_required() { | 135 bool is_required() { |
| 132 return (callback_.func && | 136 return (callback_.func && |
| 133 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)); | 137 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)); |
| 134 } | 138 } |
| 135 bool is_optional() { | |
| 136 return (callback_.func && | |
| 137 (callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL)); | |
| 138 } | |
| 139 bool has_null_target_loop() const { return target_loop_.get() == NULL; } | 139 bool has_null_target_loop() const { return target_loop_.get() == NULL; } |
| 140 | 140 |
| 141 private: | 141 // Same as PostRun(), but lock_ must already be held. |
| 142 // TrackedCallback and EnterBase manage dealing with how to invoke callbacks | 142 void PostRunWithLock(int32_t result); |
| 143 // appropriately. Pepper interface implementations and proxies should not have | 143 |
| 144 // to check the type of callback, block, or mark them complete explicitly. | 144 void SignalBlockingCallback(int32_t result); |
| 145 |
| 146 // TrackedCallback and EnterBase work together to provide appropriate behavior |
| 147 // for callbacks. Pepper interface implementations and proxies should |
| 148 // usually not have to check whether callbacks are required, optional, or |
| 149 // blocking. Nor should interface and proxy implementations have to worry |
| 150 // about blocking on a callback or marking them complete explicitly. |
| 151 // |
| 152 // (There are exceptions; e.g. FileIO checks is_blocking() in order to do |
| 153 // some operations directly on the calling thread if possible.) |
| 145 friend class ppapi::thunk::subtle::EnterBase; | 154 friend class ppapi::thunk::subtle::EnterBase; |
| 146 | 155 |
| 147 // Block until the associated operation has completed. Returns the result. | 156 // Block until the associated operation has completed. Returns the result. |
| 148 // This must only be called on a non-main thread on a blocking callback. | 157 // This must only be called on a non-main thread on a blocking callback. |
| 149 int32_t BlockUntilComplete(); | 158 int32_t BlockUntilComplete(); |
| 150 | 159 |
| 151 // Mark this object as complete and remove it from the tracker. This must only | 160 // Mark this object as complete and remove it from the tracker. This must only |
| 152 // be called once. Note that running this may result in this object being | 161 // be called once. Note that running this may result in this object being |
| 153 // deleted (so keep a reference if it'll still be needed). | 162 // deleted (so keep a reference if it'll still be needed). |
| 154 void MarkAsCompleted(); | 163 void MarkAsCompleted(); |
| 164 void MarkAsCompletedWithLock(); |
| 155 | 165 |
| 156 // This class is ref counted. | 166 // This class is ref counted. |
| 157 friend class base::RefCountedThreadSafe<TrackedCallback>; | 167 friend class base::RefCountedThreadSafe<TrackedCallback>; |
| 158 virtual ~TrackedCallback(); | 168 ~TrackedCallback(); |
| 169 |
| 170 mutable base::Lock lock_; |
| 159 | 171 |
| 160 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule | 172 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule |
| 161 // the callback more than once. | 173 // the callback more than once. |
| 162 bool is_scheduled_; | 174 bool is_scheduled_; |
| 163 | 175 |
| 164 scoped_refptr<CallbackTracker> tracker_; | 176 scoped_refptr<CallbackTracker> tracker_; |
| 165 PP_Resource resource_id_; | 177 PP_Resource resource_id_; |
| 166 bool completed_; | 178 bool completed_; |
| 167 bool aborted_; | 179 bool aborted_; |
| 168 PP_CompletionCallback callback_; | 180 PP_CompletionCallback callback_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 179 // callback. Note that in-process, there is no lock, blocking callbacks are | 191 // callback. Note that in-process, there is no lock, blocking callbacks are |
| 180 // not allowed, and therefore this pointer will be NULL. | 192 // not allowed, and therefore this pointer will be NULL. |
| 181 scoped_ptr<base::ConditionVariable> operation_completed_condvar_; | 193 scoped_ptr<base::ConditionVariable> operation_completed_condvar_; |
| 182 | 194 |
| 183 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); | 195 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback); |
| 184 }; | 196 }; |
| 185 | 197 |
| 186 } // namespace ppapi | 198 } // namespace ppapi |
| 187 | 199 |
| 188 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ | 200 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_ |
| OLD | NEW |