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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 16 #include "net/log/net_log.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 class DrainableIOBuffer; | 20 class DrainableIOBuffer; |
20 class IOBuffer; | 21 class IOBuffer; |
21 class UploadElementReader; | 22 class UploadElementReader; |
22 | 23 |
23 // A class for retrieving all data to be sent as a request body. Supports both | 24 // A class for retrieving all data to be sent as a request body. Supports both |
24 // chunked and non-chunked uploads. | 25 // chunked and non-chunked uploads. |
25 class NET_EXPORT UploadDataStream { | 26 class NET_EXPORT UploadDataStream { |
(...skipping 10 matching lines...) Expand all Loading... |
36 // destructor) if Init() fails. This method can be called multiple times. | 37 // destructor) if Init() fails. This method can be called multiple times. |
37 // Calling this method after an Init() success results in resetting the | 38 // Calling this method after an Init() success results in resetting the |
38 // state (i.e. the stream is rewound). | 39 // state (i.e. the stream is rewound). |
39 // | 40 // |
40 // Does the initialization synchronously and returns the result if possible, | 41 // Does the initialization synchronously and returns the result if possible, |
41 // otherwise returns ERR_IO_PENDING and runs the callback with the result. | 42 // otherwise returns ERR_IO_PENDING and runs the callback with the result. |
42 // | 43 // |
43 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected | 44 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected |
44 // file modification time is set (usually not set, but set for sliced | 45 // file modification time is set (usually not set, but set for sliced |
45 // files) and the target file is changed. | 46 // files) and the target file is changed. |
46 int Init(const CompletionCallback& callback); | 47 int Init(const CompletionCallback& callback, const BoundNetLog& net_log); |
47 | 48 |
48 // When possible, reads up to |buf_len| bytes synchronously from the upload | 49 // When possible, reads up to |buf_len| bytes synchronously from the upload |
49 // data stream to |buf| and returns the number of bytes read; otherwise, | 50 // data stream to |buf| and returns the number of bytes read; otherwise, |
50 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read. | 51 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read. |
51 // Partial reads are allowed. Zero is returned on a call to Read when there | 52 // Partial reads are allowed. Zero is returned on a call to Read when there |
52 // are no remaining bytes in the stream, and IsEof() will return true | 53 // are no remaining bytes in the stream, and IsEof() will return true |
53 // hereafter. | 54 // hereafter. |
54 // | 55 // |
55 // If there's less data to read than we initially observed (i.e. the actual | 56 // If there's less data to read than we initially observed (i.e. the actual |
56 // upload data is smaller than size()), zeros are padded to ensure that | 57 // upload data is smaller than size()), zeros are padded to ensure that |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 void SetSize(uint64_t size); | 101 void SetSize(uint64_t size); |
101 | 102 |
102 // Must be called for chunked uploads before the final ReadInternal call | 103 // Must be called for chunked uploads before the final ReadInternal call |
103 // completes. Must not be called for non-chunked uploads. | 104 // completes. Must not be called for non-chunked uploads. |
104 void SetIsFinalChunk(); | 105 void SetIsFinalChunk(); |
105 | 106 |
106 private: | 107 private: |
107 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called | 108 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called |
108 // once it completes. If the upload is not chunked, SetSize must be called | 109 // once it completes. If the upload is not chunked, SetSize must be called |
109 // before it completes. | 110 // before it completes. |
110 virtual int InitInternal() = 0; | 111 virtual int InitInternal(const BoundNetLog& net_log) = 0; |
111 | 112 |
112 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the | 113 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the |
113 // final chunk. For non-chunked uploads, the UploadDataStream determins which | 114 // final chunk. For non-chunked uploads, the UploadDataStream determins which |
114 // read is the last based on size. Must read 1 or more bytes on every call, | 115 // read is the last based on size. Must read 1 or more bytes on every call, |
115 // though the final chunk may be 0 bytes, for chunked requests. If it returns | 116 // though the final chunk may be 0 bytes, for chunked requests. If it returns |
116 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not | 117 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not |
117 // return any error, other than ERR_IO_PENDING. | 118 // return any error, other than ERR_IO_PENDING. |
118 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0; | 119 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0; |
119 | 120 |
120 // Resets state and cancels any pending callbacks. Guaranteed to be called | 121 // Resets state and cancels any pending callbacks. Guaranteed to be called |
121 // before all but the first call to InitInternal. | 122 // before all but the first call to InitInternal. |
122 virtual void ResetInternal() = 0; | 123 virtual void ResetInternal() = 0; |
123 | 124 |
124 uint64_t total_size_; | 125 uint64_t total_size_; |
125 uint64_t current_position_; | 126 uint64_t current_position_; |
126 | 127 |
127 const int64_t identifier_; | 128 const int64_t identifier_; |
128 | 129 |
129 const bool is_chunked_; | 130 const bool is_chunked_; |
130 | 131 |
131 // True if the initialization was successful. | 132 // True if the initialization was successful. |
132 bool initialized_successfully_; | 133 bool initialized_successfully_; |
133 | 134 |
134 bool is_eof_; | 135 bool is_eof_; |
135 | 136 |
136 CompletionCallback callback_; | 137 CompletionCallback callback_; |
137 | 138 |
| 139 BoundNetLog net_log_; |
| 140 |
138 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 141 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
139 }; | 142 }; |
140 | 143 |
141 } // namespace net | 144 } // namespace net |
142 | 145 |
143 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 146 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |