Roll src/third_party/WebKit aa8346d:dbb8a38 (svn 202629:202630)
[chromium-blink-merge.git] / components / cronet / android / test / test_upload_data_stream_handler.cc
blob5658e9e1dd45eddd69671eed5a91cd4686ae747a
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"
7 #include <string>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/bind.h"
12 #include "jni/TestUploadDataStreamHandler_jni.h"
13 #include "net/base/net_errors.h"
15 namespace cronet {
17 static const size_t kReadBufferSize = 32768;
19 TestUploadDataStreamHandler::TestUploadDataStreamHandler(
20 scoped_ptr<net::UploadDataStream> upload_data_stream,
21 JNIEnv* env,
22 jobject jtest_upload_data_stream_handler)
23 : init_callback_invoked_(false),
24 read_callback_invoked_(false),
25 bytes_read_(0),
26 network_thread_(new base::Thread("network")) {
27 upload_data_stream_ = upload_data_stream.Pass();
28 base::Thread::Options options;
29 options.message_loop_type = base::MessageLoop::TYPE_IO;
30 network_thread_->StartWithOptions(options);
31 jtest_upload_data_stream_handler_.Reset(env,
32 jtest_upload_data_stream_handler);
35 TestUploadDataStreamHandler::~TestUploadDataStreamHandler() {
38 void TestUploadDataStreamHandler::Destroy(JNIEnv* env, jobject jcaller) {
39 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
40 // Stick network_thread_ in a local, so |this| may be destroyed from the
41 // network thread before the network thread is destroyed.
42 scoped_ptr<base::Thread> network_thread = network_thread_.Pass();
43 network_thread->task_runner()->DeleteSoon(FROM_HERE, this);
44 // Deleting thread stops it after all tasks are completed.
45 network_thread.reset();
48 void TestUploadDataStreamHandler::OnInitCompleted(int res) {
49 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
50 init_callback_invoked_ = true;
51 JNIEnv* env = base::android::AttachCurrentThread();
52 cronet::Java_TestUploadDataStreamHandler_onInitCompleted(
53 env, jtest_upload_data_stream_handler_.obj(), res);
56 void TestUploadDataStreamHandler::OnReadCompleted(int res) {
57 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
58 read_callback_invoked_ = true;
59 bytes_read_ = res;
60 NotifyJavaReadCompleted();
63 void TestUploadDataStreamHandler::Init(JNIEnv* env, jobject jcaller) {
64 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
65 network_thread_->task_runner()->PostTask(
66 FROM_HERE, base::Bind(&TestUploadDataStreamHandler::InitOnNetworkThread,
67 base::Unretained(this)));
70 void TestUploadDataStreamHandler::Read(JNIEnv* env, jobject jcaller) {
71 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
72 network_thread_->task_runner()->PostTask(
73 FROM_HERE, base::Bind(&TestUploadDataStreamHandler::ReadOnNetworkThread,
74 base::Unretained(this)));
77 void TestUploadDataStreamHandler::Reset(JNIEnv* env, jobject jcaller) {
78 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
79 network_thread_->task_runner()->PostTask(
80 FROM_HERE, base::Bind(&TestUploadDataStreamHandler::ResetOnNetworkThread,
81 base::Unretained(this)));
84 void TestUploadDataStreamHandler::CheckInitCallbackNotInvoked(JNIEnv* env,
85 jobject jcaller) {
86 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
87 network_thread_->task_runner()->PostTask(
88 FROM_HERE, base::Bind(&TestUploadDataStreamHandler::
89 CheckInitCallbackNotInvokedOnNetworkThread,
90 base::Unretained(this)));
93 void TestUploadDataStreamHandler::CheckReadCallbackNotInvoked(JNIEnv* env,
94 jobject jcaller) {
95 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread());
96 network_thread_->task_runner()->PostTask(
97 FROM_HERE, base::Bind(&TestUploadDataStreamHandler::
98 CheckReadCallbackNotInvokedOnNetworkThread,
99 base::Unretained(this)));
102 void TestUploadDataStreamHandler::InitOnNetworkThread() {
103 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
104 init_callback_invoked_ = false;
105 read_buffer_ = nullptr;
106 bytes_read_ = 0;
107 int res = upload_data_stream_->Init(base::Bind(
108 &TestUploadDataStreamHandler::OnInitCompleted, base::Unretained(this)));
109 JNIEnv* env = base::android::AttachCurrentThread();
110 cronet::Java_TestUploadDataStreamHandler_onInitCalled(
111 env, jtest_upload_data_stream_handler_.obj(), res);
113 if (res == net::OK) {
114 cronet::Java_TestUploadDataStreamHandler_onInitCompleted(
115 env, jtest_upload_data_stream_handler_.obj(), res);
119 void TestUploadDataStreamHandler::ReadOnNetworkThread() {
120 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
121 read_callback_invoked_ = false;
122 if (!read_buffer_.get())
123 read_buffer_ = new net::IOBufferWithSize(kReadBufferSize);
125 int bytes_read = upload_data_stream_->Read(
126 read_buffer_.get(), kReadBufferSize,
127 base::Bind(&TestUploadDataStreamHandler::OnReadCompleted,
128 base::Unretained(this)));
129 if (bytes_read == net::OK) {
130 bytes_read_ = bytes_read;
131 NotifyJavaReadCompleted();
135 void TestUploadDataStreamHandler::ResetOnNetworkThread() {
136 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
137 read_buffer_ = nullptr;
138 bytes_read_ = 0;
139 upload_data_stream_->Reset();
140 JNIEnv* env = base::android::AttachCurrentThread();
141 cronet::Java_TestUploadDataStreamHandler_onResetCompleted(
142 env, jtest_upload_data_stream_handler_.obj());
145 void TestUploadDataStreamHandler::CheckInitCallbackNotInvokedOnNetworkThread() {
146 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
147 JNIEnv* env = base::android::AttachCurrentThread();
148 cronet::Java_TestUploadDataStreamHandler_onCheckInitCallbackNotInvoked(
149 env, jtest_upload_data_stream_handler_.obj(), !init_callback_invoked_);
152 void TestUploadDataStreamHandler::CheckReadCallbackNotInvokedOnNetworkThread() {
153 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
154 JNIEnv* env = base::android::AttachCurrentThread();
155 cronet::Java_TestUploadDataStreamHandler_onCheckReadCallbackNotInvoked(
156 env, jtest_upload_data_stream_handler_.obj(), !read_callback_invoked_);
159 void TestUploadDataStreamHandler::NotifyJavaReadCompleted() {
160 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread());
161 JNIEnv* env = base::android::AttachCurrentThread();
162 std::string data_read = "";
163 if (read_buffer_.get() && bytes_read_ > 0)
164 data_read = std::string(read_buffer_->data(), bytes_read_);
165 cronet::Java_TestUploadDataStreamHandler_onReadCompleted(
166 env, jtest_upload_data_stream_handler_.obj(), bytes_read_,
167 base::android::ConvertUTF8ToJavaString(env, data_read).obj());
170 static jlong CreateTestUploadDataStreamHandler(
171 JNIEnv* env,
172 const JavaParamRef<jobject>& jtest_upload_data_stream_handler,
173 jlong jupload_data_stream) {
174 scoped_ptr<net::UploadDataStream> upload_data_stream(
175 reinterpret_cast<net::UploadDataStream*>(jupload_data_stream));
176 TestUploadDataStreamHandler* handler = new TestUploadDataStreamHandler(
177 upload_data_stream.Pass(), env, jtest_upload_data_stream_handler);
178 return reinterpret_cast<jlong>(handler);
181 bool TestUploadDataStreamHandlerRegisterJni(JNIEnv* env) {
182 return RegisterNativesImpl(env);
185 } // namespace cronet