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