Updating trunk VERSION from 1014.0 to 1015.0
[chromium-blink-merge.git] / net / base / upload_data_stream.h
blobdf21e639a33dfb17633c197274e700d6497a6edb
1 // Copyright (c) 2011 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_
7 #pragma once
9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/net_export.h"
11 #include "net/base/upload_data.h"
13 namespace net {
15 class FileStream;
16 class IOBuffer;
18 class NET_EXPORT UploadDataStream {
19 public:
20 ~UploadDataStream();
22 // Returns a new instance of UploadDataStream if it can be created and
23 // initialized successfully. If not, NULL will be returned and the error
24 // code will be set if the output parameter error_code is not empty.
25 static UploadDataStream* Create(UploadData* data, int* error_code);
27 // Returns the stream's buffer and buffer length.
28 IOBuffer* buf() const { return buf_; }
29 size_t buf_len() const { return buf_len_; }
31 // TODO(satish): We should ideally have UploadDataStream expose a Read()
32 // method which returns data in a caller provided IOBuffer. That would do away
33 // with this method and make the interface cleaner as well with less memmove
34 // calls.
35 size_t GetMaxBufferSize() const { return kBufSize; }
37 // Call to indicate that a portion of the stream's buffer was consumed. This
38 // call modifies the stream's buffer so that it contains the next segment of
39 // the upload data to be consumed.
40 void MarkConsumedAndFillBuffer(size_t num_bytes);
42 // Sets the callback to be invoked when new chunks are available to upload.
43 void set_chunk_callback(ChunkCallback* callback) {
44 data_->set_chunk_callback(callback);
47 // Returns the total size of the data stream and the current position.
48 // size() is not to be used to determine whether the stream has ended
49 // because it is possible for the stream to end before its size is reached,
50 // for example, if the file is truncated.
51 uint64 size() const { return total_size_; }
52 uint64 position() const { return current_position_; }
54 bool is_chunked() const { return data_->is_chunked(); }
56 // Returns whether there is no more data to read, regardless of whether
57 // position < size.
58 bool eof() const { return eof_; }
60 // Returns whether the data available in buf() includes the last chunk in a
61 // chunked data stream. This method returns true once the final chunk has been
62 // placed in the IOBuffer returned by buf(), in contrast to eof() which
63 // returns true only after the data in buf() has been consumed.
64 bool IsOnLastChunk() const;
66 // This method is provided only to be used by unit tests.
67 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
69 private:
70 enum { kBufSize = 16384 };
72 // Protects from public access since now we have a static creator function
73 // which will do both creation and initialization and might return an error.
74 explicit UploadDataStream(UploadData* data);
76 // Fills the buffer with any remaining data and sets eof_ if there was nothing
77 // left to fill the buffer with.
78 // Returns OK if the operation succeeds. Otherwise error code is returned.
79 int FillBuf();
81 scoped_refptr<UploadData> data_;
83 // This buffer is filled with data to be uploaded. The data to be sent is
84 // always at the front of the buffer. If we cannot send all of the buffer at
85 // once, then we memmove the remaining portion and back-fill the buffer for
86 // the next "write" call. buf_len_ indicates how much data is in the buffer.
87 scoped_refptr<IOBuffer> buf_;
88 size_t buf_len_;
90 // Index of the upload element to be written to the send buffer next.
91 size_t next_element_;
93 // The byte offset into next_element_'s data buffer if the next element is
94 // a TYPE_BYTES element.
95 size_t next_element_offset_;
97 // A stream to the currently open file, for next_element_ if the next element
98 // is a TYPE_FILE element.
99 scoped_ptr<FileStream> next_element_stream_;
101 // The number of bytes remaining to be read from the currently open file
102 // if the next element is of TYPE_FILE.
103 uint64 next_element_remaining_;
105 // Size and current read position within the stream.
106 uint64 total_size_;
107 uint64 current_position_;
109 // Whether there is no data left to read.
110 bool eof_;
112 // TODO(satish): Remove this once we have a better way to unit test POST
113 // requests with chunked uploads.
114 static bool merge_chunks_;
116 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
119 } // namespace net
121 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_