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

Unified Diff: android_webview/browser/net/input_stream_reader.cc

Issue 11363123: [android_webview] Don't block the IO thread when reading from an InputStream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix double-free 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
Index: android_webview/browser/net/input_stream_reader.cc
diff --git a/android_webview/browser/net/input_stream_reader.cc b/android_webview/browser/net/input_stream_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c223fbc711af6ac43d90f92611420a9434d483a4
--- /dev/null
+++ b/android_webview/browser/net/input_stream_reader.cc
@@ -0,0 +1,93 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "android_webview/browser/net/input_stream_reader.h"
+
+#include "android_webview/browser/input_stream.h"
+#include "base/message_loop.h"
+#include "content/public/browser/browser_thread.h"
+#include "net/base/net_errors.h"
+#include "net/http/http_byte_range.h"
+
+using content::BrowserThread;
+
+namespace android_webview {
+
+InputStreamReader::InputStreamReader(android_webview::InputStream* stream)
+ : stream_(stream) {
+ DCHECK(stream);
+}
+
+InputStreamReader::~InputStreamReader() {
+}
+
+int InputStreamReader::Seek(net::HttpByteRange byte_range) {
+ int content_size = 0;
+
+ int error_code = VerifyRequestedRange(&byte_range, &content_size);
+ if (error_code != net::OK)
+ return error_code;
+
+ error_code = SkipToRequestedRange(byte_range);
+ if (error_code != net::OK)
+ return error_code;
+
+ DCHECK_GE(content_size, 0);
+ return content_size;
+}
+
+int InputStreamReader::ReadRawData(net::IOBuffer* dest, int dest_size) {
+ if (!dest_size)
+ return 0;
+
+ DCHECK_GT(dest_size, 0);
+
+ int bytes_read = 0;
+ if (!stream_->Read(dest, dest_size, &bytes_read))
+ return net::ERR_FAILED;
+ else
+ return bytes_read;
+}
+
+int InputStreamReader::VerifyRequestedRange(net::HttpByteRange* byte_range,
+ int* content_size) {
+ DCHECK(content_size);
+ int32_t size = 0;
+ if (!stream_->BytesAvailable(&size))
+ return net::ERR_FAILED;
+
+ if (size <= 0)
+ return net::OK;
+
+ // Check that the requested range was valid.
+ if (!byte_range->ComputeBounds(size))
+ return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
+
+ size = byte_range->last_byte_position() -
+ byte_range->first_byte_position() + 1;
+ DCHECK_GE(size, 0);
+ *content_size = size;
+
+ return net::OK;
+}
+
+int InputStreamReader::SkipToRequestedRange(
+ const net::HttpByteRange& byte_range) {
+ // Skip to the start of the requested data. This has to be done in a loop
+ // because the underlying InputStream is not guaranteed to skip the requested
+ // number of bytes.
+ if (byte_range.IsValid() && byte_range.first_byte_position() != 0) {
+ int64_t skipped, bytes_to_skip = byte_range.first_byte_position();
joth 2012/11/20 20:46:35 nit: skipped can be declared inside loop.
mkosiba (inactive) 2012/11/21 15:19:47 Done.
+ do {
+ if (!stream_->Skip(bytes_to_skip, &skipped))
+ return net::ERR_FAILED;
+ if (skipped <= 0)
+ return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
+
+ } while ((bytes_to_skip -= skipped) > 0);
joth 2012/11/20 20:46:35 handle bytes_to_skip < 0
mkosiba (inactive) 2012/11/21 15:19:47 I assume you mean handling skipped > bytes_to_skip
+ }
+ return net::OK;
+}
+
+} // namespace android_webview

Powered by Google App Engine
This is Rietveld 408576698