We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / storage / common / data_element.h
blob0efe1550a91e9a509d1cc0548854167a58c3a689
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,
32 DataElement();
33 ~DataElement();
35 Type type() const { return type_; }
36 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; }
37 const base::FilePath& path() const { return path_; }
38 const GURL& filesystem_url() const { return filesystem_url_; }
39 const std::string& blob_uuid() const { return blob_uuid_; }
40 uint64 offset() const { return offset_; }
41 uint64 length() const { return length_; }
42 const base::Time& expected_modification_time() const {
43 return expected_modification_time_;
46 // Sets TYPE_BYTES data. This copies the given data into the element.
47 void SetToBytes(const char* bytes, int bytes_len) {
48 type_ = TYPE_BYTES;
49 buf_.assign(bytes, bytes + bytes_len);
50 length_ = buf_.size();
53 // Sets TYPE_BYTES data, and clears the internal bytes buffer.
54 // For use with AppendBytes.
55 void SetToEmptyBytes() {
56 type_ = TYPE_BYTES;
57 buf_.clear();
58 length_ = 0;
59 bytes_ = nullptr;
62 // Copies and appends the given data into the element. SetToEmptyBytes or
63 // SetToBytes must be called before this method.
64 void AppendBytes(const char* bytes, int bytes_len) {
65 DCHECK_EQ(type_, TYPE_BYTES);
66 DCHECK_NE(length_, kuint64max);
67 DCHECK(!bytes_);
68 buf_.insert(buf_.end(), bytes, bytes + bytes_len);
69 length_ = buf_.size();
72 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller
73 // should make sure the data is alive when this element is accessed.
74 // You cannot use AppendBytes with this method.
75 void SetToSharedBytes(const char* bytes, int bytes_len) {
76 type_ = TYPE_BYTES;
77 bytes_ = bytes;
78 length_ = bytes_len;
81 // Sets TYPE_FILE data.
82 void SetToFilePath(const base::FilePath& path) {
83 SetToFilePathRange(path, 0, kuint64max, base::Time());
86 // Sets TYPE_BLOB data.
87 void SetToBlob(const std::string& uuid) {
88 SetToBlobRange(uuid, 0, kuint64max);
91 // Sets TYPE_FILE data with range.
92 void SetToFilePathRange(const base::FilePath& path,
93 uint64 offset, uint64 length,
94 const base::Time& expected_modification_time);
96 // Sets TYPE_BLOB data with range.
97 void SetToBlobRange(const std::string& blob_uuid,
98 uint64 offset, uint64 length);
100 // Sets TYPE_FILE_FILESYSTEM with range.
101 void SetToFileSystemUrlRange(const GURL& filesystem_url,
102 uint64 offset, uint64 length,
103 const base::Time& expected_modification_time);
105 private:
106 Type type_;
107 std::vector<char> buf_; // For TYPE_BYTES.
108 const char* bytes_; // For TYPE_BYTES.
109 base::FilePath path_; // For TYPE_FILE.
110 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM.
111 std::string blob_uuid_;
112 uint64 offset_;
113 uint64 length_;
114 base::Time expected_modification_time_;
117 #if defined(UNIT_TEST)
118 inline bool operator==(const DataElement& a, const DataElement& b) {
119 if (a.type() != b.type() ||
120 a.offset() != b.offset() ||
121 a.length() != b.length())
122 return false;
123 switch (a.type()) {
124 case DataElement::TYPE_BYTES:
125 return memcmp(a.bytes(), b.bytes(), b.length()) == 0;
126 case DataElement::TYPE_FILE:
127 return a.path() == b.path() &&
128 a.expected_modification_time() == b.expected_modification_time();
129 case DataElement::TYPE_BLOB:
130 return a.blob_uuid() == b.blob_uuid();
131 case DataElement::TYPE_FILE_FILESYSTEM:
132 return a.filesystem_url() == b.filesystem_url();
133 case DataElement::TYPE_UNKNOWN:
134 NOTREACHED();
135 return false;
137 return false;
140 inline bool operator!=(const DataElement& a, const DataElement& b) {
141 return !(a == b);
143 #endif // defined(UNIT_TEST)
145 } // namespace storage
147 #endif // STORAGE_COMMON_DATA_ELEMENT_H_