OLD | NEW |
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // Allocate transfer buffer. | 78 // Allocate transfer buffer. |
79 base::android::ScopedJavaLocalRef<jbyteArray> temp( | 79 base::android::ScopedJavaLocalRef<jbyteArray> temp( |
80 env, env->NewByteArray(kBufferSize)); | 80 env, env->NewByteArray(kBufferSize)); |
81 buffer_.Reset(temp); | 81 buffer_.Reset(temp); |
82 if (ClearException(env)) | 82 if (ClearException(env)) |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 int remaining_length = length; | 86 int remaining_length = length; |
87 char* dest_write_ptr = dest->data(); | 87 char* dest_write_ptr = dest->data(); |
88 jbyteArray buffer = buffer_.obj(); | |
89 *bytes_read = 0; | 88 *bytes_read = 0; |
90 | 89 |
91 while (remaining_length > 0) { | 90 while (remaining_length > 0) { |
92 const int max_transfer_length = std::min(remaining_length, kBufferSize); | 91 const int max_transfer_length = std::min(remaining_length, kBufferSize); |
93 const int transfer_length = Java_InputStreamUtil_read( | 92 const int transfer_length = Java_InputStreamUtil_read( |
94 env, jobject_, buffer, 0, max_transfer_length); | 93 env, jobject_, buffer_, 0, max_transfer_length); |
95 if (transfer_length == kExceptionThrownStatusCode) | 94 if (transfer_length == kExceptionThrownStatusCode) |
96 return false; | 95 return false; |
97 | 96 |
98 if (transfer_length < 0) // EOF | 97 if (transfer_length < 0) // EOF |
99 break; | 98 break; |
100 | 99 |
101 // Note: it is possible, yet unlikely, that the Java InputStream returns | 100 // Note: it is possible, yet unlikely, that the Java InputStream returns |
102 // a transfer_length == 0 from time to time. In such cases we just continue | 101 // a transfer_length == 0 from time to time. In such cases we just continue |
103 // the read until we get either valid data or reach EOF. | 102 // the read until we get either valid data or reach EOF. |
104 if (transfer_length == 0) | 103 if (transfer_length == 0) |
105 continue; | 104 continue; |
106 | 105 |
107 DCHECK_GE(max_transfer_length, transfer_length); | 106 DCHECK_GE(max_transfer_length, transfer_length); |
108 DCHECK_GE(env->GetArrayLength(buffer), transfer_length); | 107 DCHECK_GE(env->GetArrayLength(buffer_.obj()), transfer_length); |
109 | 108 |
110 // This check is to prevent a malicious InputStream implementation from | 109 // This check is to prevent a malicious InputStream implementation from |
111 // overrunning the |dest| buffer. | 110 // overrunning the |dest| buffer. |
112 if (transfer_length > max_transfer_length) | 111 if (transfer_length > max_transfer_length) |
113 return false; | 112 return false; |
114 | 113 |
115 // Copy the data over to the provided C++ IOBuffer. | 114 // Copy the data over to the provided C++ IOBuffer. |
116 DCHECK_GE(remaining_length, transfer_length); | 115 DCHECK_GE(remaining_length, transfer_length); |
117 env->GetByteArrayRegion(buffer, 0, transfer_length, | 116 env->GetByteArrayRegion(buffer_.obj(), 0, transfer_length, |
118 reinterpret_cast<jbyte*>(dest_write_ptr)); | 117 reinterpret_cast<jbyte*>(dest_write_ptr)); |
119 if (ClearException(env)) | 118 if (ClearException(env)) |
120 return false; | 119 return false; |
121 | 120 |
122 remaining_length -= transfer_length; | 121 remaining_length -= transfer_length; |
123 dest_write_ptr += transfer_length; | 122 dest_write_ptr += transfer_length; |
124 } | 123 } |
125 // bytes_read can be strictly less than the req. length if EOF is encountered. | 124 // bytes_read can be strictly less than the req. length if EOF is encountered. |
126 DCHECK_GE(remaining_length, 0); | 125 DCHECK_GE(remaining_length, 0); |
127 DCHECK_LE(remaining_length, length); | 126 DCHECK_LE(remaining_length, length); |
128 *bytes_read = length - remaining_length; | 127 *bytes_read = length - remaining_length; |
129 return true; | 128 return true; |
130 } | 129 } |
131 | 130 |
132 } // namespace android_webview | 131 } // namespace android_webview |
OLD | NEW |