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