Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/cronet/android/wrapped_channel_upload_element_reader.h" | 5 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 jclass g_class_readablebytechannel; | |
| 16 jclass g_class_channel; | |
| 17 jmethodID g_method_read; | |
| 18 jmethodID g_method_close; | |
| 19 } //namespace | |
| 20 | |
| 21 namespace cronet { | 14 namespace cronet { |
| 22 | 15 |
| 23 bool WrappedChannelRegisterJni(JNIEnv* env) { | 16 WrappedChannelElementReader::WrappedChannelElementReader( |
| 24 g_class_readablebytechannel = static_cast<jclass>(env->NewGlobalRef( | 17 JNIEnv* env, |
|
mmenke
2014/08/05 19:32:41
I don't think we need env, or the dependency on jn
mef
2014/08/05 20:44:28
Done.
| |
| 25 env->FindClass("java/nio/channels/ReadableByteChannel"))); | 18 scoped_refptr<URLRequestPeer::URLRequestPeerDelegate> delegate, |
| 26 // TODO(mef): Per previous discussions the best way to do this is to have a | 19 uint64 length) |
| 27 // cronet-specific java wrapper that exposes necessary methods annotated as | 20 : length_(length), offset_(0), delegate_(delegate) { |
| 28 // @AccessedByNative and use jni generator to generate a wrapper method for | |
| 29 // that. | |
| 30 g_method_read = env->GetMethodID( | |
| 31 g_class_readablebytechannel, "read", "(Ljava/nio/ByteBuffer;)I"); | |
| 32 | |
| 33 // Due to a bug in the version of ART that shipped with 4.4, we're looking up | |
| 34 // the close() method on the base interface. See b/15651032 for details. | |
| 35 g_class_channel = static_cast<jclass>(env->NewGlobalRef( | |
| 36 env->FindClass("java/nio/channels/Channel"))); | |
| 37 g_method_close = env->GetMethodID(g_class_channel, "close", "()V"); | |
| 38 if (!g_class_readablebytechannel || !g_method_read || !g_method_close) { | |
| 39 return false; | |
| 40 } | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 WrappedChannelElementReader::WrappedChannelElementReader(JNIEnv* env, | |
| 45 jobject channel, | |
| 46 uint64 length) | |
| 47 : length_(length), offset_(0) { | |
| 48 channel_ = env->NewGlobalRef(channel); | |
| 49 } | 21 } |
| 50 | 22 |
| 51 WrappedChannelElementReader::~WrappedChannelElementReader() { | 23 WrappedChannelElementReader::~WrappedChannelElementReader() { |
| 52 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 53 env->DeleteGlobalRef(channel_); | |
| 54 } | 24 } |
| 55 | 25 |
| 56 int WrappedChannelElementReader::Init(const net::CompletionCallback& callback) { | 26 int WrappedChannelElementReader::Init(const net::CompletionCallback& callback) { |
| 57 offset_ = 0; | 27 offset_ = 0; |
| 58 return net::OK; | 28 return net::OK; |
| 59 } | 29 } |
| 60 | 30 |
| 61 uint64 WrappedChannelElementReader::GetContentLength() const { | 31 uint64 WrappedChannelElementReader::GetContentLength() const { |
| 62 return length_; | 32 return length_; |
| 63 } | 33 } |
| 64 | 34 |
| 65 uint64 WrappedChannelElementReader::BytesRemaining() const { | 35 uint64 WrappedChannelElementReader::BytesRemaining() const { |
| 66 return length_ - offset_; | 36 return length_ - offset_; |
| 67 } | 37 } |
| 68 | 38 |
| 69 bool WrappedChannelElementReader::IsInMemory() const { | 39 bool WrappedChannelElementReader::IsInMemory() const { |
| 70 return false; | 40 return false; |
| 71 } | 41 } |
| 72 | 42 |
| 73 int WrappedChannelElementReader::Read(net::IOBuffer* buf, | 43 int WrappedChannelElementReader::Read(net::IOBuffer* buf, |
| 74 int buf_length, | 44 int buf_length, |
| 75 const net::CompletionCallback& callback) { | 45 const net::CompletionCallback& callback) { |
| 76 DCHECK(!callback.is_null()); | 46 DCHECK(!callback.is_null()); |
| 77 JNIEnv* env = base::android::AttachCurrentThread(); | 47 DCHECK(delegate_); |
| 78 jobject jbuf = env->NewDirectByteBuffer(reinterpret_cast<void*>(buf->data()), | 48 // TODO(mef): Post the read to file thread. |
| 79 buf_length); | 49 int bytes_read = delegate_->OnReadUploadData(buf, buf_length); |
|
mef
2014/08/05 17:18:39
Should I post it to file thread in this CL?
Also,
| |
| 80 jint bytes_read = env->CallIntMethod(channel_, g_method_read, jbuf); | 50 if (bytes_read > 0) |
| 81 base::android::CheckException(env); | 51 offset_ += bytes_read; |
| 82 | |
| 83 env->DeleteLocalRef(jbuf); | |
| 84 if (bytes_read <= 0) { | |
|
mef
2014/08/05 17:18:39
I imagine that |bytes_read| could temporarily be 0
| |
| 85 env->CallVoidMethod(channel_, g_method_close); | |
|
mef
2014/08/05 17:18:39
Charles, why do we need to close the channel here?
Charles
2014/08/05 18:33:32
Closing the channel is required because you need t
| |
| 86 base::android::CheckException(env); | |
| 87 return bytes_read; | |
| 88 } | |
| 89 offset_ += bytes_read; | |
| 90 return bytes_read; | 52 return bytes_read; |
|
mmenke
2014/08/05 19:32:41
BUG: The return value is bytes read or a network
mef
2014/08/05 20:44:28
Done.
| |
| 91 } | 53 } |
| 92 | 54 |
| 93 } // namespace cronet | 55 } // namespace cronet |
| 94 | 56 |
| OLD | NEW |