| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "test_upload_data_stream_handler.h" | 5 #include "components/cronet/android/test/test_upload_data_stream_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | |
| 9 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "jni/TestUploadDataStreamHandler_jni.h" | 14 #include "jni/TestUploadDataStreamHandler_jni.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 | 16 |
| 17 namespace cronet { | 17 namespace cronet { |
| 18 | 18 |
| 19 static const size_t kReadBufferSize = 32768; | 19 static const size_t kReadBufferSize = 32768; |
| 20 | 20 |
| 21 TestUploadDataStreamHandler::TestUploadDataStreamHandler( | 21 TestUploadDataStreamHandler::TestUploadDataStreamHandler( |
| 22 scoped_ptr<net::UploadDataStream> upload_data_stream, | 22 scoped_ptr<net::UploadDataStream> upload_data_stream, |
| 23 JNIEnv* env, | 23 JNIEnv* env, |
| 24 jobject jtest_upload_data_stream_handler) | 24 jobject jtest_upload_data_stream_handler) |
| 25 : init_callback_invoked_(false), | 25 : init_callback_invoked_(false), |
| 26 read_callback_invoked_(false), | 26 read_callback_invoked_(false), |
| 27 bytes_read_(0), | 27 bytes_read_(0), |
| 28 network_thread_(new base::Thread("network")) { | 28 network_thread_(new base::Thread("network")) { |
| 29 upload_data_stream_ = upload_data_stream.Pass(); | 29 upload_data_stream_ = std::move(upload_data_stream); |
| 30 base::Thread::Options options; | 30 base::Thread::Options options; |
| 31 options.message_loop_type = base::MessageLoop::TYPE_IO; | 31 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 32 network_thread_->StartWithOptions(options); | 32 network_thread_->StartWithOptions(options); |
| 33 jtest_upload_data_stream_handler_.Reset(env, | 33 jtest_upload_data_stream_handler_.Reset(env, |
| 34 jtest_upload_data_stream_handler); | 34 jtest_upload_data_stream_handler); |
| 35 } | 35 } |
| 36 | 36 |
| 37 TestUploadDataStreamHandler::~TestUploadDataStreamHandler() { | 37 TestUploadDataStreamHandler::~TestUploadDataStreamHandler() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 void TestUploadDataStreamHandler::Destroy( | 40 void TestUploadDataStreamHandler::Destroy( |
| 41 JNIEnv* env, | 41 JNIEnv* env, |
| 42 const JavaParamRef<jobject>& jcaller) { | 42 const JavaParamRef<jobject>& jcaller) { |
| 43 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread()); | 43 DCHECK(!network_thread_->task_runner()->BelongsToCurrentThread()); |
| 44 // Stick network_thread_ in a local, so |this| may be destroyed from the | 44 // Stick network_thread_ in a local, so |this| may be destroyed from the |
| 45 // network thread before the network thread is destroyed. | 45 // network thread before the network thread is destroyed. |
| 46 scoped_ptr<base::Thread> network_thread = network_thread_.Pass(); | 46 scoped_ptr<base::Thread> network_thread = std::move(network_thread_); |
| 47 network_thread->task_runner()->DeleteSoon(FROM_HERE, this); | 47 network_thread->task_runner()->DeleteSoon(FROM_HERE, this); |
| 48 // Deleting thread stops it after all tasks are completed. | 48 // Deleting thread stops it after all tasks are completed. |
| 49 network_thread.reset(); | 49 network_thread.reset(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void TestUploadDataStreamHandler::OnInitCompleted(int res) { | 52 void TestUploadDataStreamHandler::OnInitCompleted(int res) { |
| 53 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread()); | 53 DCHECK(network_thread_->task_runner()->BelongsToCurrentThread()); |
| 54 init_callback_invoked_ = true; | 54 init_callback_invoked_ = true; |
| 55 JNIEnv* env = base::android::AttachCurrentThread(); | 55 JNIEnv* env = base::android::AttachCurrentThread(); |
| 56 cronet::Java_TestUploadDataStreamHandler_onInitCompleted( | 56 cronet::Java_TestUploadDataStreamHandler_onInitCompleted( |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 base::android::ConvertUTF8ToJavaString(env, data_read).obj()); | 176 base::android::ConvertUTF8ToJavaString(env, data_read).obj()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 static jlong CreateTestUploadDataStreamHandler( | 179 static jlong CreateTestUploadDataStreamHandler( |
| 180 JNIEnv* env, | 180 JNIEnv* env, |
| 181 const JavaParamRef<jobject>& jtest_upload_data_stream_handler, | 181 const JavaParamRef<jobject>& jtest_upload_data_stream_handler, |
| 182 jlong jupload_data_stream) { | 182 jlong jupload_data_stream) { |
| 183 scoped_ptr<net::UploadDataStream> upload_data_stream( | 183 scoped_ptr<net::UploadDataStream> upload_data_stream( |
| 184 reinterpret_cast<net::UploadDataStream*>(jupload_data_stream)); | 184 reinterpret_cast<net::UploadDataStream*>(jupload_data_stream)); |
| 185 TestUploadDataStreamHandler* handler = new TestUploadDataStreamHandler( | 185 TestUploadDataStreamHandler* handler = new TestUploadDataStreamHandler( |
| 186 upload_data_stream.Pass(), env, jtest_upload_data_stream_handler); | 186 std::move(upload_data_stream), env, jtest_upload_data_stream_handler); |
| 187 return reinterpret_cast<jlong>(handler); | 187 return reinterpret_cast<jlong>(handler); |
| 188 } | 188 } |
| 189 | 189 |
| 190 bool TestUploadDataStreamHandlerRegisterJni(JNIEnv* env) { | 190 bool TestUploadDataStreamHandlerRegisterJni(JNIEnv* env) { |
| 191 return RegisterNativesImpl(env); | 191 return RegisterNativesImpl(env); |
| 192 } | 192 } |
| 193 | 193 |
| 194 } // namespace cronet | 194 } // namespace cronet |
| OLD | NEW |