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 "test_upload_data_stream_handler.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/bind.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "jni/TestUploadDataStreamHandler_jni.h"
14 #include "net/base/net_errors.h"
18 static const size_t kReadBufferSize
= 32768;
20 TestUploadDataStreamHandler::TestUploadDataStreamHandler(
21 scoped_ptr
<net::UploadDataStream
> upload_data_stream
,
23 jobject jtest_upload_data_stream_handler
)
24 : init_callback_invoked_(false),
25 read_callback_invoked_(false),
27 network_thread_(new base::Thread("network")) {
28 upload_data_stream_
= upload_data_stream
.Pass();
29 base::Thread::Options options
;
30 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
31 network_thread_
->StartWithOptions(options
);
32 jtest_upload_data_stream_handler_
.Reset(env
,
33 jtest_upload_data_stream_handler
);
36 TestUploadDataStreamHandler::~TestUploadDataStreamHandler() {
39 void TestUploadDataStreamHandler::Destroy(JNIEnv
* env
, jobject jcaller
) {
40 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
41 // Stick network_thread_ in a local, so |this| may be destroyed from the
42 // network thread before the network thread is destroyed.
43 scoped_ptr
<base::Thread
> network_thread
= network_thread_
.Pass();
44 network_thread
->task_runner()->DeleteSoon(FROM_HERE
, this);
45 // Deleting thread stops it after all tasks are completed.
46 network_thread
.reset();
49 void TestUploadDataStreamHandler::OnInitCompleted(int res
) {
50 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
51 init_callback_invoked_
= true;
52 JNIEnv
* env
= base::android::AttachCurrentThread();
53 cronet::Java_TestUploadDataStreamHandler_onInitCompleted(
54 env
, jtest_upload_data_stream_handler_
.obj(), res
);
57 void TestUploadDataStreamHandler::OnReadCompleted(int res
) {
58 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
59 read_callback_invoked_
= true;
61 NotifyJavaReadCompleted();
64 void TestUploadDataStreamHandler::Init(JNIEnv
* env
, jobject jcaller
) {
65 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
66 network_thread_
->task_runner()->PostTask(
67 FROM_HERE
, base::Bind(&TestUploadDataStreamHandler::InitOnNetworkThread
,
68 base::Unretained(this)));
71 void TestUploadDataStreamHandler::Read(JNIEnv
* env
, jobject jcaller
) {
72 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
73 network_thread_
->task_runner()->PostTask(
74 FROM_HERE
, base::Bind(&TestUploadDataStreamHandler::ReadOnNetworkThread
,
75 base::Unretained(this)));
78 void TestUploadDataStreamHandler::Reset(JNIEnv
* env
, jobject jcaller
) {
79 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
80 network_thread_
->task_runner()->PostTask(
81 FROM_HERE
, base::Bind(&TestUploadDataStreamHandler::ResetOnNetworkThread
,
82 base::Unretained(this)));
85 void TestUploadDataStreamHandler::CheckInitCallbackNotInvoked(JNIEnv
* env
,
87 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
88 network_thread_
->task_runner()->PostTask(
89 FROM_HERE
, base::Bind(&TestUploadDataStreamHandler::
90 CheckInitCallbackNotInvokedOnNetworkThread
,
91 base::Unretained(this)));
94 void TestUploadDataStreamHandler::CheckReadCallbackNotInvoked(JNIEnv
* env
,
96 DCHECK(!network_thread_
->task_runner()->BelongsToCurrentThread());
97 network_thread_
->task_runner()->PostTask(
98 FROM_HERE
, base::Bind(&TestUploadDataStreamHandler::
99 CheckReadCallbackNotInvokedOnNetworkThread
,
100 base::Unretained(this)));
103 void TestUploadDataStreamHandler::InitOnNetworkThread() {
104 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
105 init_callback_invoked_
= false;
106 read_buffer_
= nullptr;
108 int res
= upload_data_stream_
->Init(base::Bind(
109 &TestUploadDataStreamHandler::OnInitCompleted
, base::Unretained(this)));
110 JNIEnv
* env
= base::android::AttachCurrentThread();
111 cronet::Java_TestUploadDataStreamHandler_onInitCalled(
112 env
, jtest_upload_data_stream_handler_
.obj(), res
);
114 if (res
== net::OK
) {
115 cronet::Java_TestUploadDataStreamHandler_onInitCompleted(
116 env
, jtest_upload_data_stream_handler_
.obj(), res
);
120 void TestUploadDataStreamHandler::ReadOnNetworkThread() {
121 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
122 read_callback_invoked_
= false;
123 if (!read_buffer_
.get())
124 read_buffer_
= new net::IOBufferWithSize(kReadBufferSize
);
126 int bytes_read
= upload_data_stream_
->Read(
127 read_buffer_
.get(), kReadBufferSize
,
128 base::Bind(&TestUploadDataStreamHandler::OnReadCompleted
,
129 base::Unretained(this)));
130 if (bytes_read
== net::OK
) {
131 bytes_read_
= bytes_read
;
132 NotifyJavaReadCompleted();
136 void TestUploadDataStreamHandler::ResetOnNetworkThread() {
137 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
138 read_buffer_
= nullptr;
140 upload_data_stream_
->Reset();
141 JNIEnv
* env
= base::android::AttachCurrentThread();
142 cronet::Java_TestUploadDataStreamHandler_onResetCompleted(
143 env
, jtest_upload_data_stream_handler_
.obj());
146 void TestUploadDataStreamHandler::CheckInitCallbackNotInvokedOnNetworkThread() {
147 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
148 JNIEnv
* env
= base::android::AttachCurrentThread();
149 cronet::Java_TestUploadDataStreamHandler_onCheckInitCallbackNotInvoked(
150 env
, jtest_upload_data_stream_handler_
.obj(), !init_callback_invoked_
);
153 void TestUploadDataStreamHandler::CheckReadCallbackNotInvokedOnNetworkThread() {
154 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
155 JNIEnv
* env
= base::android::AttachCurrentThread();
156 cronet::Java_TestUploadDataStreamHandler_onCheckReadCallbackNotInvoked(
157 env
, jtest_upload_data_stream_handler_
.obj(), !read_callback_invoked_
);
160 void TestUploadDataStreamHandler::NotifyJavaReadCompleted() {
161 DCHECK(network_thread_
->task_runner()->BelongsToCurrentThread());
162 JNIEnv
* env
= base::android::AttachCurrentThread();
163 std::string data_read
= "";
164 if (read_buffer_
.get() && bytes_read_
> 0)
165 data_read
= std::string(read_buffer_
->data(), bytes_read_
);
166 cronet::Java_TestUploadDataStreamHandler_onReadCompleted(
167 env
, jtest_upload_data_stream_handler_
.obj(), bytes_read_
,
168 base::android::ConvertUTF8ToJavaString(env
, data_read
).Release());
171 static jlong
CreateTestUploadDataStreamHandler(
173 jobject jtest_upload_data_stream_handler
,
174 jlong jupload_data_stream
) {
175 scoped_ptr
<net::UploadDataStream
> upload_data_stream(
176 reinterpret_cast<net::UploadDataStream
*>(jupload_data_stream
));
177 TestUploadDataStreamHandler
* handler
= new TestUploadDataStreamHandler(
178 upload_data_stream
.Pass(), env
, jtest_upload_data_stream_handler
);
179 return reinterpret_cast<jlong
>(handler
);
182 bool TestUploadDataStreamHandlerRegisterJni(JNIEnv
* env
) {
183 return RegisterNativesImpl(env
);
186 } // namespace cronet