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

Side by Side Diff: ppapi/proxy/ppb_url_loader_proxy.cc

Issue 10081020: PPAPI: Make blocking completion callbacks work. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: export AssertLockHeld Created 8 years, 6 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) 2012 The Chromium Authors. All rights reserved. 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 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 #include "ppapi/proxy/ppb_url_loader_proxy.h" 5 #include "ppapi/proxy/ppb_url_loader_proxy.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <deque> 8 #include <deque>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 class URLLoader : public Resource, public PPB_URLLoader_API { 84 class URLLoader : public Resource, public PPB_URLLoader_API {
85 public: 85 public:
86 URLLoader(const HostResource& resource); 86 URLLoader(const HostResource& resource);
87 virtual ~URLLoader(); 87 virtual ~URLLoader();
88 88
89 // Resource overrides. 89 // Resource overrides.
90 virtual PPB_URLLoader_API* AsPPB_URLLoader_API() OVERRIDE; 90 virtual PPB_URLLoader_API* AsPPB_URLLoader_API() OVERRIDE;
91 91
92 // PPB_URLLoader_API implementation. 92 // PPB_URLLoader_API implementation.
93 virtual int32_t Open(PP_Resource request_id, 93 virtual int32_t Open(PP_Resource request_id,
94 PP_CompletionCallback callback) OVERRIDE; 94 scoped_refptr<TrackedCallback> callback) OVERRIDE;
95 virtual int32_t FollowRedirect(PP_CompletionCallback callback) OVERRIDE; 95 virtual int32_t FollowRedirect(
96 scoped_refptr<TrackedCallback> callback) OVERRIDE;
96 virtual PP_Bool GetUploadProgress(int64_t* bytes_sent, 97 virtual PP_Bool GetUploadProgress(int64_t* bytes_sent,
97 int64_t* total_bytes_to_be_sent) OVERRIDE; 98 int64_t* total_bytes_to_be_sent) OVERRIDE;
98 virtual PP_Bool GetDownloadProgress( 99 virtual PP_Bool GetDownloadProgress(
99 int64_t* bytes_received, 100 int64_t* bytes_received,
100 int64_t* total_bytes_to_be_received) OVERRIDE; 101 int64_t* total_bytes_to_be_received) OVERRIDE;
101 virtual PP_Resource GetResponseInfo() OVERRIDE; 102 virtual PP_Resource GetResponseInfo() OVERRIDE;
102 virtual int32_t ReadResponseBody(void* buffer, 103 virtual int32_t ReadResponseBody(
103 int32_t bytes_to_read, 104 void* buffer,
104 PP_CompletionCallback callback) OVERRIDE; 105 int32_t bytes_to_read,
106 scoped_refptr<TrackedCallback> callback) OVERRIDE;
105 virtual int32_t FinishStreamingToFile( 107 virtual int32_t FinishStreamingToFile(
106 PP_CompletionCallback callback) OVERRIDE; 108 scoped_refptr<TrackedCallback> callback) OVERRIDE;
107 virtual void Close() OVERRIDE; 109 virtual void Close() OVERRIDE;
108 virtual void GrantUniversalAccess() OVERRIDE; 110 virtual void GrantUniversalAccess() OVERRIDE;
109 virtual void SetStatusCallback( 111 virtual void SetStatusCallback(
110 PP_URLLoaderTrusted_StatusCallback cb) OVERRIDE; 112 PP_URLLoaderTrusted_StatusCallback cb) OVERRIDE;
111 113
112 // Called when the browser has new up/download progress to report. 114 // Called when the browser has new up/download progress to report.
113 void UpdateProgress(const PPBURLLoader_UpdateProgress_Params& params); 115 void UpdateProgress(const PPBURLLoader_UpdateProgress_Params& params);
114 116
115 // Called when the browser responds to our ReadResponseBody request. 117 // Called when the browser responds to our ReadResponseBody request.
116 void ReadResponseBodyAck(int32_t result, const std::string& data); 118 void ReadResponseBodyAck(int32_t result, const std::string& data);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 URLLoader::~URLLoader() { 173 URLLoader::~URLLoader() {
172 if (response_info_) 174 if (response_info_)
173 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(response_info_); 175 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(response_info_);
174 } 176 }
175 177
176 PPB_URLLoader_API* URLLoader::AsPPB_URLLoader_API() { 178 PPB_URLLoader_API* URLLoader::AsPPB_URLLoader_API() {
177 return this; 179 return this;
178 } 180 }
179 181
180 int32_t URLLoader::Open(PP_Resource request_id, 182 int32_t URLLoader::Open(PP_Resource request_id,
181 PP_CompletionCallback callback) { 183 scoped_refptr<TrackedCallback> callback) {
182 EnterResourceNoLock<thunk::PPB_URLRequestInfo_API> enter(request_id, true); 184 EnterResourceNoLock<thunk::PPB_URLRequestInfo_API> enter(request_id, true);
183 if (enter.failed()) { 185 if (enter.failed()) {
184 Log(PP_LOGLEVEL_ERROR, "PPB_URLLoader.Open: The URL you're requesting is " 186 Log(PP_LOGLEVEL_ERROR, "PPB_URLLoader.Open: The URL you're requesting is "
185 " on a different security origin than your plugin. To request " 187 " on a different security origin than your plugin. To request "
186 " cross-origin resources, see " 188 " cross-origin resources, see "
187 " PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS."); 189 " PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS.");
188 return PP_ERROR_BADRESOURCE; 190 return PP_ERROR_BADRESOURCE;
189 } 191 }
190 192
191 if (TrackedCallback::IsPending(current_callback_)) 193 if (TrackedCallback::IsPending(current_callback_))
192 return PP_ERROR_INPROGRESS; 194 return PP_ERROR_INPROGRESS;
193 195
194 if (!callback.func) 196 current_callback_ = callback;
195 return PP_ERROR_BLOCKS_MAIN_THREAD;
196 current_callback_ = new TrackedCallback(this, callback);
197 197
198 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_Open( 198 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_Open(
199 API_ID_PPB_URL_LOADER, host_resource(), enter.object()->GetData())); 199 API_ID_PPB_URL_LOADER, host_resource(), enter.object()->GetData()));
200 return PP_OK_COMPLETIONPENDING; 200 return PP_OK_COMPLETIONPENDING;
201 } 201 }
202 202
203 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { 203 int32_t URLLoader::FollowRedirect(scoped_refptr<TrackedCallback> callback) {
204 if (TrackedCallback::IsPending(current_callback_)) 204 if (TrackedCallback::IsPending(current_callback_))
205 return PP_ERROR_INPROGRESS; 205 return PP_ERROR_INPROGRESS;
206 206
207 if (!callback.func) 207 current_callback_ = callback;
208 return PP_ERROR_BLOCKS_MAIN_THREAD;
209 current_callback_ = new TrackedCallback(this, callback);
210 208
211 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect( 209 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect(
212 API_ID_PPB_URL_LOADER, host_resource())); 210 API_ID_PPB_URL_LOADER, host_resource()));
213 return PP_OK_COMPLETIONPENDING; 211 return PP_OK_COMPLETIONPENDING;
214 } 212 }
215 213
216 PP_Bool URLLoader::GetUploadProgress(int64_t* bytes_sent, 214 PP_Bool URLLoader::GetUploadProgress(int64_t* bytes_sent,
217 int64_t* total_bytes_to_be_sent) { 215 int64_t* total_bytes_to_be_sent) {
218 if (bytes_sent_ == -1) { 216 if (bytes_sent_ == -1) {
219 *bytes_sent = 0; 217 *bytes_sent = 0;
(...skipping 30 matching lines...) Expand all
250 response_id); 248 response_id);
251 } 249 }
252 250
253 // The caller expects to get a ref, and we want to keep holding ours. 251 // The caller expects to get a ref, and we want to keep holding ours.
254 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(response_info_); 252 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(response_info_);
255 return response_info_; 253 return response_info_;
256 } 254 }
257 255
258 int32_t URLLoader::ReadResponseBody(void* buffer, 256 int32_t URLLoader::ReadResponseBody(void* buffer,
259 int32_t bytes_to_read, 257 int32_t bytes_to_read,
260 PP_CompletionCallback callback) { 258 scoped_refptr<TrackedCallback> callback) {
261 if (!buffer || bytes_to_read <= 0) 259 if (!buffer || bytes_to_read <= 0)
262 return PP_ERROR_BADARGUMENT; // Must specify an output buffer. 260 return PP_ERROR_BADARGUMENT; // Must specify an output buffer.
263 if (TrackedCallback::IsPending(current_callback_)) 261 if (TrackedCallback::IsPending(current_callback_))
264 return PP_ERROR_INPROGRESS; // Can only have one request pending. 262 return PP_ERROR_INPROGRESS; // Can only have one request pending.
265 263
266 // Currently we don't support sync calls to read. We'll need to revisit
267 // how this works when we allow blocking calls (from background threads).
268 if (!callback.func)
269 return PP_ERROR_BADARGUMENT;
270
271 if (static_cast<size_t>(bytes_to_read) <= buffer_.size()) { 264 if (static_cast<size_t>(bytes_to_read) <= buffer_.size()) {
272 // Special case: we've buffered enough data to be able to synchronously 265 // Special case: we've buffered enough data to be able to synchronously
273 // return data to the caller. Do so without making IPCs. 266 // return data to the caller. Do so without making IPCs.
274 PopBuffer(buffer, bytes_to_read); 267 PopBuffer(buffer, bytes_to_read);
275 return bytes_to_read; 268 return bytes_to_read;
276 } 269 }
277 270
278 current_callback_ = new TrackedCallback(this, callback); 271 current_callback_ = callback;
279 current_read_buffer_ = buffer; 272 current_read_buffer_ = buffer;
280 current_read_buffer_size_ = bytes_to_read; 273 current_read_buffer_size_ = bytes_to_read;
281 274
282 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody( 275 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody(
283 API_ID_PPB_URL_LOADER, host_resource(), bytes_to_read)); 276 API_ID_PPB_URL_LOADER, host_resource(), bytes_to_read));
284 return PP_OK_COMPLETIONPENDING; 277 return PP_OK_COMPLETIONPENDING;
285 } 278 }
286 279
287 int32_t URLLoader::FinishStreamingToFile(PP_CompletionCallback callback) { 280 int32_t URLLoader::FinishStreamingToFile(
281 scoped_refptr<TrackedCallback> callback) {
288 if (TrackedCallback::IsPending(current_callback_)) 282 if (TrackedCallback::IsPending(current_callback_))
289 return PP_ERROR_INPROGRESS; 283 return PP_ERROR_INPROGRESS;
290 284
291 if (!callback.func) 285 current_callback_ = callback;
292 return PP_ERROR_BLOCKS_MAIN_THREAD;
293 current_callback_ = new TrackedCallback(this, callback);
294 286
295 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile( 287 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile(
296 API_ID_PPB_URL_LOADER, host_resource())); 288 API_ID_PPB_URL_LOADER, host_resource()));
297 return PP_OK_COMPLETIONPENDING; 289 return PP_OK_COMPLETIONPENDING;
298 } 290 }
299 291
300 void URLLoader::Close() { 292 void URLLoader::Close() {
301 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_Close( 293 GetDispatcher()->Send(new PpapiHostMsg_PPBURLLoader_Close(
302 API_ID_PPB_URL_LOADER, host_resource())); 294 API_ID_PPB_URL_LOADER, host_resource()));
303 } 295 }
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 } 589 }
598 590
599 void PPB_URLLoader_Proxy::OnCallback(int32_t result, 591 void PPB_URLLoader_Proxy::OnCallback(int32_t result,
600 const HostResource& resource) { 592 const HostResource& resource) {
601 dispatcher()->Send(new PpapiMsg_PPBURLLoader_CallbackComplete( 593 dispatcher()->Send(new PpapiMsg_PPBURLLoader_CallbackComplete(
602 API_ID_PPB_URL_LOADER, resource, result)); 594 API_ID_PPB_URL_LOADER, resource, result));
603 } 595 }
604 596
605 } // namespace proxy 597 } // namespace proxy
606 } // namespace ppapi 598 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_tcp_server_socket_private_proxy.cc ('k') | ppapi/proxy/ppb_video_capture_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698