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_RESOURCE_REQUEST_BODY_H_ | |
6 #define WEBKIT_GLUE_RESOURCE_REQUEST_BODY_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 namespace webkit_glue { | |
darin (slow to review)
2012/08/15 17:49:55
nit: add a new line below here
kinuko
2012/08/16 08:14:59
Done.
| |
23 // A struct used to represent upload data. The data field is populated by | |
24 // WebURLLoader from the data given as WebHTTPBody. | |
25 class WEBKIT_GLUE_EXPORT ResourceRequestBody | |
26 : public base::RefCounted<ResourceRequestBody>, | |
27 public base::SupportsUserData { | |
28 public: | |
29 enum Type { | |
30 TYPE_BYTES, | |
31 TYPE_FILE, | |
32 TYPE_BLOB, | |
33 }; | |
34 | |
35 class WEBKIT_GLUE_EXPORT Element { | |
36 public: | |
37 Element(); | |
38 ~Element(); | |
39 | |
40 Type type() const { return type_; } | |
41 // Explicitly sets the type of this Element. Used during IPC | |
42 // marshalling. | |
43 void set_type(Type type) { | |
44 type_ = type; | |
45 } | |
46 | |
47 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } | |
48 uint64 bytes_length() const { return buf_.size() + bytes_length_; } | |
49 const FilePath& file_path() const { return file_path_; } | |
50 uint64 file_range_offset() const { return file_range_offset_; } | |
51 uint64 file_range_length() const { return file_range_length_; } | |
52 // If NULL time is returned, we do not do the check. | |
53 const base::Time& expected_file_modification_time() const { | |
54 return expected_file_modification_time_; | |
55 } | |
56 const GURL& blob_url() const { return blob_url_; } | |
57 | |
58 void SetToBytes(const char* bytes, int bytes_len) { | |
59 type_ = TYPE_BYTES; | |
60 buf_.assign(bytes, bytes + bytes_len); | |
61 } | |
62 | |
63 void SetToSharedBytes(const char* bytes, int bytes_len) { | |
64 type_ = TYPE_BYTES; | |
65 bytes_start_ = bytes; | |
66 bytes_length_ = bytes_len; | |
67 } | |
68 | |
69 void SetToFilePath(const FilePath& path) { | |
70 SetToFilePathRange(path, 0, kuint64max, base::Time()); | |
71 } | |
72 | |
73 // If expected_modification_time is NULL, we do not check for the file | |
74 // change. Also note that the granularity for comparison is time_t, not | |
75 // the full precision. | |
76 void SetToFilePathRange(const FilePath& path, | |
77 uint64 offset, uint64 length, | |
78 const base::Time& expected_modification_time) { | |
79 type_ = TYPE_FILE; | |
80 file_path_ = path; | |
81 file_range_offset_ = offset; | |
82 file_range_length_ = length; | |
83 expected_file_modification_time_ = expected_modification_time; | |
84 } | |
85 | |
86 void SetToBlobUrl(const GURL& blob_url) { | |
87 type_ = TYPE_BLOB; | |
88 blob_url_ = blob_url; | |
89 } | |
90 | |
91 private: | |
92 Type type_; | |
93 std::vector<char> buf_; | |
94 const char* bytes_start_; | |
95 uint64 bytes_length_; | |
96 FilePath file_path_; | |
97 uint64 file_range_offset_; | |
98 uint64 file_range_length_; | |
99 base::Time expected_file_modification_time_; | |
100 GURL blob_url_; | |
101 }; | |
102 | |
103 ResourceRequestBody(); | |
104 | |
105 void AppendBytes(const char* bytes, int bytes_len); | |
106 void AppendFileRange(const FilePath& file_path, | |
107 uint64 offset, uint64 length, | |
108 const base::Time& expected_modification_time); | |
109 void AppendBlob(const GURL& blob_url); | |
110 | |
111 // Populate the given |net_upload_data| using the elements data. | |
112 // At this stage the elements should not contain any TYPE_BLOB items | |
113 // (i.e. must have resolved them by ResolveBlobReferencesInUploadData). | |
114 // TODO(kinuko): Clean up this hack. | |
115 void PopulateUploadData(net::UploadData* upload_data); | |
116 | |
117 const std::vector<Element>* elements() const { | |
118 return &elements_; | |
119 } | |
120 | |
121 std::vector<Element>* elements_mutable() { | |
122 return &elements_; | |
123 } | |
124 | |
125 void swap_elements(std::vector<Element>* elements) { | |
126 elements_.swap(*elements); | |
127 } | |
128 | |
129 // Identifies a particular upload instance, which is used by the cache to | |
130 // formulate a cache key. This value should be unique across browser | |
131 // sessions. A value of 0 is used to indicate an unspecified identifier. | |
132 void set_identifier(int64 id) { identifier_ = id; } | |
133 int64 identifier() const { return identifier_; } | |
134 | |
135 private: | |
136 friend class base::RefCounted<ResourceRequestBody>; | |
137 virtual ~ResourceRequestBody(); | |
138 | |
139 std::vector<Element> elements_; | |
140 int64 identifier_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(ResourceRequestBody); | |
143 }; | |
144 | |
145 #if defined(UNIT_TEST) | |
146 inline bool operator==(const ResourceRequestBody::Element& a, | |
147 const ResourceRequestBody::Element& b) { | |
148 if (a.type() != b.type()) | |
149 return false; | |
150 if (a.type() == ResourceRequestBody::TYPE_BYTES) | |
151 return a.bytes_length() == b.bytes_length() && | |
152 memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0; | |
153 if (a.type() == ResourceRequestBody::TYPE_FILE) { | |
154 return a.file_path() == b.file_path() && | |
155 a.file_range_offset() == b.file_range_offset() && | |
156 a.file_range_length() == b.file_range_length() && | |
157 a.expected_file_modification_time() == | |
158 b.expected_file_modification_time(); | |
159 } | |
160 if (a.type() == ResourceRequestBody::TYPE_BLOB) | |
161 return a.blob_url() == b.blob_url(); | |
162 return false; | |
163 } | |
164 | |
165 inline bool operator!=(const ResourceRequestBody::Element& a, | |
166 const ResourceRequestBody::Element& b) { | |
167 return !(a == b); | |
168 } | |
169 #endif // defined(UNIT_TEST) | |
170 | |
171 } // namespace webkit_glue | |
172 | |
173 #endif // WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ | |
OLD | NEW |