OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_ELEMENT_H_ | 5 #ifndef NET_BASE_UPLOAD_ELEMENT_H_ |
6 #define NET_BASE_UPLOAD_ELEMENT_H_ | 6 #define NET_BASE_UPLOAD_ELEMENT_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/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 class FileStream; | 19 class FileStream; |
20 | 20 |
21 // A class representing an element contained by UploadData. | 21 // A class representing an element contained by UploadData. |
22 class NET_EXPORT UploadElement { | 22 class NET_EXPORT UploadElement { |
23 public: | 23 public: |
24 enum Type { | 24 enum Type { |
25 TYPE_BYTES, | 25 TYPE_BYTES, |
26 TYPE_FILE, | 26 TYPE_FILE, |
27 | |
28 // A block of bytes to be sent in chunked encoding immediately, without | |
29 // waiting for rest of the data. | |
30 TYPE_CHUNK, | |
31 }; | 27 }; |
32 | 28 |
33 UploadElement(); | 29 UploadElement(); |
34 ~UploadElement(); | 30 ~UploadElement(); |
35 | 31 |
36 Type type() const { return type_; } | 32 Type type() const { return type_; } |
37 // Explicitly sets the type of this UploadElement. Used during IPC | |
38 // marshalling. | |
39 void set_type(Type type) { | |
40 type_ = type; | |
41 } | |
42 | 33 |
43 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } | 34 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } |
44 uint64 bytes_length() const { return buf_.size() + bytes_length_; } | 35 uint64 bytes_length() const { return buf_.size() + bytes_length_; } |
45 const FilePath& file_path() const { return file_path_; } | 36 const FilePath& file_path() const { return file_path_; } |
46 uint64 file_range_offset() const { return file_range_offset_; } | 37 uint64 file_range_offset() const { return file_range_offset_; } |
47 uint64 file_range_length() const { return file_range_length_; } | 38 uint64 file_range_length() const { return file_range_length_; } |
48 // If NULL time is returned, we do not do the check. | 39 // If NULL time is returned, we do not do the check. |
49 const base::Time& expected_file_modification_time() const { | 40 const base::Time& expected_file_modification_time() const { |
50 return expected_file_modification_time_; | 41 return expected_file_modification_time_; |
51 } | 42 } |
(...skipping 22 matching lines...) Expand all Loading... |
74 void SetToFilePathRange(const FilePath& path, | 65 void SetToFilePathRange(const FilePath& path, |
75 uint64 offset, uint64 length, | 66 uint64 offset, uint64 length, |
76 const base::Time& expected_modification_time) { | 67 const base::Time& expected_modification_time) { |
77 type_ = TYPE_FILE; | 68 type_ = TYPE_FILE; |
78 file_path_ = path; | 69 file_path_ = path; |
79 file_range_offset_ = offset; | 70 file_range_offset_ = offset; |
80 file_range_length_ = length; | 71 file_range_length_ = length; |
81 expected_file_modification_time_ = expected_modification_time; | 72 expected_file_modification_time_ = expected_modification_time; |
82 } | 73 } |
83 | 74 |
84 // Though similar to bytes, a chunk indicates that the element is sent via | |
85 // chunked transfer encoding and not buffered until the full upload data | |
86 // is available. | |
87 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
88 | |
89 bool is_last_chunk() const { return is_last_chunk_; } | |
90 // Sets whether this is the last chunk. Used during IPC marshalling. | |
91 void set_is_last_chunk(bool is_last_chunk) { | |
92 is_last_chunk_ = is_last_chunk; | |
93 } | |
94 | |
95 // 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 |
96 // is returned. This is done for consistency with Mozilla. | 76 // is returned. This is done for consistency with Mozilla. |
97 uint64 GetContentLength(); | 77 uint64 GetContentLength(); |
98 | 78 |
99 // Reads up to |buf_len| bytes synchronously. Returns the number of bytes | 79 // Reads up to |buf_len| bytes synchronously. Returns the number of bytes |
100 // read. This function never fails. If there's less data to read than we | 80 // read. This function never fails. If there's less data to read than we |
101 // initially observed, then pad with zero (this can happen with files). | 81 // initially observed, then pad with zero (this can happen with files). |
102 // |buf_len| must be greater than 0. | 82 // |buf_len| must be greater than 0. |
103 int ReadSync(char* buf, int buf_len); | 83 int ReadSync(char* buf, int buf_len); |
104 | 84 |
105 // Returns the number of bytes remaining to read. | 85 // Returns the number of bytes remaining to read. |
106 uint64 BytesRemaining(); | 86 uint64 BytesRemaining(); |
107 | 87 |
108 // Resets the offset to zero and closes the file stream if opened, so | 88 // Resets the offset to zero and closes the file stream if opened, so |
109 // that the element can be reread. | 89 // that the element can be reread. |
110 void ResetOffset(); | 90 void ResetOffset(); |
111 | 91 |
112 private: | 92 private: |
113 // Returns a FileStream opened for reading for this element, positioned | 93 // Returns a FileStream opened for reading for this element, positioned |
114 // at |file_range_offset_|. Returns NULL if the file is not openable. | 94 // at |file_range_offset_|. Returns NULL if the file is not openable. |
115 FileStream* OpenFileStream(); | 95 FileStream* OpenFileStream(); |
116 | 96 |
117 // Reads up to |buf_len| bytes synchronously from memory (i.e. type_ is | 97 // Reads up to |buf_len| bytes synchronously from memory (i.e. type_ is |
118 // TYPE_BYTES or TYPE_CHUNK). | 98 // TYPE_BYTES). |
119 int ReadFromMemorySync(char* buf, int buf_len); | 99 int ReadFromMemorySync(char* buf, int buf_len); |
120 | 100 |
121 // Reads up to |buf_len| bytes synchronously from a file (i.e. type_ is | 101 // Reads up to |buf_len| bytes synchronously from a file (i.e. type_ is |
122 // TYPE_FILE). | 102 // TYPE_FILE). |
123 int ReadFromFileSync(char* buf, int buf_len); | 103 int ReadFromFileSync(char* buf, int buf_len); |
124 | 104 |
125 // Allows tests to override the result of GetContentLength. | 105 // Allows tests to override the result of GetContentLength. |
126 void SetContentLength(uint64 content_length) { | 106 void SetContentLength(uint64 content_length) { |
127 override_content_length_ = true; | 107 override_content_length_ = true; |
128 content_length_ = content_length; | 108 content_length_ = content_length; |
129 } | 109 } |
130 | 110 |
131 Type type_; | 111 Type type_; |
132 std::vector<char> buf_; | 112 std::vector<char> buf_; |
133 const char* bytes_start_; | 113 const char* bytes_start_; |
134 uint64 bytes_length_; | 114 uint64 bytes_length_; |
135 FilePath file_path_; | 115 FilePath file_path_; |
136 uint64 file_range_offset_; | 116 uint64 file_range_offset_; |
137 uint64 file_range_length_; | 117 uint64 file_range_length_; |
138 base::Time expected_file_modification_time_; | 118 base::Time expected_file_modification_time_; |
139 bool is_last_chunk_; | |
140 bool override_content_length_; | 119 bool override_content_length_; |
141 bool content_length_computed_; | 120 bool content_length_computed_; |
142 uint64 content_length_; | 121 uint64 content_length_; |
143 | 122 |
144 // The byte offset from the beginning of the element data. Used to track | 123 // The byte offset from the beginning of the element data. Used to track |
145 // the current position when reading data. | 124 // the current position when reading data. |
146 size_t offset_; | 125 size_t offset_; |
147 | 126 |
148 // The stream of the element data, if this element is of TYPE_FILE. | 127 // The stream of the element data, if this element is of TYPE_FILE. |
149 FileStream* file_stream_; | 128 FileStream* file_stream_; |
(...skipping 27 matching lines...) Expand all Loading... |
177 | 156 |
178 inline bool operator!=(const UploadElement& a, | 157 inline bool operator!=(const UploadElement& a, |
179 const UploadElement& b) { | 158 const UploadElement& b) { |
180 return !(a == b); | 159 return !(a == b); |
181 } | 160 } |
182 #endif // defined(UNIT_TEST) | 161 #endif // defined(UNIT_TEST) |
183 | 162 |
184 } // namespace net | 163 } // namespace net |
185 | 164 |
186 #endif // NET_BASE_UPLOAD_ELEMENT_H_ | 165 #endif // NET_BASE_UPLOAD_ELEMENT_H_ |
OLD | NEW |