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 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "components/cronet/android/cronet_upload_data_stream_adapter.h" | 14 #include "components/cronet/android/cronet_upload_data_stream.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class SingleThreadTaskRunner; | 18 class SingleThreadTaskRunner; |
19 } // namespace base | 19 } // namespace base |
20 | 20 |
21 namespace cronet { | 21 namespace cronet { |
22 | 22 |
23 // The Delegate holds onto a reference to the IOBuffer that is currently being | 23 // The Delegate 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 | 24 // written to in Java, so may not be deleted until any read operation in Java |
25 // has completed. | 25 // has completed. |
26 // | 26 // |
27 // The Delegate is owned by the Java CronetUploadDataStream, and also owns a | 27 // The Delegate is owned by the Java CronetUploadDataStream, and also owns a |
28 // reference to it. The Delegate is only destroyed after the URLRequest | 28 // reference to it. The Delegate is only destroyed after the net::URLRequest |
29 // destroys the adapter and the CronetUploadDataStream has no read operation | 29 // destroys the C++ CronetUploadDataStream and the Java CronetUploadDataStream |
30 // pending, at which point it also releases its reference to the | 30 // has no read operation pending, at which point it also releases its reference |
31 // CronetUploadDataStream. | 31 // to the Java CronetUploadDataStream. |
32 // | 32 // |
33 // Failures don't go through the delegate, but directly to the Java request | 33 // Failures don't go through the delegate, but directly to the Java request |
34 // object, since normally reads aren't allowed to fail during an upload. | 34 // object, since normally reads aren't allowed to fail during an upload. |
35 class CronetUploadDataStreamDelegate | 35 class CronetUploadDataStreamDelegate : public CronetUploadDataStream::Delegate { |
36 : public CronetUploadDataStreamAdapter::Delegate { | |
37 public: | 36 public: |
38 CronetUploadDataStreamDelegate(JNIEnv* env, jobject jupload_data_stream); | 37 CronetUploadDataStreamDelegate(JNIEnv* env, jobject jupload_data_stream); |
39 ~CronetUploadDataStreamDelegate() override; | 38 ~CronetUploadDataStreamDelegate() override; |
40 | 39 |
41 // CronetUploadDataStreamAdapter::Delegate implementation. Called on network | 40 // CronetUploadDataStream::Delegate implementation. Called on network thread. |
42 // thread. | |
43 void InitializeOnNetworkThread( | 41 void InitializeOnNetworkThread( |
44 base::WeakPtr<CronetUploadDataStreamAdapter> adapter) override; | 42 base::WeakPtr<CronetUploadDataStream> upload_data_stream) override; |
45 void Read(net::IOBuffer* buffer, int buf_len) override; | 43 void Read(net::IOBuffer* buffer, int buf_len) override; |
46 void Rewind() override; | 44 void Rewind() override; |
47 void OnAdapterDestroyed() override; | 45 void OnUploadDataStreamDestroyed() override; |
48 | 46 |
49 // Callbacks from Java, called on some Java thread. | 47 // Callbacks from Java, called on some Java thread. |
50 void OnReadSucceeded(JNIEnv* env, | 48 void OnReadSucceeded(JNIEnv* env, |
51 jobject obj, | 49 jobject obj, |
52 int bytes_read, | 50 int bytes_read, |
53 bool final_chunk); | 51 bool final_chunk); |
54 void OnRewindSucceeded(JNIEnv* env, jobject obj); | 52 void OnRewindSucceeded(JNIEnv* env, jobject obj); |
55 | 53 |
56 private: | 54 private: |
57 // Initialized on construction, effectively constant. | 55 // Initialized on construction, effectively constant. |
58 base::android::ScopedJavaGlobalRef<jobject> jupload_data_stream_; | 56 base::android::ScopedJavaGlobalRef<jobject> jupload_data_stream_; |
59 | 57 |
60 // These are initialized in InitializeOnNetworkThread, so are safe to access | 58 // These are initialized in InitializeOnNetworkThread, so are safe to access |
61 // during Java callbacks, which all happen after initialization. | 59 // during Java callbacks, which all happen after initialization. |
62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 60 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
63 base::WeakPtr<CronetUploadDataStreamAdapter> adapter_; | 61 base::WeakPtr<CronetUploadDataStream> upload_data_stream_; |
64 | 62 |
65 // Used to keep the read buffer alive until the callback from Java has been | 63 // Used to keep the read buffer alive until the callback from Java has been |
66 // received. | 64 // received. |
67 scoped_refptr<net::IOBuffer> buffer_; | 65 scoped_refptr<net::IOBuffer> buffer_; |
68 | 66 |
69 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamDelegate); | 67 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamDelegate); |
70 }; | 68 }; |
71 | 69 |
72 // Explicitly register static JNI functions. | 70 // Explicitly register static JNI functions. |
73 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv* env); | 71 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv* env); |
74 | 72 |
75 } // namespace cronet | 73 } // namespace cronet |
76 | 74 |
77 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ | 75 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ |
OLD | NEW |