Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 NET_BASE_UPLOAD_DATA_STREAM_H_ | 5 #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ | 6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 7 | 7 |
| 8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
| 13 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
| 14 #include "net/base/upload_data.h" | 14 #include "net/base/upload_data.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 class IOBuffer; | 18 class IOBuffer; |
| 19 class UploadElementReader; | 19 class UploadElementReader; |
| 20 | 20 |
| 21 // A class to read all elements from an UploadData object. | |
| 21 class NET_EXPORT UploadDataStream { | 22 class NET_EXPORT UploadDataStream { |
| 22 public: | 23 public: |
| 23 explicit UploadDataStream(UploadData* upload_data); | 24 explicit UploadDataStream(UploadData* upload_data); |
| 24 ~UploadDataStream(); | 25 ~UploadDataStream(); |
| 25 | 26 |
| 26 // Initializes the stream. This function must be called exactly once, | 27 // Initializes the stream. This function must be called exactly once, |
| 27 // before calling any other method. It is not valid to call any method | 28 // before calling any other method. It is not valid to call any method |
| 28 // (other than the destructor) if Init() returns a failure. | 29 // (other than the destructor) if Init() returns a failure. |
| 29 // | 30 // |
| 30 // Does the initialization synchronously and returns the result if possible, | 31 // Does the initialization synchronously and returns the result if possible, |
| 31 // otherwise returns ERR_IO_PENDING and runs the callback with the result. | 32 // otherwise returns ERR_IO_PENDING and runs the callback with the result. |
| 32 // | 33 // |
| 33 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected | 34 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected |
| 34 // file modification time is set (usually not set, but set for sliced | 35 // file modification time is set (usually not set, but set for sliced |
| 35 // files) and the target file is changed. | 36 // files) and the target file is changed. |
| 36 int Init(const CompletionCallback& callback); | 37 int Init(const CompletionCallback& callback); |
| 37 | 38 |
| 38 // Initializes the stream synchronously. | 39 // Initializes the stream synchronously. |
| 39 // Use this method only in tests and Chrome Frame. | 40 // Use this method only if the thread is IO allowed or the data is in-memory. |
| 40 int InitSync(); | 41 int InitSync(); |
| 41 | 42 |
| 42 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The | 43 // When possible, reads up to |buf_len| bytes synchronously from the upload |
| 43 // number of bytes read is returned. Partial reads are allowed. Zero is | 44 // data stream to |buf| and returns the number of bytes read, otherwise, |
| 44 // returned on a call to Read when there are no remaining bytes in the | 45 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read. |
| 45 // stream, and IsEof() will return true hereafter. | 46 // Partial reads are allowed. Zero is returned on a call to Read when there |
| 47 // are no remaining bytes in the stream, and IsEof() will return true | |
| 48 // hereafter. | |
| 46 // | 49 // |
| 47 // If there's less data to read than we initially observed (i.e. the actual | 50 // If there's less data to read than we initially observed (i.e. the actual |
| 48 // upload data is smaller than size()), zeros are padded to ensure that | 51 // upload data is smaller than size()), zeros are padded to ensure that |
| 49 // size() bytes can be read, which can happen for TYPE_FILE payloads. | 52 // size() bytes can be read, which can happen for TYPE_FILE payloads. |
| 50 // | 53 // |
| 51 // If the upload data stream is chunked (i.e. is_chunked() is true), | 54 // If the data is chunked (i.e. is_chunked() is true) and there is not enough |
| 52 // ERR_IO_PENDING is returned to indicate there is nothing to read at the | 55 // data ready to be uploaded, ERR_IO_PENDING is returned and the callback set |
| 53 // moment, but more data to come at a later time. If not chunked, reads | 56 // by set_chunk_callback is called later when data gets available. |
| 54 // won't fail. | 57 // When data is available, the data is read synchronously and the number of |
| 55 int Read(IOBuffer* buf, int buf_len); | 58 // bytes read is returned. |
| 59 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 60 | |
| 61 // Reads data always synchronously. | |
| 62 // Use this method only if the thread is IO allowed or the data is in-memory. | |
| 63 int ReadSync(IOBuffer* buf, int buf_len); | |
| 56 | 64 |
| 57 // Sets the callback to be invoked when new chunks are available to upload. | 65 // Sets the callback to be invoked when new chunks are available to upload. |
| 58 void set_chunk_callback(ChunkCallback* callback) { | 66 void set_chunk_callback(ChunkCallback* callback) { |
| 59 upload_data_->set_chunk_callback(callback); | 67 upload_data_->set_chunk_callback(callback); |
| 60 } | 68 } |
| 61 | 69 |
| 62 // Returns the total size of the data stream and the current position. | 70 // Returns the total size of the data stream and the current position. |
| 63 // size() is not to be used to determine whether the stream has ended | 71 // size() is not to be used to determine whether the stream has ended |
| 64 // because it is possible for the stream to end before its size is reached, | 72 // because it is possible for the stream to end before its size is reached, |
| 65 // for example, if the file is truncated. | 73 // for example, if the file is truncated. |
|
mmenke
2012/10/12 21:30:29
Could you mention that size() returns 0 for chunke
hashimoto
2012/10/15 11:21:37
Done.
| |
| 66 uint64 size() const { return total_size_; } | 74 uint64 size() const { return total_size_; } |
| 67 uint64 position() const { return current_position_; } | 75 uint64 position() const { return current_position_; } |
| 68 | 76 |
| 69 bool is_chunked() const { return upload_data_->is_chunked(); } | 77 bool is_chunked() const { return upload_data_->is_chunked(); } |
| 70 | 78 |
| 71 // Returns true if all data has been consumed from this upload data | 79 // Returns true if all data has been consumed from this upload data |
| 72 // stream. | 80 // stream. |
| 73 bool IsEOF() const; | 81 bool IsEOF() const; |
| 74 | 82 |
| 75 // Returns true if the upload data in the stream is entirely in memory. | 83 // Returns true if the upload data in the stream is entirely in memory. |
| 76 bool IsInMemory() const; | 84 bool IsInMemory() const; |
| 77 | 85 |
| 78 private: | 86 private: |
| 79 friend class SpdyHttpStreamSpdy2Test; | 87 friend class SpdyHttpStreamSpdy2Test; |
| 80 friend class SpdyHttpStreamSpdy3Test; | 88 friend class SpdyHttpStreamSpdy3Test; |
| 81 friend class SpdyNetworkTransactionSpdy2Test; | 89 friend class SpdyNetworkTransactionSpdy2Test; |
| 82 friend class SpdyNetworkTransactionSpdy3Test; | 90 friend class SpdyNetworkTransactionSpdy3Test; |
| 83 | 91 |
| 84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and | 92 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and |
| 85 // remove these friend declarations. | 93 // remove these friend declarations. |
| 86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); | 94 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); |
| 87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); | 95 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); |
| 88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); | 96 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); |
| 97 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync); | |
| 89 | 98 |
| 90 // Runs Init() for all element readers. | 99 // Runs Init() for all element readers. |
| 91 // This method is used to implement Init(). | 100 // This method is used to implement Init(). |
| 92 void InitInternal(int start_index, | 101 void InitInternal(int start_index, |
| 93 const CompletionCallback& callback, | 102 const CompletionCallback& callback, |
| 94 int previous_result); | 103 int previous_result); |
| 95 | 104 |
| 96 // Finalizes the initialization process. | 105 // Finalizes the initialization process. |
| 97 // This method is used to implement Init(). | 106 // This method is used to implement Init(). |
| 98 void FinalizeInitialization(); | 107 void FinalizeInitialization(); |
| 99 | 108 |
| 109 // Reads data from the element readers. | |
| 110 // This method is used to implement Read(). | |
| 111 int ReadInternal(scoped_refptr<IOBuffer> buf, | |
| 112 int buf_len, | |
| 113 int bytes_copied, | |
| 114 bool invoked_asynchronously, | |
| 115 const CompletionCallback& callback, | |
| 116 int previous_result); | |
| 117 | |
| 100 // These methods are provided only to be used by unit tests. | 118 // These methods are provided only to be used by unit tests. |
| 101 static void ResetMergeChunks(); | 119 static void ResetMergeChunks(); |
| 102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } | 120 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } |
| 103 | 121 |
| 104 scoped_refptr<UploadData> upload_data_; | 122 scoped_refptr<UploadData> upload_data_; |
| 105 ScopedVector<UploadElementReader> element_readers_; | 123 ScopedVector<UploadElementReader> element_readers_; |
| 106 | 124 |
| 107 // Index of the current upload element (i.e. the element currently being | 125 // Index of the current upload element (i.e. the element currently being |
| 108 // read). The index is used as a cursor to iterate over elements in | 126 // read). The index is used as a cursor to iterate over elements in |
| 109 // |upload_data_|. | 127 // |upload_data_|. |
| 110 size_t element_index_; | 128 size_t element_index_; |
| 111 | 129 |
| 112 // Size and current read position within the upload data stream. | 130 // Size and current read position within the upload data stream. |
|
mmenke
2012/10/12 21:30:29
"Size is zero for chunked transfers."
hashimoto
2012/10/15 11:21:37
Done.
| |
| 113 uint64 total_size_; | 131 uint64 total_size_; |
| 114 uint64 current_position_; | 132 uint64 current_position_; |
| 115 | 133 |
| 116 // True if the initialization was successful. | 134 // True if the initialization was successful. |
| 117 bool initialized_successfully_; | 135 bool initialized_successfully_; |
| 118 | 136 |
| 119 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; | 137 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; |
| 120 | 138 |
| 121 // TODO(satish): Remove this once we have a better way to unit test POST | 139 // TODO(satish): Remove this once we have a better way to unit test POST |
| 122 // requests with chunked uploads. | 140 // requests with chunked uploads. |
| 123 static bool merge_chunks_; | 141 static bool merge_chunks_; |
| 124 | 142 |
| 125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 143 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
| 126 }; | 144 }; |
| 127 | 145 |
| 128 } // namespace net | 146 } // namespace net |
| 129 | 147 |
| 130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 148 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |