1 // Copyright 2015 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.
5 #include "components/cronet/android/cronet_upload_data_stream_delegate.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/single_thread_task_runner.h"
15 #include "components/cronet/android/cronet_url_request_adapter.h"
16 #include "jni/CronetUploadDataStream_jni.h"
18 using base::android::ConvertUTF8ToJavaString
;
22 CronetUploadDataStreamDelegate::CronetUploadDataStreamDelegate(
24 jobject jupload_data_stream
) {
25 jupload_data_stream_
.Reset(env
, jupload_data_stream
);
28 CronetUploadDataStreamDelegate::~CronetUploadDataStreamDelegate() {
31 void CronetUploadDataStreamDelegate::InitializeOnNetworkThread(
32 base::WeakPtr
<CronetUploadDataStreamAdapter
> adapter
) {
34 DCHECK(!network_task_runner_
.get());
37 network_task_runner_
= base::MessageLoopProxy::current();
38 DCHECK(network_task_runner_
);
41 void CronetUploadDataStreamDelegate::Read(net::IOBuffer
* buffer
, int buf_len
) {
43 DCHECK(network_task_runner_
);
44 DCHECK(network_task_runner_
->BelongsToCurrentThread());
45 DCHECK_GT(buf_len
, 0);
46 DCHECK(!buffer_
.get());
49 // TODO(mmenke): Consider preserving the java buffer across reads, when the
50 // IOBuffer's data pointer and its length are unchanged.
51 JNIEnv
* env
= base::android::AttachCurrentThread();
52 base::android::ScopedJavaLocalRef
<jobject
> java_buffer(
53 env
, env
->NewDirectByteBuffer(buffer
->data(), buf_len
));
54 Java_CronetUploadDataStream_readData(env
, jupload_data_stream_
.obj(),
58 void CronetUploadDataStreamDelegate::Rewind() {
60 DCHECK(network_task_runner_
->BelongsToCurrentThread());
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 Java_CronetUploadDataStream_rewind(env
, jupload_data_stream_
.obj());
66 void CronetUploadDataStreamDelegate::OnAdapterDestroyed() {
68 DCHECK(network_task_runner_
->BelongsToCurrentThread());
70 JNIEnv
* env
= base::android::AttachCurrentThread();
71 Java_CronetUploadDataStream_onAdapterDestroyed(env
,
72 jupload_data_stream_
.obj());
75 void CronetUploadDataStreamDelegate::OnReadSucceeded(JNIEnv
* env
,
79 DCHECK(!network_task_runner_
->BelongsToCurrentThread());
80 DCHECK(bytes_read
> 0 || (final_chunk
&& bytes_read
== 0));
83 network_task_runner_
->PostTask(
84 FROM_HERE
, base::Bind(&CronetUploadDataStreamAdapter::OnReadSuccess
,
85 adapter_
, bytes_read
, final_chunk
));
88 void CronetUploadDataStreamDelegate::OnRewindSucceeded(JNIEnv
* env
,
90 DCHECK(!network_task_runner_
->BelongsToCurrentThread());
92 network_task_runner_
->PostTask(
94 base::Bind(&CronetUploadDataStreamAdapter::OnRewindSuccess
, adapter_
));
97 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv
* env
) {
98 return RegisterNativesImpl(env
);
101 static jlong
AttachUploadDataToRequest(JNIEnv
* env
,
102 jobject jupload_data_stream
,
103 jlong jcronet_url_request_adapter
,
105 CronetURLRequestAdapter
* request_adapter
=
106 reinterpret_cast<CronetURLRequestAdapter
*>(jcronet_url_request_adapter
);
107 DCHECK(request_adapter
!= nullptr);
109 CronetUploadDataStreamDelegate
* delegate
=
110 new CronetUploadDataStreamDelegate(env
, jupload_data_stream
);
112 scoped_ptr
<CronetUploadDataStreamAdapter
> upload_adapter(
113 new CronetUploadDataStreamAdapter(delegate
, jlength
));
115 request_adapter
->SetUpload(upload_adapter
.Pass());
117 return reinterpret_cast<jlong
>(delegate
);
120 static jlong
CreateDelegateForTesting(JNIEnv
* env
,
121 jobject jupload_data_stream
) {
122 CronetUploadDataStreamDelegate
* delegate
=
123 new CronetUploadDataStreamDelegate(env
, jupload_data_stream
);
124 return reinterpret_cast<jlong
>(delegate
);
127 static jlong
CreateAdapterForTesting(JNIEnv
* env
,
128 jobject jupload_data_stream
,
131 CronetUploadDataStreamDelegate
* delegate
=
132 reinterpret_cast<CronetUploadDataStreamDelegate
*>(jdelegate
);
133 CronetUploadDataStreamAdapter
* upload_adapter
=
134 new CronetUploadDataStreamAdapter(delegate
, jlength
);
135 return reinterpret_cast<jlong
>(upload_adapter
);
138 static void DestroyDelegate(JNIEnv
* env
,
139 jclass jcronet_url_request_adapter
,
140 jlong jupload_data_stream_delegate
) {
141 CronetUploadDataStreamDelegate
* delegate
=
142 reinterpret_cast<CronetUploadDataStreamDelegate
*>(
143 jupload_data_stream_delegate
);
144 DCHECK(delegate
!= nullptr);
148 } // namespace cronet