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