| 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 WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ | 5 #ifndef WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ |
| 6 #define WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ | 6 #define WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 13 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
| 14 #include "base/time.h" | 13 #include "webkit/base/data_element.h" |
| 15 #include "googleurl/src/gurl.h" | |
| 16 #include "webkit/glue/webkit_glue_export.h" | 14 #include "webkit/glue/webkit_glue_export.h" |
| 17 | 15 |
| 18 namespace net { | 16 namespace net { |
| 19 class UploadData; | 17 class UploadData; |
| 20 class UploadElement; | 18 class UploadElement; |
| 21 } | 19 } |
| 22 | 20 |
| 23 namespace webkit_blob { | 21 namespace webkit_blob { |
| 24 class BlobStorageController; | 22 class BlobStorageController; |
| 25 } | 23 } |
| 26 | 24 |
| 27 namespace webkit_glue { | 25 namespace webkit_glue { |
| 28 | 26 |
| 29 // A struct used to represent upload data. The data field is populated by | 27 // A struct used to represent upload data. The data field is populated by |
| 30 // WebURLLoader from the data given as WebHTTPBody. | 28 // 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 | 29 class WEBKIT_GLUE_EXPORT ResourceRequestBody |
| 36 : public base::RefCounted<ResourceRequestBody>, | 30 : public base::RefCounted<ResourceRequestBody>, |
| 37 public base::SupportsUserData { | 31 public base::SupportsUserData { |
| 38 public: | 32 public: |
| 39 enum Type { | 33 typedef webkit_base::DataElement Element; |
| 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 | 34 |
| 116 ResourceRequestBody(); | 35 ResourceRequestBody(); |
| 117 | 36 |
| 118 void AppendBytes(const char* bytes, int bytes_len); | 37 void AppendBytes(const char* bytes, int bytes_len); |
| 119 void AppendFileRange(const FilePath& file_path, | 38 void AppendFileRange(const FilePath& file_path, |
| 120 uint64 offset, uint64 length, | 39 uint64 offset, uint64 length, |
| 121 const base::Time& expected_modification_time); | 40 const base::Time& expected_modification_time); |
| 122 void AppendBlob(const GURL& blob_url); | 41 void AppendBlob(const GURL& blob_url); |
| 123 | 42 |
| 124 // Creates a new UploadData from this request body. This also resolves | 43 // Creates a new UploadData from this request body. This also resolves |
| 125 // any blob references using given |blob_controller|. | 44 // any blob references using given |blob_controller|. |
| 126 // TODO(kinuko): Clean up this hack. | 45 // TODO(kinuko): Clean up this hack. |
| 127 net::UploadData* ResolveElementsAndCreateUploadData( | 46 net::UploadData* ResolveElementsAndCreateUploadData( |
| 128 webkit_blob::BlobStorageController* blob_controller); | 47 webkit_blob::BlobStorageController* blob_controller); |
| 129 | 48 |
| 130 const std::vector<Element>* elements() const { | 49 const std::vector<Element>* elements() const { return &elements_; } |
| 131 return &elements_; | 50 std::vector<Element>* elements_mutable() { return &elements_; } |
| 132 } | |
| 133 | |
| 134 std::vector<Element>* elements_mutable() { | |
| 135 return &elements_; | |
| 136 } | |
| 137 | |
| 138 void swap_elements(std::vector<Element>* elements) { | 51 void swap_elements(std::vector<Element>* elements) { |
| 139 elements_.swap(*elements); | 52 elements_.swap(*elements); |
| 140 } | 53 } |
| 141 | 54 |
| 142 // Identifies a particular upload instance, which is used by the cache to | 55 // Identifies a particular upload instance, which is used by the cache to |
| 143 // formulate a cache key. This value should be unique across browser | 56 // formulate a cache key. This value should be unique across browser |
| 144 // sessions. A value of 0 is used to indicate an unspecified identifier. | 57 // sessions. A value of 0 is used to indicate an unspecified identifier. |
| 145 void set_identifier(int64 id) { identifier_ = id; } | 58 void set_identifier(int64 id) { identifier_ = id; } |
| 146 int64 identifier() const { return identifier_; } | 59 int64 identifier() const { return identifier_; } |
| 147 | 60 |
| 148 private: | 61 private: |
| 149 friend class base::RefCounted<ResourceRequestBody>; | 62 friend class base::RefCounted<ResourceRequestBody>; |
| 150 virtual ~ResourceRequestBody(); | 63 virtual ~ResourceRequestBody(); |
| 151 | 64 |
| 152 // Resolves the |blob_url| using |blob_controller| and appends resolved | 65 // Resolves the |blob_url| using |blob_controller| and appends resolved |
| 153 // items to |elements|. | 66 // items to |elements|. |
| 154 void ResolveBlobReference(webkit_blob::BlobStorageController* blob_controller, | 67 void ResolveBlobReference(webkit_blob::BlobStorageController* blob_controller, |
| 155 const GURL& blob_url, | 68 const GURL& blob_url, |
| 156 std::vector<net::UploadElement>* elements); | 69 std::vector<net::UploadElement>* elements); |
| 157 | 70 |
| 158 std::vector<Element> elements_; | 71 std::vector<Element> elements_; |
| 159 int64 identifier_; | 72 int64 identifier_; |
| 160 | 73 |
| 161 DISALLOW_COPY_AND_ASSIGN(ResourceRequestBody); | 74 DISALLOW_COPY_AND_ASSIGN(ResourceRequestBody); |
| 162 }; | 75 }; |
| 163 | 76 |
| 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 | 77 } // namespace webkit_glue |
| 191 | 78 |
| 192 #endif // WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ | 79 #endif // WEBKIT_GLUE_RESOURCE_REQUEST_BODY_H_ |
| OLD | NEW |