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_CRONET_UPLOAD_DATA_STREAM_H_ 6 #define COMPONENTS_CRONET_CRONET_UPLOAD_DATA_STREAM_H_ 7 8 #include <stdint.h> 9 10 #include "base/memory/raw_ptr.h" 11 #include "base/memory/scoped_refptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "net/base/upload_data_stream.h" 14 15 namespace net { 16 class IOBuffer; 17 } // namespace net 18 19 namespace cronet { 20 21 // The CronetUploadDataStream is created on a client thread, but afterwards, 22 // lives and is deleted on the network thread. It's responsible for ensuring 23 // only one read/rewind request sent to client is outstanding at a time. 24 // The main complexity is around Reset/Initialize calls while there's a pending 25 // read or rewind. 26 class CronetUploadDataStream : public net::UploadDataStream { 27 public: 28 class Delegate { 29 public: 30 Delegate(const Delegate&) = delete; 31 Delegate& operator=(const Delegate&) = delete; 32 33 // Called once during initial setup on the network thread, called before 34 // all other methods. 35 virtual void InitializeOnNetworkThread( 36 base::WeakPtr<CronetUploadDataStream> upload_data_stream) = 0; 37 38 // Called for each read request. Delegate must respond by calling 39 // OnReadSuccess on the network thread asynchronous, or failing the request. 40 // Only called when there's no other pending read or rewind operation. 41 virtual void Read(scoped_refptr<net::IOBuffer> buffer, int buf_len) = 0; 42 43 // Called to rewind the stream. Not called when already at the start of the 44 // stream. The delegate must respond by calling OnRewindSuccess 45 // asynchronously on the network thread, or failing the request. Only called 46 // when there's no other pending read or rewind operation. 47 virtual void Rewind() = 0; 48 49 // Called when the CronetUploadDataStream is destroyed. The Delegate is then 50 // responsible for destroying itself. May be called when there's a pending 51 // read or rewind operation. 52 virtual void OnUploadDataStreamDestroyed() = 0; 53 54 protected: 55 Delegate() = default; 56 virtual ~Delegate() = default; 57 }; 58 59 CronetUploadDataStream(Delegate* delegate, int64_t size); 60 61 CronetUploadDataStream(const CronetUploadDataStream&) = delete; 62 CronetUploadDataStream& operator=(const CronetUploadDataStream&) = delete; 63 64 ~CronetUploadDataStream() override; 65 66 // Failure is handled at the Java layer. These two success callbacks are 67 // invoked by client UploadDataSink upon completion of the operation. 68 void OnReadSuccess(int bytes_read, bool final_chunk); 69 void OnRewindSuccess(); 70 71 private: 72 // net::UploadDataStream implementation: 73 int InitInternal(const net::NetLogWithSource& net_log) override; 74 int ReadInternal(net::IOBuffer* buf, int buf_len) override; 75 void ResetInternal() override; 76 77 // Starts rewinding the stream. Only called when not already at the front of 78 // the stream, and no operation is pending. Completes asynchronously. 79 void StartRewind(); 80 81 // Size of the upload. -1 if chunked. 82 const int64_t size_; 83 84 // True if ReadInternal has been called, the read hasn't completed, and there 85 // hasn't been a ResetInternal call yet. 86 bool waiting_on_read_; 87 // True if there's a read operation in progress. This will always be true 88 // when |waiting_on_read_| is true. This will only be set to false once it 89 // completes, even though ResetInternal may have been called since the read 90 // started. 91 bool read_in_progress_; 92 93 // True if InitInternal has been called, the rewind hasn't completed, and 94 // there hasn't been a ResetInternal call yet. Note that this may be true 95 // even when the rewind hasn't yet started, if there's a read in progress. 96 bool waiting_on_rewind_; 97 // True if there's a rewind operation in progress. Rewinding will only start 98 // when |waiting_on_rewind_| is true, and |read_in_progress_| is false. This 99 // will only be set to false once it completes, even though ResetInternal may 100 // have been called since the rewind started. 101 bool rewind_in_progress_; 102 103 // Set to false when a read starts, true when a rewind completes. 104 bool at_front_of_stream_; 105 106 const raw_ptr<Delegate, DanglingUntriaged> delegate_; 107 108 // Vends pointers on the network thread, though created on a client thread. 109 base::WeakPtrFactory<CronetUploadDataStream> weak_factory_{this}; 110 }; 111 112 } // namespace cronet 113 114 #endif // COMPONENTS_CRONET_CRONET_UPLOAD_DATA_STREAM_H_ 115