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