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_; |
100 bool override_content_length_; | 122 bool is_last_chunk_ : 1; |
vandebo (ex-Chrome)
2011/01/21 18:08:41
nit: Are the :1's needed? I suspect not.
wtc
2011/01/21 19:35:54
I also recommend removing ": 1". By listing the b
| |
101 bool content_length_computed_; | 123 bool override_content_length_ : 1; |
124 bool content_length_computed_ : 1; | |
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 void AppendChunk(const char* bytes, int bytes_len); | |
vandebo (ex-Chrome)
2011/01/21 18:08:41
nit: Add a comment about a zero size chunk indicat
| |
146 | |
147 // Sets the callback to be invoked when a new chunk is available to upload. | |
148 void set_chunk_callback(ChunkCallback* callback); | |
149 | |
150 // Initializes the object to send chunks of upload data over time rather | |
151 // than all at once. | |
152 void set_is_chunked(bool set) { is_chunked_ = set; } | |
153 bool is_chunked() const { return is_chunked_; } | |
154 | |
122 // Returns the total size in bytes of the data to upload. | 155 // Returns the total size in bytes of the data to upload. |
123 uint64 GetContentLength(); | 156 uint64 GetContentLength(); |
124 | 157 |
125 std::vector<Element>* elements() { | 158 std::vector<Element>* elements() { |
126 return &elements_; | 159 return &elements_; |
127 } | 160 } |
128 | 161 |
129 void SetElements(const std::vector<Element>& elements); | 162 void SetElements(const std::vector<Element>& elements); |
130 | 163 |
131 void swap_elements(std::vector<Element>* elements) { | 164 void swap_elements(std::vector<Element>* elements) { |
132 elements_.swap(*elements); | 165 elements_.swap(*elements); |
133 } | 166 } |
134 | 167 |
135 // Identifies a particular upload instance, which is used by the cache to | 168 // Identifies a particular upload instance, which is used by the cache to |
136 // formulate a cache key. This value should be unique across browser | 169 // formulate a cache key. This value should be unique across browser |
137 // sessions. A value of 0 is used to indicate an unspecified identifier. | 170 // sessions. A value of 0 is used to indicate an unspecified identifier. |
138 void set_identifier(int64 id) { identifier_ = id; } | 171 void set_identifier(int64 id) { identifier_ = id; } |
139 int64 identifier() const { return identifier_; } | 172 int64 identifier() const { return identifier_; } |
140 | 173 |
141 private: | 174 private: |
142 friend class base::RefCounted<UploadData>; | 175 friend class base::RefCounted<UploadData>; |
143 | 176 |
144 ~UploadData(); | 177 ~UploadData(); |
145 | 178 |
146 std::vector<Element> elements_; | 179 std::vector<Element> elements_; |
147 int64 identifier_; | 180 int64 identifier_; |
181 ChunkCallback* chunk_callback_; | |
182 bool is_chunked_; | |
148 | 183 |
149 DISALLOW_COPY_AND_ASSIGN(UploadData); | 184 DISALLOW_COPY_AND_ASSIGN(UploadData); |
150 }; | 185 }; |
151 | 186 |
152 #if defined(UNIT_TEST) | 187 #if defined(UNIT_TEST) |
153 inline bool operator==(const UploadData::Element& a, | 188 inline bool operator==(const UploadData::Element& a, |
154 const UploadData::Element& b) { | 189 const UploadData::Element& b) { |
155 if (a.type() != b.type()) | 190 if (a.type() != b.type()) |
156 return false; | 191 return false; |
157 if (a.type() == UploadData::TYPE_BYTES) | 192 if (a.type() == UploadData::TYPE_BYTES) |
(...skipping 12 matching lines...) Expand all Loading... | |
170 | 205 |
171 inline bool operator!=(const UploadData::Element& a, | 206 inline bool operator!=(const UploadData::Element& a, |
172 const UploadData::Element& b) { | 207 const UploadData::Element& b) { |
173 return !(a == b); | 208 return !(a == b); |
174 } | 209 } |
175 #endif // defined(UNIT_TEST) | 210 #endif // defined(UNIT_TEST) |
176 | 211 |
177 } // namespace net | 212 } // namespace net |
178 | 213 |
179 #endif // NET_BASE_UPLOAD_DATA_H_ | 214 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |