1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ 6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ 7 #pragma once 8 9 #include "base/memory/scoped_ptr.h" 10 #include "net/base/upload_data.h" 11 12 #ifdef ANDROID 13 #include "android/jni/platform_file_jni.h" 14 #endif 15 16 namespace net { 17 18 class FileStream; 19 class IOBuffer; 20 21 class UploadDataStream { 22 public: 23 ~UploadDataStream(); 24 25 // Returns a new instance of UploadDataStream if it can be created and 26 // initialized successfully. If not, NULL will be returned and the error 27 // code will be set if the output parameter error_code is not empty. 28 static UploadDataStream* Create(UploadData* data, int* error_code); 29 30 // Returns the stream's buffer and buffer length. 31 IOBuffer* buf() const { return buf_; } 32 size_t buf_len() const { return buf_len_; } 33 34 // TODO(satish): We should ideally have UploadDataStream expose a Read() 35 // method which returns data in a caller provided IOBuffer. That would do away 36 // with this method and make the interface cleaner as well with less memmove 37 // calls. 38 size_t GetMaxBufferSize() const { return kBufSize; } 39 40 // Call to indicate that a portion of the stream's buffer was consumed. This 41 // call modifies the stream's buffer so that it contains the next segment of 42 // the upload data to be consumed. 43 void MarkConsumedAndFillBuffer(size_t num_bytes); 44 45 // Sets the callback to be invoked when new chunks are available to upload. 46 void set_chunk_callback(ChunkCallback* callback) { 47 data_->set_chunk_callback(callback); 48 } 49 50 // Returns the total size of the data stream and the current position. 51 // size() is not to be used to determine whether the stream has ended 52 // because it is possible for the stream to end before its size is reached, 53 // for example, if the file is truncated. 54 uint64 size() const { return total_size_; } 55 uint64 position() const { return current_position_; } 56 57 bool is_chunked() const { return data_->is_chunked(); } 58 59 // Returns whether there is no more data to read, regardless of whether 60 // position < size. 61 bool eof() const { return eof_; } 62 63 // Returns whether the data available in buf() includes the last chunk in a 64 // chunked data stream. This method returns true once the final chunk has been 65 // placed in the IOBuffer returned by buf(), in contrast to eof() which 66 // returns true only after the data in buf() has been consumed. 67 bool IsOnLastChunk() const; 68 69 #if defined(UNIT_TEST) 70 static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } 71 #endif 72 73 private: 74 enum { kBufSize = 16384 }; 75 76 // Protects from public access since now we have a static creator function 77 // which will do both creation and initialization and might return an error. 78 explicit UploadDataStream(UploadData* data); 79 80 // Fills the buffer with any remaining data and sets eof_ if there was nothing 81 // left to fill the buffer with. 82 // Returns OK if the operation succeeds. Otherwise error code is returned. 83 int FillBuf(); 84 85 scoped_refptr<UploadData> data_; 86 87 // This buffer is filled with data to be uploaded. The data to be sent is 88 // always at the front of the buffer. If we cannot send all of the buffer at 89 // once, then we memmove the remaining portion and back-fill the buffer for 90 // the next "write" call. buf_len_ indicates how much data is in the buffer. 91 scoped_refptr<IOBuffer> buf_; 92 size_t buf_len_; 93 94 // Index of the upload element to be written to the send buffer next. 95 size_t next_element_; 96 97 // The byte offset into next_element_'s data buffer if the next element is 98 // a TYPE_BYTES element. 99 size_t next_element_offset_; 100 101 // A stream to the currently open file, for next_element_ if the next element 102 // is a TYPE_FILE element. 103 scoped_ptr<FileStream> next_element_stream_; 104 105 #ifdef ANDROID 106 scoped_ptr<android::JavaISWrapper> next_element_java_stream_; 107 #endif 108 109 // The number of bytes remaining to be read from the currently open file 110 // if the next element is of TYPE_FILE. 111 uint64 next_element_remaining_; 112 113 // Size and current read position within the stream. 114 uint64 total_size_; 115 uint64 current_position_; 116 117 // Whether there is no data left to read. 118 bool eof_; 119 120 // TODO(satish): Remove this once we have a better way to unit test POST 121 // requests with chunked uploads. 122 static bool merge_chunks_; 123 124 DISALLOW_COPY_AND_ASSIGN(UploadDataStream); 125 }; 126 127 } // namespace net 128 129 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 130