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
3 // found in the LICENSE file.
5 #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_
6 #define NET_BASE_UPLOAD_DATA_STREAM_H_
9 #include "base/memory/ref_counted.h"
10 #include "net/base/net_export.h"
11 #include "net/base/upload_data.h"
18 class NET_EXPORT UploadDataStream
{
20 explicit UploadDataStream(UploadData
* upload_data
);
23 // Initializes the stream. This function must be called exactly once,
24 // before calling any other method. It is not valid to call any method
25 // (other than the destructor) if Init() returns a failure.
27 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
28 // file modification time is set (usually not set, but set for sliced
29 // files) and the target file is changed.
32 // Reads up to |buf_len| bytes from the upload data stream to |buf|. The
33 // number of bytes read is returned. Partial reads are allowed. Zero is
34 // returned on a call to Read when there are no remaining bytes in the
35 // stream, and IsEof() will return true hereafter.
37 // If there's less data to read than we initially observed (i.e. the actual
38 // upload data is smaller than size()), zeros are padded to ensure that
39 // size() bytes can be read, which can happen for TYPE_FILE payloads.
41 // If the upload data stream is chunked (i.e. is_chunked() is true),
42 // ERR_IO_PENDING is returned to indicate there is nothing to read at the
43 // moment, but more data to come at a later time. If not chunked, reads
45 int Read(IOBuffer
* buf
, int buf_len
);
47 // Sets the callback to be invoked when new chunks are available to upload.
48 void set_chunk_callback(ChunkCallback
* callback
) {
49 upload_data_
->set_chunk_callback(callback
);
52 // Returns the total size of the data stream and the current position.
53 // size() is not to be used to determine whether the stream has ended
54 // because it is possible for the stream to end before its size is reached,
55 // for example, if the file is truncated.
56 uint64
size() const { return total_size_
; }
57 uint64
position() const { return current_position_
; }
59 bool is_chunked() const { return upload_data_
->is_chunked(); }
61 // Returns true if all data has been consumed from this upload data
65 // Returns true if the upload data in the stream is entirely in memory.
66 bool IsInMemory() const;
68 // This method is provided only to be used by unit tests.
69 static void set_merge_chunks(bool merge
) { merge_chunks_
= merge
; }
72 scoped_refptr
<UploadData
> upload_data_
;
74 // Index of the current upload element (i.e. the element currently being
75 // read). The index is used as a cursor to iterate over elements in
77 size_t element_index_
;
79 // Size and current read position within the upload data stream.
81 uint64 current_position_
;
83 // True if the initialization was successful.
84 bool initialized_successfully_
;
86 // TODO(satish): Remove this once we have a better way to unit test POST
87 // requests with chunked uploads.
88 static bool merge_chunks_
;
90 DISALLOW_COPY_AND_ASSIGN(UploadDataStream
);
95 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_