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