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
3 // found in the LICENSE file.
5 #ifndef PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_
6 #define PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/condition_variable.h"
15 #include "ppapi/c/pp_completion_callback.h"
16 #include "ppapi/c/pp_instance.h"
17 #include "ppapi/c/pp_resource.h"
18 #include "ppapi/shared_impl/ppapi_shared_export.h"
19 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
23 class CallbackTracker
;
24 class MessageLoopShared
;
29 // For a friend declaration below.
34 // |TrackedCallback| represents a tracked Pepper callback (from the browser to
35 // the plugin), typically still pending. Such callbacks have the standard Pepper
36 // callback semantics. Execution (i.e., completion) of callbacks happens through
37 // objects of subclasses of |TrackedCallback|. Two things are ensured: (1) that
38 // the callback is executed at most once, and (2) once a callback is marked to
39 // be aborted, any subsequent completion is abortive (even if a non-abortive
40 // completion had previously been scheduled).
42 // The details of non-abortive completion depend on the type of callback (e.g.,
43 // different parameters may be required), but basic abort functionality is core.
44 // The ability to post aborts is needed in many situations to ensure that the
45 // plugin is not re-entered into. (Note that posting a task to just run
46 // |Abort()| is different and not correct; calling |PostAbort()| additionally
47 // guarantees that all subsequent completions will be abortive.)
49 // This class is reference counted so that different things can hang on to it,
50 // and not worry too much about ensuring Pepper callback semantics. Note that
51 // the "owning" |CallbackTracker| will keep a reference until the callback is
54 // Subclasses must do several things:
55 // - They must ensure that the callback is executed at most once (by looking at
56 // |completed()| before running the callback).
57 // - They must ensure that the callback is run abortively if it is marked as to
58 // be aborted (by looking at |aborted()| before running the callback).
59 // - They must call |MarkAsCompleted()| immediately before actually running the
60 // callback; see the comment for |MarkAsCompleted()| for a caveat.
61 class PPAPI_SHARED_EXPORT TrackedCallback
62 : public base::RefCountedThreadSafe
<TrackedCallback
> {
64 // Create a tracked completion callback and register it with the tracker. The
65 // resource pointer is not stored. If |resource| is NULL, this callback will
66 // not be added to the callback tracker.
67 TrackedCallback(Resource
* resource
, const PP_CompletionCallback
& callback
);
69 // These run the callback in an abortive manner, or post a task to do so (but
70 // immediately marking the callback as to be aborted).
74 // Run the callback with the given result. If the callback had previously been
75 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and
76 // the callback will be run with result |PP_ERROR_ABORTED|.
78 // Run() will invoke the call immediately, if invoked from the target thread
79 // (as determined by target_loop_). If invoked on a different thread, the
80 // callback will be scheduled to run later on target_loop_.
81 void Run(int32_t result
);
82 // PostRun is like Run(), except it guarantees that the callback will be run
83 // later. In particular, if you invoke PostRun on the same thread on which the
84 // callback is targeted to run, it will *not* be run immediately.
85 void PostRun(int32_t result
);
89 // Returns the ID of the resource which "owns" the callback, or 0 if the
90 // callback is not associated with any resource.
91 PP_Resource
resource_id() const { return resource_id_
; }
93 // Returns true if the callback was completed (possibly aborted).
94 bool completed() const { return completed_
; }
96 // Returns true if the callback was or should be aborted; this will be the
97 // case whenever |Abort()| or |PostAbort()| is called before a non-abortive
99 bool aborted() const { return aborted_
; }
101 // Helper to determine if the given callback is set and not yet completed.
102 // The normal pattern is to use a scoped_refptr to hold a callback. This
103 // function tells you if the operation is currently in progress by checking
104 // both the null-ness of the scoped_refptr, as well as the completion state
105 // of the callback (which may still be out-standing via a PostAbort).
106 static bool IsPending(const scoped_refptr
<TrackedCallback
>& callback
);
110 return !callback_
.func
;
113 return (callback_
.func
&&
114 !(callback_
.flags
& PP_COMPLETIONCALLBACK_FLAG_OPTIONAL
));
117 return (callback_
.func
&&
118 (callback_
.flags
& PP_COMPLETIONCALLBACK_FLAG_OPTIONAL
));
120 bool has_null_target_loop() const {
121 return target_loop_
== NULL
;
125 // TrackedCallback and EnterBase manage dealing with how to invoke callbacks
126 // appropriately. Pepper interface implementations and proxies should not have
127 // to check the type of callback, block, or mark them complete explicitly.
128 friend class ppapi::thunk::subtle::EnterBase
;
130 // Block until the associated operation has completed. Returns the result.
131 // This must only be called on a non-main thread on a blocking callback.
132 int32_t BlockUntilComplete();
134 // Mark this object as complete and remove it from the tracker. This must only
135 // be called once. Note that running this may result in this object being
136 // deleted (so keep a reference if it'll still be needed).
137 void MarkAsCompleted();
139 // This class is ref counted.
140 friend class base::RefCountedThreadSafe
<TrackedCallback
>;
141 virtual ~TrackedCallback();
143 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule
144 // the callback more than once.
147 scoped_refptr
<CallbackTracker
> tracker_
;
148 PP_Resource resource_id_
;
151 PP_CompletionCallback callback_
;
153 // The MessageLoopShared on which this callback should be run. This will be
154 // NULL if we're in-process.
155 scoped_refptr
<MessageLoopShared
> target_loop_
;
157 int32_t result_for_blocked_callback_
;
158 // 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
160 // not allowed, and therefore this pointer will be NULL.
161 scoped_ptr
<base::ConditionVariable
> operation_completed_condvar_
;
163 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback
);
168 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_