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, |
|
mmenke
2012/10/15 19:54:45
nit: The comma before otherwise should be a semic
hashimoto
2012/10/16 11:52:20
Done.
| |
| 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. When the data is chunked, size() |
| 74 // always returns zero. | |
| 66 uint64 size() const { return total_size_; } | 75 uint64 size() const { return total_size_; } |
| 67 uint64 position() const { return current_position_; } | 76 uint64 position() const { return current_position_; } |
| 68 | 77 |
| 69 bool is_chunked() const { return upload_data_->is_chunked(); } | 78 bool is_chunked() const { return upload_data_->is_chunked(); } |
| 70 | 79 |
| 71 // Returns true if all data has been consumed from this upload data | 80 // Returns true if all data has been consumed from this upload data |
| 72 // stream. | 81 // stream. |
| 73 bool IsEOF() const; | 82 bool IsEOF() const; |
| 74 | 83 |
| 75 // Returns true if the upload data in the stream is entirely in memory. | 84 // Returns true if the upload data in the stream is entirely in memory. |
| 76 bool IsInMemory() const; | 85 bool IsInMemory() const; |
| 77 | 86 |
| 78 private: | 87 private: |
| 79 friend class SpdyHttpStreamSpdy2Test; | 88 friend class SpdyHttpStreamSpdy2Test; |
| 80 friend class SpdyHttpStreamSpdy3Test; | 89 friend class SpdyHttpStreamSpdy3Test; |
| 81 friend class SpdyNetworkTransactionSpdy2Test; | 90 friend class SpdyNetworkTransactionSpdy2Test; |
| 82 friend class SpdyNetworkTransactionSpdy3Test; | 91 friend class SpdyNetworkTransactionSpdy3Test; |
| 83 | 92 |
| 84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and | 93 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and |
| 85 // remove these friend declarations. | 94 // remove these friend declarations. |
| 86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); | 95 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); |
| 87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); | 96 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); |
| 88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); | 97 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); |
| 98 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync); | |
| 89 | 99 |
| 90 // Runs Init() for all element readers. | 100 // Runs Init() for all element readers. |
| 91 // This method is used to implement Init(). | 101 // This method is used to implement Init(). |
| 92 void InitInternal(int start_index, | 102 void InitInternal(int start_index, |
| 93 const CompletionCallback& callback, | 103 const CompletionCallback& callback, |
| 94 int previous_result); | 104 int previous_result); |
| 95 | 105 |
| 96 // Finalizes the initialization process. | 106 // Finalizes the initialization process. |
| 97 // This method is used to implement Init(). | 107 // This method is used to implement Init(). |
| 98 void FinalizeInitialization(); | 108 void FinalizeInitialization(); |
| 99 | 109 |
| 110 // Reads data from the element readers. | |
| 111 // This method is used to implement Read(). | |
| 112 int ReadInternal(scoped_refptr<IOBuffer> buf, | |
| 113 int buf_len, | |
| 114 int bytes_copied, | |
| 115 bool invoked_asynchronously, | |
| 116 const CompletionCallback& callback, | |
| 117 int previous_result); | |
| 118 | |
| 100 // These methods are provided only to be used by unit tests. | 119 // These methods are provided only to be used by unit tests. |
| 101 static void ResetMergeChunks(); | 120 static void ResetMergeChunks(); |
| 102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } | 121 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } |
| 103 | 122 |
| 104 scoped_refptr<UploadData> upload_data_; | 123 scoped_refptr<UploadData> upload_data_; |
| 105 ScopedVector<UploadElementReader> element_readers_; | 124 ScopedVector<UploadElementReader> element_readers_; |
| 106 | 125 |
| 107 // Index of the current upload element (i.e. the element currently being | 126 // 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 | 127 // read). The index is used as a cursor to iterate over elements in |
| 109 // |upload_data_|. | 128 // |upload_data_|. |
| 110 size_t element_index_; | 129 size_t element_index_; |
| 111 | 130 |
| 112 // Size and current read position within the upload data stream. | 131 // Size and current read position within the upload data stream. |
| 132 // total_size_ is set to zero when the data is chunked. | |
|
mmenke
2012/10/15 19:54:45
nit: |total_size_|
hashimoto
2012/10/16 11:52:20
I don't think pipes (|) are necessary here because
mmenke
2012/10/16 19:13:41
While I agree that it's not necessary per google s
hashimoto
2012/10/17 08:04:31
OK, done.
| |
| 113 uint64 total_size_; | 133 uint64 total_size_; |
| 114 uint64 current_position_; | 134 uint64 current_position_; |
| 115 | 135 |
| 116 // True if the initialization was successful. | 136 // True if the initialization was successful. |
| 117 bool initialized_successfully_; | 137 bool initialized_successfully_; |
| 118 | 138 |
| 119 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; | 139 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; |
| 120 | 140 |
| 121 // TODO(satish): Remove this once we have a better way to unit test POST | 141 // TODO(satish): Remove this once we have a better way to unit test POST |
| 122 // requests with chunked uploads. | 142 // requests with chunked uploads. |
| 123 static bool merge_chunks_; | 143 static bool merge_chunks_; |
| 124 | 144 |
| 125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 145 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
| 126 }; | 146 }; |
| 127 | 147 |
| 128 } // namespace net | 148 } // namespace net |
| 129 | 149 |
| 130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 150 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |