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