Chromium Code Reviews| 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_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ | |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_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/weak_ptr.h" | |
| 14 #include "components/cronet/android/cronet_upload_data_stream_adapter.h" | |
| 15 #include "net/base/io_buffer.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class SingleThreadTaskRunner; | |
| 19 } // namespace base | |
| 20 | |
| 21 namespace cronet { | |
| 22 | |
| 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 | |
| 25 // has completed. | |
| 26 // | |
| 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 | |
| 29 // destroys the adapter and the CronetUploadDataStream has no operation pending, | |
| 
 
mmenke
2015/02/18 17:08:40
"has no operation pending" -> "has no read operati
 
xunjieli
2015/02/19 14:59:11
Done.
 
 | |
| 30 // at which point it also releases its reference to the CronetUploadDataStream. | |
| 31 // | |
| 32 // Failures don't go through the delegate, but directly to the Java request | |
| 33 // object, since normally reads aren't allowed to fail during an upload. | |
| 34 class CronetUploadDataStreamDelegate | |
| 35 : public CronetUploadDataStreamAdapter::Delegate { | |
| 36 public: | |
| 37 CronetUploadDataStreamDelegate(JNIEnv* env, jobject jupload_data_stream); | |
| 38 ~CronetUploadDataStreamDelegate() override {} | |
| 39 | |
| 40 // CronetUploadDataStreamAdapter::Delegate implementation. Called on network | |
| 41 // thread. | |
| 42 void InitializeOnNetworkThread( | |
| 43 base::WeakPtr<CronetUploadDataStreamAdapter> adapter) override; | |
| 44 void Read(net::IOBuffer* buffer, int buf_len) override; | |
| 45 void Rewind() override; | |
| 46 void OnAdapterDestroyed() override; | |
| 47 | |
| 48 // Callbacks from Java, called on some Java thread. | |
| 49 void OnReadSucceeded(JNIEnv* env, jobject obj, | |
| 50 int bytes_read, bool final_chunk); | |
| 51 void OnRewindSucceeded(JNIEnv* env, jobject obj); | |
| 52 | |
| 53 private: | |
| 54 // Initialized on construction, effectively constant. | |
| 55 base::android::ScopedJavaGlobalRef<jobject> jupload_data_stream_; | |
| 56 | |
| 57 // These are initialized in InitializeOnNetworkThread, so are safe to access | |
| 58 // during Java callbacks, which all happen after initialization. | |
| 59 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | |
| 60 base::WeakPtr<CronetUploadDataStreamAdapter> adapter_; | |
| 61 | |
| 62 // Used to keep the read buffer alive until the callback from Java has been | |
| 63 // received. | |
| 64 scoped_refptr<net::IOBuffer> buffer_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamDelegate); | |
| 67 }; | |
| 68 | |
| 69 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv* env); | |
| 70 | |
| 71 } // namespace cronet | |
| 72 | |
| 73 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_ | |
| OLD | NEW |