| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_url_loader.h" | 5 #include "webkit/glue/plugins/pepper_url_loader.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/ppapi/c/pp_completion_callback.h" | 8 #include "third_party/ppapi/c/pp_completion_callback.h" |
| 9 #include "third_party/ppapi/c/pp_errors.h" | 9 #include "third_party/ppapi/c/pp_errors.h" |
| 10 #include "third_party/ppapi/c/ppb_url_loader.h" | 10 #include "third_party/ppapi/c/ppb_url_loader.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 char* buffer, | 115 char* buffer, |
| 116 int32_t bytes_to_read, | 116 int32_t bytes_to_read, |
| 117 PP_CompletionCallback callback) { | 117 PP_CompletionCallback callback) { |
| 118 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); | 118 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); |
| 119 if (!loader) | 119 if (!loader) |
| 120 return PP_ERROR_BADRESOURCE; | 120 return PP_ERROR_BADRESOURCE; |
| 121 | 121 |
| 122 return loader->ReadResponseBody(buffer, bytes_to_read, callback); | 122 return loader->ReadResponseBody(buffer, bytes_to_read, callback); |
| 123 } | 123 } |
| 124 | 124 |
| 125 int32_t FinishStreamingToFile(PP_Resource loader_id, |
| 126 PP_CompletionCallback callback) { |
| 127 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); |
| 128 if (!loader) |
| 129 return PP_ERROR_BADRESOURCE; |
| 130 |
| 131 return loader->FinishStreamingToFile(callback); |
| 132 } |
| 133 |
| 125 void Close(PP_Resource loader_id) { | 134 void Close(PP_Resource loader_id) { |
| 126 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); | 135 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); |
| 127 if (!loader) | 136 if (!loader) |
| 128 return; | 137 return; |
| 129 | 138 |
| 130 loader->Close(); | 139 loader->Close(); |
| 131 } | 140 } |
| 132 | 141 |
| 133 const PPB_URLLoader ppb_urlloader = { | 142 const PPB_URLLoader ppb_urlloader = { |
| 134 &Create, | 143 &Create, |
| 135 &IsURLLoader, | 144 &IsURLLoader, |
| 136 &Open, | 145 &Open, |
| 137 &FollowRedirect, | 146 &FollowRedirect, |
| 138 &GetUploadProgress, | 147 &GetUploadProgress, |
| 139 &GetDownloadProgress, | 148 &GetDownloadProgress, |
| 140 &GetResponseInfo, | 149 &GetResponseInfo, |
| 141 &ReadResponseBody, | 150 &ReadResponseBody, |
| 151 &FinishStreamingToFile, |
| 142 &Close | 152 &Close |
| 143 }; | 153 }; |
| 144 | 154 |
| 145 } // namespace | 155 } // namespace |
| 146 | 156 |
| 147 URLLoader::URLLoader(PluginInstance* instance) | 157 URLLoader::URLLoader(PluginInstance* instance) |
| 148 : Resource(instance->module()), | 158 : Resource(instance->module()), |
| 149 instance_(instance), | 159 instance_(instance), |
| 150 pending_callback_(), | 160 pending_callback_(), |
| 151 bytes_sent_(0), | 161 bytes_sent_(0), |
| 152 total_bytes_to_be_sent_(-1), | 162 total_bytes_to_be_sent_(-1), |
| 153 bytes_received_(0), | 163 bytes_received_(0), |
| 154 total_bytes_to_be_received_(-1), | 164 total_bytes_to_be_received_(-1), |
| 155 user_buffer_(NULL), | 165 user_buffer_(NULL), |
| 156 user_buffer_size_(0), | 166 user_buffer_size_(0), |
| 157 done_(false) { | 167 done_status_(PP_ERROR_WOULDBLOCK) { |
| 158 } | 168 } |
| 159 | 169 |
| 160 URLLoader::~URLLoader() { | 170 URLLoader::~URLLoader() { |
| 161 } | 171 } |
| 162 | 172 |
| 163 // static | 173 // static |
| 164 const PPB_URLLoader* URLLoader::GetInterface() { | 174 const PPB_URLLoader* URLLoader::GetInterface() { |
| 165 return &ppb_urlloader; | 175 return &ppb_urlloader; |
| 166 } | 176 } |
| 167 | 177 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 193 return PP_ERROR_WOULDBLOCK; | 203 return PP_ERROR_WOULDBLOCK; |
| 194 } | 204 } |
| 195 | 205 |
| 196 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { | 206 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { |
| 197 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 207 NOTIMPLEMENTED(); // TODO(darin): Implement me. |
| 198 return PP_ERROR_FAILED; | 208 return PP_ERROR_FAILED; |
| 199 } | 209 } |
| 200 | 210 |
| 201 int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, | 211 int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, |
| 202 PP_CompletionCallback callback) { | 212 PP_CompletionCallback callback) { |
| 213 if (!response_info_ || response_info_->body()) |
| 214 return PP_ERROR_FAILED; |
| 203 if (bytes_to_read <= 0 || !buffer) | 215 if (bytes_to_read <= 0 || !buffer) |
| 204 return PP_ERROR_BADARGUMENT; | 216 return PP_ERROR_BADARGUMENT; |
| 205 if (pending_callback_.func) | 217 if (pending_callback_.func) |
| 206 return PP_ERROR_INPROGRESS; | 218 return PP_ERROR_INPROGRESS; |
| 207 | 219 |
| 208 // We only support non-blocking calls. | 220 // We only support non-blocking calls. |
| 209 if (!callback.func) | 221 if (!callback.func) |
| 210 return PP_ERROR_BADARGUMENT; | 222 return PP_ERROR_BADARGUMENT; |
| 211 | 223 |
| 212 user_buffer_ = buffer; | 224 user_buffer_ = buffer; |
| 213 user_buffer_size_ = bytes_to_read; | 225 user_buffer_size_ = bytes_to_read; |
| 214 | 226 |
| 215 if (!buffer_.empty()) | 227 if (!buffer_.empty()) |
| 216 return FillUserBuffer(); | 228 return FillUserBuffer(); |
| 217 | 229 |
| 218 if (done_) { | 230 // We may have already reached EOF. |
| 231 if (done_status_ != PP_ERROR_WOULDBLOCK) { |
| 219 user_buffer_ = NULL; | 232 user_buffer_ = NULL; |
| 220 user_buffer_size_ = 0; | 233 user_buffer_size_ = 0; |
| 221 return 0; | 234 return done_status_; |
| 222 } | 235 } |
| 223 | 236 |
| 224 pending_callback_ = callback; | 237 pending_callback_ = callback; |
| 225 return PP_ERROR_WOULDBLOCK; | 238 return PP_ERROR_WOULDBLOCK; |
| 226 } | 239 } |
| 227 | 240 |
| 241 int32_t URLLoader::FinishStreamingToFile(PP_CompletionCallback callback) { |
| 242 if (!response_info_ || !response_info_->body()) |
| 243 return PP_ERROR_FAILED; |
| 244 if (pending_callback_.func) |
| 245 return PP_ERROR_INPROGRESS; |
| 246 |
| 247 // We may have already reached EOF. |
| 248 if (done_status_ != PP_ERROR_WOULDBLOCK) |
| 249 return done_status_; |
| 250 |
| 251 // Wait for didFinishLoading / didFail. |
| 252 pending_callback_ = callback; |
| 253 return PP_ERROR_WOULDBLOCK; |
| 254 } |
| 255 |
| 228 void URLLoader::Close() { | 256 void URLLoader::Close() { |
| 229 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 257 NOTIMPLEMENTED(); // TODO(darin): Implement me. |
| 230 } | 258 } |
| 231 | 259 |
| 232 void URLLoader::willSendRequest(WebURLLoader* loader, | 260 void URLLoader::willSendRequest(WebURLLoader* loader, |
| 233 WebURLRequest& new_request, | 261 WebURLRequest& new_request, |
| 234 const WebURLResponse& redirect_response) { | 262 const WebURLResponse& redirect_response) { |
| 235 NOTIMPLEMENTED(); // TODO(darin): Allow the plugin to inspect redirects. | 263 NOTIMPLEMENTED(); // TODO(darin): Allow the plugin to inspect redirects. |
| 236 } | 264 } |
| 237 | 265 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 267 | 295 |
| 268 buffer_.insert(buffer_.end(), data, data + data_length); | 296 buffer_.insert(buffer_.end(), data, data + data_length); |
| 269 if (user_buffer_) { | 297 if (user_buffer_) { |
| 270 RunCallback(FillUserBuffer()); | 298 RunCallback(FillUserBuffer()); |
| 271 } else { | 299 } else { |
| 272 DCHECK(!pending_callback_.func); | 300 DCHECK(!pending_callback_.func); |
| 273 } | 301 } |
| 274 } | 302 } |
| 275 | 303 |
| 276 void URLLoader::didFinishLoading(WebURLLoader* loader) { | 304 void URLLoader::didFinishLoading(WebURLLoader* loader) { |
| 277 done_ = true; | 305 done_status_ = PP_OK; |
| 278 RunCallback(PP_OK); | 306 RunCallback(done_status_); |
| 279 } | 307 } |
| 280 | 308 |
| 281 void URLLoader::didFail(WebURLLoader* loader, const WebURLError& error) { | 309 void URLLoader::didFail(WebURLLoader* loader, const WebURLError& error) { |
| 282 done_ = true; | |
| 283 // TODO(darin): Provide more detailed error information. | 310 // TODO(darin): Provide more detailed error information. |
| 284 RunCallback(PP_ERROR_FAILED); | 311 done_status_ = PP_ERROR_FAILED; |
| 312 RunCallback(done_status_); |
| 285 } | 313 } |
| 286 | 314 |
| 287 void URLLoader::RunCallback(int32_t result) { | 315 void URLLoader::RunCallback(int32_t result) { |
| 288 if (!pending_callback_.func) | 316 if (!pending_callback_.func) |
| 289 return; | 317 return; |
| 290 | 318 |
| 291 PP_CompletionCallback callback = {0}; | 319 PP_CompletionCallback callback = {0}; |
| 292 std::swap(callback, pending_callback_); | 320 std::swap(callback, pending_callback_); |
| 293 PP_RunCompletionCallback(&callback, result); | 321 PP_RunCompletionCallback(&callback, result); |
| 294 } | 322 } |
| 295 | 323 |
| 296 size_t URLLoader::FillUserBuffer() { | 324 size_t URLLoader::FillUserBuffer() { |
| 297 DCHECK(user_buffer_); | 325 DCHECK(user_buffer_); |
| 298 DCHECK(user_buffer_size_); | 326 DCHECK(user_buffer_size_); |
| 299 | 327 |
| 300 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); | 328 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); |
| 301 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); | 329 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); |
| 302 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); | 330 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); |
| 303 | 331 |
| 304 // Reset for next time. | 332 // Reset for next time. |
| 305 user_buffer_ = NULL; | 333 user_buffer_ = NULL; |
| 306 user_buffer_size_ = 0; | 334 user_buffer_size_ = 0; |
| 307 return bytes_to_copy; | 335 return bytes_to_copy; |
| 308 } | 336 } |
| 309 | 337 |
| 310 } // namespace pepper | 338 } // namespace pepper |
| OLD | NEW |