| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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_BLOB_BLOB_DATA_H_ |
| 6 #define WEBKIT_BLOB_BLOB_DATA_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" |
| 12 #include "base/ref_counted.h" |
| 13 #include "base/time.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 |
| 16 namespace WebKit { |
| 17 class WebBlobData; |
| 18 } |
| 19 |
| 20 namespace webkit_blob { |
| 21 |
| 22 class BlobData : public base::RefCounted<BlobData> { |
| 23 public: |
| 24 enum Type { |
| 25 TYPE_DATA, |
| 26 TYPE_FILE, |
| 27 TYPE_BLOB |
| 28 }; |
| 29 |
| 30 class Item { |
| 31 public: |
| 32 Item() |
| 33 : type_(TYPE_DATA), |
| 34 offset_(0), |
| 35 length_(0) { |
| 36 } |
| 37 |
| 38 Type type() const { return type_; } |
| 39 const std::string& data() const { return data_; } |
| 40 const FilePath& file_path() const { return file_path_; } |
| 41 const GURL& blob_url() const { return blob_url_; } |
| 42 uint64 offset() const { return offset_; } |
| 43 uint64 length() const { return length_; } |
| 44 const base::Time& expected_modification_time() const { |
| 45 return expected_modification_time_; |
| 46 } |
| 47 |
| 48 void SetToData(const std::string& data) { |
| 49 SetToData(data, 0, static_cast<uint32>(data.size())); |
| 50 } |
| 51 |
| 52 void SetToData(const std::string& data, uint32 offset, uint32 length) { |
| 53 // TODO(jianli): Need to implement ref-counting vector storage. |
| 54 type_ = TYPE_DATA; |
| 55 data_ = data; |
| 56 offset_ = offset; |
| 57 length_ = length; |
| 58 } |
| 59 |
| 60 void SetToFile(const FilePath& file_path, uint64 offset, uint64 length, |
| 61 const base::Time& expected_modification_time) { |
| 62 type_ = TYPE_FILE; |
| 63 file_path_ = file_path; |
| 64 offset_ = offset; |
| 65 length_ = length; |
| 66 expected_modification_time_ = expected_modification_time; |
| 67 } |
| 68 |
| 69 void SetToBlob(const GURL& blob_url, uint64 offset, uint64 length) { |
| 70 type_ = TYPE_BLOB; |
| 71 blob_url_ = blob_url; |
| 72 offset_ = offset; |
| 73 length_ = length; |
| 74 } |
| 75 |
| 76 #if defined(UNIT_TEST) |
| 77 bool operator==(const Item& other) const { |
| 78 if (type_ != other.type_) |
| 79 return false; |
| 80 if (type_ == TYPE_DATA) { |
| 81 return data_ == other.data_ && |
| 82 offset_ == other.offset_ && |
| 83 length_ == other.length_; |
| 84 } |
| 85 if (type_ == TYPE_FILE) { |
| 86 return file_path_ == other.file_path_ && |
| 87 offset_ == other.offset_ && |
| 88 length_ == other.length_ && |
| 89 expected_modification_time_ == other.expected_modification_time_; |
| 90 } |
| 91 if (type_ == TYPE_BLOB) { |
| 92 return blob_url_ == other.blob_url_ && |
| 93 offset_ == other.offset_ && |
| 94 length_ == other.length_; |
| 95 } |
| 96 return false; |
| 97 } |
| 98 |
| 99 bool operator!=(const Item& other) const { |
| 100 return !(*this == other); |
| 101 } |
| 102 #endif // defined(UNIT_TEST) |
| 103 |
| 104 private: |
| 105 Type type_; |
| 106 |
| 107 // For Data type. |
| 108 std::string data_; |
| 109 |
| 110 // For File type. |
| 111 FilePath file_path_; |
| 112 |
| 113 // For Blob typ. |
| 114 GURL blob_url_; |
| 115 |
| 116 uint64 offset_; |
| 117 uint64 length_; |
| 118 base::Time expected_modification_time_; |
| 119 }; |
| 120 |
| 121 BlobData() { } |
| 122 explicit BlobData(const WebKit::WebBlobData& data); |
| 123 |
| 124 void AppendData(const std::string& data) { |
| 125 // TODO(jianli): Consider writing the big data to the disk. |
| 126 AppendData(data, 0, static_cast<uint32>(data.size())); |
| 127 } |
| 128 |
| 129 void AppendData(const std::string& data, uint32 offset, uint32 length) { |
| 130 if (length > 0) { |
| 131 items_.push_back(Item()); |
| 132 items_.back().SetToData(data, offset, length); |
| 133 } |
| 134 } |
| 135 |
| 136 void AppendFile(const FilePath& file_path, uint64 offset, uint64 length, |
| 137 const base::Time& expected_modification_time) { |
| 138 items_.push_back(Item()); |
| 139 items_.back().SetToFile(file_path, offset, length, |
| 140 expected_modification_time); |
| 141 } |
| 142 |
| 143 void AppendBlob(const GURL& blob_url, uint64 offset, uint64 length) { |
| 144 items_.push_back(Item()); |
| 145 items_.back().SetToBlob(blob_url, offset, length); |
| 146 } |
| 147 |
| 148 const std::vector<Item>& items() const { return items_; } |
| 149 void set_items(const std::vector<Item>& items) { |
| 150 items_ = items; |
| 151 } |
| 152 void swap_items(std::vector<Item>* items) { |
| 153 items_.swap(*items); |
| 154 } |
| 155 |
| 156 const std::string& content_type() const { return content_type_; } |
| 157 void set_content_type(const std::string& content_type) { |
| 158 content_type_ = content_type; |
| 159 } |
| 160 |
| 161 const std::string& content_disposition() const { |
| 162 return content_disposition_; |
| 163 } |
| 164 void set_content_disposition(const std::string& content_disposition) { |
| 165 content_disposition_ = content_disposition; |
| 166 } |
| 167 |
| 168 #if defined(UNIT_TEST) |
| 169 bool operator==(const BlobData& other) const { |
| 170 if (content_type_ != other.content_type_) |
| 171 return false; |
| 172 if (content_disposition_ != other.content_disposition_) |
| 173 return false; |
| 174 if (items_.size() != other.items_.size()) |
| 175 return false; |
| 176 for (size_t i = 0; i < items_.size(); ++i) { |
| 177 if (items_[i] != other.items_[i]) |
| 178 return false; |
| 179 } |
| 180 return true; |
| 181 } |
| 182 #endif // defined(UNIT_TEST) |
| 183 |
| 184 private: |
| 185 friend class base::RefCounted<BlobData>; |
| 186 |
| 187 virtual ~BlobData() { } |
| 188 |
| 189 std::string content_type_; |
| 190 std::string content_disposition_; |
| 191 std::vector<Item> items_; |
| 192 }; |
| 193 |
| 194 } // namespace webkit_blob |
| 195 |
| 196 #endif // WEBKIT_BLOB_BLOB_DATA_H_ |
| OLD | NEW |