Chromium Code Reviews| 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/completion_callback.h" | |
| 10 #include "net/base/upload_data.h" | 11 #include "net/base/upload_data.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 class FileStream; | 15 class FileStream; |
| 15 class IOBuffer; | 16 class IOBuffer; |
| 16 | 17 |
| 17 class UploadDataStream { | 18 class UploadDataStream : public UploadData::ChunkCallback { |
| 18 public: | 19 public: |
| 20 // Interface implemented by callers who require callbacks when new chunks | |
| 21 // of data are received. | |
| 22 class ChunkCallback { | |
|
vandebo (ex-Chrome)
2011/01/14 05:53:44
This interface class is the same as UploadData::Ch
Satish
2011/01/14 18:09:29
Wan-Teh suggests I rename this to Delegate. I'm no
| |
| 23 public: | |
| 24 // Invoked when a new data chunk was given for a chunked transfer upload. | |
| 25 virtual void OnChunkAvailable() = 0; | |
| 26 | |
| 27 protected: | |
| 28 virtual ~ChunkCallback() {} | |
| 29 }; | |
| 30 | |
| 19 // Returns a new instance of UploadDataStream if it can be created and | 31 // Returns a new instance of UploadDataStream if it can be created and |
| 20 // initialized successfully. If not, NULL will be returned and the error | 32 // initialized successfully. If not, NULL will be returned and the error |
| 21 // code will be set if the output parameter error_code is not empty. | 33 // code will be set if the output parameter error_code is not empty. |
| 22 static UploadDataStream* Create(UploadData* data, int* error_code); | 34 static UploadDataStream* Create(UploadData* data, int* error_code); |
| 23 | 35 |
| 24 ~UploadDataStream(); | 36 ~UploadDataStream(); |
| 25 | 37 |
| 26 // Returns the stream's buffer and buffer length. | 38 // Returns the stream's buffer and buffer length. |
| 27 IOBuffer* buf() const { return buf_; } | 39 IOBuffer* buf() const { return buf_; } |
| 28 size_t buf_len() const { return buf_len_; } | 40 size_t buf_len() const { return buf_len_; } |
| 29 | 41 |
| 30 // Call to indicate that a portion of the stream's buffer was consumed. This | 42 // 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 | 43 // call modifies the stream's buffer so that it contains the next segment of |
| 32 // the upload data to be consumed. | 44 // the upload data to be consumed. |
| 33 void DidConsume(size_t num_bytes); | 45 void DidConsume(size_t num_bytes); |
| 34 | 46 |
| 47 // Sets the callback to be invoked when new data is available to upload. | |
| 48 // The callback is invoked only once and needs to be set again if the caller | |
| 49 // needs to be indicated again in future. | |
| 50 void set_chunk_callback(ChunkCallback* callback); | |
| 51 | |
| 35 // Returns the total size of the data stream and the current position. | 52 // 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 | 53 // 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, | 54 // because it is possible for the stream to end before its size is reached, |
| 38 // for example, if the file is truncated. | 55 // for example, if the file is truncated. |
| 39 uint64 size() const { return total_size_; } | 56 uint64 size() const { return total_size_; } |
| 40 uint64 position() const { return current_position_; } | 57 uint64 position() const { return current_position_; } |
| 41 | 58 |
| 59 bool is_chunked() const { return data_->is_chunked(); } | |
| 60 | |
| 42 // Returns whether there is no more data to read, regardless of whether | 61 // Returns whether there is no more data to read, regardless of whether |
| 43 // position < size. | 62 // position < size. |
| 44 bool eof() const { return eof_; } | 63 bool eof() const { return eof_; } |
| 45 | 64 |
| 65 // UploadData::ChunkCallback members. | |
| 66 | |
| 67 // Callback invoked by UploadData when new data is available. | |
| 68 void OnChunkAvailable(); | |
| 69 | |
| 46 private: | 70 private: |
| 47 // Protects from public access since now we have a static creator function | 71 // Protects from public access since now we have a static creator function |
| 48 // which will do both creation and initialization and might return an error. | 72 // which will do both creation and initialization and might return an error. |
| 49 explicit UploadDataStream(UploadData* data); | 73 explicit UploadDataStream(UploadData* data); |
| 50 | 74 |
| 51 // Fills the buffer with any remaining data and sets eof_ if there was nothing | 75 // Fills the buffer with any remaining data and sets eof_ if there was nothing |
| 52 // left to fill the buffer with. | 76 // left to fill the buffer with. |
| 53 // Returns OK if the operation succeeds. Otherwise error code is returned. | 77 // Returns OK if the operation succeeds. Otherwise error code is returned. |
| 54 int FillBuf(); | 78 int FillBuf(); |
| 55 | 79 |
| 56 UploadData* data_; | 80 UploadData* data_; |
| 57 | 81 |
| 58 // This buffer is filled with data to be uploaded. The data to be sent is | 82 // This buffer is filled with data to be uploaded. The data to be sent is |
| 59 // always at the front of the buffer. If we cannot send all of the buffer at | 83 // always at the front of the buffer. If we cannot send all of the buffer at |
| 60 // once, then we memmove the remaining portion and back-fill the buffer for | 84 // once, then we memmove the remaining portion and back-fill the buffer for |
| 61 // the next "write" call. buf_len_ indicates how much data is in the buffer. | 85 // the next "write" call. buf_len_ indicates how much data is in the buffer. |
| 62 enum { kBufSize = 16384 }; | 86 enum { kBufSize = 16384 }; |
| 63 scoped_refptr<IOBuffer> buf_; | 87 scoped_refptr<IOBuffer> buf_; |
| 64 size_t buf_len_; | 88 size_t buf_len_; |
| 65 | 89 |
| 66 // Iterator to the upload element to be written to the send buffer next. | 90 // Index of the upload element to be written to the send buffer next. |
| 67 std::vector<UploadData::Element>::iterator next_element_; | 91 size_t next_element_; |
|
wtc
2011/01/14 03:09:31
Why does next_element_ need to be an array index?
Satish
2011/01/14 18:09:29
Yes
| |
| 68 | 92 |
| 69 // The byte offset into next_element_'s data buffer if the next element is | 93 // The byte offset into next_element_'s data buffer if the next element is |
| 70 // a TYPE_BYTES element. | 94 // a TYPE_BYTES element. |
| 71 size_t next_element_offset_; | 95 size_t next_element_offset_; |
| 72 | 96 |
| 73 // A stream to the currently open file, for next_element_ if the next element | 97 // A stream to the currently open file, for next_element_ if the next element |
| 74 // is a TYPE_FILE element. | 98 // is a TYPE_FILE element. |
| 75 scoped_ptr<FileStream> next_element_stream_; | 99 scoped_ptr<FileStream> next_element_stream_; |
| 76 | 100 |
| 77 // The number of bytes remaining to be read from the currently open file | 101 // The number of bytes remaining to be read from the currently open file |
| 78 // if the next element is of TYPE_FILE. | 102 // if the next element is of TYPE_FILE. |
| 79 uint64 next_element_remaining_; | 103 uint64 next_element_remaining_; |
| 80 | 104 |
| 81 // Size and current read position within the stream. | 105 // Size and current read position within the stream. |
| 82 uint64 total_size_; | 106 uint64 total_size_; |
| 83 uint64 current_position_; | 107 uint64 current_position_; |
| 84 | 108 |
| 109 // Whether we have run out of data and waiting for more to come in. | |
| 110 bool waiting_for_data_; | |
| 111 | |
| 112 // Callback to be invoked by us when new data is available. | |
| 113 ChunkCallback* chunk_callback_; | |
| 114 | |
| 85 // Whether there is no data left to read. | 115 // Whether there is no data left to read. |
| 86 bool eof_; | 116 bool eof_; |
| 87 | 117 |
| 88 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); | 118 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); |
| 89 }; | 119 }; |
| 90 | 120 |
| 91 } // namespace net | 121 } // namespace net |
| 92 | 122 |
| 93 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ | 123 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |
| OLD | NEW |