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_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 | |
|
pauljensen
2015/02/12 17:15:39
too many "only"'s
xunjieli
2015/02/12 20:56:27
Done.
| |
| 23 // send to Java is outstanding at a time. The main complexity is around | |
|
pauljensen
2015/02/12 17:15:39
send->sent
xunjieli
2015/02/12 20:56:27
Done.
| |
| 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. These two success callbacks are | |
| 62 // invoked by Java UploadDataSink upon completion of the operation. | |
| 63 void OnReadSuccess(int bytes_read, bool final_chunk); | |
| 64 void OnRewindSuccess(); | |
| 65 | |
| 66 private: | |
| 67 | |
| 68 // net::UploadDataStream implementation: | |
| 69 int InitInternal() override; | |
| 70 int ReadInternal(net::IOBuffer* buf, int buf_len) override; | |
| 71 void ResetInternal() override; | |
| 72 | |
| 73 | |
| 74 // Starts rewinding the stream. Only called when not already at the front of | |
| 75 // the stream, and no operation is pending. Completes asynchronously. | |
| 76 void StartRewind(); | |
| 77 | |
| 78 // Size of the upload. -1 if chunked. | |
|
pauljensen
2015/02/12 17:15:39
extra space after period
xunjieli
2015/02/12 20:56:27
Done.
| |
| 79 const int64 size_; | |
| 80 | |
| 81 // True if ReadInternal has been called, the read hasn't completed, and there | |
| 82 // hasn't been a ResetInternal call yet. | |
| 83 bool waiting_on_read_; | |
| 84 // True if there's a read operation in progress. This will always be true | |
| 85 // when |waiting_on_read_| is true. This will only be set to false once it | |
| 86 // completes, even though ResetInternal may have been called since the read | |
| 87 // started. | |
| 88 bool read_in_progress_; | |
| 89 | |
| 90 // True if ReadInternal has been called, the rewind hasn't completed, and | |
|
pauljensen
2015/02/12 17:15:39
Should this say InitInternal instead of ReadIntern
xunjieli
2015/02/12 20:56:27
Done. Good catch!
| |
| 91 // there hasn't been a ResetInternal call yet. Note that this may be true | |
| 92 // even when the rewind hasn't yet started, if there's a read in progress. | |
| 93 bool waiting_on_rewind_; | |
| 94 // True if there's a rewind operation in progress. Rewinding will only start | |
| 95 // when |waiting_on_rewind_| is true, and |read_in_progress_| is false. This | |
| 96 // will only be set to false once it completes, even though ResetInternal may | |
| 97 // have been called since the rewind started. | |
| 98 bool rewind_in_progress_; | |
| 99 | |
| 100 // Set to false when a read starts, true when a rewind completes. | |
| 101 bool at_front_of_stream_; | |
| 102 | |
| 103 Delegate* delegate_; | |
|
pauljensen
2015/02/12 17:15:39
const
xunjieli
2015/02/12 20:56:27
Done.
| |
| 104 | |
| 105 // Vends pointers on the network thread, though created on a Java thread. | |
| 106 base::WeakPtrFactory<CronetUploadDataStreamAdapter> weak_factory_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamAdapter); | |
| 109 }; | |
| 110 | |
| 111 } // namespace cronet | |
| 112 | |
| 113 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_ | |
| OLD | NEW |