| 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_H_ | 5 #ifndef NET_BASE_UPLOAD_DATA_H_ |
| 6 #define NET_BASE_UPLOAD_DATA_H_ | 6 #define NET_BASE_UPLOAD_DATA_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/logging.h" |
| 12 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 14 #include "net/base/file_stream.h" |
| 13 #include "base/time.h" | 15 #include "base/time.h" |
| 14 #include "testing/gtest/include/gtest/gtest_prod.h" | 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 15 | 17 |
| 16 namespace net { | 18 namespace net { |
| 17 | 19 |
| 18 class UploadData : public base::RefCounted<UploadData> { | 20 class UploadData : public base::RefCounted<UploadData> { |
| 19 public: | 21 public: |
| 20 UploadData() : identifier_(0) {} | 22 UploadData() : identifier_(0) {} |
| 21 | 23 |
| 22 enum Type { | 24 enum Type { |
| 23 TYPE_BYTES, | 25 TYPE_BYTES, |
| 24 TYPE_FILE | 26 TYPE_FILE |
| 25 }; | 27 }; |
| 26 | 28 |
| 27 class Element { | 29 class Element { |
| 28 public: | 30 public: |
| 29 Element() : type_(TYPE_BYTES), file_range_offset_(0), | 31 Element() : type_(TYPE_BYTES), file_range_offset_(0), |
| 30 file_range_length_(kuint64max), | 32 file_range_length_(kuint64max), |
| 31 override_content_length_(false) { | 33 override_content_length_(false), |
| 34 content_length_computed_(false), |
| 35 file_stream_(NULL) { |
| 36 } |
| 37 |
| 38 ~Element() { |
| 39 // In the common case |file__stream_| will be null. |
| 40 delete file_stream_; |
| 32 } | 41 } |
| 33 | 42 |
| 34 Type type() const { return type_; } | 43 Type type() const { return type_; } |
| 35 const std::vector<char>& bytes() const { return bytes_; } | 44 const std::vector<char>& bytes() const { return bytes_; } |
| 36 const FilePath& file_path() const { return file_path_; } | 45 const FilePath& file_path() const { return file_path_; } |
| 37 uint64 file_range_offset() const { return file_range_offset_; } | 46 uint64 file_range_offset() const { return file_range_offset_; } |
| 38 uint64 file_range_length() const { return file_range_length_; } | 47 uint64 file_range_length() const { return file_range_length_; } |
| 39 // If NULL time is returned, we do not do the check. | 48 // If NULL time is returned, we do not do the check. |
| 40 const base::Time& expected_file_modification_time() const { | 49 const base::Time& expected_file_modification_time() const { |
| 41 return expected_file_modification_time_; | 50 return expected_file_modification_time_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 const base::Time& expected_modification_time) { | 67 const base::Time& expected_modification_time) { |
| 59 type_ = TYPE_FILE; | 68 type_ = TYPE_FILE; |
| 60 file_path_ = path; | 69 file_path_ = path; |
| 61 file_range_offset_ = offset; | 70 file_range_offset_ = offset; |
| 62 file_range_length_ = length; | 71 file_range_length_ = length; |
| 63 expected_file_modification_time_ = expected_modification_time; | 72 expected_file_modification_time_ = expected_modification_time; |
| 64 } | 73 } |
| 65 | 74 |
| 66 // Returns the byte-length of the element. For files that do not exist, 0 | 75 // Returns the byte-length of the element. For files that do not exist, 0 |
| 67 // is returned. This is done for consistency with Mozilla. | 76 // is returned. This is done for consistency with Mozilla. |
| 68 uint64 GetContentLength() const; | 77 // Once called, this function will always return the same value. |
| 78 uint64 GetContentLength(); |
| 79 |
| 80 // Returns a FileStream opened for reading for this element, positioned at |
| 81 // |file_range_offset_|. The caller gets ownership and is responsible |
| 82 // for cleaning up the FileStream. Returns NULL if this element is not of |
| 83 // type TYPE_FILE or if the file is not openable. |
| 84 FileStream* NewFileStreamForReading(); |
| 69 | 85 |
| 70 private: | 86 private: |
| 71 // Allows tests to override the result of GetContentLength. | 87 // Allows tests to override the result of GetContentLength. |
| 72 void SetContentLength(uint64 content_length) { | 88 void SetContentLength(uint64 content_length) { |
| 73 override_content_length_ = true; | 89 override_content_length_ = true; |
| 74 content_length_ = content_length; | 90 content_length_ = content_length; |
| 75 } | 91 } |
| 76 | 92 |
| 77 Type type_; | 93 Type type_; |
| 78 std::vector<char> bytes_; | 94 std::vector<char> bytes_; |
| 79 FilePath file_path_; | 95 FilePath file_path_; |
| 80 uint64 file_range_offset_; | 96 uint64 file_range_offset_; |
| 81 uint64 file_range_length_; | 97 uint64 file_range_length_; |
| 82 base::Time expected_file_modification_time_; | 98 base::Time expected_file_modification_time_; |
| 83 bool override_content_length_; | 99 bool override_content_length_; |
| 100 bool content_length_computed_; |
| 84 uint64 content_length_; | 101 uint64 content_length_; |
| 102 FileStream* file_stream_; |
| 85 | 103 |
| 86 FRIEND_TEST(UploadDataStreamTest, FileSmallerThanLength); | 104 FRIEND_TEST(UploadDataStreamTest, FileSmallerThanLength); |
| 87 FRIEND_TEST(HttpNetworkTransactionTest, UploadFileSmallerThanLength); | 105 FRIEND_TEST(HttpNetworkTransactionTest, UploadFileSmallerThanLength); |
| 88 }; | 106 }; |
| 89 | 107 |
| 90 void AppendBytes(const char* bytes, int bytes_len) { | 108 void AppendBytes(const char* bytes, int bytes_len) { |
| 91 if (bytes_len > 0) { | 109 if (bytes_len > 0) { |
| 92 elements_.push_back(Element()); | 110 elements_.push_back(Element()); |
| 93 elements_.back().SetToBytes(bytes, bytes_len); | 111 elements_.back().SetToBytes(bytes, bytes_len); |
| 94 } | 112 } |
| 95 } | 113 } |
| 96 | 114 |
| 97 void AppendFile(const FilePath& file_path) { | 115 void AppendFile(const FilePath& file_path) { |
| 98 elements_.push_back(Element()); | 116 elements_.push_back(Element()); |
| 99 elements_.back().SetToFilePath(file_path); | 117 elements_.back().SetToFilePath(file_path); |
| 100 } | 118 } |
| 101 | 119 |
| 102 void AppendFileRange(const FilePath& file_path, | 120 void AppendFileRange(const FilePath& file_path, |
| 103 uint64 offset, uint64 length, | 121 uint64 offset, uint64 length, |
| 104 const base::Time& expected_modification_time) { | 122 const base::Time& expected_modification_time) { |
| 105 elements_.push_back(Element()); | 123 elements_.push_back(Element()); |
| 106 elements_.back().SetToFilePathRange(file_path, offset, length, | 124 elements_.back().SetToFilePathRange(file_path, offset, length, |
| 107 expected_modification_time); | 125 expected_modification_time); |
| 108 } | 126 } |
| 109 | 127 |
| 110 // Returns the total size in bytes of the data to upload. | 128 // Returns the total size in bytes of the data to upload. |
| 111 uint64 GetContentLength() const; | 129 uint64 GetContentLength(); |
| 112 | 130 |
| 113 const std::vector<Element>& elements() const { | 131 std::vector<Element>* elements() { |
| 114 return elements_; | 132 return &elements_; |
| 115 } | 133 } |
| 116 | 134 |
| 117 void set_elements(const std::vector<Element>& elements) { | 135 void set_elements(const std::vector<Element>& elements) { |
| 118 elements_ = elements; | 136 elements_ = elements; |
| 119 } | 137 } |
| 120 | 138 |
| 121 void swap_elements(std::vector<Element>* elements) { | 139 void swap_elements(std::vector<Element>* elements) { |
| 122 elements_.swap(*elements); | 140 elements_.swap(*elements); |
| 123 } | 141 } |
| 124 | 142 |
| 125 // Identifies a particular upload instance, which is used by the cache to | 143 // Identifies a particular upload instance, which is used by the cache to |
| 126 // formulate a cache key. This value should be unique across browser | 144 // formulate a cache key. This value should be unique across browser |
| 127 // sessions. A value of 0 is used to indicate an unspecified identifier. | 145 // sessions. A value of 0 is used to indicate an unspecified identifier. |
| 128 void set_identifier(int64 id) { identifier_ = id; } | 146 void set_identifier(int64 id) { identifier_ = id; } |
| 129 int64 identifier() const { return identifier_; } | 147 int64 identifier() const { return identifier_; } |
| 130 | 148 |
| 131 private: | 149 private: |
| 132 friend class base::RefCounted<UploadData>; | 150 friend class base::RefCounted<UploadData>; |
| 133 | 151 |
| 134 ~UploadData() {} | 152 ~UploadData() {} |
| 135 | 153 |
| 136 std::vector<Element> elements_; | 154 std::vector<Element> elements_; |
| 137 int64 identifier_; | 155 int64 identifier_; |
| 138 }; | 156 }; |
| 139 | 157 |
| 140 } // namespace net | 158 } // namespace net |
| 141 | 159 |
| 142 #endif // NET_BASE_UPLOAD_DATA_H_ | 160 #endif // NET_BASE_UPLOAD_DATA_H_ |
| OLD | NEW |