| 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 WEBKIT_GLUE_PLUGINS_PEPPER_CALLBACKS_H_ | 5 #ifndef PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ |
| 6 #define WEBKIT_GLUE_PLUGINS_PEPPER_CALLBACKS_H_ | 6 #define PPAPI_SHARED_IMPL_CALLBACK_TRACKER_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/task.h" | 13 #include "base/task.h" |
| 14 #include "ppapi/c/pp_completion_callback.h" | |
| 15 #include "ppapi/c/pp_resource.h" | 14 #include "ppapi/c/pp_resource.h" |
| 16 #include "webkit/plugins/webkit_plugins_export.h" | 15 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 17 | 16 |
| 18 namespace webkit { | |
| 19 namespace ppapi { | 17 namespace ppapi { |
| 20 | 18 |
| 21 class TrackedCallback; | 19 class TrackedCallback; |
| 22 | 20 |
| 23 // Pepper callbacks have the following semantics (unless otherwise specified; | 21 // Pepper callbacks have the following semantics (unless otherwise specified; |
| 24 // in particular, the below apply to all completion callbacks): | 22 // in particular, the below apply to all completion callbacks): |
| 25 // - Callbacks are always run on the main thread. | 23 // - Callbacks are always run on the main thread. |
| 26 // - Callbacks are always called from the main message loop. In particular, | 24 // - Callbacks are always called from the main message loop. In particular, |
| 27 // calling into Pepper will not result in the plugin being re-entered via a | 25 // calling into Pepper will not result in the plugin being re-entered via a |
| 28 // synchronously-run callback. | 26 // synchronously-run callback. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 57 // (possibly causing destruction) is also okay. Otherwise, all methods should be | 55 // (possibly causing destruction) is also okay. Otherwise, all methods should be |
| 58 // called only from the main thread. | 56 // called only from the main thread. |
| 59 | 57 |
| 60 // |CallbackTracker| tracks pending Pepper callbacks for a single module. It | 58 // |CallbackTracker| tracks pending Pepper callbacks for a single module. It |
| 61 // also tracks, for each resource ID, which callbacks are pending. When a | 59 // also tracks, for each resource ID, which callbacks are pending. When a |
| 62 // callback is (just about to be) completed, it is removed from the tracker. We | 60 // callback is (just about to be) completed, it is removed from the tracker. We |
| 63 // use |CallbackTracker| for two things: (1) to ensure that all callbacks are | 61 // use |CallbackTracker| for two things: (1) to ensure that all callbacks are |
| 64 // properly aborted before module shutdown, and (2) to ensure that all callbacks | 62 // properly aborted before module shutdown, and (2) to ensure that all callbacks |
| 65 // associated to a given resource are aborted when a plugin (module) releases | 63 // associated to a given resource are aborted when a plugin (module) releases |
| 66 // its last reference to that resource. | 64 // its last reference to that resource. |
| 67 class CallbackTracker : public base::RefCountedThreadSafe<CallbackTracker> { | 65 class PPAPI_SHARED_EXPORT CallbackTracker |
| 66 : public base::RefCountedThreadSafe<CallbackTracker> { |
| 68 public: | 67 public: |
| 69 CallbackTracker(); | 68 CallbackTracker(); |
| 70 | 69 |
| 71 // Abort all callbacks (synchronously). | 70 // Abort all callbacks (synchronously). |
| 72 void AbortAll(); | 71 void AbortAll(); |
| 73 | 72 |
| 74 // Abort all callbacks associated to the given resource ID (which must be | 73 // Abort all callbacks associated to the given resource ID (which must be |
| 75 // valid, i.e., nonzero) by posting a task (or tasks). | 74 // valid, i.e., nonzero) by posting a task (or tasks). |
| 76 void PostAbortForResource(PP_Resource resource_id); | 75 void PostAbortForResource(PP_Resource resource_id); |
| 77 | 76 |
| 78 private: | 77 private: |
| 79 friend class base::RefCountedThreadSafe<CallbackTracker>; | 78 friend class base::RefCountedThreadSafe<CallbackTracker>; |
| 80 WEBKIT_PLUGINS_EXPORT ~CallbackTracker(); | 79 ~CallbackTracker(); |
| 81 | 80 |
| 82 // |TrackedCallback| are expected to automatically add and | 81 // |TrackedCallback| are expected to automatically add and |
| 83 // remove themselves from their provided |CallbackTracker|. | 82 // remove themselves from their provided |CallbackTracker|. |
| 84 friend class TrackedCallback; | 83 friend class TrackedCallback; |
| 85 void Add(const scoped_refptr<TrackedCallback>& tracked_callback); | 84 void Add(const scoped_refptr<TrackedCallback>& tracked_callback); |
| 86 void Remove(const scoped_refptr<TrackedCallback>& tracked_callback); | 85 void Remove(const scoped_refptr<TrackedCallback>& tracked_callback); |
| 87 | 86 |
| 88 // For each resource ID with a pending callback, store a set with its pending | 87 // For each resource ID with a pending callback, store a set with its pending |
| 89 // callbacks. (Resource ID 0 is used for callbacks not associated to a valid | 88 // callbacks. (Resource ID 0 is used for callbacks not associated to a valid |
| 90 // resource.) If a resource ID is re-used for another resource, there may be | 89 // resource.) If a resource ID is re-used for another resource, there may be |
| 91 // aborted callbacks corresponding to the original resource in that set; these | 90 // aborted callbacks corresponding to the original resource in that set; these |
| 92 // will be removed when they are completed (abortively). | 91 // will be removed when they are completed (abortively). |
| 93 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; | 92 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; |
| 94 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; | 93 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; |
| 95 CallbackSetMap pending_callbacks_; | 94 CallbackSetMap pending_callbacks_; |
| 96 | 95 |
| 97 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); | 96 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); |
| 98 }; | 97 }; |
| 99 | 98 |
| 100 // |TrackedCallback| represents a tracked Pepper callback (from the browser to | 99 } // namespace ppapi |
| 101 // the plugin), typically still pending. Such callbacks have the standard Pepper | |
| 102 // callback semantics. Execution (i.e., completion) of callbacks happens through | |
| 103 // objects of subclasses of |TrackedCallback|. Two things are ensured: (1) that | |
| 104 // the callback is executed at most once, and (2) once a callback is marked to | |
| 105 // be aborted, any subsequent completion is abortive (even if a non-abortive | |
| 106 // completion had previously been scheduled). | |
| 107 // | |
| 108 // The details of non-abortive completion depend on the type of callback (e.g., | |
| 109 // different parameters may be required), but basic abort functionality is core. | |
| 110 // The ability to post aborts is needed in many situations to ensure that the | |
| 111 // plugin is not re-entered into. (Note that posting a task to just run | |
| 112 // |Abort()| is different and not correct; calling |PostAbort()| additionally | |
| 113 // guarantees that all subsequent completions will be abortive.) | |
| 114 // | |
| 115 // This class is reference counted so that different things can hang on to it, | |
| 116 // and not worry too much about ensuring Pepper callback semantics. Note that | |
| 117 // the "owning" |CallbackTracker| will keep a reference until the callback is | |
| 118 // completed. | |
| 119 // | |
| 120 // Subclasses must do several things: | |
| 121 // - They must ensure that the callback is executed at most once (by looking at | |
| 122 // |completed()| before running the callback). | |
| 123 // - They must ensure that the callback is run abortively if it is marked as to | |
| 124 // be aborted (by looking at |aborted()| before running the callback). | |
| 125 // - They must call |MarkAsCompleted()| immediately before actually running the | |
| 126 // callback; see the comment for |MarkAsCompleted()| for a caveat. | |
| 127 class TrackedCallback : public base::RefCountedThreadSafe<TrackedCallback> { | |
| 128 public: | |
| 129 // The constructor will add the new object to the tracker. The resource ID is | |
| 130 // optional -- set it to 0 if no resource is associated to the callback. | |
| 131 TrackedCallback(const scoped_refptr<CallbackTracker>& tracker, | |
| 132 PP_Resource resource_id); | |
| 133 | 100 |
| 134 // These run the callback in an abortive manner, or post a task to do so (but | 101 #endif // PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ |
| 135 // immediately marking the callback as to be aborted). | |
| 136 WEBKIT_PLUGINS_EXPORT void Abort(); | |
| 137 void PostAbort(); | |
| 138 | |
| 139 // Returns the ID of the resource which "owns" the callback, or 0 if the | |
| 140 // callback is not associated with any resource. | |
| 141 PP_Resource resource_id() const { return resource_id_; } | |
| 142 | |
| 143 // Returns true if the callback was completed (possibly aborted). | |
| 144 bool completed() const { return completed_; } | |
| 145 | |
| 146 // Returns true if the callback was or should be aborted; this will be the | |
| 147 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive | |
| 148 // completion. | |
| 149 bool aborted() const { return aborted_; } | |
| 150 | |
| 151 protected: | |
| 152 // This class is ref counted. | |
| 153 friend class base::RefCountedThreadSafe<TrackedCallback>; | |
| 154 virtual ~TrackedCallback(); | |
| 155 | |
| 156 // To be implemented by subclasses: Actually run the callback abortively. | |
| 157 virtual void AbortImpl() = 0; | |
| 158 | |
| 159 // Mark this object as complete and remove it from the tracker. This must only | |
| 160 // be called once. Note that running this may result in this object being | |
| 161 // deleted (so keep a reference if it'll still be needed). | |
| 162 void MarkAsCompleted(); | |
| 163 | |
| 164 // Factory used by |PostAbort()|. Note that it's safe to cancel any pending | |
| 165 // posted aborts on destruction -- before it's destroyed, the "owning" | |
| 166 // |CallbackTracker| must have gone through and done (synchronous) |Abort()|s. | |
| 167 base::WeakPtrFactory<TrackedCallback> abort_impl_factory_; | |
| 168 | |
| 169 private: | |
| 170 scoped_refptr<CallbackTracker> tracker_; | |
| 171 PP_Resource resource_id_; | |
| 172 bool completed_; | |
| 173 bool aborted_; | |
| 174 | |
| 175 DISALLOW_COPY_AND_ASSIGN(TrackedCallback); | |
| 176 }; | |
| 177 | |
| 178 // |TrackedCompletionCallback| represents a tracked Pepper completion callback. | |
| 179 class TrackedCompletionCallback : public TrackedCallback { | |
| 180 public: | |
| 181 // Create a tracked completion callback and register it with the tracker. The | |
| 182 // resource ID may be 0 if the callback is not associated to any resource. | |
| 183 WEBKIT_PLUGINS_EXPORT TrackedCompletionCallback( | |
| 184 const scoped_refptr<CallbackTracker>& tracker, | |
| 185 PP_Resource resource_id, | |
| 186 const PP_CompletionCallback& callback); | |
| 187 | |
| 188 // Run the callback with the given result. If the callback had previously been | |
| 189 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and | |
| 190 // the callback will be run with result |PP_ERROR_ABORTED|. | |
| 191 WEBKIT_PLUGINS_EXPORT void Run(int32_t result); | |
| 192 | |
| 193 protected: | |
| 194 // |TrackedCallback| method: | |
| 195 virtual void AbortImpl(); | |
| 196 | |
| 197 private: | |
| 198 PP_CompletionCallback callback_; | |
| 199 | |
| 200 DISALLOW_COPY_AND_ASSIGN(TrackedCompletionCallback); | |
| 201 }; | |
| 202 | |
| 203 } // namespace ppapi | |
| 204 } // namespace webkit | |
| 205 | |
| 206 #endif // WEBKIT_PLUGINS_PPAPI_CALLBACKS_H_ | |
| OLD | NEW |