| 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 peek_end_of_chunks() const; |
| 64 |
| 53 private: | 65 private: |
| 54 enum { kBufSize = 16384 }; | 66 enum { kBufSize = 16384 }; |
| 55 | 67 |
| 56 // Protects from public access since now we have a static creator function | 68 // 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. | 69 // which will do both creation and initialization and might return an error. |
| 58 explicit UploadDataStream(UploadData* data); | 70 explicit UploadDataStream(UploadData* data); |
| 59 | 71 |
| 60 // Fills the buffer with any remaining data and sets eof_ if there was nothing | 72 // Fills the buffer with any remaining data and sets eof_ if there was nothing |
| 61 // left to fill the buffer with. | 73 // left to fill the buffer with. |
| 62 // Returns OK if the operation succeeds. Otherwise error code is returned. | 74 // Returns OK if the operation succeeds. Otherwise error code is returned. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 | 104 |
| 93 // Whether there is no data left to read. | 105 // Whether there is no data left to read. |
| 94 bool eof_; | 106 bool eof_; |
| 95 | 107 |
| 96 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 108 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
| 97 }; | 109 }; |
| 98 | 110 |
| 99 } // namespace net | 111 } // namespace net |
| 100 | 112 |
| 101 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 113 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |