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

Side by Side Diff: android_webview/native/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 clang error 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/native/input_stream_reader.h"
6
7 #include "android_webview/native/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
joth 2012/11/07 16:55:38 ISTM this class is now fairly well abstracted from
mkosiba (inactive) 2012/11/15 19:18:50 Done.
13 using base::android::AttachCurrentThread;
14 using content::BrowserThread;
15
16 namespace android_webview {
17
18 InputStreamReader::InputStreamReader(
19 android_webview::InputStream* stream)
20 : stream_(stream) {
21 DCHECK(stream);
22 }
23
24 InputStreamReader::~InputStreamReader() {
25 }
26
27 void InputStreamReader::Seek(net::HttpByteRange byte_range,
28 net::CompletionCallback on_seek_completion) {
29 JNIEnv* env = AttachCurrentThread();
30 int content_size = 0;
31 DCHECK(env);
32
33 int error_code = VerifyRequestedRange(env, &byte_range, &content_size);
34 if (error_code != net::OK) {
35 PostCompletionCallback(on_seek_completion, error_code);
36 return;
37 }
38
39 error_code = SkipToRequestedRange(env, byte_range);
40 if (error_code != net::OK) {
41 PostCompletionCallback(on_seek_completion, error_code);
42 return;
43 }
44
45 DCHECK_GE(content_size, 0);
46 PostCompletionCallback(on_seek_completion, content_size);
47 }
48
49 void InputStreamReader::ReadRawData(
50 net::IOBuffer* dest,
51 int dest_size,
52 net::CompletionCallback on_read_completion) {
53 if (!dest_size) {
54 PostCompletionCallback(on_read_completion, 0);
55 return;
56 }
57
58 JNIEnv* env = AttachCurrentThread();
59 DCHECK(env);
60 DCHECK_GT(dest_size, 0);
61
62 int bytes_read = 0;
63 if (!stream_->Read(env, dest, dest_size, &bytes_read))
64 PostCompletionCallback(on_read_completion, net::ERR_FAILED);
65 else
66 PostCompletionCallback(on_read_completion, bytes_read);
67 }
68
69 int InputStreamReader::VerifyRequestedRange(
70 JNIEnv* env,
71 net::HttpByteRange* byte_range,
72 int* content_size) {
73 DCHECK(content_size);
74 int32_t size = 0;
75 if (!stream_->BytesAvailable(env, &size)) {
76 return net::ERR_FAILED;
77 }
78
79 if (size <= 0)
80 return net::OK;
81
82 // Check that the requested range was valid.
83 if (!byte_range->ComputeBounds(size)) {
84 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
85 }
86
87 size = byte_range->last_byte_position() -
88 byte_range->first_byte_position() + 1;
89 DCHECK_GE(size, 0);
90 *content_size = size;
91
92 return net::OK;
93 }
94
95 int InputStreamReader::SkipToRequestedRange(
96 JNIEnv* env,
97 const net::HttpByteRange& byte_range) {
98 // Skip to the start of the requested data. This has to be done in a loop
99 // because the underlying InputStream is not guaranteed to skip the requested
100 // number of bytes.
101 if (byte_range.IsValid() && byte_range.first_byte_position() != 0) {
102 int64_t skipped, bytes_to_skip = byte_range.first_byte_position();
103 do {
104 if (!stream_->Skip(env, bytes_to_skip, &skipped)) {
105 return net::ERR_FAILED;
106 }
107 if (skipped <= 0) {
108 return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
109 }
110 } while ((bytes_to_skip -= skipped) > 0);
111 }
112 return net::OK;
113 }
114
115 void InputStreamReader::PostCompletionCallback(
116 const net::CompletionCallback& callback,
117 int result) {
118 BrowserThread::PostTask(BrowserThread::IO,
joth 2012/11/07 16:55:38 this class is a little bit shady, that you can cal
benm (inactive) 2012/11/15 17:39:01 Agree, it'd be nice if you're always called back o
mkosiba (inactive) 2012/11/15 19:18:50 Done.
119 FROM_HERE,
120 base::Bind(callback, result));
121 }
122
123 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698