Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: ppapi/utility/completion_callback_factory.h

Issue 9030001: Move paint aggregator and the completion callback factory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix DEPS Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 PPAPI_CPP_COMPLETION_CALLBACK_H_ 5 #ifndef PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
6 #define PPAPI_CPP_COMPLETION_CALLBACK_H_ 6 #define PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
7 7
8 #include "ppapi/c/pp_completion_callback.h" 8 #include "ppapi/cpp/completion_callback.h"
9 #include "ppapi/c/pp_errors.h" 9 #include "ppapi/utility/non_thread_safe_ref_count.h"
10 #include "ppapi/cpp/logging.h" 10
11 #include "ppapi/cpp/non_thread_safe_ref_count.h"
12
13 /// @file
14 /// This file defines the API to create and run a callback.
15 namespace pp { 11 namespace pp {
16 12
17 /// This API enables you to implement and receive callbacks when
18 /// Pepper operations complete asynchronously.
19 class CompletionCallback {
20 public:
21 /// The default constructor will create a blocking
22 /// <code>CompletionCallback</code> that can be passed to a method to
23 /// indicate that the calling thread should be blocked until the asynchronous
24 /// operation corresponding to the method completes.
25 ///
26 /// <strong>Note:</strong> Blocking completion callbacks are only allowed from
27 /// from background threads.
28 CompletionCallback() {
29 cc_ = PP_BlockUntilComplete();
30 }
31
32 /// A constructor for creating a <code>CompletionCallback</code>.
33 ///
34 /// @param[in] func The function to be called on completion.
35 /// @param[in] user_data The user data to be passed to the callback function.
36 /// This is optional and is typically used to help track state in case of
37 /// multiple pending callbacks.
38 CompletionCallback(PP_CompletionCallback_Func func, void* user_data) {
39 cc_ = PP_MakeCompletionCallback(func, user_data);
40 }
41
42 /// A constructor for creating a <code>CompletionCallback</code> with
43 /// specified flags.
44 ///
45 /// @param[in] func The function to be called on completion.
46 /// @param[in] user_data The user data to be passed to the callback function.
47 /// This is optional and is typically used to help track state in case of
48 /// multiple pending callbacks.
49 /// @param[in] flags Bit field combination of
50 /// <code>PP_CompletionCallback_Flag</code> flags used to control how
51 /// non-NULL callbacks are scheduled by asynchronous methods.
52 CompletionCallback(PP_CompletionCallback_Func func, void* user_data,
53 int32_t flags) {
54 cc_ = PP_MakeCompletionCallback(func, user_data);
55 cc_.flags = flags;
56 }
57
58 /// The set_flags() function is used to set the flags used to control
59 /// how non-NULL callbacks are scheduled by asynchronous methods.
60 ///
61 /// @param[in] flags Bit field combination of
62 /// <code>PP_CompletionCallback_Flag</code> flags used to control how
63 /// non-NULL callbacks are scheduled by asynchronous methods.
64 void set_flags(int32_t flags) { cc_.flags = flags; }
65
66 /// Run() is used to run the <code>CompletionCallback</code>.
67 /// Normally, the system runs a <code>CompletionCallback</code> after an
68 /// asynchronous operation completes, but programs may wish to run the
69 /// <code>CompletionCallback</code> manually in order to reuse the same code
70 /// paths.
71 ///
72 /// @param[in] result The result of the operation to be passed to the
73 /// callback function. Non-positive values correspond to the error codes
74 /// from <code>pp_errors.h</code> (excluding
75 /// <code>PP_OK_COMPLETIONPENDING</code>). Positive values indicate
76 /// additional information such as bytes read.
77 void Run(int32_t result) {
78 PP_DCHECK(cc_.func);
79 PP_RunCompletionCallback(&cc_, result);
80 }
81
82 /// IsOptional() is used to determine the setting of the
83 /// <code>PP_COMPLETIONCALLBACK_FLAG_OPTIONAL</code> flag. This flag allows
84 /// any method taking such callback to complete synchronously
85 /// and not call the callback if the operation would not block. This is useful
86 /// when performance is an issue, and the operation bandwidth should not be
87 /// limited to the processing speed of the message loop.
88 ///
89 /// On synchronous method completion, the completion result will be returned
90 /// by the method itself. Otherwise, the method will return
91 /// PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously
92 /// on the main thread of Pepper execution.
93 ///
94 /// @return true if this callback is optional, otherwise false.
95 bool IsOptional() const {
96 return (cc_.func == NULL ||
97 (cc_.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL) != 0);
98 }
99
100 /// The pp_completion_callback() function returns the underlying
101 /// <code>PP_CompletionCallback</code>
102 ///
103 /// @return A <code>PP_CompletionCallback</code>.
104 const PP_CompletionCallback& pp_completion_callback() const { return cc_; }
105
106 /// The flags() function returns flags used to control how non-NULL callbacks
107 /// are scheduled by asynchronous methods.
108 ///
109 /// @return An int32_t containing a bit field combination of
110 /// <code>PP_CompletionCallback_Flag</code> flags.
111 int32_t flags() const { return cc_.flags; }
112
113 /// MayForce() is used when implementing functions taking callbacks.
114 /// If the callback is required and <code>result</code> indicates that it has
115 /// not been scheduled, it will be forced on the main thread.
116 ///
117 /// <strong>Example:</strong>
118 ///
119 /// @code
120 ///
121 /// int32_t OpenURL(pp::URLLoader* loader,
122 /// pp::URLRequestInfo* url_request_info,
123 /// const CompletionCallback& cc) {
124 /// if (loader == NULL || url_request_info == NULL)
125 /// return cc.MayForce(PP_ERROR_BADRESOURCE);
126 /// return loader->Open(*loader, *url_request_info, cc);
127 /// }
128 ///
129 /// @endcode
130 ///
131 /// @param[in] result PP_OK_COMPLETIONPENDING or the result of the completed
132 /// operation to be passed to the callback function. PP_OK_COMPLETIONPENDING
133 /// indicates that the callback has already been scheduled. Other
134 /// non-positive values correspond to error codes from
135 /// <code>pp_errors.h</code>. Positive values indicate additional information
136 /// such as bytes read.
137 ///
138 /// @return <code>PP_OK_COMPLETIONPENDING</code> if the callback has been
139 /// forced, result parameter otherwise.
140 int32_t MayForce(int32_t result) const {
141 if (result == PP_OK_COMPLETIONPENDING || IsOptional())
142 return result;
143 Module::Get()->core()->CallOnMainThread(0, *this, result);
144 return PP_OK_COMPLETIONPENDING;
145 }
146
147 protected:
148 PP_CompletionCallback cc_;
149 };
150
151 /// BlockUntilComplete() is used in place of an actual completion callback
152 /// to request blocking behavior. If specified, the calling thread will block
153 /// until the function completes. Blocking completion callbacks are only
154 /// allowed from background threads.
155 ///
156 /// @return A <code>CompletionCallback</code> corresponding to a NULL callback.
157 CompletionCallback BlockUntilComplete();
158
159 /// CompletionCallbackFactory<T> may be used to create CompletionCallback 13 /// CompletionCallbackFactory<T> may be used to create CompletionCallback
160 /// objects that are bound to member functions. 14 /// objects that are bound to member functions.
161 /// 15 ///
162 /// If a factory is destroyed, then any pending callbacks will be cancelled 16 /// If a factory is destroyed, then any pending callbacks will be cancelled
163 /// preventing any bound member functions from being called. The CancelAll() 17 /// preventing any bound member functions from being called. The CancelAll()
164 /// method allows pending callbacks to be cancelled without destroying the 18 /// method allows pending callbacks to be cancelled without destroying the
165 /// factory. 19 /// factory.
166 /// 20 ///
167 /// <strong>Note: </strong><code>CompletionCallbackFactory<T></code> isn't 21 /// <strong>Note: </strong><code>CompletionCallbackFactory<T></code> isn't
168 /// thread safe, but you can make it more thread-friendly by passing a 22 /// thread safe, but you can make it more thread-friendly by passing a
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 // Disallowed: 554 // Disallowed:
701 CompletionCallbackFactory(const CompletionCallbackFactory&); 555 CompletionCallbackFactory(const CompletionCallbackFactory&);
702 CompletionCallbackFactory& operator=(const CompletionCallbackFactory&); 556 CompletionCallbackFactory& operator=(const CompletionCallbackFactory&);
703 557
704 T* object_; 558 T* object_;
705 BackPointer* back_pointer_; 559 BackPointer* back_pointer_;
706 }; 560 };
707 561
708 } // namespace pp 562 } // namespace pp
709 563
710 #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ 564 #endif // PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698