OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "net/base/upload_data.h" | 10 #include "net/base/upload_data.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 // code will be set if the output parameter error_code is not empty. | 23 // code will be set if the output parameter error_code is not empty. |
24 static UploadDataStream* Create(UploadData* data, int* error_code); | 24 static UploadDataStream* Create(UploadData* data, int* error_code); |
25 | 25 |
26 // Returns the stream's buffer and buffer length. | 26 // Returns the stream's buffer and buffer length. |
27 IOBuffer* buf() const { return buf_; } | 27 IOBuffer* buf() const { return buf_; } |
28 size_t buf_len() const { return buf_len_; } | 28 size_t buf_len() const { return buf_len_; } |
29 | 29 |
30 // Call to indicate that a portion of the stream's buffer was consumed. This | 30 // Call to indicate that a portion of the stream's buffer was consumed. This |
31 // call modifies the stream's buffer so that it contains the next segment of | 31 // call modifies the stream's buffer so that it contains the next segment of |
32 // the upload data to be consumed. | 32 // the upload data to be consumed. |
33 void DidConsume(size_t num_bytes); | 33 void MarkConsumedAndFillBuffer(size_t num_bytes); |
| 34 |
| 35 // Sets the callback to be invoked when new chunks are available to upload. |
| 36 void set_chunk_callback(ChunkCallback* callback) { |
| 37 data_->set_chunk_callback(callback); |
| 38 } |
34 | 39 |
35 // Returns the total size of the data stream and the current position. | 40 // Returns the total size of the data stream and the current position. |
36 // size() is not to be used to determine whether the stream has ended | 41 // size() is not to be used to determine whether the stream has ended |
37 // because it is possible for the stream to end before its size is reached, | 42 // because it is possible for the stream to end before its size is reached, |
38 // for example, if the file is truncated. | 43 // for example, if the file is truncated. |
39 uint64 size() const { return total_size_; } | 44 uint64 size() const { return total_size_; } |
40 uint64 position() const { return current_position_; } | 45 uint64 position() const { return current_position_; } |
41 | 46 |
| 47 bool is_chunked() const { return data_->is_chunked(); } |
| 48 |
42 // Returns whether there is no more data to read, regardless of whether | 49 // Returns whether there is no more data to read, regardless of whether |
43 // position < size. | 50 // position < size. |
44 bool eof() const { return eof_; } | 51 bool eof() const { return eof_; } |
45 | 52 |
46 private: | 53 private: |
47 enum { kBufSize = 16384 }; | 54 enum { kBufSize = 16384 }; |
48 | 55 |
49 // Protects from public access since now we have a static creator function | 56 // Protects from public access since now we have a static creator function |
50 // which will do both creation and initialization and might return an error. | 57 // which will do both creation and initialization and might return an error. |
51 explicit UploadDataStream(UploadData* data); | 58 explicit UploadDataStream(UploadData* data); |
52 | 59 |
53 // Fills the buffer with any remaining data and sets eof_ if there was nothing | 60 // Fills the buffer with any remaining data and sets eof_ if there was nothing |
54 // left to fill the buffer with. | 61 // left to fill the buffer with. |
55 // Returns OK if the operation succeeds. Otherwise error code is returned. | 62 // Returns OK if the operation succeeds. Otherwise error code is returned. |
56 int FillBuf(); | 63 int FillBuf(); |
57 | 64 |
58 UploadData* data_; | 65 UploadData* data_; |
59 | 66 |
60 // This buffer is filled with data to be uploaded. The data to be sent is | 67 // This buffer is filled with data to be uploaded. The data to be sent is |
61 // always at the front of the buffer. If we cannot send all of the buffer at | 68 // always at the front of the buffer. If we cannot send all of the buffer at |
62 // once, then we memmove the remaining portion and back-fill the buffer for | 69 // once, then we memmove the remaining portion and back-fill the buffer for |
63 // the next "write" call. buf_len_ indicates how much data is in the buffer. | 70 // the next "write" call. buf_len_ indicates how much data is in the buffer. |
64 scoped_refptr<IOBuffer> buf_; | 71 scoped_refptr<IOBuffer> buf_; |
65 size_t buf_len_; | 72 size_t buf_len_; |
66 | 73 |
67 // Iterator to the upload element to be written to the send buffer next. | 74 // Index of the upload element to be written to the send buffer next. |
68 std::vector<UploadData::Element>::iterator next_element_; | 75 size_t next_element_; |
69 | 76 |
70 // The byte offset into next_element_'s data buffer if the next element is | 77 // The byte offset into next_element_'s data buffer if the next element is |
71 // a TYPE_BYTES element. | 78 // a TYPE_BYTES element. |
72 size_t next_element_offset_; | 79 size_t next_element_offset_; |
73 | 80 |
74 // A stream to the currently open file, for next_element_ if the next element | 81 // A stream to the currently open file, for next_element_ if the next element |
75 // is a TYPE_FILE element. | 82 // is a TYPE_FILE element. |
76 scoped_ptr<FileStream> next_element_stream_; | 83 scoped_ptr<FileStream> next_element_stream_; |
77 | 84 |
78 // The number of bytes remaining to be read from the currently open file | 85 // The number of bytes remaining to be read from the currently open file |
79 // if the next element is of TYPE_FILE. | 86 // if the next element is of TYPE_FILE. |
80 uint64 next_element_remaining_; | 87 uint64 next_element_remaining_; |
81 | 88 |
82 // Size and current read position within the stream. | 89 // Size and current read position within the stream. |
83 uint64 total_size_; | 90 uint64 total_size_; |
84 uint64 current_position_; | 91 uint64 current_position_; |
85 | 92 |
86 // Whether there is no data left to read. | 93 // Whether there is no data left to read. |
87 bool eof_; | 94 bool eof_; |
88 | 95 |
89 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 96 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
90 }; | 97 }; |
91 | 98 |
92 } // namespace net | 99 } // namespace net |
93 | 100 |
94 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 101 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |