1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ 6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ 7 8 #include <jni.h> 9 10 #include "base/android/scoped_java_ref.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/weak_ptr.h" 13 #include "components/cronet/cronet_upload_data_stream.h" 14 #include "net/base/io_buffer.h" 15 16 namespace base { 17 class SingleThreadTaskRunner; 18 } // namespace base 19 20 namespace cronet { 21 class ByteBufferWithIOBuffer; 22 23 // The Adapter holds onto a reference to the IOBuffer that is currently being 24 // written to in Java, so may not be deleted until any read operation in Java 25 // has completed. 26 // 27 // The Adapter is owned by the Java CronetUploadDataStream, and also owns a 28 // reference to it. The Adapter is only destroyed after the net::URLRequest 29 // destroys the C++ CronetUploadDataStream and the Java CronetUploadDataStream 30 // has no read operation pending, at which point it also releases its reference 31 // to the Java CronetUploadDataStream. 32 // 33 // Failures don't go back through the Adapter, but directly to the Java request 34 // object, since normally reads aren't allowed to fail during an upload. 35 class CronetUploadDataStreamAdapter : public CronetUploadDataStream::Delegate { 36 public: 37 CronetUploadDataStreamAdapter(JNIEnv* env, jobject jupload_data_stream); 38 39 CronetUploadDataStreamAdapter(const CronetUploadDataStreamAdapter&) = delete; 40 CronetUploadDataStreamAdapter& operator=( 41 const CronetUploadDataStreamAdapter&) = delete; 42 43 ~CronetUploadDataStreamAdapter() override; 44 45 // CronetUploadDataStream::Delegate implementation. Called on network thread. 46 void InitializeOnNetworkThread( 47 base::WeakPtr<CronetUploadDataStream> upload_data_stream) override; 48 void Read(scoped_refptr<net::IOBuffer> buffer, int buf_len) override; 49 void Rewind() override; 50 void OnUploadDataStreamDestroyed() override; 51 52 // Callbacks from Java, called on some Java thread. 53 void OnReadSucceeded(JNIEnv* env, 54 const base::android::JavaParamRef<jobject>& obj, 55 int bytes_read, 56 bool final_chunk); 57 void OnRewindSucceeded(JNIEnv* env, 58 const base::android::JavaParamRef<jobject>& obj); 59 60 // Destroys |this|. Can be called from any thread, but needs to be protected 61 // by the adapter lock. 62 void Destroy(JNIEnv* env); 63 64 private: 65 // Initialized on construction, effectively constant. 66 base::android::ScopedJavaGlobalRef<jobject> jupload_data_stream_; 67 68 // These are initialized in InitializeOnNetworkThread, so are safe to access 69 // during Java callbacks, which all happen after initialization. 70 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 71 base::WeakPtr<CronetUploadDataStream> upload_data_stream_; 72 73 // Keeps the net::IOBuffer and Java ByteBuffer alive until the next Read(). 74 std::unique_ptr<ByteBufferWithIOBuffer> buffer_; 75 }; 76 77 } // namespace cronet 78 79 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ 80