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> | |
8 | |
9 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
10 #include "base/logging.h" | 8 #include "base/logging.h" |
11 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
13 | 11 |
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 { | 12 namespace cronet { |
22 | 13 |
23 bool WrappedChannelRegisterJni(JNIEnv* env) { | 14 WrappedChannelElementReader::WrappedChannelElementReader( |
24 g_class_readablebytechannel = static_cast<jclass>(env->NewGlobalRef( | 15 scoped_refptr<URLRequestPeer::URLRequestPeerDelegate> delegate, |
25 env->FindClass("java/nio/channels/ReadableByteChannel"))); | 16 uint64 length) |
26 // TODO(mef): Per previous discussions the best way to do this is to have a | 17 : length_(length), offset_(0), delegate_(delegate) { |
27 // cronet-specific java wrapper that exposes necessary methods annotated as | |
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 } | 18 } |
50 | 19 |
51 WrappedChannelElementReader::~WrappedChannelElementReader() { | 20 WrappedChannelElementReader::~WrappedChannelElementReader() { |
52 JNIEnv* env = base::android::AttachCurrentThread(); | |
53 env->DeleteGlobalRef(channel_); | |
54 } | 21 } |
55 | 22 |
56 int WrappedChannelElementReader::Init(const net::CompletionCallback& callback) { | 23 int WrappedChannelElementReader::Init(const net::CompletionCallback& callback) { |
57 offset_ = 0; | 24 offset_ = 0; |
58 return net::OK; | 25 return net::OK; |
59 } | 26 } |
60 | 27 |
61 uint64 WrappedChannelElementReader::GetContentLength() const { | 28 uint64 WrappedChannelElementReader::GetContentLength() const { |
62 return length_; | 29 return length_; |
63 } | 30 } |
64 | 31 |
65 uint64 WrappedChannelElementReader::BytesRemaining() const { | 32 uint64 WrappedChannelElementReader::BytesRemaining() const { |
66 return length_ - offset_; | 33 return length_ - offset_; |
67 } | 34 } |
68 | 35 |
69 bool WrappedChannelElementReader::IsInMemory() const { | 36 bool WrappedChannelElementReader::IsInMemory() const { |
70 return false; | 37 return false; |
71 } | 38 } |
72 | 39 |
73 int WrappedChannelElementReader::Read(net::IOBuffer* buf, | 40 int WrappedChannelElementReader::Read(net::IOBuffer* buf, |
74 int buf_length, | 41 int buf_length, |
75 const net::CompletionCallback& callback) { | 42 const net::CompletionCallback& callback) { |
76 DCHECK(!callback.is_null()); | 43 DCHECK(!callback.is_null()); |
77 JNIEnv* env = base::android::AttachCurrentThread(); | 44 DCHECK(delegate_); |
78 jobject jbuf = env->NewDirectByteBuffer(reinterpret_cast<void*>(buf->data()), | 45 // TODO(mef): Post the read to file thread. |
79 buf_length); | 46 int bytes_read = delegate_->ReadFromUploadChannel(buf, buf_length); |
80 jint bytes_read = env->CallIntMethod(channel_, g_method_read, jbuf); | 47 if (bytes_read < 0) |
81 base::android::CheckException(env); | 48 return net::ERR_FAILED; |
82 | |
83 env->DeleteLocalRef(jbuf); | |
84 if (bytes_read <= 0) { | |
85 env->CallVoidMethod(channel_, g_method_close); | |
86 base::android::CheckException(env); | |
87 return bytes_read; | |
88 } | |
89 offset_ += bytes_read; | 49 offset_ += bytes_read; |
90 return bytes_read; | 50 return bytes_read; |
91 } | 51 } |
92 | 52 |
93 } // namespace cronet | 53 } // namespace cronet |
94 | 54 |
OLD | NEW |