| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef WEBKIT_GLUE_PLUGINS_PEPPER_CALLBACKS_H_ | |
| 6 #define WEBKIT_GLUE_PLUGINS_PEPPER_CALLBACKS_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/task.h" | |
| 14 #include "ppapi/c/pp_completion_callback.h" | |
| 15 #include "ppapi/c/pp_resource.h" | |
| 16 #include "webkit/plugins/webkit_plugins_export.h" | |
| 17 | |
| 18 namespace webkit { | |
| 19 namespace ppapi { | |
| 20 | |
| 21 class TrackedCallback; | |
| 22 | |
| 23 // Pepper callbacks have the following semantics (unless otherwise specified; | |
| 24 // in particular, the below apply to all completion callbacks): | |
| 25 // - Callbacks are always run on the main thread. | |
| 26 // - 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 | |
| 28 // synchronously-run callback. | |
| 29 // - Each callback will be executed (a.k.a. completed) exactly once. | |
| 30 // - Each callback may be *aborted*, which means that it will be executed with | |
| 31 // result |PP_ERROR_ABORTED| (in the case of completion callbacks). | |
| 32 // - Before |PPP_ShutdownModule()| is called, every pending callback (for that | |
| 33 // module) will be aborted. | |
| 34 // - Callbacks are usually associated to a resource, whose "deletion" provides | |
| 35 // a "cancellation" (or abort) mechanism -- see below. | |
| 36 // - When a plugin releases its last reference to resource, all callbacks | |
| 37 // associated to that resource are aborted. Even if a non-abortive completion | |
| 38 // of such a callback had previously been scheduled (i.e., posted), that | |
| 39 // callback must now be aborted. The aborts should be scheduled immediately | |
| 40 // (upon the last reference being given up) and should not rely on anything | |
| 41 // else (e.g., a background task to complete or further action from the | |
| 42 // plugin). | |
| 43 // - Abortive completion gives no information about the status of the | |
| 44 // asynchronous operation: The operation may have not yet begun, may be in | |
| 45 // progress, or may be completed (successfully or not). In fact, the | |
| 46 // operation may still proceed after the callback has been aborted. | |
| 47 // - Any output data buffers provided to Pepper are associated with a resource. | |
| 48 // Once that resource is released, no subsequent writes to those buffers. (If | |
| 49 // background threads are set up to write into such buffers, the final | |
| 50 // release operation should not return into the plugin until it can | |
| 51 // guaranteed that those threads will no longer write into the buffers.) | |
| 52 // | |
| 53 // Thread-safety notes: | |
| 54 // Currently, everything should happen on the main thread. The objects are | |
| 55 // thread-safe ref-counted, so objects which live on different threads may keep | |
| 56 // references. Releasing a reference to |TrackedCallback| on a different thread | |
| 57 // (possibly causing destruction) is also okay. Otherwise, all methods should be | |
| 58 // called only from the main thread. | |
| 59 | |
| 60 // |CallbackTracker| tracks pending Pepper callbacks for a single module. It | |
| 61 // 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 | |
| 63 // 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 | |
| 65 // associated to a given resource are aborted when a plugin (module) releases | |
| 66 // its last reference to that resource. | |
| 67 class CallbackTracker : public base::RefCountedThreadSafe<CallbackTracker> { | |
| 68 public: | |
| 69 CallbackTracker(); | |
| 70 | |
| 71 // Abort all callbacks (synchronously). | |
| 72 void AbortAll(); | |
| 73 | |
| 74 // Abort all callbacks associated to the given resource ID (which must be | |
| 75 // valid, i.e., nonzero) by posting a task (or tasks). | |
| 76 void PostAbortForResource(PP_Resource resource_id); | |
| 77 | |
| 78 private: | |
| 79 friend class base::RefCountedThreadSafe<CallbackTracker>; | |
| 80 WEBKIT_PLUGINS_EXPORT ~CallbackTracker(); | |
| 81 | |
| 82 // |TrackedCallback| are expected to automatically add and | |
| 83 // remove themselves from their provided |CallbackTracker|. | |
| 84 friend class TrackedCallback; | |
| 85 void Add(const scoped_refptr<TrackedCallback>& tracked_callback); | |
| 86 void Remove(const scoped_refptr<TrackedCallback>& tracked_callback); | |
| 87 | |
| 88 // 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 | |
| 90 // 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 | |
| 92 // will be removed when they are completed (abortively). | |
| 93 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; | |
| 94 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; | |
| 95 CallbackSetMap pending_callbacks_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); | |
| 98 }; | |
| 99 | |
| 100 // |TrackedCallback| represents a tracked Pepper callback (from the browser to | |
| 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 | |
| 134 // These run the callback in an abortive manner, or post a task to do so (but | |
| 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 |