| 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/platform_file.h" |
| 12 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 13 #include "testing/gtest/include/gtest/gtest_prod.h" | 14 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 class UploadData : public base::RefCounted<UploadData> { | 18 class UploadData : public base::RefCounted<UploadData> { |
| 18 public: | 19 public: |
| 19 UploadData() : identifier_(0) {} | 20 UploadData() : identifier_(0) {} |
| 20 | 21 |
| 21 enum Type { | 22 enum Type { |
| 22 TYPE_BYTES, | 23 TYPE_BYTES, |
| 23 TYPE_FILE | 24 TYPE_FILE |
| 24 }; | 25 }; |
| 25 | 26 |
| 26 class Element { | 27 class Element { |
| 27 public: | 28 public: |
| 28 Element() : type_(TYPE_BYTES), file_range_offset_(0), | 29 Element() : type_(TYPE_BYTES), file_range_offset_(0), |
| 29 file_range_length_(kuint64max), | 30 file_range_length_(0), |
| 31 file_(base::kInvalidPlatformFileValue), |
| 30 override_content_length_(false) { | 32 override_content_length_(false) { |
| 31 } | 33 } |
| 32 | 34 |
| 33 Type type() const { return type_; } | 35 Type type() const { return type_; } |
| 34 const std::vector<char>& bytes() const { return bytes_; } | 36 const std::vector<char>& bytes() const { return bytes_; } |
| 35 const FilePath& file_path() const { return file_path_; } | 37 const FilePath& file_path() const { return file_path_; } |
| 36 uint64 file_range_offset() const { return file_range_offset_; } | 38 uint64 file_range_offset() const { return file_range_offset_; } |
| 37 uint64 file_range_length() const { return file_range_length_; } | 39 uint64 file_range_length() const { return file_range_length_; } |
| 38 | 40 |
| 39 void SetToBytes(const char* bytes, int bytes_len) { | 41 void SetToBytes(const char* bytes, int bytes_len) { |
| 40 type_ = TYPE_BYTES; | 42 type_ = TYPE_BYTES; |
| 41 bytes_.assign(bytes, bytes + bytes_len); | 43 bytes_.assign(bytes, bytes + bytes_len); |
| 42 } | 44 } |
| 43 | 45 |
| 44 void SetToFilePath(const FilePath& path) { | 46 void SetToFilePath(const FilePath& path) { |
| 45 SetToFilePathRange(path, 0, kuint64max); | 47 SetToFilePathRange(path, 0, kuint64max); |
| 46 } | 48 } |
| 47 | 49 |
| 48 void SetToFilePathRange(const FilePath& path, | 50 void SetToFilePathRange(const FilePath& path, uint64 offset, uint64 length); |
| 49 uint64 offset, uint64 length) { | |
| 50 type_ = TYPE_FILE; | |
| 51 file_path_ = path; | |
| 52 file_range_offset_ = offset; | |
| 53 file_range_length_ = length; | |
| 54 } | |
| 55 | 51 |
| 56 // Returns the byte-length of the element. For files that do not exist, 0 | 52 // Returns the byte-length of the element. For files that do not exist, 0 |
| 57 // is returned. This is done for consistency with Mozilla. | 53 // is returned. This is done for consistency with Mozilla. |
| 58 uint64 GetContentLength() const; | 54 uint64 GetContentLength() const { |
| 55 if (override_content_length_) |
| 56 return content_length_; |
| 57 |
| 58 if (type_ == TYPE_BYTES) { |
| 59 return bytes_.size(); |
| 60 } else { |
| 61 return file_range_length_; |
| 62 } |
| 63 } |
| 64 |
| 65 // For a TYPE_FILE, return a handle to the file. The caller does not take |
| 66 // ownership and should not close the file handle. |
| 67 base::PlatformFile platform_file() const; |
| 68 |
| 69 // For a TYPE_FILE, this closes the file handle. It's a fatal error to call |
| 70 // platform_file() after this. |
| 71 void Close(); |
| 59 | 72 |
| 60 private: | 73 private: |
| 74 // type_ == TYPE_BYTES: |
| 75 // bytes_ is valid |
| 76 // type_ == TYPE_FILE: |
| 77 // file_path_ should always be valid. |
| 78 // |
| 79 // platform_file() may be invalid, in which case file_range_* are 0 and |
| 80 // file_ is invalid. This occurs when we cannot open the requested file. |
| 81 // |
| 82 // Else, then file_range_* are within range of the length of the file |
| 83 // that we found when opening the file. Also, the sum of offset and |
| 84 // length will not overflow a uint64. file_ will be handle to the file. |
| 85 |
| 61 // Allows tests to override the result of GetContentLength. | 86 // Allows tests to override the result of GetContentLength. |
| 62 void SetContentLength(uint64 content_length) { | 87 void SetContentLength(uint64 content_length) { |
| 63 override_content_length_ = true; | 88 override_content_length_ = true; |
| 64 content_length_ = content_length; | 89 content_length_ = content_length; |
| 65 } | 90 } |
| 66 | 91 |
| 67 Type type_; | 92 Type type_; |
| 68 std::vector<char> bytes_; | 93 std::vector<char> bytes_; |
| 69 FilePath file_path_; | 94 FilePath file_path_; |
| 70 uint64 file_range_offset_; | 95 uint64 file_range_offset_; |
| 71 uint64 file_range_length_; | 96 uint64 file_range_length_; |
| 97 base::PlatformFile file_; |
| 72 bool override_content_length_; | 98 bool override_content_length_; |
| 73 uint64 content_length_; | 99 uint64 content_length_; |
| 74 | 100 |
| 75 FRIEND_TEST(UploadDataStreamTest, FileSmallerThanLength); | 101 FRIEND_TEST(UploadDataStreamTest, FileSmallerThanLength); |
| 76 FRIEND_TEST(HttpNetworkTransactionTest, UploadFileSmallerThanLength); | 102 FRIEND_TEST(HttpNetworkTransactionTest, UploadFileSmallerThanLength); |
| 77 }; | 103 }; |
| 78 | 104 |
| 79 void AppendBytes(const char* bytes, int bytes_len) { | 105 void AppendBytes(const char* bytes, int bytes_len) { |
| 80 if (bytes_len > 0) { | 106 if (bytes_len > 0) { |
| 81 elements_.push_back(Element()); | 107 elements_.push_back(Element()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 102 } | 128 } |
| 103 | 129 |
| 104 void set_elements(const std::vector<Element>& elements) { | 130 void set_elements(const std::vector<Element>& elements) { |
| 105 elements_ = elements; | 131 elements_ = elements; |
| 106 } | 132 } |
| 107 | 133 |
| 108 void swap_elements(std::vector<Element>* elements) { | 134 void swap_elements(std::vector<Element>* elements) { |
| 109 elements_.swap(*elements); | 135 elements_.swap(*elements); |
| 110 } | 136 } |
| 111 | 137 |
| 138 // CloseFiles closes the file handles of all Elements of type TYPE_FILE. |
| 139 void CloseFiles(); |
| 140 |
| 112 // Identifies a particular upload instance, which is used by the cache to | 141 // Identifies a particular upload instance, which is used by the cache to |
| 113 // formulate a cache key. This value should be unique across browser | 142 // formulate a cache key. This value should be unique across browser |
| 114 // sessions. A value of 0 is used to indicate an unspecified identifier. | 143 // sessions. A value of 0 is used to indicate an unspecified identifier. |
| 115 void set_identifier(int64 id) { identifier_ = id; } | 144 void set_identifier(int64 id) { identifier_ = id; } |
| 116 int64 identifier() const { return identifier_; } | 145 int64 identifier() const { return identifier_; } |
| 117 | 146 |
| 118 private: | 147 private: |
| 119 friend class base::RefCounted<UploadData>; | 148 friend class base::RefCounted<UploadData>; |
| 120 | 149 |
| 121 ~UploadData() {} | 150 ~UploadData() {} |
| 122 | 151 |
| 123 std::vector<Element> elements_; | 152 std::vector<Element> elements_; |
| 124 int64 identifier_; | 153 int64 identifier_; |
| 125 }; | 154 }; |
| 126 | 155 |
| 127 } // namespace net | 156 } // namespace net |
| 128 | 157 |
| 129 #endif // NET_BASE_UPLOAD_DATA_H_ | 158 #endif // NET_BASE_UPLOAD_DATA_H_ |
| OLD | NEW |