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" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 class FileStream; | 14 class FileStream; |
15 class IOBuffer; | 15 class IOBuffer; |
16 | 16 |
17 class UploadDataStream { | 17 class UploadDataStream { |
18 public: | 18 public: |
19 ~UploadDataStream(); | 19 ~UploadDataStream(); |
20 | 20 |
21 // Returns a new instance of UploadDataStream if it can be created and | 21 // Returns a new instance of UploadDataStream if it can be created and |
22 // initialized successfully. If not, NULL will be returned and the error | 22 // initialized successfully. If not, NULL will be returned and the error |
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 // TODO(satish): We should ideally have UploadDataStream expose a Read() | |
31 // method which returns data in a caller provided IOBuffer. That would do away | |
32 // with this method and make the interface cleaner as well with less memmove | |
33 // calls. | |
34 size_t GetMaxBufferSize() const { return kBufSize; } | |
35 | |
30 // Call to indicate that a portion of the stream's buffer was consumed. This | 36 // 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 | 37 // call modifies the stream's buffer so that it contains the next segment of |
32 // the upload data to be consumed. | 38 // the upload data to be consumed. |
33 void MarkConsumedAndFillBuffer(size_t num_bytes); | 39 void MarkConsumedAndFillBuffer(size_t num_bytes); |
34 | 40 |
35 // Sets the callback to be invoked when new chunks are available to upload. | 41 // Sets the callback to be invoked when new chunks are available to upload. |
36 void set_chunk_callback(ChunkCallback* callback) { | 42 void set_chunk_callback(ChunkCallback* callback) { |
37 data_->set_chunk_callback(callback); | 43 data_->set_chunk_callback(callback); |
38 } | 44 } |
39 | 45 |
40 // Returns the total size of the data stream and the current position. | 46 // Returns the total size of the data stream and the current position. |
41 // size() is not to be used to determine whether the stream has ended | 47 // size() is not to be used to determine whether the stream has ended |
42 // because it is possible for the stream to end before its size is reached, | 48 // because it is possible for the stream to end before its size is reached, |
43 // for example, if the file is truncated. | 49 // for example, if the file is truncated. |
44 uint64 size() const { return total_size_; } | 50 uint64 size() const { return total_size_; } |
45 uint64 position() const { return current_position_; } | 51 uint64 position() const { return current_position_; } |
46 | 52 |
47 bool is_chunked() const { return data_->is_chunked(); } | 53 bool is_chunked() const { return data_->is_chunked(); } |
48 | 54 |
49 // Returns whether there is no more data to read, regardless of whether | 55 // Returns whether there is no more data to read, regardless of whether |
50 // position < size. | 56 // position < size. |
51 bool eof() const { return eof_; } | 57 bool eof() const { return eof_; } |
52 | 58 |
59 // Returns whether the data available in buf() includes the last chunk in a | |
60 // chunked data stream. This method returns true once the final chunk has been | |
61 // placed in the IOBuffer returned by buf(), in contrast to eof() which | |
62 // returns true only after the data in buf() has been consumed. | |
63 bool IsOnLastChunk() const; | |
64 | |
65 #if UNIT_TEST | |
willchan no longer on Chromium
2011/03/03 23:42:32
most chromium code does defined(UNIT_TEST), not ju
Satish
2011/03/03 23:50:59
Will do.
| |
66 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } | |
67 #endif | |
68 | |
53 private: | 69 private: |
54 enum { kBufSize = 16384 }; | 70 enum { kBufSize = 16384 }; |
55 | 71 |
56 // Protects from public access since now we have a static creator function | 72 // Protects from public access since now we have a static creator function |
57 // which will do both creation and initialization and might return an error. | 73 // which will do both creation and initialization and might return an error. |
58 explicit UploadDataStream(UploadData* data); | 74 explicit UploadDataStream(UploadData* data); |
59 | 75 |
60 // Fills the buffer with any remaining data and sets eof_ if there was nothing | 76 // Fills the buffer with any remaining data and sets eof_ if there was nothing |
61 // left to fill the buffer with. | 77 // left to fill the buffer with. |
62 // Returns OK if the operation succeeds. Otherwise error code is returned. | 78 // Returns OK if the operation succeeds. Otherwise error code is returned. |
(...skipping 23 matching lines...) Expand all Loading... | |
86 // if the next element is of TYPE_FILE. | 102 // if the next element is of TYPE_FILE. |
87 uint64 next_element_remaining_; | 103 uint64 next_element_remaining_; |
88 | 104 |
89 // Size and current read position within the stream. | 105 // Size and current read position within the stream. |
90 uint64 total_size_; | 106 uint64 total_size_; |
91 uint64 current_position_; | 107 uint64 current_position_; |
92 | 108 |
93 // Whether there is no data left to read. | 109 // Whether there is no data left to read. |
94 bool eof_; | 110 bool eof_; |
95 | 111 |
112 static bool merge_chunks_; | |
113 | |
96 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 114 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
97 }; | 115 }; |
98 | 116 |
99 } // namespace net | 117 } // namespace net |
100 | 118 |
101 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 119 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
OLD | NEW |