Stop retring GCM unregistration requests after reaching maximum number
[chromium-blink-merge.git] / ppapi / shared_impl / tracked_callback.h
blob8bf7a4b33737549005dd14a82ce3e75d19e84585
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_
8 #include <map>
9 #include <set>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/condition_variable.h"
16 #include "base/synchronization/lock.h"
17 #include "ppapi/c/pp_completion_callback.h"
18 #include "ppapi/c/pp_instance.h"
19 #include "ppapi/c/pp_resource.h"
20 #include "ppapi/shared_impl/ppapi_shared_export.h"
21 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
23 namespace ppapi {
25 class CallbackTracker;
26 class MessageLoopShared;
27 class Resource;
29 namespace thunk {
30 namespace subtle {
31 // For a friend declaration below.
32 class EnterBase;
36 // |TrackedCallback| represents a tracked Pepper callback (from the browser to
37 // the plugin), typically still pending. Such callbacks have the standard Pepper
38 // callback semantics. Execution (i.e., completion) of callbacks happens through
39 // objects of subclasses of |TrackedCallback|. Two things are ensured: (1) that
40 // the callback is executed at most once, and (2) once a callback is marked to
41 // be aborted, any subsequent completion is abortive (even if a non-abortive
42 // completion had previously been scheduled).
44 // The details of non-abortive completion depend on the type of callback (e.g.,
45 // different parameters may be required), but basic abort functionality is core.
46 // The ability to post aborts is needed in many situations to ensure that the
47 // plugin is not re-entered into. (Note that posting a task to just run
48 // |Abort()| is different and not correct; calling |PostAbort()| additionally
49 // guarantees that all subsequent completions will be abortive.)
51 // This class is reference counted so that different things can hang on to it,
52 // and not worry too much about ensuring Pepper callback semantics. Note that
53 // the "owning" |CallbackTracker| will keep a reference until the callback is
54 // completed.
56 // A note on threading:
57 // TrackedCallback is usable on any thread. It is *mostly* only used when
58 // ppapi::ProxyLock is held. However, it's necessary that Run() can be called
59 // without the ProxyLock. This is used to allow running the callback from
60 // the IO thread. In particular, blocking callbacks may not have a message loop
61 // to which we could post, so Run() must be able to signal the condition
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.
64 class PPAPI_SHARED_EXPORT TrackedCallback
65 : public base::RefCountedThreadSafe<TrackedCallback> {
66 public:
67 // Create a tracked completion callback and register it with the tracker. The
68 // resource pointer is not stored. If |resource| is NULL, this callback will
69 // not be added to the callback tracker.
70 TrackedCallback(Resource* resource, const PP_CompletionCallback& callback);
72 // These run the callback in an abortive manner, or post a task to do so (but
73 // immediately marking the callback as to be aborted).
74 void Abort();
75 void PostAbort();
77 // Run the callback with the given result. If the callback had previously been
78 // marked as to be aborted (by |PostAbort()|), |result| will be ignored and
79 // the callback will be run with result |PP_ERROR_ABORTED|.
81 // Run() will invoke the call immediately, if invoked from the target thread
82 // (as determined by target_loop_). If invoked on a different thread, the
83 // callback will be scheduled to run later on target_loop_.
84 void Run(int32_t result);
85 void AcquireProxyLockAndRun(int32_t result);
86 // PostRun is like Run(), except it guarantees that the callback will be run
87 // later. In particular, if you invoke PostRun on the same thread on which the
88 // callback is targeted to run, it will *not* be run immediately.
89 void PostRun(int32_t result);
91 // A task to perform cleanup or write output parameters before the callback
92 // returns a result to the plugin. The |result| parameter has the result so
93 // far, e.g. whether the callback has been aborted. If the callback hasn't
94 // been aborted the return value of the task will become the callback result.
95 // The task is always called on the same thread as the callback to the plugin.
96 typedef base::Callback<int32_t(int32_t /* result */)> CompletionTask;
98 // Sets a task that is run just before calling back into the plugin. This
99 // should only be called once. Note that the CompletionTask always runs while
100 // holding the ppapi::ProxyLock.
101 void set_completion_task(const CompletionTask& completion_task);
103 // Returns the ID of the resource which "owns" the callback, or 0 if the
104 // callback is not associated with any resource.
105 PP_Resource resource_id() const { return resource_id_; }
107 // Returns true if this is a blocking callback.
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;
114 MessageLoopShared* target_loop() const {
115 // This is set on construction and never changes after that, so there is
116 // no need to lock.
117 return target_loop_.get();
120 // Determines if the given callback is pending. A callback is pending if it
121 // has not completed and has not been aborted. When receiving a plugin call,
122 // use this to detect if |callback| represents an operation in progress. When
123 // finishing a plugin call, use this to determine whether to write 'out'
124 // params and Run |callback|.
125 // NOTE: an aborted callback has not necessarily completed, so a false result
126 // doesn't imply that the callback has completed.
127 // As a convenience, if |callback| is null, this returns false.
128 static bool IsPending(const scoped_refptr<TrackedCallback>& callback);
130 // Helper to determine if the given callback is scheduled to run on another
131 // message loop.
132 static bool IsScheduledToRun(const scoped_refptr<TrackedCallback>& callback);
134 private:
135 bool is_required() {
136 return (callback_.func &&
137 !(callback_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL));
139 bool has_null_target_loop() const { return target_loop_.get() == NULL; }
141 // Same as PostRun(), but lock_ must already be held.
142 void PostRunWithLock(int32_t result);
144 void SignalBlockingCallback(int32_t result);
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.
152 // (There are exceptions; e.g. FileIO checks is_blocking() in order to do
153 // some operations directly on the calling thread if possible.)
154 friend class ppapi::thunk::subtle::EnterBase;
156 // Block until the associated operation has completed. Returns the result.
157 // This must only be called on a non-main thread on a blocking callback.
158 int32_t BlockUntilComplete();
160 // Mark this object as complete and remove it from the tracker. This must only
161 // be called once. Note that running this may result in this object being
162 // deleted (so keep a reference if it'll still be needed).
163 void MarkAsCompleted();
164 void MarkAsCompletedWithLock();
166 // This class is ref counted.
167 friend class base::RefCountedThreadSafe<TrackedCallback>;
168 ~TrackedCallback();
170 mutable base::Lock lock_;
172 // Flag used by |PostAbort()| and |PostRun()| to check that we don't schedule
173 // the callback more than once.
174 bool is_scheduled_;
176 scoped_refptr<CallbackTracker> tracker_;
177 PP_Resource resource_id_;
178 bool completed_;
179 bool aborted_;
180 PP_CompletionCallback callback_;
182 // Task to run just before calling back into the plugin.
183 CompletionTask completion_task_;
185 // The MessageLoopShared on which this callback should be run. This will be
186 // NULL if we're in-process.
187 scoped_refptr<MessageLoopShared> target_loop_;
189 int32_t result_for_blocked_callback_;
190 // Used for pausing/waking the blocked thread if this is a blocking completion
191 // callback. Note that in-process, there is no lock, blocking callbacks are
192 // not allowed, and therefore this pointer will be NULL.
193 scoped_ptr<base::ConditionVariable> operation_completed_condvar_;
195 DISALLOW_IMPLICIT_CONSTRUCTORS(TrackedCallback);
198 } // namespace ppapi
200 #endif // PPAPI_SHARED_IMPL_TRACKED_CALLBACK_H_