| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/ref_counted.h" | 15 #include "base/ref_counted.h" |
| 16 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/file_stream.h" | 17 #include "net/base/file_stream.h" |
| 17 #include "base/time.h" | 18 #include "base/time.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 class UploadData : public base::RefCounted<UploadData> { | 22 class UploadData : public base::RefCounted<UploadData> { |
| 22 public: | 23 public: |
| 23 UploadData() : identifier_(0) {} | 24 UploadData() : identifier_(0) {} |
| 24 | 25 |
| 25 enum Type { | 26 enum Type { |
| 26 TYPE_BYTES, | 27 TYPE_BYTES, |
| 27 TYPE_FILE | 28 TYPE_FILE, |
| 29 TYPE_BLOB |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 class Element { | 32 class Element { |
| 31 public: | 33 public: |
| 32 Element() : type_(TYPE_BYTES), file_range_offset_(0), | 34 Element() : type_(TYPE_BYTES), file_range_offset_(0), |
| 33 file_range_length_(kuint64max), | 35 file_range_length_(kuint64max), |
| 34 override_content_length_(false), | 36 override_content_length_(false), |
| 35 content_length_computed_(false), | 37 content_length_computed_(false), |
| 36 file_stream_(NULL) { | 38 file_stream_(NULL) { |
| 37 } | 39 } |
| 38 | 40 |
| 39 ~Element() { | 41 ~Element() { |
| 40 // In the common case |file__stream_| will be null. | 42 // In the common case |file__stream_| will be null. |
| 41 delete file_stream_; | 43 delete file_stream_; |
| 42 } | 44 } |
| 43 | 45 |
| 44 Type type() const { return type_; } | 46 Type type() const { return type_; } |
| 45 const std::vector<char>& bytes() const { return bytes_; } | 47 const std::vector<char>& bytes() const { return bytes_; } |
| 46 const FilePath& file_path() const { return file_path_; } | 48 const FilePath& file_path() const { return file_path_; } |
| 47 uint64 file_range_offset() const { return file_range_offset_; } | 49 uint64 file_range_offset() const { return file_range_offset_; } |
| 48 uint64 file_range_length() const { return file_range_length_; } | 50 uint64 file_range_length() const { return file_range_length_; } |
| 49 // If NULL time is returned, we do not do the check. | 51 // If NULL time is returned, we do not do the check. |
| 50 const base::Time& expected_file_modification_time() const { | 52 const base::Time& expected_file_modification_time() const { |
| 51 return expected_file_modification_time_; | 53 return expected_file_modification_time_; |
| 52 } | 54 } |
| 55 const GURL& blob_url() const { return blob_url_; } |
| 53 | 56 |
| 54 void SetToBytes(const char* bytes, int bytes_len) { | 57 void SetToBytes(const char* bytes, int bytes_len) { |
| 55 type_ = TYPE_BYTES; | 58 type_ = TYPE_BYTES; |
| 56 bytes_.assign(bytes, bytes + bytes_len); | 59 bytes_.assign(bytes, bytes + bytes_len); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void SetToFilePath(const FilePath& path) { | 62 void SetToFilePath(const FilePath& path) { |
| 60 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 63 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
| 61 } | 64 } |
| 62 | 65 |
| 63 // If expected_modification_time is NULL, we do not check for the file | 66 // If expected_modification_time is NULL, we do not check for the file |
| 64 // change. Also note that the granularity for comparison is time_t, not | 67 // change. Also note that the granularity for comparison is time_t, not |
| 65 // the full precision. | 68 // the full precision. |
| 66 void SetToFilePathRange(const FilePath& path, | 69 void SetToFilePathRange(const FilePath& path, |
| 67 uint64 offset, uint64 length, | 70 uint64 offset, uint64 length, |
| 68 const base::Time& expected_modification_time) { | 71 const base::Time& expected_modification_time) { |
| 69 type_ = TYPE_FILE; | 72 type_ = TYPE_FILE; |
| 70 file_path_ = path; | 73 file_path_ = path; |
| 71 file_range_offset_ = offset; | 74 file_range_offset_ = offset; |
| 72 file_range_length_ = length; | 75 file_range_length_ = length; |
| 73 expected_file_modification_time_ = expected_modification_time; | 76 expected_file_modification_time_ = expected_modification_time; |
| 74 } | 77 } |
| 75 | 78 |
| 79 // TODO(jianli): UploadData should not contain any blob reference. We need |
| 80 // to define another structure to represent WebKit::WebHTTPBody. |
| 81 void SetToBlobUrl(const GURL& blob_url) { |
| 82 type_ = TYPE_BLOB; |
| 83 blob_url_ = blob_url; |
| 84 } |
| 85 |
| 76 // Returns the byte-length of the element. For files that do not exist, 0 | 86 // Returns the byte-length of the element. For files that do not exist, 0 |
| 77 // is returned. This is done for consistency with Mozilla. | 87 // is returned. This is done for consistency with Mozilla. |
| 78 // Once called, this function will always return the same value. | 88 // Once called, this function will always return the same value. |
| 79 uint64 GetContentLength(); | 89 uint64 GetContentLength(); |
| 80 | 90 |
| 81 // Returns a FileStream opened for reading for this element, positioned at | 91 // Returns a FileStream opened for reading for this element, positioned at |
| 82 // |file_range_offset_|. The caller gets ownership and is responsible | 92 // |file_range_offset_|. The caller gets ownership and is responsible |
| 83 // for cleaning up the FileStream. Returns NULL if this element is not of | 93 // for cleaning up the FileStream. Returns NULL if this element is not of |
| 84 // type TYPE_FILE or if the file is not openable. | 94 // type TYPE_FILE or if the file is not openable. |
| 85 FileStream* NewFileStreamForReading(); | 95 FileStream* NewFileStreamForReading(); |
| 86 | 96 |
| 87 private: | 97 private: |
| 88 // Allows tests to override the result of GetContentLength. | 98 // Allows tests to override the result of GetContentLength. |
| 89 void SetContentLength(uint64 content_length) { | 99 void SetContentLength(uint64 content_length) { |
| 90 override_content_length_ = true; | 100 override_content_length_ = true; |
| 91 content_length_ = content_length; | 101 content_length_ = content_length; |
| 92 } | 102 } |
| 93 | 103 |
| 94 Type type_; | 104 Type type_; |
| 95 std::vector<char> bytes_; | 105 std::vector<char> bytes_; |
| 96 FilePath file_path_; | 106 FilePath file_path_; |
| 97 uint64 file_range_offset_; | 107 uint64 file_range_offset_; |
| 98 uint64 file_range_length_; | 108 uint64 file_range_length_; |
| 99 base::Time expected_file_modification_time_; | 109 base::Time expected_file_modification_time_; |
| 110 GURL blob_url_; |
| 100 bool override_content_length_; | 111 bool override_content_length_; |
| 101 bool content_length_computed_; | 112 bool content_length_computed_; |
| 102 uint64 content_length_; | 113 uint64 content_length_; |
| 103 FileStream* file_stream_; | 114 FileStream* file_stream_; |
| 104 | 115 |
| 105 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 116 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
| 106 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 117 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
| 107 UploadFileSmallerThanLength); | 118 UploadFileSmallerThanLength); |
| 108 }; | 119 }; |
| 109 | 120 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 120 } | 131 } |
| 121 | 132 |
| 122 void AppendFileRange(const FilePath& file_path, | 133 void AppendFileRange(const FilePath& file_path, |
| 123 uint64 offset, uint64 length, | 134 uint64 offset, uint64 length, |
| 124 const base::Time& expected_modification_time) { | 135 const base::Time& expected_modification_time) { |
| 125 elements_.push_back(Element()); | 136 elements_.push_back(Element()); |
| 126 elements_.back().SetToFilePathRange(file_path, offset, length, | 137 elements_.back().SetToFilePathRange(file_path, offset, length, |
| 127 expected_modification_time); | 138 expected_modification_time); |
| 128 } | 139 } |
| 129 | 140 |
| 141 void AppendBlob(const GURL& blob_url) { |
| 142 elements_.push_back(Element()); |
| 143 elements_.back().SetToBlobUrl(blob_url); |
| 144 } |
| 145 |
| 130 // Returns the total size in bytes of the data to upload. | 146 // Returns the total size in bytes of the data to upload. |
| 131 uint64 GetContentLength(); | 147 uint64 GetContentLength(); |
| 132 | 148 |
| 133 std::vector<Element>* elements() { | 149 std::vector<Element>* elements() { |
| 134 return &elements_; | 150 return &elements_; |
| 135 } | 151 } |
| 136 | 152 |
| 137 void set_elements(const std::vector<Element>& elements) { | 153 void set_elements(const std::vector<Element>& elements) { |
| 138 elements_ = elements; | 154 elements_ = elements; |
| 139 } | 155 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 153 | 169 |
| 154 ~UploadData() {} | 170 ~UploadData() {} |
| 155 | 171 |
| 156 std::vector<Element> elements_; | 172 std::vector<Element> elements_; |
| 157 int64 identifier_; | 173 int64 identifier_; |
| 158 }; | 174 }; |
| 159 | 175 |
| 160 } // namespace net | 176 } // namespace net |
| 161 | 177 |
| 162 #endif // NET_BASE_UPLOAD_DATA_H_ | 178 #endif // NET_BASE_UPLOAD_DATA_H_ |
| OLD | NEW |