OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEBKIT_GLUE_WEBUPLOAD_DATA_H_ | |
6 #define WEBKIT_GLUE_WEBUPLOAD_DATA_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/file_path.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/supports_user_data.h" | |
14 #include "base/time.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "webkit/glue/webkit_glue_export.h" | |
17 | |
18 namespace net { | |
19 class UploadData; | |
20 } | |
21 | |
22 // A struct used to represent upload data. The data field is populated by | |
23 // WebURLLoader from the data given as WebHTTPBody. | |
24 class WEBKIT_GLUE_EXPORT WebUploadData | |
kinuko
2012/08/13 15:52:09
Having this in webkit/glue is probably not ideal
darin (slow to review)
2012/08/13 16:48:49
Seems like it could live in content/public/common/
darin (slow to review)
2012/08/13 20:47:36
Also, it might be good to use a prefix of "Resourc
kinuko
2012/08/14 15:33:32
I think I like the latter idea (since it can kill
| |
25 : public base::RefCounted<WebUploadData>, | |
26 public base::SupportsUserData { | |
27 public: | |
28 enum Type { | |
29 TYPE_BYTES, | |
30 TYPE_FILE, | |
31 TYPE_BLOB, | |
32 }; | |
33 | |
34 class WEBKIT_GLUE_EXPORT Element { | |
35 public: | |
36 Element(); | |
37 ~Element(); | |
38 | |
39 Type type() const { return type_; } | |
40 // Explicitly sets the type of this Element. Used during IPC | |
41 // marshalling. | |
42 void set_type(Type type) { | |
43 type_ = type; | |
44 } | |
45 | |
46 const std::vector<char>& bytes() const { return bytes_; } | |
47 const FilePath& file_path() const { return file_path_; } | |
48 uint64 file_range_offset() const { return file_range_offset_; } | |
49 uint64 file_range_length() const { return file_range_length_; } | |
50 // If NULL time is returned, we do not do the check. | |
51 const base::Time& expected_file_modification_time() const { | |
52 return expected_file_modification_time_; | |
53 } | |
54 const GURL& blob_url() const { return blob_url_; } | |
55 | |
56 void SetToBytes(const char* bytes, int bytes_len) { | |
57 type_ = TYPE_BYTES; | |
58 bytes_.assign(bytes, bytes + bytes_len); | |
59 } | |
60 | |
61 void SetToFilePath(const FilePath& path) { | |
62 SetToFilePathRange(path, 0, kuint64max, base::Time()); | |
63 } | |
64 | |
65 // If expected_modification_time is NULL, we do not check for the file | |
66 // change. Also note that the granularity for comparison is time_t, not | |
67 // the full precision. | |
68 void SetToFilePathRange(const FilePath& path, | |
69 uint64 offset, uint64 length, | |
70 const base::Time& expected_modification_time) { | |
71 type_ = TYPE_FILE; | |
72 file_path_ = path; | |
73 file_range_offset_ = offset; | |
74 file_range_length_ = length; | |
75 expected_file_modification_time_ = expected_modification_time; | |
76 } | |
77 | |
78 void SetToBlobUrl(const GURL& blob_url) { | |
79 type_ = TYPE_BLOB; | |
80 blob_url_ = blob_url; | |
81 } | |
82 | |
83 private: | |
84 Type type_; | |
85 std::vector<char> bytes_; | |
86 FilePath file_path_; | |
87 uint64 file_range_offset_; | |
88 uint64 file_range_length_; | |
89 base::Time expected_file_modification_time_; | |
90 GURL blob_url_; | |
91 }; | |
92 | |
93 WebUploadData(); | |
94 | |
95 void AppendBytes(const char* bytes, int bytes_len); | |
96 void AppendFileRange(const FilePath& file_path, | |
97 uint64 offset, uint64 length, | |
98 const base::Time& expected_modification_time); | |
99 void AppendBlob(const GURL& blob_url); | |
100 | |
101 // Populate the given |net_upload_data| using the elements data. | |
102 // At this stage the elements should not contain any TYPE_BLOB items | |
103 // (i.e. must have resolved them by ResolveBlobReferencesInUploadData). | |
104 // TODO(kinuko): Clean up this hack. | |
105 void PopulateUploadData(net::UploadData* upload_data) const; | |
106 | |
107 const std::vector<Element>* elements() const { | |
108 return &elements_; | |
109 } | |
110 | |
111 std::vector<Element>* elements_mutable() { | |
112 return &elements_; | |
113 } | |
114 | |
115 void swap_elements(std::vector<Element>* elements) { | |
116 elements_.swap(*elements); | |
117 } | |
118 | |
119 // Identifies a particular upload instance, which is used by the cache to | |
120 // formulate a cache key. This value should be unique across browser | |
121 // sessions. A value of 0 is used to indicate an unspecified identifier. | |
122 void set_identifier(int64 id) { identifier_ = id; } | |
123 int64 identifier() const { return identifier_; } | |
124 | |
125 private: | |
126 friend class base::RefCounted<WebUploadData>; | |
127 virtual ~WebUploadData(); | |
128 | |
129 std::vector<Element> elements_; | |
130 int64 identifier_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(WebUploadData); | |
133 }; | |
134 | |
135 #if defined(UNIT_TEST) | |
136 inline bool operator==(const WebUploadData::Element& a, | |
137 const WebUploadData::Element& b) { | |
138 if (a.type() != b.type()) | |
139 return false; | |
140 if (a.type() == WebUploadData::TYPE_BYTES) | |
141 return a.bytes() == b.bytes(); | |
142 if (a.type() == WebUploadData::TYPE_FILE) { | |
143 return a.file_path() == b.file_path() && | |
144 a.file_range_offset() == b.file_range_offset() && | |
145 a.file_range_length() == b.file_range_length() && | |
146 a.expected_file_modification_time() == | |
147 b.expected_file_modification_time(); | |
148 } | |
149 if (a.type() == WebUploadData::TYPE_BLOB) | |
150 return a.blob_url() == b.blob_url(); | |
151 return false; | |
152 } | |
153 | |
154 inline bool operator!=(const WebUploadData::Element& a, | |
155 const WebUploadData::Element& b) { | |
156 return !(a == b); | |
157 } | |
158 #endif // defined(UNIT_TEST) | |
159 | |
160 #endif // WEBKIT_GLUE_WEBUPLOAD_DATA_H_ | |
OLD | NEW |