Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / components / cronet / android / cronet_upload_data_stream_adapter.cc
blob70f98d331c958e4ab120352de5a26edd760480b0
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_adapter.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 "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "components/cronet/android/cronet_url_request_adapter.h"
16 #include "jni/CronetUploadDataStream_jni.h"
18 namespace cronet {
20 CronetUploadDataStreamAdapter::CronetUploadDataStreamAdapter(
21 JNIEnv* env,
22 jobject jupload_data_stream) {
23 jupload_data_stream_.Reset(env, jupload_data_stream);
26 CronetUploadDataStreamAdapter::~CronetUploadDataStreamAdapter() {
29 void CronetUploadDataStreamAdapter::InitializeOnNetworkThread(
30 base::WeakPtr<CronetUploadDataStream> upload_data_stream) {
31 DCHECK(!upload_data_stream_);
32 DCHECK(!network_task_runner_.get());
34 upload_data_stream_ = upload_data_stream;
35 network_task_runner_ = base::ThreadTaskRunnerHandle::Get();
36 DCHECK(network_task_runner_);
39 void CronetUploadDataStreamAdapter::Read(net::IOBuffer* buffer, int buf_len) {
40 DCHECK(upload_data_stream_);
41 DCHECK(network_task_runner_);
42 DCHECK(network_task_runner_->BelongsToCurrentThread());
43 DCHECK_GT(buf_len, 0);
44 DCHECK(!buffer_.get());
45 buffer_ = buffer;
47 // TODO(mmenke): Consider preserving the java buffer across reads, when the
48 // IOBuffer's data pointer and its length are unchanged.
49 JNIEnv* env = base::android::AttachCurrentThread();
50 base::android::ScopedJavaLocalRef<jobject> java_buffer(
51 env, env->NewDirectByteBuffer(buffer->data(), buf_len));
52 Java_CronetUploadDataStream_readData(env, jupload_data_stream_.obj(),
53 java_buffer.obj());
56 void CronetUploadDataStreamAdapter::Rewind() {
57 DCHECK(upload_data_stream_);
58 DCHECK(network_task_runner_->BelongsToCurrentThread());
60 JNIEnv* env = base::android::AttachCurrentThread();
61 Java_CronetUploadDataStream_rewind(env, jupload_data_stream_.obj());
64 void CronetUploadDataStreamAdapter::OnUploadDataStreamDestroyed() {
65 // If CronetUploadDataStream::InitInternal was never called,
66 // |upload_data_stream_| and |network_task_runner_| will be NULL.
67 DCHECK(!network_task_runner_ ||
68 network_task_runner_->BelongsToCurrentThread());
70 JNIEnv* env = base::android::AttachCurrentThread();
71 Java_CronetUploadDataStream_onUploadDataStreamDestroyed(
72 env, jupload_data_stream_.obj());
75 void CronetUploadDataStreamAdapter::OnReadSucceeded(JNIEnv* env,
76 jobject jcaller,
77 int bytes_read,
78 bool final_chunk) {
79 DCHECK(!network_task_runner_->BelongsToCurrentThread());
80 DCHECK(bytes_read > 0 || (final_chunk && bytes_read == 0));
82 buffer_ = nullptr;
83 network_task_runner_->PostTask(
84 FROM_HERE, base::Bind(&CronetUploadDataStream::OnReadSuccess,
85 upload_data_stream_, bytes_read, final_chunk));
88 void CronetUploadDataStreamAdapter::OnRewindSucceeded(JNIEnv* env,
89 jobject jcaller) {
90 DCHECK(!network_task_runner_->BelongsToCurrentThread());
92 network_task_runner_->PostTask(
93 FROM_HERE,
94 base::Bind(&CronetUploadDataStream::OnRewindSuccess,
95 upload_data_stream_));
98 bool CronetUploadDataStreamAdapterRegisterJni(JNIEnv* env) {
99 return RegisterNativesImpl(env);
102 static jlong AttachUploadDataToRequest(
103 JNIEnv* env,
104 const JavaParamRef<jobject>& jupload_data_stream,
105 jlong jcronet_url_request_adapter,
106 jlong jlength) {
107 CronetURLRequestAdapter* request_adapter =
108 reinterpret_cast<CronetURLRequestAdapter*>(jcronet_url_request_adapter);
109 DCHECK(request_adapter != nullptr);
111 CronetUploadDataStreamAdapter* adapter =
112 new CronetUploadDataStreamAdapter(env, jupload_data_stream);
114 scoped_ptr<CronetUploadDataStream> upload_data_stream(
115 new CronetUploadDataStream(adapter, jlength));
117 request_adapter->SetUpload(upload_data_stream.Pass());
119 return reinterpret_cast<jlong>(adapter);
122 static jlong CreateAdapterForTesting(
123 JNIEnv* env,
124 const JavaParamRef<jobject>& jupload_data_stream) {
125 CronetUploadDataStreamAdapter* adapter =
126 new CronetUploadDataStreamAdapter(env, jupload_data_stream);
127 return reinterpret_cast<jlong>(adapter);
130 static jlong CreateUploadDataStreamForTesting(
131 JNIEnv* env,
132 const JavaParamRef<jobject>& jupload_data_stream,
133 jlong jlength,
134 jlong jadapter) {
135 CronetUploadDataStreamAdapter* adapter =
136 reinterpret_cast<CronetUploadDataStreamAdapter*>(jadapter);
137 CronetUploadDataStream* upload_data_stream =
138 new CronetUploadDataStream(adapter, jlength);
139 return reinterpret_cast<jlong>(upload_data_stream);
142 static void DestroyAdapter(
143 JNIEnv* env,
144 const JavaParamRef<jclass>& jcronet_url_request_adapter,
145 jlong jupload_data_stream_adapter) {
146 CronetUploadDataStreamAdapter* adapter =
147 reinterpret_cast<CronetUploadDataStreamAdapter*>(
148 jupload_data_stream_adapter);
149 DCHECK(adapter != nullptr);
150 delete adapter;
153 } // namespace cronet