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

Side by Side Diff: android_webview/native/input_stream_impl.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: remove jobj from browser/input_stream.h 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_impl.h"
6
7 #include "base/android/jni_android.h"
8 #include "net/base/io_buffer.h"
9 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java
10 // system class and we have to generate C++ hooks for all methods in the class
11 // even if they're unused.
12 #pragma GCC diagnostic ignored "-Wunused-function"
13 #include "jni/InputStream_jni.h"
14
15 using base::android::ClearException;
16 using base::android::JavaRef;
17 using JNI_InputStream::Java_InputStream_available;
18 using JNI_InputStream::Java_InputStream_skip;
19 using JNI_InputStream::Java_InputStream_readI_AB_I_I;
20
21 namespace android_webview {
22
23 bool RegisterInputStream(JNIEnv* env) {
24 return JNI_InputStream::RegisterNativesImpl(env);
25 }
26
27 // Maximum number of bytes to be read in a single read.
28 const int InputStreamImpl::kBufferSize = 4096;
29
30 //static
31 const InputStreamImpl* InputStreamImpl::FromInputStream(
32 const InputStream* input_stream) {
33 return reinterpret_cast<const InputStreamImpl*>(input_stream);
mkosiba (inactive) 2012/11/15 19:18:51 so according to the standard this is undefined beh
joth 2012/11/15 20:04:26 use a static_cast, and it's totally legit (unless
mkosiba (inactive) 2012/11/16 14:47:01 duh.. I tried using a static_cast before and gcc c
34 }
35
36 InputStreamImpl::InputStreamImpl() {
37 }
38
39 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream)
40 : jobject_(stream) {
41 DCHECK(!stream.is_null());
42 }
43
44 InputStreamImpl::~InputStreamImpl() {
45 }
46
47 bool InputStreamImpl::BytesAvailable(JNIEnv* env,
48 int* bytes_available) const {
49 // TODO: Use unsafe version once BUG 157880 is fixed.
50 int bytes = Java_InputStream_available(env, jobject_.obj());
51 if (ClearException(env))
52 return false;
53 *bytes_available = bytes;
54 return true;
55 }
56
57 bool InputStreamImpl::Skip(JNIEnv* env,
58 int64_t n,
59 int64_t* bytes_skipped) {
60 // TODO: Use unsafe version once BUG 157880 is fixed.
61 int bytes = Java_InputStream_skip(env, jobject_.obj(), n);
62 if (ClearException(env))
63 return false;
64 *bytes_skipped = bytes;
65 return true;
66 }
67
68 bool InputStreamImpl::Read(JNIEnv* env,
69 net::IOBuffer* dest,
70 int length,
71 int* bytes_read) {
72 if (!buffer_.obj()) {
73 // Allocate transfer buffer.
74 buffer_.Reset(env, env->NewByteArray(kBufferSize));
75 if (ClearException(env))
76 return false;
77 }
78
79 jbyteArray buffer = buffer_.obj();
80 int read_size = std::min(length, kBufferSize);
81 *bytes_read = 0;
82
83 while (length > 0) {
84 int32_t byte_count =
85 Java_InputStream_readI_AB_I_I(
86 env, jobject_.obj(), buffer, 0, read_size);
87
88 if (ClearException(env))
89 return false;
90
91 if (byte_count <= 0)
92 break;
93
94 #ifndef NDEBUG
95 int32_t buffer_length = env->GetArrayLength(buffer);
96 DCHECK_GE(read_size, byte_count);
97 DCHECK_GE(buffer_length, byte_count);
98 #endif // NDEBUG
99
100 // Copy the data over to the provided C++ side buffer.
101 DCHECK_GE(length, byte_count);
102 env->GetByteArrayRegion(buffer, 0, byte_count,
103 reinterpret_cast<jbyte*>(dest->data() + *bytes_read));
104 if (ClearException(env))
105 return false;
106
107 *bytes_read += byte_count;
108 length -= byte_count;
109 }
110 return true;
111 }
112
113 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/native/input_stream_impl.h ('k') | android_webview/native/input_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698