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