1 // Copyright (c) 2012 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_ELEMENT_H_ 6 #define NET_BASE_UPLOAD_ELEMENT_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/files/file_path.h" 12 #include "base/time/time.h" 13 #include "net/base/net_export.h" 14 15 namespace net { 16 17 // A class representing an element contained by UploadData. 18 class NET_EXPORT UploadElement { 19 public: 20 enum Type { 21 TYPE_BYTES, 22 TYPE_FILE, 23 }; 24 25 UploadElement(); 26 ~UploadElement(); 27 28 Type type() const { return type_; } 29 30 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } 31 uint64 bytes_length() const { return buf_.size() + bytes_length_; } 32 const base::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 // If NULL time is returned, we do not do the check. 36 const base::Time& expected_file_modification_time() const { 37 return expected_file_modification_time_; 38 } 39 40 void SetToBytes(const char* bytes, int bytes_len) { 41 type_ = TYPE_BYTES; 42 buf_.assign(bytes, bytes + bytes_len); 43 } 44 45 // This does not copy the given data and the caller should make sure 46 // the data is secured somewhere else (e.g. by attaching the data 47 // using SetUserData). 48 void SetToSharedBytes(const char* bytes, int bytes_len) { 49 type_ = TYPE_BYTES; 50 bytes_start_ = bytes; 51 bytes_length_ = bytes_len; 52 } 53 54 void SetToFilePath(const base::FilePath& path) { 55 SetToFilePathRange(path, 0, kuint64max, base::Time()); 56 } 57 58 // If expected_modification_time is NULL, we do not check for the file 59 // change. Also note that the granularity for comparison is time_t, not 60 // the full precision. 61 void SetToFilePathRange(const base::FilePath& path, 62 uint64 offset, uint64 length, 63 const base::Time& expected_modification_time) { 64 type_ = TYPE_FILE; 65 file_path_ = path; 66 file_range_offset_ = offset; 67 file_range_length_ = length; 68 expected_file_modification_time_ = expected_modification_time; 69 } 70 71 private: 72 Type type_; 73 std::vector<char> buf_; 74 const char* bytes_start_; 75 uint64 bytes_length_; 76 base::FilePath file_path_; 77 uint64 file_range_offset_; 78 uint64 file_range_length_; 79 base::Time expected_file_modification_time_; 80 81 DISALLOW_COPY_AND_ASSIGN(UploadElement); 82 }; 83 84 #if defined(UNIT_TEST) 85 inline bool operator==(const UploadElement& a, 86 const UploadElement& b) { 87 if (a.type() != b.type()) 88 return false; 89 if (a.type() == UploadElement::TYPE_BYTES) 90 return a.bytes_length() == b.bytes_length() && 91 memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0; 92 if (a.type() == UploadElement::TYPE_FILE) { 93 return a.file_path() == b.file_path() && 94 a.file_range_offset() == b.file_range_offset() && 95 a.file_range_length() == b.file_range_length() && 96 a.expected_file_modification_time() == 97 b.expected_file_modification_time(); 98 } 99 return false; 100 } 101 102 inline bool operator!=(const UploadElement& a, 103 const UploadElement& b) { 104 return !(a == b); 105 } 106 #endif // defined(UNIT_TEST) 107 108 } // namespace net 109 110 #endif // NET_BASE_UPLOAD_ELEMENT_H_ 111