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_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/callback_forward.h" | |
12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 class FileStream; | 22 class FileStream; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 101 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
101 | 102 |
102 bool is_last_chunk() const { return is_last_chunk_; } | 103 bool is_last_chunk() const { return is_last_chunk_; } |
103 // Sets whether this is the last chunk. Used during IPC marshalling. | 104 // Sets whether this is the last chunk. Used during IPC marshalling. |
104 void set_is_last_chunk(bool is_last_chunk) { | 105 void set_is_last_chunk(bool is_last_chunk) { |
105 is_last_chunk_ = is_last_chunk; | 106 is_last_chunk_ = is_last_chunk; |
106 } | 107 } |
107 | 108 |
108 // Returns the byte-length of the element. For files that do not exist, 0 | 109 // Returns the byte-length of the element. For files that do not exist, 0 |
109 // is returned. This is done for consistency with Mozilla. | 110 // is returned. This is done for consistency with Mozilla. |
110 // Once called, this function will always return the same value. | 111 // Once called, this function will always return the same value. |
robertshield
2012/02/04 05:39:04
it looks like this last sentence is only true if t
satorux1
2012/02/04 07:18:32
You are right. The caching works only for TYPE_FIL
| |
111 uint64 GetContentLength(); | 112 uint64 GetContentLength(); |
112 | 113 |
113 // Returns a FileStream opened for reading for this element, positioned at | 114 // Returns a FileStream opened for reading for this element, positioned at |
114 // |file_range_offset_|. The caller gets ownership and is responsible | 115 // |file_range_offset_|. The caller gets ownership and is responsible |
115 // for cleaning up the FileStream. Returns NULL if this element is not of | 116 // for cleaning up the FileStream. Returns NULL if this element is not of |
116 // type TYPE_FILE or if the file is not openable. | 117 // type TYPE_FILE or if the file is not openable. |
117 FileStream* NewFileStreamForReading(); | 118 FileStream* NewFileStreamForReading(); |
118 | 119 |
119 private: | 120 private: |
120 // Allows tests to override the result of GetContentLength. | 121 // Allows tests to override the result of GetContentLength. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 157 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
157 | 158 |
158 // Sets the callback to be invoked when a new chunk is available to upload. | 159 // Sets the callback to be invoked when a new chunk is available to upload. |
159 void set_chunk_callback(ChunkCallback* callback); | 160 void set_chunk_callback(ChunkCallback* callback); |
160 | 161 |
161 // Initializes the object to send chunks of upload data over time rather | 162 // Initializes the object to send chunks of upload data over time rather |
162 // than all at once. | 163 // than all at once. |
163 void set_is_chunked(bool set) { is_chunked_ = set; } | 164 void set_is_chunked(bool set) { is_chunked_ = set; } |
164 bool is_chunked() const { return is_chunked_; } | 165 bool is_chunked() const { return is_chunked_; } |
165 | 166 |
167 // Gets the total size in bytes of the data to upload. Computing the | |
168 // content length can result in performing file IO hence the operation is | |
169 // done asynchronously. Runs the callback with the content length once the | |
170 // computation is done. | |
171 typedef base::Callback<void(uint64 content_length)> ContentLengthCallback; | |
172 void GetContentLength(const ContentLengthCallback& callback); | |
robertshield
2012/02/04 05:39:04
This only hits the IO thread when there are elemen
satorux1
2012/02/04 07:18:32
Sounds reasonable. I originally thought it'd be si
| |
173 | |
166 // Returns the total size in bytes of the data to upload. | 174 // Returns the total size in bytes of the data to upload. |
167 uint64 GetContentLength(); | 175 // This version may perform file IO on the current thread. |
176 // TODO(satorux): Remove this once clients are gone. | |
robertshield
2012/02/04 05:39:04
Are you planning on attempting to convert the Urlm
satorux1
2012/02/04 07:18:32
No. I don't plan to make a major change to chrome_
robertshield
2012/02/04 21:10:56
Thanks! That looks good to me.
| |
177 uint64 GetContentLengthSyncHack(); | |
178 | |
179 // Returns the total size in bytes of the data to upload, for testing. | |
180 // This version may perform file IO on the current thread. | |
181 // Should only be used in tests where file IO is allowed. | |
182 uint64 GetContentLengthSyncForTesting(); | |
168 | 183 |
169 // Returns true if the upload data is entirely in memory (i.e. the | 184 // Returns true if the upload data is entirely in memory (i.e. the |
170 // upload data is not chunked, and all elemnts are of TYPE_BYTES). | 185 // upload data is not chunked, and all elemnts are of TYPE_BYTES). |
171 bool IsInMemory() const; | 186 bool IsInMemory() const; |
172 | 187 |
173 std::vector<Element>* elements() { | 188 std::vector<Element>* elements() { |
174 return &elements_; | 189 return &elements_; |
175 } | 190 } |
176 | 191 |
177 void SetElements(const std::vector<Element>& elements); | 192 void SetElements(const std::vector<Element>& elements); |
178 | 193 |
179 void swap_elements(std::vector<Element>* elements) { | 194 void swap_elements(std::vector<Element>* elements) { |
180 elements_.swap(*elements); | 195 elements_.swap(*elements); |
181 } | 196 } |
182 | 197 |
183 // Identifies a particular upload instance, which is used by the cache to | 198 // Identifies a particular upload instance, which is used by the cache to |
184 // formulate a cache key. This value should be unique across browser | 199 // formulate a cache key. This value should be unique across browser |
185 // sessions. A value of 0 is used to indicate an unspecified identifier. | 200 // sessions. A value of 0 is used to indicate an unspecified identifier. |
186 void set_identifier(int64 id) { identifier_ = id; } | 201 void set_identifier(int64 id) { identifier_ = id; } |
187 int64 identifier() const { return identifier_; } | 202 int64 identifier() const { return identifier_; } |
188 | 203 |
189 private: | 204 private: |
205 // Helper function for GetContentLength(). | |
206 void DoGetContentLength(uint64* content_length); | |
207 | |
190 friend class base::RefCounted<UploadData>; | 208 friend class base::RefCounted<UploadData>; |
191 | 209 |
192 ~UploadData(); | 210 ~UploadData(); |
193 | 211 |
194 std::vector<Element> elements_; | 212 std::vector<Element> elements_; |
195 int64 identifier_; | 213 int64 identifier_; |
196 ChunkCallback* chunk_callback_; | 214 ChunkCallback* chunk_callback_; |
197 bool is_chunked_; | 215 bool is_chunked_; |
198 | 216 |
199 DISALLOW_COPY_AND_ASSIGN(UploadData); | 217 DISALLOW_COPY_AND_ASSIGN(UploadData); |
(...skipping 20 matching lines...) Expand all Loading... | |
220 | 238 |
221 inline bool operator!=(const UploadData::Element& a, | 239 inline bool operator!=(const UploadData::Element& a, |
222 const UploadData::Element& b) { | 240 const UploadData::Element& b) { |
223 return !(a == b); | 241 return !(a == b); |
224 } | 242 } |
225 #endif // defined(UNIT_TEST) | 243 #endif // defined(UNIT_TEST) |
226 | 244 |
227 } // namespace net | 245 } // namespace net |
228 | 246 |
229 #endif // NET_BASE_UPLOAD_DATA_H_ | 247 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |