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_
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"
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
{
30 TYPE_DISK_CACHE_ENTRY
,
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
) {
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() {
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
);
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
) {
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
);
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_
;
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())
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.
141 case DataElement::TYPE_UNKNOWN
:
148 inline bool operator!=(const DataElement
& a
, const DataElement
& b
) {
151 #endif // defined(UNIT_TEST)
153 } // namespace storage
155 #endif // STORAGE_COMMON_DATA_ELEMENT_H_