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"
17 class DrainableIOBuffer
;
19 class UploadElementReader
;
21 // A class to read all elements from an UploadData object.
22 class NET_EXPORT UploadDataStream
{
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
,
32 // Constructs a chunked data stream.
33 UploadDataStream(Chunked chunked
, int64 identifier
);
37 // Creates UploadDataStream with a reader.
38 static UploadDataStream
* CreateWithReader(
39 scoped_ptr
<UploadElementReader
> reader
,
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
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
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
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
);
99 // Resets this instance to the uninitialized state.
102 // Runs Init() for all element readers.
103 // This method is used to implement Init().
104 int InitInternal(int start_index
, const CompletionCallback
& callback
);
106 // Resumes initialization and runs callback with the result when necessary.
107 void ResumePendingInit(int start_index
,
108 const CompletionCallback
& callback
,
109 int previous_result
);
111 // Reads data from the element readers.
112 // This method is used to implement Read().
113 int ReadInternal(scoped_refptr
<DrainableIOBuffer
> buf
,
114 const CompletionCallback
& callback
);
116 // Resumes pending read and calls callback with the result when necessary.
117 void ResumePendingRead(scoped_refptr
<DrainableIOBuffer
> buf
,
118 const CompletionCallback
& callback
,
119 int previous_result
);
121 // Processes result of UploadElementReader::Read(). If |result| indicates
122 // success, updates |buf|'s offset. Otherwise, sets |read_failed_| to true.
123 void ProcessReadResult(scoped_refptr
<DrainableIOBuffer
> buf
,
126 ScopedVector
<UploadElementReader
> element_readers_
;
128 // Index of the current upload element (i.e. the element currently being
129 // read). The index is used as a cursor to iterate over elements in
131 size_t element_index_
;
133 // Size and current read position within the upload data stream.
134 // |total_size_| is set to zero when the data is chunked.
136 uint64 current_position_
;
138 const int64 identifier_
;
140 const bool is_chunked_
;
141 bool last_chunk_appended_
;
143 // True if an error occcured during read operation.
146 // True if the initialization was successful.
147 bool initialized_successfully_
;
149 // Callback to resume reading chunked data.
150 base::Closure pending_chunked_read_callback_
;
152 base::WeakPtrFactory
<UploadDataStream
> weak_ptr_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(UploadDataStream
);
159 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_