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 STORAGE_COMMON_BLOB_BLOB_DATA_H_ | 5 #ifndef STORAGE_COMMON_BLOB_BLOB_DATA_H_ |
6 #define STORAGE_COMMON_BLOB_BLOB_DATA_H_ | 6 #define STORAGE_COMMON_BLOB_BLOB_DATA_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "storage/common/blob/shareable_file_reference.h" | 15 #include "storage/common/blob/shareable_file_reference.h" |
16 #include "storage/common/data_element.h" | 16 #include "storage/common/data_element.h" |
17 #include "storage/common/storage_common_export.h" | 17 #include "storage/common/storage_common_export.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace storage { | 20 namespace storage { |
21 class BlobDataBuilder; | |
22 class BlobStorageContext; | |
21 | 23 |
22 class STORAGE_COMMON_EXPORT BlobData | 24 // Ref counted blob item. This class owns the backing data of the blob item. |
23 : public base::RefCounted<BlobData> { | 25 // The backing data is immutable, and cannot change after creation. |
26 // The purpose of this class is to allow the resource to stick around in the | |
27 // snapshot even after the resource was swapped in the blob (either to disk or | |
28 // to memory) by the BlobStorageContext. | |
29 class STORAGE_COMMON_EXPORT BlobDataItem | |
30 : public base::RefCountedThreadSafe<BlobDataItem> { | |
24 public: | 31 public: |
25 typedef storage::DataElement Item; | 32 storage::DataElement::Type type() const { return item_->type(); } |
33 const char* bytes() const { return item_->bytes(); } | |
34 const base::FilePath& path() const { return item_->path(); } | |
35 const GURL& filesystem_url() const { return item_->filesystem_url(); } | |
36 const std::string& blob_uuid() const { return item_->blob_uuid(); } | |
37 uint64 offset() const { return item_->offset(); } | |
38 uint64 length() const { return item_->length(); } | |
39 const base::Time& expected_modification_time() const { | |
40 return item_->expected_modification_time(); | |
41 } | |
42 const DataElement& data_element() const { return *item_; } | |
43 const DataElement* data_element_ptr() const { return item_.get(); } | |
26 | 44 |
27 // TODO(michaeln): remove the empty ctor when we fully transition to uuids. | 45 private: |
28 BlobData(); | 46 friend class BlobDataBuilder; |
29 explicit BlobData(const std::string& uuid); | 47 friend class BlobStorageContext; |
48 friend class base::RefCountedThreadSafe<BlobDataItem>; | |
49 BlobDataItem(scoped_ptr<DataElement> item); | |
50 BlobDataItem(scoped_ptr<DataElement> item, | |
51 scoped_refptr<ShareableFileReference> file_handle); | |
52 virtual ~BlobDataItem(); | |
53 | |
54 scoped_ptr<storage::DataElement> item_; | |
55 scoped_refptr<ShareableFileReference> file_handle_; | |
56 }; | |
57 | |
58 // Snapshot of a Blob. This snapshot holds a refcount of the current | |
59 // blob item resources so the backing storage for these items will stick | |
60 // around for the lifetime of this object. (The data represented by a blob is | |
61 // immutable, but the backing store can change). This structure thread safe. | |
62 class STORAGE_COMMON_EXPORT BlobDataSnapshot { | |
63 public: | |
64 BlobDataSnapshot(const BlobDataSnapshot& other); | |
65 ~BlobDataSnapshot(); | |
66 const std::string& uuid() const { return uuid_; } | |
67 const std::vector<scoped_refptr<BlobDataItem>>& items() const { | |
68 return items_; | |
69 } | |
70 const std::string& content_type() const { return content_type_; } | |
71 const std::string& content_disposition() const { | |
72 return content_disposition_; | |
73 } | |
74 size_t GetMemoryUsage() const; | |
75 | |
76 private: | |
77 friend class BlobDataBuilder; | |
78 BlobDataSnapshot(const std::string& uuid, | |
79 const std::string& content_type, | |
80 const std::string& content_disposition, | |
81 const std::vector<scoped_refptr<BlobDataItem>>& items); | |
82 | |
83 const std::string uuid_; | |
84 const std::string content_type_; | |
85 const std::string content_disposition_; | |
86 const std::vector<scoped_refptr<BlobDataItem>> items_; | |
87 }; | |
88 | |
89 class STORAGE_COMMON_EXPORT BlobDataBuilder { | |
90 public: | |
91 explicit BlobDataBuilder(const std::string& uuid); | |
92 virtual ~BlobDataBuilder(); | |
93 | |
94 const std::string& uuid() const { return uuid_; } | |
30 | 95 |
31 void AppendData(const std::string& data) { | 96 void AppendData(const std::string& data) { |
32 AppendData(data.c_str(), data.size()); | 97 AppendData(data.c_str(), data.size()); |
33 } | 98 } |
34 | 99 |
35 void AppendData(const char* data, size_t length); | 100 void AppendData(const char* data, size_t length); |
36 | 101 |
37 void AppendFile(const base::FilePath& file_path, uint64 offset, uint64 length, | 102 void AppendFile(const base::FilePath& file_path, |
103 uint64 offset, | |
104 uint64 length, | |
38 const base::Time& expected_modification_time); | 105 const base::Time& expected_modification_time); |
106 | |
107 void AppendFile(const base::FilePath& file_path, | |
108 uint64 offset, | |
109 uint64 length, | |
110 const base::Time& expected_modification_time, | |
111 scoped_refptr<ShareableFileReference> shareable_file); | |
112 | |
39 void AppendBlob(const std::string& uuid, uint64 offset, uint64 length); | 113 void AppendBlob(const std::string& uuid, uint64 offset, uint64 length); |
40 void AppendFileSystemFile(const GURL& url, uint64 offset, uint64 length, | 114 void AppendFileSystemFile(const GURL& url, uint64 offset, uint64 length, |
41 const base::Time& expected_modification_time); | 115 const base::Time& expected_modification_time); |
42 | 116 |
43 void AttachShareableFileReference(ShareableFileReference* reference) { | |
44 shareable_files_.push_back(reference); | |
45 } | |
46 | |
47 const std::string& uuid() const { return uuid_; } | |
48 const std::vector<Item>& items() const { return items_; } | |
49 const std::string& content_type() const { return content_type_; } | |
50 void set_content_type(const std::string& content_type) { | 117 void set_content_type(const std::string& content_type) { |
51 content_type_ = content_type; | 118 content_type_ = content_type; |
52 } | 119 } |
53 | 120 |
54 const std::string& content_disposition() const { | |
55 return content_disposition_; | |
56 } | |
57 void set_content_disposition(const std::string& content_disposition) { | 121 void set_content_disposition(const std::string& content_disposition) { |
58 content_disposition_ = content_disposition; | 122 content_disposition_ = content_disposition; |
59 } | 123 } |
60 | 124 |
61 int64 GetMemoryUsage() const; | 125 size_t GetMemoryUsage() const; |
126 | |
127 scoped_ptr<BlobDataSnapshot> Build(); | |
62 | 128 |
63 private: | 129 private: |
64 friend class base::RefCounted<BlobData>; | 130 friend class BlobStorageContext; |
65 virtual ~BlobData(); | 131 friend bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b); |
132 friend bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b); | |
66 | 133 |
67 std::string uuid_; | 134 std::string uuid_; |
68 std::string content_type_; | 135 std::string content_type_; |
69 std::string content_disposition_; | 136 std::string content_disposition_; |
70 std::vector<Item> items_; | 137 std::vector<scoped_refptr<BlobDataItem>> items_; |
71 std::vector<scoped_refptr<ShareableFileReference> > shareable_files_; | |
72 | 138 |
73 DISALLOW_COPY_AND_ASSIGN(BlobData); | 139 DISALLOW_COPY_AND_ASSIGN(BlobDataBuilder); |
74 }; | 140 }; |
75 | 141 |
76 #if defined(UNIT_TEST) | 142 #if defined(UNIT_TEST) |
77 inline bool operator==(const BlobData& a, const BlobData& b) { | 143 inline bool operator==(const BlobDataItem& a, const BlobDataItem& b) { |
78 if (a.content_type() != b.content_type()) | 144 return a.data_element() == b.data_element(); |
145 } | |
146 | |
147 inline bool operator!=(const BlobDataItem& a, const BlobDataItem& b) { | |
148 return !(a == b); | |
149 } | |
150 | |
151 inline bool operator==(const BlobDataBuilder& a, const BlobDataBuilder& b) { | |
152 if (a.content_type_ != b.content_type_) | |
79 return false; | 153 return false; |
80 if (a.content_disposition() != b.content_disposition()) | 154 if (a.content_disposition_ != b.content_disposition_) |
81 return false; | 155 return false; |
82 if (a.items().size() != b.items().size()) | 156 if (a.items_.size() != b.items_.size()) |
83 return false; | 157 return false; |
84 for (size_t i = 0; i < a.items().size(); ++i) { | 158 for (size_t i = 0; i < a.items_.size(); ++i) { |
85 if (a.items()[i] != b.items()[i]) | 159 if (a.items_[i] != b.items_[i]) |
86 return false; | 160 return false; |
87 } | 161 } |
88 return true; | 162 return true; |
89 } | 163 } |
90 | 164 |
91 inline bool operator!=(const BlobData& a, const BlobData& b) { | 165 inline bool operator==(const BlobDataSnapshot& a, const BlobDataBuilder& b) { |
michaeln
2015/01/16 00:09:09
an odd operator
dmurph
2015/01/16 23:45:56
It's to make testing easier. We grab a snapshot a
| |
166 if (a.content_type() != b.content_type_) { | |
167 printf("content type\n"); | |
168 return false; | |
169 } | |
170 if (a.content_disposition() != b.content_disposition_) { | |
171 printf("content disposition\n"); | |
172 return false; | |
173 } | |
174 if (a.items().size() != b.items_.size()) { | |
175 printf("items size\n"); | |
176 return false; | |
177 } | |
178 for (size_t i = 0; i < a.items().size(); ++i) { | |
179 if (*(a.items()[i]) != *(b.items_[i])) { | |
180 printf("item\n"); | |
181 return false; | |
182 } | |
183 } | |
184 return true; | |
185 } | |
186 | |
187 inline bool operator!=(const BlobDataSnapshot& a, const BlobDataBuilder& b) { | |
188 return !(a == b); | |
189 } | |
190 | |
191 inline bool operator!=(const BlobDataBuilder& a, const BlobDataBuilder& b) { | |
92 return !(a == b); | 192 return !(a == b); |
93 } | 193 } |
94 #endif // defined(UNIT_TEST) | 194 #endif // defined(UNIT_TEST) |
95 | 195 |
96 } // namespace storage | 196 } // namespace storage |
97 | 197 |
98 #endif // STORAGE_COMMON_BLOB_BLOB_DATA_H_ | 198 #endif // STORAGE_COMMON_BLOB_BLOB_DATA_H_ |
OLD | NEW |