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

Side by Side Diff: ppapi/proxy/enter_proxy.h

Issue 10081020: PPAPI: Make blocking completion callbacks work. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated TestURLLoader to test blocking callbacks. Created 8 years, 8 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_PROXY_ENTER_PROXY_H_ 5 #ifndef PPAPI_PROXY_ENTER_PROXY_H_
6 #define PPAPI_PROXY_ENTER_PROXY_H_ 6 #define PPAPI_PROXY_ENTER_PROXY_H_
7 7
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "ppapi/cpp/completion_callback.h" 9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/proxy/host_dispatcher.h" 10 #include "ppapi/proxy/host_dispatcher.h"
(...skipping 27 matching lines...) Expand all
38 DCHECK(this->failed() || 38 DCHECK(this->failed() ||
39 PluginDispatcher::GetForInstance(host_resource.instance())); 39 PluginDispatcher::GetForInstance(host_resource.instance()));
40 } 40 }
41 }; 41 };
42 42
43 template<typename ResourceT> 43 template<typename ResourceT>
44 class EnterHostFromHostResource 44 class EnterHostFromHostResource
45 : public thunk::EnterResourceNoLock<ResourceT> { 45 : public thunk::EnterResourceNoLock<ResourceT> {
46 public: 46 public:
47 explicit EnterHostFromHostResource(const HostResource& host_resource) 47 explicit EnterHostFromHostResource(const HostResource& host_resource)
48 : thunk::EnterResourceNoLock<ResourceT>( 48 : thunk::EnterResourceNoLock<ResourceT>(host_resource.host_resource(),
49 host_resource.host_resource(), false) { 49 false) {
50 // Validate that we're in the host rather than the plugin. Otherwise this 50 // Validate that we're in the host rather than the plugin. Otherwise this
51 // object will do the wrong thing. In the host, the instance should have 51 // object will do the wrong thing. In the host, the instance should have
52 // a corresponding host disptacher (assuming the resource is valid). 52 // a corresponding host disptacher (assuming the resource is valid).
53 DCHECK(this->failed() ||
54 HostDispatcher::GetForInstance(host_resource.instance()));
55 }
56 EnterHostFromHostResource(const HostResource& host_resource,
57 const pp::CompletionCallback& callback)
58 : thunk::EnterResourceNoLock<ResourceT>(host_resource.host_resource(),
59 callback.pp_completion_callback(),
60 false) {
61 // Validate that we're in the host rather than the plugin. Otherwise this
62 // object will do the wrong thing. In the host, the instance should have
63 // a corresponding host disptacher (assuming the resource is valid).
53 DCHECK(this->failed() || 64 DCHECK(this->failed() ||
54 HostDispatcher::GetForInstance(host_resource.instance())); 65 HostDispatcher::GetForInstance(host_resource.instance()));
55 } 66 }
56 }; 67 };
57 68
58 // Enters a resource and forces a completion callback to be issued. 69 // Enters a resource and forces a completion callback to be issued.
59 // 70 //
60 // This is used when implementing the host (renderer) side of a resource 71 // This is used when implementing the host (renderer) side of a resource
61 // function that issues a completion callback. In all cases, we need to issue 72 // function that issues a completion callback. In all cases, we need to issue
62 // the callback to avoid hanging the plugin. 73 // the callback to avoid hanging the plugin.
(...skipping 22 matching lines...) Expand all
85 // void MyClass::SendResult(int32_t result, const HostResource& res) { 96 // void MyClass::SendResult(int32_t result, const HostResource& res) {
86 // Send(new FooMsg_FooComplete(..., result, res)); 97 // Send(new FooMsg_FooComplete(..., result, res));
87 // } 98 // }
88 template<typename ResourceT> 99 template<typename ResourceT>
89 class EnterHostFromHostResourceForceCallback 100 class EnterHostFromHostResourceForceCallback
90 : public EnterHostFromHostResource<ResourceT> { 101 : public EnterHostFromHostResource<ResourceT> {
91 public: 102 public:
92 EnterHostFromHostResourceForceCallback( 103 EnterHostFromHostResourceForceCallback(
93 const HostResource& host_resource, 104 const HostResource& host_resource,
94 const pp::CompletionCallback& callback) 105 const pp::CompletionCallback& callback)
95 : EnterHostFromHostResource<ResourceT>(host_resource), 106 : EnterHostFromHostResource<ResourceT>(host_resource, callback),
96 needs_running_(true), 107 needs_running_(true) {
97 callback_(callback) {
98 } 108 }
99 109
100 // For callbacks that take no parameters except the "int32_t result". Most 110 // For callbacks that take no parameters except the "int32_t result". Most
101 // implementations will use the 1-extra-argument constructor below. 111 // implementations will use the 1-extra-argument constructor below.
102 template<class CallbackFactory, typename Method> 112 template<class CallbackFactory, typename Method>
103 EnterHostFromHostResourceForceCallback( 113 EnterHostFromHostResourceForceCallback(
104 const HostResource& host_resource, 114 const HostResource& host_resource,
105 CallbackFactory& factory, 115 CallbackFactory& factory,
106 Method method) 116 Method method)
107 : EnterHostFromHostResource<ResourceT>(host_resource), 117 : EnterHostFromHostResource<ResourceT>(host_resource,
108 needs_running_(true), 118 factory.NewOptionalCallback(method)),
109 callback_(factory.NewOptionalCallback(method)) { 119 needs_running_(true) {
110 if (this->failed()) 120 if (this->failed())
111 RunCallback(PP_ERROR_BADRESOURCE); 121 this->callback()->Run(PP_ERROR_BADRESOURCE);
112 } 122 }
113 123
114 // For callbacks that take an extra parameter as a closure. 124 // For callbacks that take an extra parameter as a closure.
115 template<class CallbackFactory, typename Method, typename A> 125 template<class CallbackFactory, typename Method, typename A>
116 EnterHostFromHostResourceForceCallback( 126 EnterHostFromHostResourceForceCallback(
117 const HostResource& host_resource, 127 const HostResource& host_resource,
118 CallbackFactory& factory, 128 CallbackFactory& factory,
119 Method method, 129 Method method,
120 const A& a) 130 const A& a)
121 : EnterHostFromHostResource<ResourceT>(host_resource), 131 : EnterHostFromHostResource<ResourceT>(host_resource,
122 needs_running_(true), 132 factory.NewOptionalCallback(method, a)),
123 callback_(factory.NewOptionalCallback(method, a)) { 133 needs_running_(true) {
124 if (this->failed()) 134 if (this->failed())
125 RunCallback(PP_ERROR_BADRESOURCE); 135 RunCallback(PP_ERROR_BADRESOURCE);
126 } 136 }
127 137
128 // For callbacks that take two extra parameters as a closure. 138 // For callbacks that take two extra parameters as a closure.
129 template<class CallbackFactory, typename Method, typename A, typename B> 139 template<class CallbackFactory, typename Method, typename A, typename B>
130 EnterHostFromHostResourceForceCallback( 140 EnterHostFromHostResourceForceCallback(
131 const HostResource& host_resource, 141 const HostResource& host_resource,
132 CallbackFactory& factory, 142 CallbackFactory& factory,
133 Method method, 143 Method method,
134 const A& a, 144 const A& a,
135 const B& b) 145 const B& b)
136 : EnterHostFromHostResource<ResourceT>(host_resource), 146 : EnterHostFromHostResource<ResourceT>(host_resource,
137 needs_running_(true), 147 factory.NewOptionalCallback(method, a, b)),
138 callback_(factory.NewOptionalCallback(method, a, b)) { 148 needs_running_(true) {
139 if (this->failed()) 149 if (this->failed())
140 RunCallback(PP_ERROR_BADRESOURCE); 150 RunCallback(PP_ERROR_BADRESOURCE);
141 } 151 }
142 152
143 ~EnterHostFromHostResourceForceCallback() { 153 ~EnterHostFromHostResourceForceCallback() {
144 if (needs_running_) { 154 if (needs_running_) {
145 NOTREACHED() << "Should always call SetResult except in the " 155 NOTREACHED() << "Should always call SetResult except in the "
146 "initialization failed case."; 156 "initialization failed case.";
147 RunCallback(PP_ERROR_FAILED); 157 RunCallback(PP_ERROR_FAILED);
148 } 158 }
149 } 159 }
150 160
151 void SetResult(int32_t result) { 161 void SetResult(int32_t result) {
152 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; 162 DCHECK(needs_running_) << "Don't call SetResult when there already is one.";
153 needs_running_ = false; 163 needs_running_ = false;
154 if (result != PP_OK_COMPLETIONPENDING) 164 if (result != PP_OK_COMPLETIONPENDING)
155 callback_.Run(result); 165 this->callback()->Run(result);
156 }
157
158 PP_CompletionCallback callback() {
159 return callback_.pp_completion_callback();
160 } 166 }
161 167
162 private: 168 private:
163 void RunCallback(int32_t result) { 169 void RunCallback(int32_t result) {
164 DCHECK(needs_running_); 170 DCHECK(needs_running_);
165 needs_running_ = false; 171 needs_running_ = false;
166 callback_.Run(result); 172 this->callback()->Run(result);
167 } 173 }
168 174
169 bool needs_running_; 175 bool needs_running_;
170 pp::CompletionCallback callback_;
171 }; 176 };
172 177
173 // Like EnterHostFromHostResourceForceCallback but for Function APIs. It takes 178 // Like EnterHostFromHostResourceForceCallback but for Function APIs. It takes
174 // an instance instead of a resource ID. 179 // an instance instead of a resource ID.
175 template<typename FunctionT> 180 template<typename FunctionT>
176 class EnterHostFunctionForceCallback 181 class EnterHostFunctionForceCallback
177 : public thunk::EnterFunctionNoLock<FunctionT> { 182 : public thunk::EnterFunctionNoLock<FunctionT> {
178 public: 183 public:
179 EnterHostFunctionForceCallback( 184 EnterHostFunctionForceCallback(
180 PP_Instance instance, 185 PP_Instance instance,
181 const pp::CompletionCallback& callback) 186 const pp::CompletionCallback& callback)
182 : thunk::EnterFunctionNoLock<FunctionT>(instance, false), 187 : thunk::EnterFunctionNoLock<FunctionT>(instance, false),
183 needs_running_(true), 188 needs_running_(true) {
184 callback_(callback) {
185 if (this->failed()) 189 if (this->failed())
186 RunCallback(PP_ERROR_BADARGUMENT); 190 RunCallback(PP_ERROR_BADARGUMENT);
187 } 191 }
188 192
189 ~EnterHostFunctionForceCallback() { 193 ~EnterHostFunctionForceCallback() {
190 if (needs_running_) { 194 if (needs_running_) {
191 NOTREACHED() << "Should always call SetResult except in the " 195 NOTREACHED() << "Should always call SetResult except in the "
192 "initialization failed case."; 196 "initialization failed case.";
193 RunCallback(PP_ERROR_FAILED); 197 RunCallback(PP_ERROR_FAILED);
194 } 198 }
195 } 199 }
196 200
197 void SetResult(int32_t result) { 201 void SetResult(int32_t result) {
198 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; 202 DCHECK(needs_running_) << "Don't call SetResult when there already is one.";
199 needs_running_ = false; 203 needs_running_ = false;
200 if (result != PP_OK_COMPLETIONPENDING) 204 if (result != PP_OK_COMPLETIONPENDING)
201 callback_.Run(result); 205 this->callback()->Run(result);
202 }
203
204 PP_CompletionCallback callback() {
205 return callback_.pp_completion_callback();
206 } 206 }
207 207
208 private: 208 private:
209 void RunCallback(int32_t result) { 209 void RunCallback(int32_t result) {
210 DCHECK(needs_running_); 210 DCHECK(needs_running_);
211 needs_running_ = false; 211 needs_running_ = false;
212 callback_.Run(result); 212 this->callback()->Run(result);
213 } 213 }
214 214
215 bool needs_running_; 215 bool needs_running_;
216 pp::CompletionCallback callback_;
217 }; 216 };
218 217
219 } // namespace proxy 218 } // namespace proxy
220 } // namespace ppapi 219 } // namespace ppapi
221 220
222 #endif // PPAPI_PROXY_ENTER_PROXY_H_ 221 #endif // PPAPI_PROXY_ENTER_PROXY_H_
OLDNEW
« no previous file with comments | « no previous file | ppapi/proxy/plugin_dispatcher.h » ('j') | ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698