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