OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "android_webview/browser/net/input_stream_reader.h" |
| 6 |
| 7 #include "android_webview/browser/input_stream.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_byte_range.h" |
| 12 |
| 13 using base::android::AttachCurrentThread; |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace android_webview { |
| 17 |
| 18 InputStreamReader::InputStreamReader(android_webview::InputStream* stream) |
| 19 : stream_(stream) { |
| 20 DCHECK(stream); |
| 21 } |
| 22 |
| 23 InputStreamReader::~InputStreamReader() { |
| 24 } |
| 25 |
| 26 int InputStreamReader::Seek(net::HttpByteRange byte_range) { |
| 27 JNIEnv* env = AttachCurrentThread(); |
| 28 int content_size = 0; |
| 29 DCHECK(env); |
| 30 |
| 31 int error_code = VerifyRequestedRange(env, &byte_range, &content_size); |
| 32 if (error_code != net::OK) |
| 33 return error_code; |
| 34 |
| 35 error_code = SkipToRequestedRange(env, byte_range); |
| 36 if (error_code != net::OK) |
| 37 return error_code; |
| 38 |
| 39 DCHECK_GE(content_size, 0); |
| 40 return content_size; |
| 41 } |
| 42 |
| 43 int InputStreamReader::ReadRawData(net::IOBuffer* dest, int dest_size) { |
| 44 if (!dest_size) |
| 45 return 0; |
| 46 |
| 47 JNIEnv* env = AttachCurrentThread(); |
| 48 DCHECK(env); |
| 49 DCHECK_GT(dest_size, 0); |
| 50 |
| 51 int bytes_read = 0; |
| 52 if (!stream_->Read(env, dest, dest_size, &bytes_read)) |
| 53 return net::ERR_FAILED; |
| 54 else |
| 55 return bytes_read; |
| 56 } |
| 57 |
| 58 int InputStreamReader::VerifyRequestedRange( |
| 59 JNIEnv* env, |
| 60 net::HttpByteRange* byte_range, |
| 61 int* content_size) { |
| 62 DCHECK(content_size); |
| 63 int32_t size = 0; |
| 64 if (!stream_->BytesAvailable(env, &size)) |
| 65 return net::ERR_FAILED; |
| 66 |
| 67 if (size <= 0) |
| 68 return net::OK; |
| 69 |
| 70 // Check that the requested range was valid. |
| 71 if (!byte_range->ComputeBounds(size)) |
| 72 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
| 73 |
| 74 size = byte_range->last_byte_position() - |
| 75 byte_range->first_byte_position() + 1; |
| 76 DCHECK_GE(size, 0); |
| 77 *content_size = size; |
| 78 |
| 79 return net::OK; |
| 80 } |
| 81 |
| 82 int InputStreamReader::SkipToRequestedRange( |
| 83 JNIEnv* env, |
| 84 const net::HttpByteRange& byte_range) { |
| 85 // Skip to the start of the requested data. This has to be done in a loop |
| 86 // because the underlying InputStream is not guaranteed to skip the requested |
| 87 // number of bytes. |
| 88 if (byte_range.IsValid() && byte_range.first_byte_position() != 0) { |
| 89 int64_t skipped, bytes_to_skip = byte_range.first_byte_position(); |
| 90 do { |
| 91 if (!stream_->Skip(env, bytes_to_skip, &skipped)) |
| 92 return net::ERR_FAILED; |
| 93 if (skipped <= 0) |
| 94 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE; |
| 95 |
| 96 } while ((bytes_to_skip -= skipped) > 0); |
| 97 } |
| 98 return net::OK; |
| 99 } |
| 100 |
| 101 } // namespace android_webview |
OLD | NEW |