Move pending tile priorities to active on tree activation
[chromium-blink-merge.git] / net / base / upload_data_stream.h
blobc527e9e4e90ef61fdece161f2c9f568db9ada054
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_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/net_export.h"
15 namespace net {
17 class DrainableIOBuffer;
18 class IOBuffer;
19 class UploadElementReader;
21 // A class to read all elements from an UploadData object.
22 class NET_EXPORT UploadDataStream {
23 public:
24 // An enum used to construct chunked data stream.
25 enum Chunked { CHUNKED };
27 // Constructs a non-chunked data stream.
28 // |element_readers| is cleared by this ctor.
29 UploadDataStream(ScopedVector<UploadElementReader>* element_readers,
30 int64 identifier);
32 // Constructs a chunked data stream.
33 UploadDataStream(Chunked chunked, int64 identifier);
35 ~UploadDataStream();
37 // Creates UploadDataStream with a reader.
38 static UploadDataStream* CreateWithReader(
39 scoped_ptr<UploadElementReader> reader,
40 int64 identifier);
42 // Initializes the stream. This function must be called before calling any
43 // other method. It is not valid to call any method (other than the
44 // destructor) if Init() returns a failure. This method can be called multiple
45 // times. Calling this method after a Init() success results in resetting the
46 // state.
48 // Does the initialization synchronously and returns the result if possible,
49 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
51 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
52 // file modification time is set (usually not set, but set for sliced
53 // files) and the target file is changed.
54 int Init(const CompletionCallback& callback);
56 // When possible, reads up to |buf_len| bytes synchronously from the upload
57 // data stream to |buf| and returns the number of bytes read; otherwise,
58 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
59 // Partial reads are allowed. Zero is returned on a call to Read when there
60 // are no remaining bytes in the stream, and IsEof() will return true
61 // hereafter.
63 // If there's less data to read than we initially observed (i.e. the actual
64 // upload data is smaller than size()), zeros are padded to ensure that
65 // size() bytes can be read, which can happen for TYPE_FILE payloads.
66 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
68 // Identifies a particular upload instance, which is used by the cache to
69 // formulate a cache key. This value should be unique across browser
70 // sessions. A value of 0 is used to indicate an unspecified identifier.
71 int64 identifier() const { return identifier_; }
73 // Returns the total size of the data stream and the current position.
74 // size() is not to be used to determine whether the stream has ended
75 // because it is possible for the stream to end before its size is reached,
76 // for example, if the file is truncated. When the data is chunked, size()
77 // always returns zero.
78 uint64 size() const { return total_size_; }
79 uint64 position() const { return current_position_; }
81 bool is_chunked() const { return is_chunked_; }
82 bool last_chunk_appended() const { return last_chunk_appended_; }
84 const ScopedVector<UploadElementReader>& element_readers() const {
85 return element_readers_;
88 // Returns true if all data has been consumed from this upload data
89 // stream.
90 bool IsEOF() const;
92 // Returns true if the upload data in the stream is entirely in memory.
93 bool IsInMemory() const;
95 // Adds the given chunk of bytes to be sent with chunked transfer encoding.
96 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
98 private:
99 friend class SpdyHttpStreamSpdy2Test;
100 friend class SpdyHttpStreamSpdy3Test;
101 friend class SpdyNetworkTransactionSpdy2Test;
102 friend class SpdyNetworkTransactionSpdy3Test;
104 // Resets this instance to the uninitialized state.
105 void Reset();
107 // Runs Init() for all element readers.
108 // This method is used to implement Init().
109 int InitInternal(int start_index, const CompletionCallback& callback);
111 // Resumes initialization and runs callback with the result when necessary.
112 void ResumePendingInit(int start_index,
113 const CompletionCallback& callback,
114 int previous_result);
116 // Reads data from the element readers.
117 // This method is used to implement Read().
118 int ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
119 const CompletionCallback& callback);
121 // Resumes pending read and calls callback with the result when necessary.
122 void ResumePendingRead(scoped_refptr<DrainableIOBuffer> buf,
123 const CompletionCallback& callback,
124 int previous_result);
126 // Processes result of UploadElementReader::Read(). If |result| indicates
127 // success, updates |buf|'s offset. Otherwise, sets |read_failed_| to true.
128 void ProcessReadResult(scoped_refptr<DrainableIOBuffer> buf,
129 int result);
131 // These methods are provided only to be used by unit tests.
132 static void ResetMergeChunks();
133 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; }
135 ScopedVector<UploadElementReader> element_readers_;
137 // Index of the current upload element (i.e. the element currently being
138 // read). The index is used as a cursor to iterate over elements in
139 // |upload_data_|.
140 size_t element_index_;
142 // Size and current read position within the upload data stream.
143 // |total_size_| is set to zero when the data is chunked.
144 uint64 total_size_;
145 uint64 current_position_;
147 const int64 identifier_;
149 const bool is_chunked_;
150 bool last_chunk_appended_;
152 // True if an error occcured during read operation.
153 bool read_failed_;
155 // True if the initialization was successful.
156 bool initialized_successfully_;
158 // Callback to resume reading chunked data.
159 base::Closure pending_chunked_read_callback_;
161 base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_;
163 // TODO(satish): Remove this once we have a better way to unit test POST
164 // requests with chunked uploads.
165 static bool merge_chunks_;
167 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
170 } // namespace net
172 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_