OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_DATA_ELEMENT_H_ | 5 #ifndef STORAGE_COMMON_DATA_ELEMENT_H_ |
6 #define STORAGE_COMMON_DATA_ELEMENT_H_ | 6 #define STORAGE_COMMON_DATA_ELEMENT_H_ |
7 | 7 |
| 8 #include <ostream> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "storage/common/storage_common_export.h" | 16 #include "storage/common/storage_common_export.h" |
16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
17 | 18 |
18 namespace storage { | 19 namespace storage { |
19 | 20 |
20 // Represents a base Web data element. This could be either one of | 21 // Represents a base Web data element. This could be either one of |
21 // bytes, file or blob data. | 22 // bytes, file or blob data. |
22 class STORAGE_COMMON_EXPORT DataElement { | 23 class STORAGE_COMMON_EXPORT DataElement { |
23 public: | 24 public: |
24 enum Type { | 25 enum Type { |
25 TYPE_UNKNOWN = -1, | 26 TYPE_UNKNOWN = -1, |
26 TYPE_BYTES, | 27 TYPE_BYTES, |
| 28 // Only used with BlobStorageMsg_StartBuildingBlob |
| 29 TYPE_BYTES_DESCRIPTION, |
27 TYPE_FILE, | 30 TYPE_FILE, |
28 TYPE_BLOB, | 31 TYPE_BLOB, |
29 TYPE_FILE_FILESYSTEM, | 32 TYPE_FILE_FILESYSTEM, |
30 TYPE_DISK_CACHE_ENTRY, | 33 TYPE_DISK_CACHE_ENTRY, |
31 }; | 34 }; |
32 | 35 |
33 DataElement(); | 36 DataElement(); |
34 ~DataElement(); | 37 ~DataElement(); |
35 | 38 |
36 Type type() const { return type_; } | 39 Type type() const { return type_; } |
37 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; } | 40 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; } |
38 const base::FilePath& path() const { return path_; } | 41 const base::FilePath& path() const { return path_; } |
39 const GURL& filesystem_url() const { return filesystem_url_; } | 42 const GURL& filesystem_url() const { return filesystem_url_; } |
40 const std::string& blob_uuid() const { return blob_uuid_; } | 43 const std::string& blob_uuid() const { return blob_uuid_; } |
41 uint64 offset() const { return offset_; } | 44 uint64 offset() const { return offset_; } |
42 uint64 length() const { return length_; } | 45 uint64 length() const { return length_; } |
43 const base::Time& expected_modification_time() const { | 46 const base::Time& expected_modification_time() const { |
44 return expected_modification_time_; | 47 return expected_modification_time_; |
45 } | 48 } |
46 | 49 |
| 50 // For use with SetToAllocatedBytes. Should only be used after calling |
| 51 // SetToAllocatedBytes. |
| 52 char* mutable_bytes() { return &buf_[0]; } |
| 53 |
47 // Sets TYPE_BYTES data. This copies the given data into the element. | 54 // Sets TYPE_BYTES data. This copies the given data into the element. |
48 void SetToBytes(const char* bytes, int bytes_len) { | 55 void SetToBytes(const char* bytes, int bytes_len) { |
49 type_ = TYPE_BYTES; | 56 type_ = TYPE_BYTES; |
| 57 bytes_ = nullptr; |
50 buf_.assign(bytes, bytes + bytes_len); | 58 buf_.assign(bytes, bytes + bytes_len); |
51 length_ = buf_.size(); | 59 length_ = buf_.size(); |
52 } | 60 } |
53 | 61 |
54 // Sets TYPE_BYTES data, and clears the internal bytes buffer. | 62 // Sets TYPE_BYTES data, and clears the internal bytes buffer. |
55 // For use with AppendBytes. | 63 // For use with AppendBytes. |
56 void SetToEmptyBytes() { | 64 void SetToEmptyBytes() { |
57 type_ = TYPE_BYTES; | 65 type_ = TYPE_BYTES; |
58 buf_.clear(); | 66 buf_.clear(); |
59 length_ = 0; | 67 length_ = 0; |
60 bytes_ = nullptr; | 68 bytes_ = nullptr; |
61 } | 69 } |
62 | 70 |
63 // Copies and appends the given data into the element. SetToEmptyBytes or | 71 // Copies and appends the given data into the element. SetToEmptyBytes or |
64 // SetToBytes must be called before this method. | 72 // SetToBytes must be called before this method. |
65 void AppendBytes(const char* bytes, int bytes_len) { | 73 void AppendBytes(const char* bytes, int bytes_len) { |
66 DCHECK_EQ(type_, TYPE_BYTES); | 74 DCHECK_EQ(type_, TYPE_BYTES); |
67 DCHECK_NE(length_, kuint64max); | 75 DCHECK_NE(length_, kuint64max); |
68 DCHECK(!bytes_); | 76 DCHECK(!bytes_); |
69 buf_.insert(buf_.end(), bytes, bytes + bytes_len); | 77 buf_.insert(buf_.end(), bytes, bytes + bytes_len); |
70 length_ = buf_.size(); | 78 length_ = buf_.size(); |
71 } | 79 } |
72 | 80 |
| 81 void SetToBytesDescription(size_t bytes_len) { |
| 82 type_ = TYPE_BYTES_DESCRIPTION; |
| 83 bytes_ = nullptr; |
| 84 length_ = bytes_len; |
| 85 } |
| 86 |
73 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller | 87 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller |
74 // should make sure the data is alive when this element is accessed. | 88 // should make sure the data is alive when this element is accessed. |
75 // You cannot use AppendBytes with this method. | 89 // You cannot use AppendBytes with this method. |
76 void SetToSharedBytes(const char* bytes, int bytes_len) { | 90 void SetToSharedBytes(const char* bytes, int bytes_len) { |
77 type_ = TYPE_BYTES; | 91 type_ = TYPE_BYTES; |
78 bytes_ = bytes; | 92 bytes_ = bytes; |
79 length_ = bytes_len; | 93 length_ = bytes_len; |
80 } | 94 } |
81 | 95 |
| 96 // Sets TYPE_BYTES data. This allocates the space for the bytes in the |
| 97 // internal vector but does not populate it with anything. The caller can |
| 98 // then use the bytes() method to access this buffer and populate it. |
| 99 void SetToAllocatedBytes(size_t bytes_len) { |
| 100 type_ = TYPE_BYTES; |
| 101 bytes_ = nullptr; |
| 102 buf_.resize(bytes_len); |
| 103 length_ = bytes_len; |
| 104 } |
| 105 |
82 // Sets TYPE_FILE data. | 106 // Sets TYPE_FILE data. |
83 void SetToFilePath(const base::FilePath& path) { | 107 void SetToFilePath(const base::FilePath& path) { |
84 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 108 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
85 } | 109 } |
86 | 110 |
87 // Sets TYPE_BLOB data. | 111 // Sets TYPE_BLOB data. |
88 void SetToBlob(const std::string& uuid) { | 112 void SetToBlob(const std::string& uuid) { |
89 SetToBlobRange(uuid, 0, kuint64max); | 113 SetToBlobRange(uuid, 0, kuint64max); |
90 } | 114 } |
91 | 115 |
92 // Sets TYPE_FILE data with range. | 116 // Sets TYPE_FILE data with range. |
93 void SetToFilePathRange(const base::FilePath& path, | 117 void SetToFilePathRange(const base::FilePath& path, |
94 uint64 offset, uint64 length, | 118 uint64 offset, uint64 length, |
95 const base::Time& expected_modification_time); | 119 const base::Time& expected_modification_time); |
96 | 120 |
97 // Sets TYPE_BLOB data with range. | 121 // Sets TYPE_BLOB data with range. |
98 void SetToBlobRange(const std::string& blob_uuid, | 122 void SetToBlobRange(const std::string& blob_uuid, |
99 uint64 offset, uint64 length); | 123 uint64 offset, uint64 length); |
100 | 124 |
101 // Sets TYPE_FILE_FILESYSTEM with range. | 125 // Sets TYPE_FILE_FILESYSTEM with range. |
102 void SetToFileSystemUrlRange(const GURL& filesystem_url, | 126 void SetToFileSystemUrlRange(const GURL& filesystem_url, |
103 uint64 offset, uint64 length, | 127 uint64 offset, uint64 length, |
104 const base::Time& expected_modification_time); | 128 const base::Time& expected_modification_time); |
105 | 129 |
106 // Sets to TYPE_DISK_CACHE_ENTRY with range. | 130 // Sets to TYPE_DISK_CACHE_ENTRY with range. |
107 void SetToDiskCacheEntryRange(uint64 offset, uint64 length); | 131 void SetToDiskCacheEntryRange(uint64 offset, uint64 length); |
108 | 132 |
109 private: | 133 private: |
| 134 friend STORAGE_COMMON_EXPORT void PrintTo(const DataElement& x, |
| 135 ::std::ostream* os); |
110 Type type_; | 136 Type type_; |
111 std::vector<char> buf_; // For TYPE_BYTES. | 137 std::vector<char> buf_; // For TYPE_BYTES. |
112 const char* bytes_; // For TYPE_BYTES. | 138 const char* bytes_; // For TYPE_BYTES. |
113 base::FilePath path_; // For TYPE_FILE. | 139 base::FilePath path_; // For TYPE_FILE. |
114 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM. | 140 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM. |
115 std::string blob_uuid_; | 141 std::string blob_uuid_; |
116 uint64 offset_; | 142 uint64 offset_; |
117 uint64 length_; | 143 uint64 length_; |
118 base::Time expected_modification_time_; | 144 base::Time expected_modification_time_; |
119 }; | 145 }; |
(...skipping 11 matching lines...) Expand all Loading... |
131 return a.path() == b.path() && | 157 return a.path() == b.path() && |
132 a.expected_modification_time() == b.expected_modification_time(); | 158 a.expected_modification_time() == b.expected_modification_time(); |
133 case DataElement::TYPE_BLOB: | 159 case DataElement::TYPE_BLOB: |
134 return a.blob_uuid() == b.blob_uuid(); | 160 return a.blob_uuid() == b.blob_uuid(); |
135 case DataElement::TYPE_FILE_FILESYSTEM: | 161 case DataElement::TYPE_FILE_FILESYSTEM: |
136 return a.filesystem_url() == b.filesystem_url(); | 162 return a.filesystem_url() == b.filesystem_url(); |
137 case DataElement::TYPE_DISK_CACHE_ENTRY: | 163 case DataElement::TYPE_DISK_CACHE_ENTRY: |
138 // We compare only length and offset; we trust the entry itself was | 164 // We compare only length and offset; we trust the entry itself was |
139 // compared at some higher level such as in BlobDataItem. | 165 // compared at some higher level such as in BlobDataItem. |
140 return true; | 166 return true; |
| 167 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 168 return true; |
141 case DataElement::TYPE_UNKNOWN: | 169 case DataElement::TYPE_UNKNOWN: |
142 NOTREACHED(); | 170 NOTREACHED(); |
143 return false; | 171 return false; |
144 } | 172 } |
145 return false; | 173 return false; |
146 } | 174 } |
147 | 175 |
148 inline bool operator!=(const DataElement& a, const DataElement& b) { | 176 inline bool operator!=(const DataElement& a, const DataElement& b) { |
149 return !(a == b); | 177 return !(a == b); |
150 } | 178 } |
151 #endif // defined(UNIT_TEST) | 179 #endif // defined(UNIT_TEST) |
152 | 180 |
153 } // namespace storage | 181 } // namespace storage |
154 | 182 |
155 #endif // STORAGE_COMMON_DATA_ELEMENT_H_ | 183 #endif // STORAGE_COMMON_DATA_ELEMENT_H_ |
OLD | NEW |