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 "net/base/completion_callback.h" | |
16 #include "base/time.h" | 17 #include "base/time.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 class FileStream; | 21 class FileStream; |
21 | 22 |
22 class UploadData : public base::RefCounted<UploadData> { | 23 class UploadData : public base::RefCounted<UploadData> { |
23 public: | 24 public: |
24 enum Type { | 25 enum Type { |
25 TYPE_BYTES, | 26 TYPE_BYTES, |
27 TYPE_CHUNK, // More data is to come and this should be sent immediately. | |
wtc
2011/01/14 03:09:31
The comment should say:
The data should be sent
Satish
2011/01/14 18:09:29
Done.
| |
26 TYPE_FILE, | 28 TYPE_FILE, |
27 TYPE_BLOB | 29 TYPE_BLOB |
28 }; | 30 }; |
29 | 31 |
32 // Interface implemented by callers who require callbacks when new chunks | |
33 // of data are received. | |
vandebo (ex-Chrome)
2011/01/14 05:53:44
Note, applies only for TYPE_CHUNK.
| |
34 class ChunkCallback { | |
35 public: | |
36 // Invoked when a new data chunk was given for a chunked transfer upload. | |
37 virtual void OnChunkAvailable() = 0; | |
38 | |
39 protected: | |
40 virtual ~ChunkCallback() {} | |
vandebo (ex-Chrome)
2011/01/14 05:53:44
Does this need an implementation? If so, shouldn'
| |
41 }; | |
42 | |
30 class Element { | 43 class Element { |
31 public: | 44 public: |
32 Element(); | 45 Element(); |
33 ~Element(); | 46 ~Element(); |
34 | 47 |
35 Type type() const { return type_; } | 48 Type type() const { return type_; } |
36 const std::vector<char>& bytes() const { return bytes_; } | 49 const std::vector<char>& bytes() const { return bytes_; } |
37 const FilePath& file_path() const { return file_path_; } | 50 const FilePath& file_path() const { return file_path_; } |
38 uint64 file_range_offset() const { return file_range_offset_; } | 51 uint64 file_range_offset() const { return file_range_offset_; } |
39 uint64 file_range_length() const { return file_range_length_; } | 52 uint64 file_range_length() const { return file_range_length_; } |
(...skipping 25 matching lines...) Expand all Loading... | |
65 expected_file_modification_time_ = expected_modification_time; | 78 expected_file_modification_time_ = expected_modification_time; |
66 } | 79 } |
67 | 80 |
68 // TODO(jianli): UploadData should not contain any blob reference. We need | 81 // TODO(jianli): UploadData should not contain any blob reference. We need |
69 // to define another structure to represent WebKit::WebHTTPBody. | 82 // to define another structure to represent WebKit::WebHTTPBody. |
70 void SetToBlobUrl(const GURL& blob_url) { | 83 void SetToBlobUrl(const GURL& blob_url) { |
71 type_ = TYPE_BLOB; | 84 type_ = TYPE_BLOB; |
72 blob_url_ = blob_url; | 85 blob_url_ = blob_url; |
73 } | 86 } |
74 | 87 |
88 // Though similar to bytes, a chunk indicates that the stream has more such | |
89 // items after this item. | |
wtc
2011/01/14 03:09:31
Again, the difference is that a chunk should be se
Satish
2011/01/14 18:09:29
Done.
| |
90 void SetToChunk(const char* bytes, int bytes_len); | |
91 | |
92 // Identifies this element as the last item in a series of chunks. | |
93 void SetToLastChunk(); | |
94 | |
75 // Returns the byte-length of the element. For files that do not exist, 0 | 95 // Returns the byte-length of the element. For files that do not exist, 0 |
76 // is returned. This is done for consistency with Mozilla. | 96 // is returned. This is done for consistency with Mozilla. |
77 // Once called, this function will always return the same value. | 97 // Once called, this function will always return the same value. |
78 uint64 GetContentLength(); | 98 uint64 GetContentLength(); |
79 | 99 |
80 // Returns a FileStream opened for reading for this element, positioned at | 100 // Returns a FileStream opened for reading for this element, positioned at |
81 // |file_range_offset_|. The caller gets ownership and is responsible | 101 // |file_range_offset_|. The caller gets ownership and is responsible |
82 // for cleaning up the FileStream. Returns NULL if this element is not of | 102 // for cleaning up the FileStream. Returns NULL if this element is not of |
83 // type TYPE_FILE or if the file is not openable. | 103 // type TYPE_FILE or if the file is not openable. |
84 FileStream* NewFileStreamForReading(); | 104 FileStream* NewFileStreamForReading(); |
(...skipping 27 matching lines...) Expand all Loading... | |
112 void AppendBytes(const char* bytes, int bytes_len); | 132 void AppendBytes(const char* bytes, int bytes_len); |
113 | 133 |
114 void AppendFile(const FilePath& file_path); | 134 void AppendFile(const FilePath& file_path); |
115 | 135 |
116 void AppendFileRange(const FilePath& file_path, | 136 void AppendFileRange(const FilePath& file_path, |
117 uint64 offset, uint64 length, | 137 uint64 offset, uint64 length, |
118 const base::Time& expected_modification_time); | 138 const base::Time& expected_modification_time); |
119 | 139 |
120 void AppendBlob(const GURL& blob_url); | 140 void AppendBlob(const GURL& blob_url); |
121 | 141 |
142 void AppendChunk(const char* bytes, int bytes_len); | |
143 | |
144 // Sets the callback to be invoked when a new chunk is available to upload. | |
145 void set_chunk_callback(ChunkCallback* callback); | |
146 | |
147 // Iniitalizes the object to receive chunks of upload data over time rather | |
148 // than all at once. | |
149 void set_is_chunked(bool set) { is_chunked_ = set; } | |
150 bool is_chunked() { return is_chunked_; } | |
wtc
2011/01/14 03:09:31
Nit: mark this method 'const'.
Satish
2011/01/14 18:09:29
Done.
| |
151 | |
122 // Returns the total size in bytes of the data to upload. | 152 // Returns the total size in bytes of the data to upload. |
123 uint64 GetContentLength(); | 153 uint64 GetContentLength(); |
124 | 154 |
125 std::vector<Element>* elements() { | 155 std::vector<Element>* elements() { |
126 return &elements_; | 156 return &elements_; |
127 } | 157 } |
128 | 158 |
129 void SetElements(const std::vector<Element>& elements); | 159 void SetElements(const std::vector<Element>& elements); |
130 | 160 |
131 void swap_elements(std::vector<Element>* elements) { | 161 void swap_elements(std::vector<Element>* elements) { |
132 elements_.swap(*elements); | 162 elements_.swap(*elements); |
133 } | 163 } |
134 | 164 |
135 // Identifies a particular upload instance, which is used by the cache to | 165 // Identifies a particular upload instance, which is used by the cache to |
136 // formulate a cache key. This value should be unique across browser | 166 // formulate a cache key. This value should be unique across browser |
137 // sessions. A value of 0 is used to indicate an unspecified identifier. | 167 // sessions. A value of 0 is used to indicate an unspecified identifier. |
138 void set_identifier(int64 id) { identifier_ = id; } | 168 void set_identifier(int64 id) { identifier_ = id; } |
139 int64 identifier() const { return identifier_; } | 169 int64 identifier() const { return identifier_; } |
140 | 170 |
141 private: | 171 private: |
142 friend class base::RefCounted<UploadData>; | 172 friend class base::RefCounted<UploadData>; |
143 | 173 |
144 ~UploadData(); | 174 ~UploadData(); |
145 | 175 |
146 std::vector<Element> elements_; | 176 std::vector<Element> elements_; |
147 int64 identifier_; | 177 int64 identifier_; |
178 ChunkCallback* chunk_callback_; | |
179 bool is_chunked_; | |
148 | 180 |
149 DISALLOW_COPY_AND_ASSIGN(UploadData); | 181 DISALLOW_COPY_AND_ASSIGN(UploadData); |
150 }; | 182 }; |
151 | 183 |
152 #if defined(UNIT_TEST) | 184 #if defined(UNIT_TEST) |
153 inline bool operator==(const UploadData::Element& a, | 185 inline bool operator==(const UploadData::Element& a, |
154 const UploadData::Element& b) { | 186 const UploadData::Element& b) { |
155 if (a.type() != b.type()) | 187 if (a.type() != b.type()) |
156 return false; | 188 return false; |
157 if (a.type() == UploadData::TYPE_BYTES) | 189 if (a.type() == UploadData::TYPE_BYTES) |
(...skipping 12 matching lines...) Expand all Loading... | |
170 | 202 |
171 inline bool operator!=(const UploadData::Element& a, | 203 inline bool operator!=(const UploadData::Element& a, |
172 const UploadData::Element& b) { | 204 const UploadData::Element& b) { |
173 return !(a == b); | 205 return !(a == b); |
174 } | 206 } |
175 #endif // defined(UNIT_TEST) | 207 #endif // defined(UNIT_TEST) |
176 | 208 |
177 } // namespace net | 209 } // namespace net |
178 | 210 |
179 #endif // NET_BASE_UPLOAD_DATA_H_ | 211 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |