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" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // | 32 // |
33 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected | 33 // 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 | 34 // file modification time is set (usually not set, but set for sliced |
35 // files) and the target file is changed. | 35 // files) and the target file is changed. |
36 int Init(const CompletionCallback& callback); | 36 int Init(const CompletionCallback& callback); |
37 | 37 |
38 // Initializes the stream synchronously. | 38 // Initializes the stream synchronously. |
39 // Use this method only in tests and Chrome Frame. | 39 // Use this method only in tests and Chrome Frame. |
40 int InitSync(); | 40 int InitSync(); |
41 | 41 |
42 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The | 42 // Reads up to |buf_len| bytes synchronously from the upload data stream to |
43 // number of bytes read is returned. Partial reads are allowed. Zero is | 43 // |buf| and returns the number of bytes read when possible, otherwise, |
44 // returned on a call to Read when there are no remaining bytes in the | 44 // returns ERR_IO_PENDING and calls |callback| with the result. Partial reads |
45 // stream, and IsEof() will return true hereafter. | 45 // are allowed. Zero is returned on a call to Read when there are no |
| 46 // remaining bytes in the stream, and IsEof() will return true hereafter. |
46 // | 47 // |
47 // If there's less data to read than we initially observed (i.e. the actual | 48 // 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 | 49 // 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. | 50 // size() bytes can be read, which can happen for TYPE_FILE payloads. |
50 // | 51 // |
51 // If the upload data stream is chunked (i.e. is_chunked() is true), | 52 // If the upload data stream is chunked (i.e. is_chunked() is true), |
52 // ERR_IO_PENDING is returned to indicate there is nothing to read at the | 53 // ERR_IO_PENDING is returned to indicate there is nothing to read at the |
53 // moment, but more data to come at a later time. If not chunked, reads | 54 // moment, but more data to come at a later time. |
54 // won't fail. | 55 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
55 int Read(IOBuffer* buf, int buf_len); | 56 |
| 57 // Reads data always synchronously. |
| 58 // Use this method only in tests and Chrome Frame. |
| 59 int ReadSync(IOBuffer* buf, int buf_len); |
56 | 60 |
57 // Sets the callback to be invoked when new chunks are available to upload. | 61 // Sets the callback to be invoked when new chunks are available to upload. |
58 void set_chunk_callback(ChunkCallback* callback) { | 62 void set_chunk_callback(ChunkCallback* callback) { |
59 upload_data_->set_chunk_callback(callback); | 63 upload_data_->set_chunk_callback(callback); |
60 } | 64 } |
61 | 65 |
62 // Returns the total size of the data stream and the current position. | 66 // 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 | 67 // 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, | 68 // because it is possible for the stream to end before its size is reached, |
65 // for example, if the file is truncated. | 69 // for example, if the file is truncated. |
(...skipping 13 matching lines...) Expand all Loading... |
79 friend class SpdyHttpStreamSpdy2Test; | 83 friend class SpdyHttpStreamSpdy2Test; |
80 friend class SpdyHttpStreamSpdy3Test; | 84 friend class SpdyHttpStreamSpdy3Test; |
81 friend class SpdyNetworkTransactionSpdy2Test; | 85 friend class SpdyNetworkTransactionSpdy2Test; |
82 friend class SpdyNetworkTransactionSpdy3Test; | 86 friend class SpdyNetworkTransactionSpdy3Test; |
83 | 87 |
84 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and | 88 // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and |
85 // remove these friend declarations. | 89 // remove these friend declarations. |
86 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); | 90 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); |
87 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); | 91 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); |
88 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); | 92 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); |
| 93 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync); |
89 | 94 |
90 // Runs Init() for all element readers. | 95 // Runs Init() for all element readers. |
91 // This method is used to implement Init(). | 96 // This method is used to implement Init(). |
92 void InitInternal(int start_index, | 97 void InitInternal(int start_index, |
93 const CompletionCallback& callback, | 98 const CompletionCallback& callback, |
94 int previous_result); | 99 int previous_result); |
95 | 100 |
96 // Finalizes the initialization process. | 101 // Finalizes the initialization process. |
97 // This method is used to implement Init(). | 102 // This method is used to implement Init(). |
98 void FinalizeInitialization(); | 103 void FinalizeInitialization(); |
99 | 104 |
| 105 // Reads data from the element readers. |
| 106 // This method is used to implement Read(). |
| 107 void ReadInternal(scoped_refptr<IOBuffer> buf, |
| 108 int buf_len, |
| 109 int bytes_copied, |
| 110 const CompletionCallback& callback, |
| 111 int previous_result); |
| 112 |
100 // These methods are provided only to be used by unit tests. | 113 // These methods are provided only to be used by unit tests. |
101 static void ResetMergeChunks(); | 114 static void ResetMergeChunks(); |
102 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } | 115 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } |
103 | 116 |
104 scoped_refptr<UploadData> upload_data_; | 117 scoped_refptr<UploadData> upload_data_; |
105 ScopedVector<UploadElementReader> element_readers_; | 118 ScopedVector<UploadElementReader> element_readers_; |
106 | 119 |
107 // Index of the current upload element (i.e. the element currently being | 120 // 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 | 121 // read). The index is used as a cursor to iterate over elements in |
109 // |upload_data_|. | 122 // |upload_data_|. |
(...skipping 11 matching lines...) Expand all Loading... |
121 // TODO(satish): Remove this once we have a better way to unit test POST | 134 // TODO(satish): Remove this once we have a better way to unit test POST |
122 // requests with chunked uploads. | 135 // requests with chunked uploads. |
123 static bool merge_chunks_; | 136 static bool merge_chunks_; |
124 | 137 |
125 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 138 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
126 }; | 139 }; |
127 | 140 |
128 } // namespace net | 141 } // namespace net |
129 | 142 |
130 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 143 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |