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

Unified Diff: webkit/plugins/ppapi/ppb_url_loader_impl.cc

Issue 11417145: Provide a safer URLLoader ReadResponseBody API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webkit/plugins/ppapi/ppb_url_loader_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webkit/plugins/ppapi/ppb_url_loader_impl.cc
diff --git a/webkit/plugins/ppapi/ppb_url_loader_impl.cc b/webkit/plugins/ppapi/ppb_url_loader_impl.cc
index e707f261a20293f5e5424157f6b6a69c4573de4a..0310f53b2da06407a128b13aa49baa36c26e9694 100644
--- a/webkit/plugins/ppapi/ppb_url_loader_impl.cc
+++ b/webkit/plugins/ppapi/ppb_url_loader_impl.cc
@@ -10,6 +10,7 @@
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_url_loader.h"
#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
+#include "ppapi/shared_impl/array_writer.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/url_response_info_data.h"
#include "ppapi/thunk/enter.h"
@@ -66,6 +67,12 @@ WebFrame* GetFrameForResource(const Resource* resource) {
return plugin_instance->container()->element().document().frame();
}
+// An adapter to let ReadResponseBody() share the same implementation with
+// ReadResponseBodyToArray().
+void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) {
+ return user_data;
+}
+
} // namespace
PPB_URLLoader_Impl::PPB_URLLoader_Impl(PP_Instance instance,
@@ -247,24 +254,34 @@ int32_t PPB_URLLoader_Impl::ReadResponseBody(
void* buffer,
int32_t bytes_to_read,
scoped_refptr<TrackedCallback> callback) {
+ if (!buffer)
+ return PP_ERROR_BADARGUMENT;
+ PP_ArrayOutput output_adapter = { &DummyGetDataBuffer, buffer };
+ return ReadResponseBodyInternal(bytes_to_read, output_adapter, callback);
+}
+
+int32_t PPB_URLLoader_Impl::ReadResponseBodyInternal(
+ int32_t max_read_length,
+ const PP_ArrayOutput& array_output,
+ scoped_refptr<TrackedCallback> callback) {
int32_t rv = ValidateCallback(callback);
if (rv != PP_OK)
return rv;
if (!response_info_.get() ||
!response_info_->body_as_file_ref.resource.is_null())
return PP_ERROR_FAILED;
- if (bytes_to_read <= 0 || !buffer)
+ if (max_read_length <= 0)
return PP_ERROR_BADARGUMENT;
- user_buffer_ = static_cast<char*>(buffer);
- user_buffer_size_ = bytes_to_read;
+ user_buffer_.reset(new PP_ArrayOutput(array_output));
+ user_buffer_size_ = max_read_length;
if (!buffer_.empty())
return FillUserBuffer();
// We may have already reached EOF.
if (done_status_ != PP_OK_COMPLETIONPENDING) {
- user_buffer_ = NULL;
+ user_buffer_.reset(NULL);
user_buffer_size_ = 0;
return done_status_;
}
@@ -273,6 +290,15 @@ int32_t PPB_URLLoader_Impl::ReadResponseBody(
return PP_OK_COMPLETIONPENDING;
}
+int32_t PPB_URLLoader_Impl::ReadResponseBodyToArray(
+ int32_t max_read_length,
+ PP_ArrayOutput* array_output,
+ scoped_refptr<TrackedCallback> callback) {
+ if (!array_output)
+ return PP_ERROR_BADARGUMENT;
+ return ReadResponseBodyInternal(max_read_length, *array_output, callback);
+}
+
int32_t PPB_URLLoader_Impl::FinishStreamingToFile(
scoped_refptr<TrackedCallback> callback) {
int32_t rv = ValidateCallback(callback);
@@ -437,7 +463,7 @@ void PPB_URLLoader_Impl::SetDefersLoading(bool defers_loading) {
void PPB_URLLoader_Impl::FinishLoading(int32_t done_status) {
done_status_ = done_status;
- user_buffer_ = NULL;
+ user_buffer_.reset(NULL);
user_buffer_size_ = 0;
// If the client hasn't called any function that takes a callback since
// the initial call to Open, or called ReadResponseBody and got a
@@ -481,18 +507,22 @@ void PPB_URLLoader_Impl::RunCallback(int32_t result) {
// As a second line of defense, clear the |user_buffer_| in case the
// callbacks get called in an unexpected order.
- user_buffer_ = NULL;
+ user_buffer_.reset(NULL);
user_buffer_size_ = 0;
pending_callback_->Run(result);
}
size_t PPB_URLLoader_Impl::FillUserBuffer() {
- DCHECK(user_buffer_);
+ DCHECK(user_buffer_.get());
DCHECK(user_buffer_size_);
size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_);
- std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_);
- buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy);
+ ::ppapi::ArrayWriter output;
+ output.set_pp_array_output(*user_buffer_);
+ if (output.is_valid() && !buffer_.empty()) {
+ output.StoreArray(&buffer_[0], bytes_to_copy);
+ buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy);
+ }
// If the buffer is getting too empty, resume asynchronous loading.
if (is_asynchronous_load_suspended_ &&
@@ -503,7 +533,7 @@ size_t PPB_URLLoader_Impl::FillUserBuffer() {
}
// Reset for next time.
- user_buffer_ = NULL;
+ user_buffer_.reset(NULL);
user_buffer_size_ = 0;
return bytes_to_copy;
}
« no previous file with comments | « webkit/plugins/ppapi/ppb_url_loader_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698