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_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_ 6 #define COMPONENTS_CRONET_ANDROID_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_ 7 8 #include <jni.h> 9 10 #include <memory> 11 12 #include "base/android/scoped_java_ref.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/task/single_thread_task_runner.h" 15 #include "net/base/io_buffer.h" 16 #include "net/base/upload_data_stream.h" 17 18 namespace cronet { 19 20 /** 21 * This class allows a net::UploadDataStream to be driven directly from 22 * Java, for use in tests. 23 */ 24 class TestUploadDataStreamHandler { 25 public: 26 TestUploadDataStreamHandler( 27 std::unique_ptr<net::UploadDataStream> upload_data_stream, 28 JNIEnv* env, 29 jobject jtest_upload_data_stream_handler, 30 jlong jcontext_adapter); 31 32 TestUploadDataStreamHandler(const TestUploadDataStreamHandler&) = delete; 33 TestUploadDataStreamHandler& operator=(const TestUploadDataStreamHandler&) = 34 delete; 35 36 ~TestUploadDataStreamHandler(); 37 38 // Destroys |network_thread_| created by this class. 39 void Destroy(JNIEnv* env); 40 41 // Posts a task to |network_thread_| to call the corresponding method of 42 // net::UploadDataStream on |upload_data_stream_|. 43 44 void Init(JNIEnv* env); 45 void Read(JNIEnv* env); 46 void Reset(JNIEnv* env); 47 48 // Posts a task to |network_thread_| to check whether init complete callback 49 // has been invoked by net::UploadDataStream asynchronously, and notifies the 50 // Java side of the result. 51 void CheckInitCallbackNotInvoked(JNIEnv* env); 52 // Posts a task to |network_thread_| to check whether read complete callback 53 // has been invoked by net::UploadDataStream asynchronously, and notifies the 54 // Java side of the result. 55 void CheckReadCallbackNotInvoked(JNIEnv* env); 56 57 private: 58 // Complete callbacks that are passed to the |upload_data_stream_|. 59 void OnInitCompleted(int res); 60 void OnReadCompleted(int res); 61 62 // Helper methods that run corresponding task on |network_thread_|. 63 64 void InitOnNetworkThread(); 65 void ReadOnNetworkThread(); 66 void ResetOnNetworkThread(); 67 void CheckInitCallbackNotInvokedOnNetworkThread(); 68 void CheckReadCallbackNotInvokedOnNetworkThread(); 69 70 // Notify the Java TestUploadDataStreamHandler that read has completed. 71 void NotifyJavaReadCompleted(); 72 73 // True if |OnInitCompleted| callback has been invoked. It is set to false 74 // when init or reset is called again. Created on a Java thread, but is only 75 // accessed from |network_thread_|. 76 bool init_callback_invoked_; 77 // True if |OnReadCompleted| callback has been invoked. It is set to false 78 // when init or reset is called again. Created on a Java thread, but is only 79 // accessed from |network_thread_|. 80 bool read_callback_invoked_; 81 // Indicates the number of bytes read. It is reset to 0 when init, reset, or 82 // read is called again. Created on a Java thread, but is only accessed from 83 // |network_thread_|. 84 int bytes_read_; 85 86 // Created and destroyed on the same Java thread. This is where methods of 87 // net::UploadDataStream run on. 88 scoped_refptr<base::SingleThreadTaskRunner> network_thread_; 89 // Created on a Java thread. Accessed only on |network_thread_|. 90 std::unique_ptr<net::UploadDataStream> upload_data_stream_; 91 // Created and accessed only on |network_thread_|. 92 scoped_refptr<net::IOBufferWithSize> read_buffer_; 93 // A Java reference pointer for calling methods on the Java 94 // TestUploadDataStreamHandler object. Initialized during construction. 95 base::android::ScopedJavaGlobalRef<jobject> jtest_upload_data_stream_handler_; 96 }; 97 98 } // namespace cronet 99 100 #endif // COMPONENTS_CRONET_ANDROID_TEST_TEST_UPLOAD_DATA_STREAM_HANDLER_H_ 101