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 uint64 GetContentLength(); | 111 uint64 GetContentLength(); |
112 | 112 |
113 // Returns a FileStream opened for reading for this element, positioned at | 113 // Returns a FileStream opened for reading for this element, positioned at |
114 // |file_range_offset_|. The caller gets ownership and is responsible | 114 // |file_range_offset_|. The caller gets ownership and is responsible |
115 // for cleaning up the FileStream. Returns NULL if this element is not of | 115 // for cleaning up the FileStream. Returns NULL if this element is not of |
116 // type TYPE_FILE or if the file is not openable. | 116 // type TYPE_FILE or if the file is not openable. |
117 FileStream* NewFileStreamForReading(); | 117 FileStream* NewFileStreamForReading(); |
118 | 118 |
119 private: | 119 private: |
120 // Allows tests to override the result of GetContentLength. | 120 // 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); | 156 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
157 | 157 |
158 // Sets the callback to be invoked when a new chunk is available to upload. | 158 // Sets the callback to be invoked when a new chunk is available to upload. |
159 void set_chunk_callback(ChunkCallback* callback); | 159 void set_chunk_callback(ChunkCallback* callback); |
160 | 160 |
161 // Initializes the object to send chunks of upload data over time rather | 161 // Initializes the object to send chunks of upload data over time rather |
162 // than all at once. | 162 // than all at once. |
163 void set_is_chunked(bool set) { is_chunked_ = set; } | 163 void set_is_chunked(bool set) { is_chunked_ = set; } |
164 bool is_chunked() const { return is_chunked_; } | 164 bool is_chunked() const { return is_chunked_; } |
165 | 165 |
| 166 // Gets the total size in bytes of the data to upload. Computing the |
| 167 // content length can result in performing file IO hence the operation is |
| 168 // done asynchronously. Runs the callback with the content length once the |
| 169 // computation is done. |
| 170 typedef base::Callback<void(uint64 content_length)> ContentLengthCallback; |
| 171 void GetContentLength(const ContentLengthCallback& callback); |
| 172 |
| 173 // Returns the total size in bytes of the data to upload, for testing. |
| 174 // This version may perform file IO on the current thread. This function |
| 175 // will fail if called on a thread where file IO is prohibited. Usually |
| 176 // used for testing, but Chrome Frame also uses this version. |
| 177 uint64 GetContentLengthSync(); |
| 178 |
166 // Returns the total size in bytes of the data to upload. | 179 // Returns the total size in bytes of the data to upload. |
167 uint64 GetContentLength(); | 180 // This version may perform file IO on the current thread, but |
| 181 // ThreadRestrictions::AllowIO is placed so this function won't crash even |
| 182 // if it's called on a thread where file IO is prohibited. |
| 183 // |
| 184 // TODO(satorux): Remove this once clients are gone. |
| 185 uint64 GetContentLengthSyncHack(); |
168 | 186 |
169 // Returns true if the upload data is entirely in memory (i.e. the | 187 // 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). | 188 // upload data is not chunked, and all elemnts are of TYPE_BYTES). |
171 bool IsInMemory() const; | 189 bool IsInMemory() const; |
172 | 190 |
173 std::vector<Element>* elements() { | 191 std::vector<Element>* elements() { |
174 return &elements_; | 192 return &elements_; |
175 } | 193 } |
176 | 194 |
177 void SetElements(const std::vector<Element>& elements); | 195 void SetElements(const std::vector<Element>& elements); |
178 | 196 |
179 void swap_elements(std::vector<Element>* elements) { | 197 void swap_elements(std::vector<Element>* elements) { |
180 elements_.swap(*elements); | 198 elements_.swap(*elements); |
181 } | 199 } |
182 | 200 |
183 // Identifies a particular upload instance, which is used by the cache to | 201 // Identifies a particular upload instance, which is used by the cache to |
184 // formulate a cache key. This value should be unique across browser | 202 // formulate a cache key. This value should be unique across browser |
185 // sessions. A value of 0 is used to indicate an unspecified identifier. | 203 // sessions. A value of 0 is used to indicate an unspecified identifier. |
186 void set_identifier(int64 id) { identifier_ = id; } | 204 void set_identifier(int64 id) { identifier_ = id; } |
187 int64 identifier() const { return identifier_; } | 205 int64 identifier() const { return identifier_; } |
188 | 206 |
189 private: | 207 private: |
| 208 // Helper function for GetContentLength(). |
| 209 void DoGetContentLength(uint64* content_length); |
| 210 |
190 friend class base::RefCounted<UploadData>; | 211 friend class base::RefCounted<UploadData>; |
191 | 212 |
192 ~UploadData(); | 213 ~UploadData(); |
193 | 214 |
194 std::vector<Element> elements_; | 215 std::vector<Element> elements_; |
195 int64 identifier_; | 216 int64 identifier_; |
196 ChunkCallback* chunk_callback_; | 217 ChunkCallback* chunk_callback_; |
197 bool is_chunked_; | 218 bool is_chunked_; |
198 | 219 |
199 DISALLOW_COPY_AND_ASSIGN(UploadData); | 220 DISALLOW_COPY_AND_ASSIGN(UploadData); |
(...skipping 20 matching lines...) Expand all Loading... |
220 | 241 |
221 inline bool operator!=(const UploadData::Element& a, | 242 inline bool operator!=(const UploadData::Element& a, |
222 const UploadData::Element& b) { | 243 const UploadData::Element& b) { |
223 return !(a == b); | 244 return !(a == b); |
224 } | 245 } |
225 #endif // defined(UNIT_TEST) | 246 #endif // defined(UNIT_TEST) |
226 | 247 |
227 } // namespace net | 248 } // namespace net |
228 | 249 |
229 #endif // NET_BASE_UPLOAD_DATA_H_ | 250 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |