blob: c4bf5cfd279d3c23fe134835301c94a5001584a3 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_
6#define PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_
7
8#include <map>
9#include <set>
10
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"
20
21namespace ppapi {
22
23class CallbackTracker;
24class MessageLoopShared;
25class Resource;
26
27namespace thunk {
28namespace subtle {
29// For a friend declaration below.
30class EnterBase;
31}
32}
33
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).
41//
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.)
48//
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
52// completed.
53//
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.
61class PPAPI_SHARED_EXPORT TrackedCallback
62 : public base::RefCountedThreadSafe<TrackedCallback> {
63 public:
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);
68
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).
71 void Abort();
72 void PostAbort();
73
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|.
77 //
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);
86
87 void BlockUntilRun();
88
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_; }
92
93 // Returns true if the callback was completed (possibly aborted).
94 bool completed() const { return completed_; }
95
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
98 // completion.
99 bool aborted() const { return aborted_; }
100
Ben Murdochbb1529c2013-08-08 10:24:53 +0100101 // Returns true if this is a blocking callback.
102 bool is_blocking() {
103 return !callback_.func;
104 }
105
Ben Murdochfb250652013-07-31 11:42:55 +0100106 // Determines if the given callback is pending. A callback is pending if it
107 // has not completed and has not been aborted. When receiving a plugin call,
108 // use this to detect if |callback| represents an operation in progress. When
109 // finishing a plugin call, use this to determine whether to write 'out'
110 // params and Run |callback|.
111 // NOTE: an aborted callback has not necessarily completed, so a false result
112 // doesn't imply that the callback has completed.
113 // As a convenience, if |callback| is null, this returns false.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000114 static bool IsPending(const scoped_refptr<TrackedCallback>& callback);
115
Ben Murdoch558790d2013-07-30 15:19:42 +0100116 // Helper to determine if the given callback is scheduled to run on another
117 // message loop.
118 static bool IsScheduledToRun(const scoped_refptr<TrackedCallback>& callback);
119
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000120 protected:
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000121 bool is_required() {
122 return (callback_.func &&
123 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL));
124 }
125 bool is_optional() {
126 return (callback_.func &&
127 (callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL));
128 }
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100129 bool has_null_target_loop() const { return target_loop_.get() == NULL; }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000130
131 private:
132 // TrackedCallback and EnterBase manage dealing with how to invoke callbacks
133 // appropriately. Pepper interface implementations and proxies should not have
134 // to check the type of callback, block, or mark them complete explicitly.
135 friend class ppapi::thunk::subtle::EnterBase;
136
137 // Block until the associated operation has completed. Returns the result.
138 // This must only be called on a non-main thread on a blocking callback.
139 int32_t BlockUntilComplete();
140
141 // Mark this object as complete and remove it from the tracker. This must only
142 // be called once. Note that running this may result in this object being
143 // deleted (so keep a reference if it'll still be needed).
144 void MarkAsCompleted();
145
146 // This class is ref counted.
147 friend class base::RefCountedThreadSafe<TrackedCallback>;
148 virtual ~TrackedCallback();
149
150 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule
151 // the callback more than once.
152 bool is_scheduled_;
153
154 scoped_refptr<CallbackTracker> tracker_;
155 PP_Resource resource_id_;
156 bool completed_;
157 bool aborted_;
158 PP_CompletionCallback callback_;
159
160 // The MessageLoopShared on which this callback should be run. This will be
161 // NULL if we're in-process.
162 scoped_refptr<MessageLoopShared> target_loop_;
163
164 int32_t result_for_blocked_callback_;
165 // Used for pausing/waking the blocked thread if this is a blocking completion
166 // callback. Note that in-process, there is no lock, blocking callbacks are
167 // not allowed, and therefore this pointer will be NULL.
168 scoped_ptr<base::ConditionVariable> operation_completed_condvar_;
169
170 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback);
171};
172
173} // namespace ppapi
174
175#endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_