Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 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 
      8 #include "net/base/file_stream.h"
      9 #include "net/base/upload_data.h"
     10 
     11 namespace net {
     12 
     13 class IOBuffer;
     14 
     15 class UploadDataStream {
     16  public:
     17   explicit UploadDataStream(const UploadData* data);
     18   ~UploadDataStream();
     19 
     20   // Returns the stream's buffer and buffer length.
     21   IOBuffer* buf() const { return buf_; }
     22   size_t buf_len() const { return buf_len_; }
     23 
     24   // Call to indicate that a portion of the stream's buffer was consumed.  This
     25   // call modifies the stream's buffer so that it contains the next segment of
     26   // the upload data to be consumed.
     27   void DidConsume(size_t num_bytes);
     28 
     29   // Returns the total size of the data stream and the current position.
     30   uint64 size() const { return total_size_; }
     31   uint64 position() const { return current_position_; }
     32 
     33  private:
     34   void FillBuf();
     35 
     36   const UploadData* data_;
     37 
     38   // This buffer is filled with data to be uploaded.  The data to be sent is
     39   // always at the front of the buffer.  If we cannot send all of the buffer at
     40   // once, then we memmove the remaining portion and back-fill the buffer for
     41   // the next "write" call.  buf_len_ indicates how much data is in the buffer.
     42   enum { kBufSize = 16384 };
     43   scoped_refptr<IOBuffer> buf_;
     44   size_t buf_len_;
     45 
     46   // Iterator to the upload element to be written to the send buffer next.
     47   std::vector<UploadData::Element>::const_iterator next_element_;
     48 
     49   // The byte offset into next_element_'s data buffer if the next element is
     50   // a TYPE_BYTES element.
     51   size_t next_element_offset_;
     52 
     53   // A stream to the currently open file, for next_element_ if the next element
     54   // is a TYPE_FILE element.
     55   FileStream next_element_stream_;
     56 
     57   // The number of bytes remaining to be read from the currently open file
     58   // if the next element is of TYPE_FILE.
     59   uint64 next_element_remaining_;
     60 
     61   // Size and current read position within the stream.
     62   uint64 total_size_;
     63   uint64 current_position_;
     64 
     65   DISALLOW_EVIL_CONSTRUCTORS(UploadDataStream);
     66 };
     67 
     68 }  // namespace net
     69 
     70 #endif  // NET_BASE_UPLOAD_DATA_STREAM_H_
     71