Chromium Code Reviews| 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" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 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_FILE, | 27 TYPE_FILE, |
| 28 TYPE_BLOB | 28 TYPE_BLOB |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 class Item { | 31 struct Item { |
| 32 public: | |
| 33 Item(); | 32 Item(); |
| 34 ~Item(); | 33 ~Item(); |
| 35 | 34 |
| 36 Type type() const { return type_; } | 35 Type type() const { return type_; } |
| 37 const std::string& data() const { return data_; } | 36 const std::string& data() const { return data_; } |
| 38 const FilePath& file_path() const { return file_path_; } | 37 const FilePath& file_path() const { return file_path_; } |
| 39 const GURL& blob_url() const { return blob_url_; } | 38 const GURL& blob_url() const { return blob_url_; } |
| 40 uint64 offset() const { return offset_; } | 39 uint64 offset() const { return offset_; } |
| 41 uint64 length() const { return length_; } | 40 uint64 length() const { return length_; } |
| 42 const base::Time& expected_modification_time() const { | 41 const base::Time& expected_modification_time() const { |
| 43 return expected_modification_time_; | 42 return expected_modification_time_; |
| 44 } | 43 } |
| 45 | 44 |
| 46 void SetToData(const std::string& data) { | 45 void SetToData(const std::string& data) { |
| 47 SetToData(data, 0, static_cast<uint32>(data.size())); | 46 SetToData(data, 0, static_cast<uint32>(data.size())); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void SetToData(const std::string& data, uint32 offset, uint32 length) { | 49 void SetToData(const std::string& data, uint32 offset, uint32 length) { |
| 51 // TODO(jianli): Need to implement ref-counting vector storage. | 50 // TODO(jianli): Need to implement ref-counting vector storage. |
| 52 type_ = TYPE_DATA; | 51 type_ = TYPE_DATA; |
| 53 data_ = data; | 52 data_ = data; |
| 54 offset_ = offset; | 53 offset_ = offset; |
| 55 length_ = length; | 54 length_ = length; |
| 56 } | 55 } |
| 57 | 56 |
| 57 void SetToData(const char* data, int length) { | |
|
jianli
2011/09/27 01:12:18
Why not using uint32?
michaeln
2011/09/27 23:27:31
Done, switched to using size_t which is what std::
| |
| 58 type_ = TYPE_DATA; | |
| 59 data_.assign(data, length); | |
| 60 offset_ = 0; | |
| 61 length_ = length; | |
| 62 } | |
| 63 | |
| 58 void SetToFile(const FilePath& file_path, uint64 offset, uint64 length, | 64 void SetToFile(const FilePath& file_path, uint64 offset, uint64 length, |
| 59 const base::Time& expected_modification_time) { | 65 const base::Time& expected_modification_time) { |
| 60 type_ = TYPE_FILE; | 66 type_ = TYPE_FILE; |
| 61 file_path_ = file_path; | 67 file_path_ = file_path; |
| 62 offset_ = offset; | 68 offset_ = offset; |
| 63 length_ = length; | 69 length_ = length; |
| 64 expected_modification_time_ = expected_modification_time; | 70 expected_modification_time_ = expected_modification_time; |
| 65 } | 71 } |
| 66 | 72 |
| 67 void SetToBlob(const GURL& blob_url, uint64 offset, uint64 length) { | 73 void SetToBlob(const GURL& blob_url, uint64 offset, uint64 length) { |
| 68 type_ = TYPE_BLOB; | 74 type_ = TYPE_BLOB; |
| 69 blob_url_ = blob_url; | 75 blob_url_ = blob_url; |
| 70 offset_ = offset; | 76 offset_ = offset; |
| 71 length_ = length; | 77 length_ = length; |
| 72 } | 78 } |
| 73 | 79 |
| 74 private: | |
| 75 Type type_; | 80 Type type_; |
| 76 | 81 |
| 77 // For Data type. | 82 // For Data type. |
| 78 std::string data_; | 83 std::string data_; |
| 79 | 84 |
| 80 // For File type. | 85 // For File type. |
| 81 FilePath file_path_; | 86 FilePath file_path_; |
| 82 | 87 |
| 83 // For Blob typ. | 88 // For Blob typ. |
| 84 GURL blob_url_; | 89 GURL blob_url_; |
| 85 | 90 |
| 86 uint64 offset_; | 91 uint64 offset_; |
| 87 uint64 length_; | 92 uint64 length_; |
| 88 base::Time expected_modification_time_; | 93 base::Time expected_modification_time_; |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 BlobData(); | 96 BlobData(); |
| 92 explicit BlobData(const WebKit::WebBlobData& data); | 97 explicit BlobData(const WebKit::WebBlobData& data); |
| 93 | 98 |
| 99 void AppendItem(const Item& item) { | |
| 100 items_.push_back(item); | |
| 101 } | |
| 102 | |
| 94 void AppendData(const std::string& data) { | 103 void AppendData(const std::string& data) { |
| 95 // TODO(jianli): Consider writing the big data to the disk. | 104 // TODO(jianli): Consider writing the big data to the disk. |
| 96 AppendData(data, 0, static_cast<uint32>(data.size())); | 105 AppendData(data, 0, static_cast<uint32>(data.size())); |
| 97 } | 106 } |
| 98 | 107 |
| 99 void AppendData(const std::string& data, uint32 offset, uint32 length) { | 108 void AppendData(const std::string& data, uint32 offset, uint32 length) { |
| 100 if (length > 0) { | 109 if (length > 0) { |
| 101 items_.push_back(Item()); | 110 items_.push_back(Item()); |
| 102 items_.back().SetToData(data, offset, length); | 111 items_.back().SetToData(data, offset, length); |
| 103 } | 112 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 126 content_type_ = content_type; | 135 content_type_ = content_type; |
| 127 } | 136 } |
| 128 | 137 |
| 129 const std::string& content_disposition() const { | 138 const std::string& content_disposition() const { |
| 130 return content_disposition_; | 139 return content_disposition_; |
| 131 } | 140 } |
| 132 void set_content_disposition(const std::string& content_disposition) { | 141 void set_content_disposition(const std::string& content_disposition) { |
| 133 content_disposition_ = content_disposition; | 142 content_disposition_ = content_disposition; |
| 134 } | 143 } |
| 135 | 144 |
| 136 // Should only be called by the IPC ParamTraits for this class. | 145 int64 GetMemoryUsage() const { |
| 137 void swap_items(std::vector<Item>* items) { | 146 int64 memory = 0; |
| 138 items_.swap(*items); | 147 for (std::vector<Item>::const_iterator iter = items_.begin(); |
| 148 iter != items_.end(); ++iter) { | |
| 149 if (iter->type() == TYPE_DATA) | |
| 150 memory += iter->data_.size(); | |
| 151 } | |
| 152 return memory; | |
| 139 } | 153 } |
| 140 | 154 |
| 141 private: | 155 private: |
| 142 friend class base::RefCounted<BlobData>; | 156 friend class base::RefCounted<BlobData>; |
| 143 | 157 |
| 144 virtual ~BlobData(); | 158 virtual ~BlobData(); |
| 145 | 159 |
| 146 std::string content_type_; | 160 std::string content_type_; |
| 147 std::string content_disposition_; | 161 std::string content_disposition_; |
| 148 std::vector<Item> items_; | 162 std::vector<Item> items_; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 } | 207 } |
| 194 | 208 |
| 195 inline bool operator!=(const BlobData& a, const BlobData& b) { | 209 inline bool operator!=(const BlobData& a, const BlobData& b) { |
| 196 return !(a == b); | 210 return !(a == b); |
| 197 } | 211 } |
| 198 #endif // defined(UNIT_TEST) | 212 #endif // defined(UNIT_TEST) |
| 199 | 213 |
| 200 } // namespace webkit_blob | 214 } // namespace webkit_blob |
| 201 | 215 |
| 202 #endif // WEBKIT_BLOB_BLOB_DATA_H_ | 216 #endif // WEBKIT_BLOB_BLOB_DATA_H_ |
| OLD | NEW |