Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / storage / common / data_element.h
blob2eeca8e1d9513eb53e384bad97c2e2cea8d9cfc3
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
3 // found in the LICENSE file.
5 #ifndef STORAGE_COMMON_DATA_ELEMENT_H_
6 #define STORAGE_COMMON_DATA_ELEMENT_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/time/time.h"
15 #include "storage/common/storage_common_export.h"
16 #include "url/gurl.h"
18 namespace storage {
20 // Represents a base Web data element. This could be either one of
21 // bytes, file or blob data.
22 class STORAGE_COMMON_EXPORT DataElement {
23 public:
24 enum Type {
25 TYPE_UNKNOWN = -1,
26 TYPE_BYTES,
27 TYPE_FILE,
28 TYPE_BLOB,
29 TYPE_FILE_FILESYSTEM,
30 TYPE_DISK_CACHE_ENTRY,
33 DataElement();
34 ~DataElement();
36 Type type() const { return type_; }
37 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; }
38 const base::FilePath& path() const { return path_; }
39 const GURL& filesystem_url() const { return filesystem_url_; }
40 const std::string& blob_uuid() const { return blob_uuid_; }
41 uint64 offset() const { return offset_; }
42 uint64 length() const { return length_; }
43 const base::Time& expected_modification_time() const {
44 return expected_modification_time_;
47 // Sets TYPE_BYTES data. This copies the given data into the element.
48 void SetToBytes(const char* bytes, int bytes_len) {
49 type_ = TYPE_BYTES;
50 buf_.assign(bytes, bytes + bytes_len);
51 length_ = buf_.size();
54 // Sets TYPE_BYTES data, and clears the internal bytes buffer.
55 // For use with AppendBytes.
56 void SetToEmptyBytes() {
57 type_ = TYPE_BYTES;
58 buf_.clear();
59 length_ = 0;
60 bytes_ = nullptr;
63 // Copies and appends the given data into the element. SetToEmptyBytes or
64 // SetToBytes must be called before this method.
65 void AppendBytes(const char* bytes, int bytes_len) {
66 DCHECK_EQ(type_, TYPE_BYTES);
67 DCHECK_NE(length_, kuint64max);
68 DCHECK(!bytes_);
69 buf_.insert(buf_.end(), bytes, bytes + bytes_len);
70 length_ = buf_.size();
73 // 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.
75 // You cannot use AppendBytes with this method.
76 void SetToSharedBytes(const char* bytes, int bytes_len) {
77 type_ = TYPE_BYTES;
78 bytes_ = bytes;
79 length_ = bytes_len;
82 // Sets TYPE_FILE data.
83 void SetToFilePath(const base::FilePath& path) {
84 SetToFilePathRange(path, 0, kuint64max, base::Time());
87 // Sets TYPE_BLOB data.
88 void SetToBlob(const std::string& uuid) {
89 SetToBlobRange(uuid, 0, kuint64max);
92 // Sets TYPE_FILE data with range.
93 void SetToFilePathRange(const base::FilePath& path,
94 uint64 offset, uint64 length,
95 const base::Time& expected_modification_time);
97 // Sets TYPE_BLOB data with range.
98 void SetToBlobRange(const std::string& blob_uuid,
99 uint64 offset, uint64 length);
101 // Sets TYPE_FILE_FILESYSTEM with range.
102 void SetToFileSystemUrlRange(const GURL& filesystem_url,
103 uint64 offset, uint64 length,
104 const base::Time& expected_modification_time);
106 // Sets to TYPE_DISK_CACHE_ENTRY with range.
107 void SetToDiskCacheEntryRange(uint64 offset, uint64 length);
109 private:
110 Type type_;
111 std::vector<char> buf_; // For TYPE_BYTES.
112 const char* bytes_; // For TYPE_BYTES.
113 base::FilePath path_; // For TYPE_FILE.
114 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM.
115 std::string blob_uuid_;
116 uint64 offset_;
117 uint64 length_;
118 base::Time expected_modification_time_;
121 #if defined(UNIT_TEST)
122 inline bool operator==(const DataElement& a, const DataElement& b) {
123 if (a.type() != b.type() ||
124 a.offset() != b.offset() ||
125 a.length() != b.length())
126 return false;
127 switch (a.type()) {
128 case DataElement::TYPE_BYTES:
129 return memcmp(a.bytes(), b.bytes(), b.length()) == 0;
130 case DataElement::TYPE_FILE:
131 return a.path() == b.path() &&
132 a.expected_modification_time() == b.expected_modification_time();
133 case DataElement::TYPE_BLOB:
134 return a.blob_uuid() == b.blob_uuid();
135 case DataElement::TYPE_FILE_FILESYSTEM:
136 return a.filesystem_url() == b.filesystem_url();
137 case DataElement::TYPE_DISK_CACHE_ENTRY:
138 // We compare only length and offset; we trust the entry itself was
139 // compared at some higher level such as in BlobDataItem.
140 return true;
141 case DataElement::TYPE_UNKNOWN:
142 NOTREACHED();
143 return false;
145 return false;
148 inline bool operator!=(const DataElement& a, const DataElement& b) {
149 return !(a == b);
151 #endif // defined(UNIT_TEST)
153 } // namespace storage
155 #endif // STORAGE_COMMON_DATA_ELEMENT_H_