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_H_
      6 #define NET_BASE_UPLOAD_DATA_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/file_path.h"
     11 #include "base/ref_counted.h"
     12 
     13 namespace net {
     14 
     15 class UploadData : public base::RefCounted<UploadData> {
     16  public:
     17   UploadData() : identifier_(0) {}
     18 
     19   enum Type {
     20     TYPE_BYTES,
     21     TYPE_FILE
     22   };
     23 
     24   class Element {
     25    public:
     26     Element() : type_(TYPE_BYTES), file_range_offset_(0),
     27                 file_range_length_(kuint64max) {
     28     }
     29 
     30     Type type() const { return type_; }
     31     const std::vector<char>& bytes() const { return bytes_; }
     32     const FilePath& file_path() const { return file_path_; }
     33     uint64 file_range_offset() const { return file_range_offset_; }
     34     uint64 file_range_length() const { return file_range_length_; }
     35 
     36     void SetToBytes(const char* bytes, int bytes_len) {
     37       type_ = TYPE_BYTES;
     38       bytes_.assign(bytes, bytes + bytes_len);
     39     }
     40 
     41     void SetToFilePath(const FilePath& path) {
     42       SetToFilePathRange(path, 0, kuint64max);
     43     }
     44 
     45     void SetToFilePathRange(const FilePath& path,
     46                             uint64 offset, uint64 length) {
     47       type_ = TYPE_FILE;
     48       file_path_ = path;
     49       file_range_offset_ = offset;
     50       file_range_length_ = length;
     51     }
     52 
     53     // Returns the byte-length of the element.  For files that do not exist, 0
     54     // is returned.  This is done for consistency with Mozilla.
     55     uint64 GetContentLength() const;
     56 
     57    private:
     58     Type type_;
     59     std::vector<char> bytes_;
     60     FilePath file_path_;
     61     uint64 file_range_offset_;
     62     uint64 file_range_length_;
     63   };
     64 
     65   void AppendBytes(const char* bytes, int bytes_len) {
     66     if (bytes_len > 0) {
     67       elements_.push_back(Element());
     68       elements_.back().SetToBytes(bytes, bytes_len);
     69     }
     70   }
     71 
     72   void AppendFile(const FilePath& file_path) {
     73     elements_.push_back(Element());
     74     elements_.back().SetToFilePath(file_path);
     75   }
     76 
     77   void AppendFileRange(const FilePath& file_path,
     78                        uint64 offset, uint64 length) {
     79     elements_.push_back(Element());
     80     elements_.back().SetToFilePathRange(file_path, offset, length);
     81   }
     82 
     83   // Returns the total size in bytes of the data to upload.
     84   uint64 GetContentLength() const;
     85 
     86   const std::vector<Element>& elements() const {
     87     return elements_;
     88   }
     89 
     90   void set_elements(const std::vector<Element>& elements) {
     91     elements_ = elements;
     92   }
     93 
     94   void swap_elements(std::vector<Element>* elements) {
     95     elements_.swap(*elements);
     96   }
     97 
     98   // Identifies a particular upload instance, which is used by the cache to
     99   // formulate a cache key.  This value should be unique across browser
    100   // sessions.  A value of 0 is used to indicate an unspecified identifier.
    101   void set_identifier(int64 id) { identifier_ = id; }
    102   int64 identifier() const { return identifier_; }
    103 
    104  private:
    105   friend class base::RefCounted<UploadData>;
    106 
    107   ~UploadData() {}
    108 
    109   std::vector<Element> elements_;
    110   int64 identifier_;
    111 };
    112 
    113 }  // namespace net
    114 
    115 #endif  // NET_BASE_UPLOAD_DATA_H_
    116