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() {
67 // If the CronetUploadDataStreamAdapter was never initialized, |adapter_|
68 // and |network_task_runner_| will be NULL.
69 DCHECK(!network_task_runner_
||
70 network_task_runner_
->BelongsToCurrentThread());
72 JNIEnv
* env
= base::android::AttachCurrentThread();
73 Java_CronetUploadDataStream_onAdapterDestroyed(env
,
74 jupload_data_stream_
.obj());
77 void CronetUploadDataStreamDelegate::OnReadSucceeded(JNIEnv
* env
,
81 DCHECK(!network_task_runner_
->BelongsToCurrentThread());
82 DCHECK(bytes_read
> 0 || (final_chunk
&& bytes_read
== 0));
85 network_task_runner_
->PostTask(
86 FROM_HERE
, base::Bind(&CronetUploadDataStreamAdapter::OnReadSuccess
,
87 adapter_
, bytes_read
, final_chunk
));
90 void CronetUploadDataStreamDelegate::OnRewindSucceeded(JNIEnv
* env
,
92 DCHECK(!network_task_runner_
->BelongsToCurrentThread());
94 network_task_runner_
->PostTask(
96 base::Bind(&CronetUploadDataStreamAdapter::OnRewindSuccess
, adapter_
));
99 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv
* env
) {
100 return RegisterNativesImpl(env
);
103 static jlong
AttachUploadDataToRequest(JNIEnv
* env
,
104 jobject jupload_data_stream
,
105 jlong jcronet_url_request_adapter
,
107 CronetURLRequestAdapter
* request_adapter
=
108 reinterpret_cast<CronetURLRequestAdapter
*>(jcronet_url_request_adapter
);
109 DCHECK(request_adapter
!= nullptr);
111 CronetUploadDataStreamDelegate
* delegate
=
112 new CronetUploadDataStreamDelegate(env
, jupload_data_stream
);
114 scoped_ptr
<CronetUploadDataStreamAdapter
> upload_adapter(
115 new CronetUploadDataStreamAdapter(delegate
, jlength
));
117 request_adapter
->SetUpload(upload_adapter
.Pass());
119 return reinterpret_cast<jlong
>(delegate
);
122 static jlong
CreateDelegateForTesting(JNIEnv
* env
,
123 jobject jupload_data_stream
) {
124 CronetUploadDataStreamDelegate
* delegate
=
125 new CronetUploadDataStreamDelegate(env
, jupload_data_stream
);
126 return reinterpret_cast<jlong
>(delegate
);
129 static jlong
CreateAdapterForTesting(JNIEnv
* env
,
130 jobject jupload_data_stream
,
133 CronetUploadDataStreamDelegate
* delegate
=
134 reinterpret_cast<CronetUploadDataStreamDelegate
*>(jdelegate
);
135 CronetUploadDataStreamAdapter
* upload_adapter
=
136 new CronetUploadDataStreamAdapter(delegate
, jlength
);
137 return reinterpret_cast<jlong
>(upload_adapter
);
140 static void DestroyDelegate(JNIEnv
* env
,
141 jclass jcronet_url_request_adapter
,
142 jlong jupload_data_stream_delegate
) {
143 CronetUploadDataStreamDelegate
* delegate
=
144 reinterpret_cast<CronetUploadDataStreamDelegate
*>(
145 jupload_data_stream_delegate
);
146 DCHECK(delegate
!= nullptr);
150 } // namespace cronet