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

Side by Side Diff: android_webview/native/input_stream_impl.cc

Issue 24082006: [Android WebView] Make InputStreamImpl::Read fill the IOBuffer. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed tests Created 7 years, 3 months 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
« no previous file with comments | « no previous file | android_webview/native/input_stream_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "android_webview/native/input_stream_impl.h" 5 #include "android_webview/native/input_stream_impl.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java 8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java
9 // system class and we have to generate C++ hooks for all methods in the class 9 // system class and we have to generate C++ hooks for all methods in the class
10 // even if they're unused. 10 // even if they're unused.
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 JNIEnv* env = AttachCurrentThread(); 77 JNIEnv* env = AttachCurrentThread();
78 if (!buffer_.obj()) { 78 if (!buffer_.obj()) {
79 // Allocate transfer buffer. 79 // Allocate transfer buffer.
80 base::android::ScopedJavaLocalRef<jbyteArray> temp( 80 base::android::ScopedJavaLocalRef<jbyteArray> temp(
81 env, env->NewByteArray(kBufferSize)); 81 env, env->NewByteArray(kBufferSize));
82 buffer_.Reset(temp); 82 buffer_.Reset(temp);
83 if (ClearException(env)) 83 if (ClearException(env))
84 return false; 84 return false;
85 } 85 }
86 86
87 int remaining_length = length;
88 char* dest_write_ptr = dest->data();
87 jbyteArray buffer = buffer_.obj(); 89 jbyteArray buffer = buffer_.obj();
88 *bytes_read = 0; 90 *bytes_read = 0;
89 91
90 const int read_size = std::min(length, kBufferSize); 92 while (remaining_length > 0) {
91 int32_t byte_count; 93 const int max_transfer_length = std::min(remaining_length, kBufferSize);
92 do { 94 const int transfer_length = Java_InputStream_readI_AB_I_I(
93 // Unfortunately it is valid for the Java InputStream to read 0 bytes some 95 env, jobject_.obj(), buffer, 0, max_transfer_length);
94 // number of times before returning any more data. Because this method
95 // signals EOF by setting |bytes_read| to 0 and returning true necessary to
96 // call the Java-side read method until it returns something other than 0.
97 byte_count = Java_InputStream_readI_AB_I_I(
98 env, jobject_.obj(), buffer, 0, read_size);
99 if (ClearException(env)) 96 if (ClearException(env))
100 return false; 97 return false;
101 } while (byte_count == 0);
102 98
103 // We've reached the end of the stream. 99 if (transfer_length < 0) // EOF
104 if (byte_count < 0) 100 break;
105 return true;
106 101
107 #ifndef NDEBUG 102 // Note: it is possible, yet unlikely, that the Java InputStream returns
108 int32_t buffer_length = env->GetArrayLength(buffer); 103 // a transfer_length == 0 from time to time. In such cases we just continue
109 DCHECK_GE(read_size, byte_count); 104 // the read until we get either valid data or reach EOF.
110 DCHECK_GE(buffer_length, byte_count); 105 if (transfer_length == 0)
111 #endif // NDEBUG 106 continue;
112 107
113 // The DCHECKs are in place to help Chromium developers in case of bugs, 108 DCHECK_GE(max_transfer_length, transfer_length);
114 // this check is to prevent a malicious InputStream implementation from 109 DCHECK_GE(env->GetArrayLength(buffer), transfer_length);
115 // overrunning the |dest| buffer.
116 if (byte_count > read_size)
117 return false;
118 110
119 // Copy the data over to the provided C++ side buffer. 111 // This check is to prevent a malicious InputStream implementation from
120 DCHECK_GE(length, byte_count); 112 // overrunning the |dest| buffer.
121 env->GetByteArrayRegion(buffer, 0, byte_count, 113 if (transfer_length > max_transfer_length)
122 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); 114 return false;
123 if (ClearException(env))
124 return false;
125 115
126 *bytes_read = byte_count; 116 // Copy the data over to the provided C++ IOBuffer.
117 DCHECK_GE(remaining_length, transfer_length);
118 env->GetByteArrayRegion(buffer, 0, transfer_length,
119 reinterpret_cast<jbyte*>(dest_write_ptr));
120 if (ClearException(env))
121 return false;
122
123 remaining_length -= transfer_length;
124 dest_write_ptr += transfer_length;
125 }
126 // bytes_read can be strictly less than the req. length if EOF is encountered.
127 DCHECK(remaining_length >= 0 && remaining_length <= length);
128 *bytes_read = length - remaining_length;
127 return true; 129 return true;
128 } 130 }
129 131
130 } // namespace android_webview 132 } // namespace android_webview
OLDNEW
« no previous file with comments | « no previous file | android_webview/native/input_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698