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/ref_counted.h" | 14 #include "base/ref_counted.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 class FileStream; | 20 class FileStream; |
21 | 21 |
| 22 // Interface implemented by callers who require callbacks when new chunks |
| 23 // of data are added. |
| 24 class ChunkCallback { |
| 25 public: |
| 26 // Invoked when a new data chunk was given for a chunked transfer upload. |
| 27 virtual void OnChunkAvailable() = 0; |
| 28 |
| 29 protected: |
| 30 virtual ~ChunkCallback() {} |
| 31 }; |
| 32 |
22 class UploadData : public base::RefCounted<UploadData> { | 33 class UploadData : public base::RefCounted<UploadData> { |
23 public: | 34 public: |
24 enum Type { | 35 enum Type { |
25 TYPE_BYTES, | 36 TYPE_BYTES, |
26 TYPE_FILE, | 37 TYPE_FILE, |
27 TYPE_BLOB | 38 TYPE_BLOB, |
| 39 |
| 40 // A block of bytes to be sent in chunked encoding immediately, without |
| 41 // waiting for rest of the data. |
| 42 TYPE_CHUNK, |
28 }; | 43 }; |
29 | 44 |
30 class Element { | 45 class Element { |
31 public: | 46 public: |
32 Element(); | 47 Element(); |
33 ~Element(); | 48 ~Element(); |
34 | 49 |
35 Type type() const { return type_; } | 50 Type type() const { return type_; } |
36 const std::vector<char>& bytes() const { return bytes_; } | 51 const std::vector<char>& bytes() const { return bytes_; } |
37 const FilePath& file_path() const { return file_path_; } | 52 const FilePath& file_path() const { return file_path_; } |
(...skipping 27 matching lines...) Expand all Loading... |
65 expected_file_modification_time_ = expected_modification_time; | 80 expected_file_modification_time_ = expected_modification_time; |
66 } | 81 } |
67 | 82 |
68 // TODO(jianli): UploadData should not contain any blob reference. We need | 83 // TODO(jianli): UploadData should not contain any blob reference. We need |
69 // to define another structure to represent WebKit::WebHTTPBody. | 84 // to define another structure to represent WebKit::WebHTTPBody. |
70 void SetToBlobUrl(const GURL& blob_url) { | 85 void SetToBlobUrl(const GURL& blob_url) { |
71 type_ = TYPE_BLOB; | 86 type_ = TYPE_BLOB; |
72 blob_url_ = blob_url; | 87 blob_url_ = blob_url; |
73 } | 88 } |
74 | 89 |
| 90 // Though similar to bytes, a chunk indicates that the element is sent via |
| 91 // chunked transfer encoding and not buffered until the full upload data |
| 92 // is available. |
| 93 void SetToChunk(const char* bytes, int bytes_len); |
| 94 |
| 95 bool is_last_chunk() const { return is_last_chunk_; } |
| 96 |
75 // Returns the byte-length of the element. For files that do not exist, 0 | 97 // Returns the byte-length of the element. For files that do not exist, 0 |
76 // is returned. This is done for consistency with Mozilla. | 98 // is returned. This is done for consistency with Mozilla. |
77 // Once called, this function will always return the same value. | 99 // Once called, this function will always return the same value. |
78 uint64 GetContentLength(); | 100 uint64 GetContentLength(); |
79 | 101 |
80 // Returns a FileStream opened for reading for this element, positioned at | 102 // Returns a FileStream opened for reading for this element, positioned at |
81 // |file_range_offset_|. The caller gets ownership and is responsible | 103 // |file_range_offset_|. The caller gets ownership and is responsible |
82 // for cleaning up the FileStream. Returns NULL if this element is not of | 104 // for cleaning up the FileStream. Returns NULL if this element is not of |
83 // type TYPE_FILE or if the file is not openable. | 105 // type TYPE_FILE or if the file is not openable. |
84 FileStream* NewFileStreamForReading(); | 106 FileStream* NewFileStreamForReading(); |
85 | 107 |
86 private: | 108 private: |
87 // Allows tests to override the result of GetContentLength. | 109 // Allows tests to override the result of GetContentLength. |
88 void SetContentLength(uint64 content_length) { | 110 void SetContentLength(uint64 content_length) { |
89 override_content_length_ = true; | 111 override_content_length_ = true; |
90 content_length_ = content_length; | 112 content_length_ = content_length; |
91 } | 113 } |
92 | 114 |
93 Type type_; | 115 Type type_; |
94 std::vector<char> bytes_; | 116 std::vector<char> bytes_; |
95 FilePath file_path_; | 117 FilePath file_path_; |
96 uint64 file_range_offset_; | 118 uint64 file_range_offset_; |
97 uint64 file_range_length_; | 119 uint64 file_range_length_; |
98 base::Time expected_file_modification_time_; | 120 base::Time expected_file_modification_time_; |
99 GURL blob_url_; | 121 GURL blob_url_; |
| 122 bool is_last_chunk_; |
100 bool override_content_length_; | 123 bool override_content_length_; |
101 bool content_length_computed_; | 124 bool content_length_computed_; |
102 uint64 content_length_; | 125 uint64 content_length_; |
103 FileStream* file_stream_; | 126 FileStream* file_stream_; |
104 | 127 |
105 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 128 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
106 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 129 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
107 UploadFileSmallerThanLength); | 130 UploadFileSmallerThanLength); |
108 }; | 131 }; |
109 | 132 |
110 UploadData(); | 133 UploadData(); |
111 | 134 |
112 void AppendBytes(const char* bytes, int bytes_len); | 135 void AppendBytes(const char* bytes, int bytes_len); |
113 | 136 |
114 void AppendFile(const FilePath& file_path); | 137 void AppendFile(const FilePath& file_path); |
115 | 138 |
116 void AppendFileRange(const FilePath& file_path, | 139 void AppendFileRange(const FilePath& file_path, |
117 uint64 offset, uint64 length, | 140 uint64 offset, uint64 length, |
118 const base::Time& expected_modification_time); | 141 const base::Time& expected_modification_time); |
119 | 142 |
120 void AppendBlob(const GURL& blob_url); | 143 void AppendBlob(const GURL& blob_url); |
121 | 144 |
| 145 // Adds the given chunk of bytes to be sent immediately with chunked transfer |
| 146 // encoding. Set bytes_len to zero for the last chunk. |
| 147 void AppendChunk(const char* bytes, int bytes_len); |
| 148 |
| 149 // Sets the callback to be invoked when a new chunk is available to upload. |
| 150 void set_chunk_callback(ChunkCallback* callback); |
| 151 |
| 152 // Initializes the object to send chunks of upload data over time rather |
| 153 // than all at once. |
| 154 void set_is_chunked(bool set) { is_chunked_ = set; } |
| 155 bool is_chunked() const { return is_chunked_; } |
| 156 |
122 // Returns the total size in bytes of the data to upload. | 157 // Returns the total size in bytes of the data to upload. |
123 uint64 GetContentLength(); | 158 uint64 GetContentLength(); |
124 | 159 |
125 std::vector<Element>* elements() { | 160 std::vector<Element>* elements() { |
126 return &elements_; | 161 return &elements_; |
127 } | 162 } |
128 | 163 |
129 void SetElements(const std::vector<Element>& elements); | 164 void SetElements(const std::vector<Element>& elements); |
130 | 165 |
131 void swap_elements(std::vector<Element>* elements) { | 166 void swap_elements(std::vector<Element>* elements) { |
132 elements_.swap(*elements); | 167 elements_.swap(*elements); |
133 } | 168 } |
134 | 169 |
135 // Identifies a particular upload instance, which is used by the cache to | 170 // Identifies a particular upload instance, which is used by the cache to |
136 // formulate a cache key. This value should be unique across browser | 171 // formulate a cache key. This value should be unique across browser |
137 // sessions. A value of 0 is used to indicate an unspecified identifier. | 172 // sessions. A value of 0 is used to indicate an unspecified identifier. |
138 void set_identifier(int64 id) { identifier_ = id; } | 173 void set_identifier(int64 id) { identifier_ = id; } |
139 int64 identifier() const { return identifier_; } | 174 int64 identifier() const { return identifier_; } |
140 | 175 |
141 private: | 176 private: |
142 friend class base::RefCounted<UploadData>; | 177 friend class base::RefCounted<UploadData>; |
143 | 178 |
144 ~UploadData(); | 179 ~UploadData(); |
145 | 180 |
146 std::vector<Element> elements_; | 181 std::vector<Element> elements_; |
147 int64 identifier_; | 182 int64 identifier_; |
| 183 ChunkCallback* chunk_callback_; |
| 184 bool is_chunked_; |
148 | 185 |
149 DISALLOW_COPY_AND_ASSIGN(UploadData); | 186 DISALLOW_COPY_AND_ASSIGN(UploadData); |
150 }; | 187 }; |
151 | 188 |
152 #if defined(UNIT_TEST) | 189 #if defined(UNIT_TEST) |
153 inline bool operator==(const UploadData::Element& a, | 190 inline bool operator==(const UploadData::Element& a, |
154 const UploadData::Element& b) { | 191 const UploadData::Element& b) { |
155 if (a.type() != b.type()) | 192 if (a.type() != b.type()) |
156 return false; | 193 return false; |
157 if (a.type() == UploadData::TYPE_BYTES) | 194 if (a.type() == UploadData::TYPE_BYTES) |
(...skipping 12 matching lines...) Expand all Loading... |
170 | 207 |
171 inline bool operator!=(const UploadData::Element& a, | 208 inline bool operator!=(const UploadData::Element& a, |
172 const UploadData::Element& b) { | 209 const UploadData::Element& b) { |
173 return !(a == b); | 210 return !(a == b); |
174 } | 211 } |
175 #endif // defined(UNIT_TEST) | 212 #endif // defined(UNIT_TEST) |
176 | 213 |
177 } // namespace net | 214 } // namespace net |
178 | 215 |
179 #endif // NET_BASE_UPLOAD_DATA_H_ | 216 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |