Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "webkit/plugins/ppapi/ppb_url_loader_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_url_loader_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
| 9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/c/ppb_core.h" | |
| 10 #include "ppapi/c/ppb_url_loader.h" | 11 #include "ppapi/c/ppb_url_loader.h" |
| 11 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 12 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 40 #ifdef _MSC_VER | 41 #ifdef _MSC_VER |
| 41 // Do not warn about use of std::copy with raw pointers. | 42 // Do not warn about use of std::copy with raw pointers. |
| 42 #pragma warning(disable : 4996) | 43 #pragma warning(disable : 4996) |
| 43 #endif | 44 #endif |
| 44 | 45 |
| 45 namespace webkit { | 46 namespace webkit { |
| 46 namespace ppapi { | 47 namespace ppapi { |
| 47 | 48 |
| 48 namespace { | 49 namespace { |
| 49 | 50 |
| 51 int32_t CompletionResult(PP_CompletionCallback callback, int32_t result) { | |
| 52 if (callback.func == NULL || | |
| 53 (callback.flags & PP_COMPLETIONCALLBACK_FLAG_NOFORCEASYNC) != 0) | |
| 54 return result; | |
| 55 | |
| 56 PluginModule::GetCore()->CallOnMainThread(0, callback, result); | |
| 57 return PP_OK_COMPLETIONPENDING; | |
| 58 } | |
| 59 | |
| 50 PP_Resource Create(PP_Instance instance_id) { | 60 PP_Resource Create(PP_Instance instance_id) { |
| 51 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 61 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 52 if (!instance) | 62 if (!instance) |
| 53 return 0; | 63 return 0; |
| 54 | 64 |
| 55 PPB_URLLoader_Impl* loader = new PPB_URLLoader_Impl(instance, false); | 65 PPB_URLLoader_Impl* loader = new PPB_URLLoader_Impl(instance, false); |
| 56 return loader->GetReference(); | 66 return loader->GetReference(); |
| 57 } | 67 } |
| 58 | 68 |
| 59 PP_Bool IsURLLoader(PP_Resource resource) { | 69 PP_Bool IsURLLoader(PP_Resource resource) { |
| 60 return BoolToPPBool(!!Resource::GetAs<PPB_URLLoader_Impl>(resource)); | 70 return BoolToPPBool(!!Resource::GetAs<PPB_URLLoader_Impl>(resource)); |
| 61 } | 71 } |
| 62 | 72 |
| 63 int32_t Open(PP_Resource loader_id, | 73 int32_t Open(PP_Resource loader_id, |
| 64 PP_Resource request_id, | 74 PP_Resource request_id, |
| 65 PP_CompletionCallback callback) { | 75 PP_CompletionCallback callback) { |
| 66 scoped_refptr<PPB_URLLoader_Impl> loader( | 76 scoped_refptr<PPB_URLLoader_Impl> loader( |
| 67 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); | 77 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); |
| 68 if (!loader) | 78 if (!loader) |
| 69 return PP_ERROR_BADRESOURCE; | 79 return CompletionResult(callback, PP_ERROR_BADRESOURCE); |
| 70 | 80 |
| 71 scoped_refptr<PPB_URLRequestInfo_Impl> request( | 81 scoped_refptr<PPB_URLRequestInfo_Impl> request( |
| 72 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id)); | 82 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id)); |
| 73 if (!request) | 83 if (!request) |
| 74 return PP_ERROR_BADRESOURCE; | 84 return CompletionResult(callback, PP_ERROR_BADRESOURCE); |
| 75 | 85 |
| 76 return loader->Open(request, callback); | 86 return loader->Open(request, callback); |
| 77 } | 87 } |
| 78 | 88 |
| 79 int32_t FollowRedirect(PP_Resource loader_id, | 89 int32_t FollowRedirect(PP_Resource loader_id, |
| 80 PP_CompletionCallback callback) { | 90 PP_CompletionCallback callback) { |
| 81 scoped_refptr<PPB_URLLoader_Impl> loader( | 91 scoped_refptr<PPB_URLLoader_Impl> loader( |
| 82 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); | 92 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); |
| 83 if (!loader) | 93 if (!loader) |
| 84 return PP_ERROR_BADRESOURCE; | 94 return PP_ERROR_BADRESOURCE; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 return response_info->GetReference(); | 133 return response_info->GetReference(); |
| 124 } | 134 } |
| 125 | 135 |
| 126 int32_t ReadResponseBody(PP_Resource loader_id, | 136 int32_t ReadResponseBody(PP_Resource loader_id, |
| 127 void* buffer, | 137 void* buffer, |
| 128 int32_t bytes_to_read, | 138 int32_t bytes_to_read, |
| 129 PP_CompletionCallback callback) { | 139 PP_CompletionCallback callback) { |
| 130 scoped_refptr<PPB_URLLoader_Impl> loader( | 140 scoped_refptr<PPB_URLLoader_Impl> loader( |
| 131 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); | 141 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); |
| 132 if (!loader) | 142 if (!loader) |
| 133 return PP_ERROR_BADRESOURCE; | 143 return PP_ERROR_BADRESOURCE; |
|
darin (slow to review)
2011/04/25 18:31:06
what about this case?
polina
2011/04/25 19:44:44
I could not get ppapi_tests for url_loader get pas
| |
| 134 | 144 |
| 135 return loader->ReadResponseBody(buffer, bytes_to_read, callback); | 145 return loader->ReadResponseBody(buffer, bytes_to_read, callback); |
| 136 } | 146 } |
| 137 | 147 |
| 138 int32_t FinishStreamingToFile(PP_Resource loader_id, | 148 int32_t FinishStreamingToFile(PP_Resource loader_id, |
| 139 PP_CompletionCallback callback) { | 149 PP_CompletionCallback callback) { |
| 140 scoped_refptr<PPB_URLLoader_Impl> loader( | 150 scoped_refptr<PPB_URLLoader_Impl> loader( |
| 141 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); | 151 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); |
| 142 if (!loader) | 152 if (!loader) |
| 143 return PP_ERROR_BADRESOURCE; | 153 return PP_ERROR_BADRESOURCE; |
|
darin (slow to review)
2011/04/25 18:31:06
what about this case? and the others like this...
polina
2011/04/25 19:44:44
same reply as above.
| |
| 144 | 154 |
| 145 return loader->FinishStreamingToFile(callback); | 155 return loader->FinishStreamingToFile(callback); |
| 146 } | 156 } |
| 147 | 157 |
| 148 void Close(PP_Resource loader_id) { | 158 void Close(PP_Resource loader_id) { |
| 149 scoped_refptr<PPB_URLLoader_Impl> loader( | 159 scoped_refptr<PPB_URLLoader_Impl> loader( |
| 150 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); | 160 Resource::GetAs<PPB_URLLoader_Impl>(loader_id)); |
| 151 if (!loader) | 161 if (!loader) |
| 152 return; | 162 return; |
| 153 | 163 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 // Free the WebKit request when the instance has been destroyed to avoid | 245 // Free the WebKit request when the instance has been destroyed to avoid |
| 236 // using bandwidth just in case this object lives longer than the instance. | 246 // using bandwidth just in case this object lives longer than the instance. |
| 237 loader_.reset(); | 247 loader_.reset(); |
| 238 } | 248 } |
| 239 } | 249 } |
| 240 | 250 |
| 241 int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, | 251 int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, |
| 242 PP_CompletionCallback callback) { | 252 PP_CompletionCallback callback) { |
| 243 int32_t rv = ValidateCallback(callback); | 253 int32_t rv = ValidateCallback(callback); |
| 244 if (rv != PP_OK) | 254 if (rv != PP_OK) |
| 245 return rv; | 255 return CompletionResult(callback, rv); |
|
darin (slow to review)
2011/04/25 18:31:06
I think it would be better to put the callback fix
polina
2011/04/25 19:44:44
Do you mean put this in outer Open where it has:
| |
| 246 | 256 |
| 247 if (request->RequiresUniversalAccess() && !has_universal_access_) | 257 if (request->RequiresUniversalAccess() && !has_universal_access_) |
| 248 return PP_ERROR_BADARGUMENT; | 258 // Why is this not PP_ERROR_NOACCESS? |
| 259 return CompletionResult(callback, PP_ERROR_BADARGUMENT); | |
| 249 | 260 |
| 250 if (loader_.get()) | 261 if (loader_.get()) |
| 251 return PP_ERROR_INPROGRESS; | 262 return CompletionResult(callback, PP_ERROR_INPROGRESS); |
| 252 | 263 |
| 253 WebFrame* frame = GetFrame(instance()); | 264 WebFrame* frame = GetFrame(instance()); |
| 254 if (!frame) | 265 if (!frame) |
| 255 return PP_ERROR_FAILED; | 266 return CompletionResult(callback, PP_ERROR_FAILED); |
| 256 WebURLRequest web_request(request->ToWebURLRequest(frame)); | |
| 257 | 267 |
| 258 WebURLLoaderOptions options; | 268 WebURLLoaderOptions options; |
| 259 if (has_universal_access_) { | 269 if (has_universal_access_) { |
| 260 // Universal access allows cross-origin requests and sends credentials. | 270 // Universal access allows cross-origin requests and sends credentials. |
| 261 options.crossOriginRequestPolicy = | 271 options.crossOriginRequestPolicy = |
| 262 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 272 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 263 options.allowCredentials = true; | 273 options.allowCredentials = true; |
| 264 } else if (request->allow_cross_origin_requests()) { | 274 } else if (request->allow_cross_origin_requests()) { |
| 265 // Otherwise, allow cross-origin requests with access control. | 275 // Otherwise, allow cross-origin requests with access control. |
| 266 options.crossOriginRequestPolicy = | 276 options.crossOriginRequestPolicy = |
| 267 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; | 277 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
| 268 options.allowCredentials = request->allow_credentials(); | 278 options.allowCredentials = request->allow_credentials(); |
| 269 } | 279 } |
| 270 | 280 |
| 271 loader_.reset(frame->createAssociatedURLLoader(options)); | 281 loader_.reset(frame->createAssociatedURLLoader(options)); |
| 272 if (!loader_.get()) | 282 if (!loader_.get()) |
| 273 return PP_ERROR_FAILED; | 283 return CompletionResult(callback, PP_ERROR_FAILED); |
| 274 | 284 |
| 285 WebURLRequest web_request(request->ToWebURLRequest(frame)); | |
| 275 loader_->loadAsynchronously(web_request, this); | 286 loader_->loadAsynchronously(web_request, this); |
| 276 // Check for immediate failure; The AssociatedURLLoader will call our | 287 // Check for immediate failure; The AssociatedURLLoader will call our |
| 277 // didFail method synchronously for certain kinds of access violations | 288 // didFail method synchronously for certain kinds of access violations |
| 278 // so we must return an error to the caller. | 289 // so we must return an error to the caller. |
| 279 // TODO(bbudge) Modify the underlying AssociatedURLLoader to only call | 290 // TODO(bbudge) Modify the underlying AssociatedURLLoader to only call |
| 280 // back asynchronously. | 291 // back asynchronously. |
| 281 if (done_status_ == PP_ERROR_FAILED) | 292 if (done_status_ == PP_ERROR_FAILED) |
| 282 return PP_ERROR_NOACCESS; | 293 return CompletionResult(callback, PP_ERROR_NOACCESS); |
| 283 | 294 |
| 284 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request); | 295 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request); |
| 285 | 296 |
| 286 // Notify completion when we receive a redirect or response headers. | 297 // Notify completion when we receive a redirect or response headers. |
| 287 RegisterCallback(callback); | 298 RegisterCallback(callback); |
| 288 return PP_OK_COMPLETIONPENDING; | 299 return PP_OK_COMPLETIONPENDING; |
| 289 } | 300 } |
| 290 | 301 |
| 291 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { | 302 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { |
| 292 int32_t rv = ValidateCallback(callback); | 303 int32_t rv = ValidateCallback(callback); |
| 293 if (rv != PP_OK) | 304 if (rv != PP_OK) |
| 294 return rv; | 305 return CompletionResult(callback, rv); |
| 295 | 306 |
| 296 WebURL redirect_url = GURL(response_info_->redirect_url()); | 307 WebURL redirect_url = GURL(response_info_->redirect_url()); |
| 297 | 308 |
| 298 loader_->setDefersLoading(false); // Allow the redirect to continue. | 309 loader_->setDefersLoading(false); // Allow the redirect to continue. |
| 299 RegisterCallback(callback); | 310 RegisterCallback(callback); |
| 300 return PP_OK_COMPLETIONPENDING; | 311 return PP_OK_COMPLETIONPENDING; |
| 301 } | 312 } |
| 302 | 313 |
| 303 bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent, | 314 bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent, |
| 304 int64_t* total_bytes_to_be_sent) { | 315 int64_t* total_bytes_to_be_sent) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 323 *bytes_received = bytes_received_; | 334 *bytes_received = bytes_received_; |
| 324 *total_bytes_to_be_received = total_bytes_to_be_received_; | 335 *total_bytes_to_be_received = total_bytes_to_be_received_; |
| 325 return true; | 336 return true; |
| 326 } | 337 } |
| 327 | 338 |
| 328 int32_t PPB_URLLoader_Impl::ReadResponseBody(void* buffer, | 339 int32_t PPB_URLLoader_Impl::ReadResponseBody(void* buffer, |
| 329 int32_t bytes_to_read, | 340 int32_t bytes_to_read, |
| 330 PP_CompletionCallback callback) { | 341 PP_CompletionCallback callback) { |
| 331 int32_t rv = ValidateCallback(callback); | 342 int32_t rv = ValidateCallback(callback); |
| 332 if (rv != PP_OK) | 343 if (rv != PP_OK) |
| 333 return rv; | 344 return CompletionResult(callback, rv); |
| 334 if (!response_info_ || response_info_->body()) | 345 if (!response_info_ || response_info_->body()) |
| 335 return PP_ERROR_FAILED; | 346 return CompletionResult(callback, PP_ERROR_FAILED); |
| 336 if (bytes_to_read <= 0 || !buffer) | 347 if (bytes_to_read <= 0 || !buffer) |
| 337 return PP_ERROR_BADARGUMENT; | 348 return CompletionResult(callback, PP_ERROR_BADARGUMENT); |
| 338 | 349 |
| 339 user_buffer_ = static_cast<char*>(buffer); | 350 user_buffer_ = static_cast<char*>(buffer); |
| 340 user_buffer_size_ = bytes_to_read; | 351 user_buffer_size_ = bytes_to_read; |
| 341 | 352 |
| 342 if (!buffer_.empty()) | 353 if (!buffer_.empty()) |
| 343 return FillUserBuffer(); | 354 return CompletionResult(callback, FillUserBuffer()); |
| 344 | 355 |
| 345 // We may have already reached EOF. | 356 // We may have already reached EOF. |
| 346 if (done_status_ != PP_OK_COMPLETIONPENDING) { | 357 if (done_status_ != PP_OK_COMPLETIONPENDING) { |
| 347 user_buffer_ = NULL; | 358 user_buffer_ = NULL; |
| 348 user_buffer_size_ = 0; | 359 user_buffer_size_ = 0; |
| 349 return done_status_; | 360 return CompletionResult(callback, done_status_); |
| 350 } | 361 } |
| 351 | 362 |
| 352 RegisterCallback(callback); | 363 RegisterCallback(callback); |
| 353 return PP_OK_COMPLETIONPENDING; | 364 return PP_OK_COMPLETIONPENDING; |
| 354 } | 365 } |
| 355 | 366 |
| 356 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( | 367 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( |
| 357 PP_CompletionCallback callback) { | 368 PP_CompletionCallback callback) { |
| 358 int32_t rv = ValidateCallback(callback); | 369 int32_t rv = ValidateCallback(callback); |
| 359 if (rv != PP_OK) | 370 if (rv != PP_OK) |
| 360 return rv; | 371 return CompletionResult(callback, rv); |
| 361 if (!response_info_ || !response_info_->body()) | 372 if (!response_info_ || !response_info_->body()) |
| 362 return PP_ERROR_FAILED; | 373 return CompletionResult(callback, PP_ERROR_FAILED); |
| 363 | 374 |
| 364 // We may have already reached EOF. | 375 // We may have already reached EOF. |
| 365 if (done_status_ != PP_OK_COMPLETIONPENDING) | 376 if (done_status_ != PP_OK_COMPLETIONPENDING) |
| 366 return done_status_; | 377 return CompletionResult(callback, done_status_); |
| 367 | 378 |
| 368 // Wait for didFinishLoading / didFail. | 379 // Wait for didFinishLoading / didFail. |
| 369 RegisterCallback(callback); | 380 RegisterCallback(callback); |
| 370 return PP_OK_COMPLETIONPENDING; | 381 return PP_OK_COMPLETIONPENDING; |
| 371 } | 382 } |
| 372 | 383 |
| 373 void PPB_URLLoader_Impl::Close() { | 384 void PPB_URLLoader_Impl::Close() { |
| 374 if (loader_.get()) { | 385 if (loader_.get()) { |
| 375 loader_->cancel(); | 386 loader_->cancel(); |
| 376 } else if (main_document_loader_) { | 387 } else if (main_document_loader_) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { | 549 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { |
| 539 return request_info_ && request_info_->record_download_progress(); | 550 return request_info_ && request_info_->record_download_progress(); |
| 540 } | 551 } |
| 541 | 552 |
| 542 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 553 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
| 543 return request_info_ && request_info_->record_upload_progress(); | 554 return request_info_ && request_info_->record_upload_progress(); |
| 544 } | 555 } |
| 545 | 556 |
| 546 } // namespace ppapi | 557 } // namespace ppapi |
| 547 } // namespace webkit | 558 } // namespace webkit |
| OLD | NEW |