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.
5 #include "android_webview/browser/net/input_stream_reader.h"
7 #include "android_webview/browser/input_stream.h"
8 #include "base/message_loop/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"
13 using content::BrowserThread
;
15 namespace android_webview
{
17 InputStreamReader::InputStreamReader(android_webview::InputStream
* stream
)
22 InputStreamReader::~InputStreamReader() {
25 int InputStreamReader::Seek(const net::HttpByteRange
& byte_range
) {
27 net::HttpByteRange
verified_byte_range(byte_range
);
29 int error_code
= VerifyRequestedRange(&verified_byte_range
, &content_size
);
30 if (error_code
!= net::OK
)
33 error_code
= SkipToRequestedRange(verified_byte_range
);
34 if (error_code
!= net::OK
)
37 DCHECK_GE(content_size
, 0);
41 int InputStreamReader::ReadRawData(net::IOBuffer
* dest
, int dest_size
) {
45 DCHECK_GT(dest_size
, 0);
48 if (!stream_
->Read(dest
, dest_size
, &bytes_read
))
49 return net::ERR_FAILED
;
54 int InputStreamReader::VerifyRequestedRange(net::HttpByteRange
* byte_range
,
58 if (!stream_
->BytesAvailable(&size
))
59 return net::ERR_FAILED
;
64 // Check that the requested range was valid.
65 if (!byte_range
->ComputeBounds(size
))
66 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
;
68 size
= byte_range
->last_byte_position() -
69 byte_range
->first_byte_position() + 1;
76 int InputStreamReader::SkipToRequestedRange(
77 const net::HttpByteRange
& byte_range
) {
78 // Skip to the start of the requested data. This has to be done in a loop
79 // because the underlying InputStream is not guaranteed to skip the requested
81 if (byte_range
.IsValid() && byte_range
.first_byte_position() > 0) {
82 int64_t bytes_to_skip
= byte_range
.first_byte_position();
85 if (!stream_
->Skip(bytes_to_skip
, &skipped
))
86 return net::ERR_FAILED
;
89 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE
;
90 DCHECK_LE(skipped
, bytes_to_skip
);
92 bytes_to_skip
-= skipped
;
93 } while (bytes_to_skip
> 0);
98 } // namespace android_webview