| 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_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/blob_export.h" | 15 #include "webkit/blob/blob_export.h" |
| 16 #include "webkit/blob/shareable_file_reference.h" | 16 #include "webkit/blob/shareable_file_reference.h" |
| 17 | 17 #include "webkit/base/data_element.h" |
| 18 namespace WebKit { | |
| 19 class WebBlobData; | |
| 20 } | |
| 21 | 18 |
| 22 namespace webkit_blob { | 19 namespace webkit_blob { |
| 23 | 20 |
| 24 class BLOB_EXPORT BlobData : public base::RefCounted<BlobData> { | 21 class BLOB_EXPORT BlobData : public base::RefCounted<BlobData> { |
| 25 public: | 22 public: |
| 26 enum Type { | 23 typedef webkit_base::DataElement Item; |
| 27 TYPE_DATA, | |
| 28 TYPE_DATA_EXTERNAL, | |
| 29 TYPE_FILE, | |
| 30 TYPE_BLOB | |
| 31 }; | |
| 32 | |
| 33 struct BLOB_EXPORT Item { | |
| 34 Item(); | |
| 35 ~Item(); | |
| 36 | |
| 37 void SetToData(const std::string& data) { | |
| 38 SetToData(data.c_str(), data.size()); | |
| 39 } | |
| 40 | |
| 41 void SetToData(const char* data, size_t length) { | |
| 42 type = TYPE_DATA; | |
| 43 this->data.assign(data, length); | |
| 44 this->offset = 0; | |
| 45 this->length = length; | |
| 46 } | |
| 47 | |
| 48 void SetToDataExternal(const char* data, size_t length) { | |
| 49 type = TYPE_DATA_EXTERNAL; | |
| 50 this->data_external = data; | |
| 51 this->offset = 0; | |
| 52 this->length = length; | |
| 53 } | |
| 54 | |
| 55 void SetToFile(const FilePath& file_path, uint64 offset, uint64 length, | |
| 56 const base::Time& expected_modification_time) { | |
| 57 type = TYPE_FILE; | |
| 58 this->file_path = file_path; | |
| 59 this->offset = offset; | |
| 60 this->length = length; | |
| 61 this->expected_modification_time = expected_modification_time; | |
| 62 } | |
| 63 | |
| 64 void SetToBlob(const GURL& blob_url, uint64 offset, uint64 length) { | |
| 65 type = TYPE_BLOB; | |
| 66 this->blob_url = blob_url; | |
| 67 this->offset = offset; | |
| 68 this->length = length; | |
| 69 } | |
| 70 | |
| 71 Type type; | |
| 72 std::string data; // For Data type. | |
| 73 const char* data_external; // For DataExternal type. | |
| 74 GURL blob_url; // For Blob type. | |
| 75 FilePath file_path; // For File type. | |
| 76 base::Time expected_modification_time; // Also for File type. | |
| 77 uint64 offset; | |
| 78 uint64 length; | |
| 79 }; | |
| 80 | 24 |
| 81 BlobData(); | 25 BlobData(); |
| 82 | 26 |
| 83 void AppendData(const std::string& data) { | 27 void AppendData(const std::string& data) { |
| 84 AppendData(data.c_str(), data.size()); | 28 AppendData(data.c_str(), data.size()); |
| 85 } | 29 } |
| 86 | 30 |
| 87 void AppendData(const char* data, size_t length); | 31 void AppendData(const char* data, size_t length); |
| 88 | 32 |
| 89 void AppendFile(const FilePath& file_path, uint64 offset, uint64 length, | 33 void AppendFile(const FilePath& file_path, uint64 offset, uint64 length, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 106 return content_disposition_; | 50 return content_disposition_; |
| 107 } | 51 } |
| 108 void set_content_disposition(const std::string& content_disposition) { | 52 void set_content_disposition(const std::string& content_disposition) { |
| 109 content_disposition_ = content_disposition; | 53 content_disposition_ = content_disposition; |
| 110 } | 54 } |
| 111 | 55 |
| 112 int64 GetMemoryUsage() const; | 56 int64 GetMemoryUsage() const; |
| 113 | 57 |
| 114 private: | 58 private: |
| 115 friend class base::RefCounted<BlobData>; | 59 friend class base::RefCounted<BlobData>; |
| 116 | |
| 117 virtual ~BlobData(); | 60 virtual ~BlobData(); |
| 118 | 61 |
| 119 std::string content_type_; | 62 std::string content_type_; |
| 120 std::string content_disposition_; | 63 std::string content_disposition_; |
| 121 std::vector<Item> items_; | 64 std::vector<Item> items_; |
| 122 std::vector<scoped_refptr<ShareableFileReference> > shareable_files_; | 65 std::vector<scoped_refptr<ShareableFileReference> > shareable_files_; |
| 123 | 66 |
| 124 DISALLOW_COPY_AND_ASSIGN(BlobData); | 67 DISALLOW_COPY_AND_ASSIGN(BlobData); |
| 125 }; | 68 }; |
| 126 | 69 |
| 127 #if defined(UNIT_TEST) | 70 #if defined(UNIT_TEST) |
| 128 inline bool operator==(const BlobData::Item& a, const BlobData::Item& b) { | |
| 129 if (a.type != b.type) | |
| 130 return false; | |
| 131 if (a.type == BlobData::TYPE_DATA) { | |
| 132 return a.data == b.data && | |
| 133 a.offset == b.offset && | |
| 134 a.length == b.length; | |
| 135 } | |
| 136 if (a.type == BlobData::TYPE_FILE) { | |
| 137 return a.file_path == b.file_path && | |
| 138 a.offset == b.offset && | |
| 139 a.length == b.length && | |
| 140 a.expected_modification_time == b.expected_modification_time; | |
| 141 } | |
| 142 if (a.type == BlobData::TYPE_BLOB) { | |
| 143 return a.blob_url == b.blob_url && | |
| 144 a.offset == b.offset && | |
| 145 a.length == b.length; | |
| 146 } | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 inline bool operator!=(const BlobData::Item& a, const BlobData::Item& b) { | |
| 151 return !(a == b); | |
| 152 } | |
| 153 | |
| 154 inline bool operator==(const BlobData& a, const BlobData& b) { | 71 inline bool operator==(const BlobData& a, const BlobData& b) { |
| 155 if (a.content_type() != b.content_type()) | 72 if (a.content_type() != b.content_type()) |
| 156 return false; | 73 return false; |
| 157 if (a.content_disposition() != b.content_disposition()) | 74 if (a.content_disposition() != b.content_disposition()) |
| 158 return false; | 75 return false; |
| 159 if (a.items().size() != b.items().size()) | 76 if (a.items().size() != b.items().size()) |
| 160 return false; | 77 return false; |
| 161 for (size_t i = 0; i < a.items().size(); ++i) { | 78 for (size_t i = 0; i < a.items().size(); ++i) { |
| 162 if (a.items()[i] != b.items()[i]) | 79 if (a.items()[i] != b.items()[i]) |
| 163 return false; | 80 return false; |
| 164 } | 81 } |
| 165 return true; | 82 return true; |
| 166 } | 83 } |
| 167 | 84 |
| 168 inline bool operator!=(const BlobData& a, const BlobData& b) { | 85 inline bool operator!=(const BlobData& a, const BlobData& b) { |
| 169 return !(a == b); | 86 return !(a == b); |
| 170 } | 87 } |
| 171 #endif // defined(UNIT_TEST) | 88 #endif // defined(UNIT_TEST) |
| 172 | 89 |
| 173 } // namespace webkit_blob | 90 } // namespace webkit_blob |
| 174 | 91 |
| 175 #endif // WEBKIT_BLOB_BLOB_DATA_H_ | 92 #endif // WEBKIT_BLOB_BLOB_DATA_H_ |
| OLD | NEW |