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 WEBKIT_COMMON_DATA_ELEMENT_H_
6 #define WEBKIT_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"
16 #include "webkit/common/webkit_common_export.h"
18 namespace webkit_common
{
20 // Represents a base Web data element. This could be either one of
21 // bytes, file or blob data.
22 class WEBKIT_COMMON_EXPORT 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
) {
49 buf_
.assign(bytes
, bytes
+ bytes_len
);
50 length_
= buf_
.size();
53 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller
54 // should make sure the data is alive when this element is accessed.
55 void SetToSharedBytes(const char* bytes
, int bytes_len
) {
61 // Sets TYPE_FILE data.
62 void SetToFilePath(const base::FilePath
& path
) {
63 SetToFilePathRange(path
, 0, kuint64max
, base::Time());
66 // Sets TYPE_BLOB data.
67 void SetToBlob(const std::string
& uuid
) {
68 SetToBlobRange(uuid
, 0, kuint64max
);
71 // Sets TYPE_FILE data with range.
72 void SetToFilePathRange(const base::FilePath
& path
,
73 uint64 offset
, uint64 length
,
74 const base::Time
& expected_modification_time
);
76 // Sets TYPE_BLOB data with range.
77 void SetToBlobRange(const std::string
& blob_uuid
,
78 uint64 offset
, uint64 length
);
80 // Sets TYPE_FILE_FILESYSTEM with range.
81 void SetToFileSystemUrlRange(const GURL
& filesystem_url
,
82 uint64 offset
, uint64 length
,
83 const base::Time
& expected_modification_time
);
87 std::vector
<char> buf_
; // For TYPE_BYTES.
88 const char* bytes_
; // For TYPE_BYTES.
89 base::FilePath path_
; // For TYPE_FILE.
90 GURL filesystem_url_
; // For TYPE_FILE_FILESYSTEM.
91 std::string blob_uuid_
;
94 base::Time expected_modification_time_
;
97 #if defined(UNIT_TEST)
98 inline bool operator==(const DataElement
& a
, const DataElement
& b
) {
99 if (a
.type() != b
.type() ||
100 a
.offset() != b
.offset() ||
101 a
.length() != b
.length())
104 case DataElement::TYPE_BYTES
:
105 return memcmp(a
.bytes(), b
.bytes(), b
.length()) == 0;
106 case DataElement::TYPE_FILE
:
107 return a
.path() == b
.path() &&
108 a
.expected_modification_time() == b
.expected_modification_time();
109 case DataElement::TYPE_BLOB
:
110 return a
.blob_uuid() == b
.blob_uuid();
111 case DataElement::TYPE_FILE_FILESYSTEM
:
112 return a
.filesystem_url() == b
.filesystem_url();
113 case DataElement::TYPE_UNKNOWN
:
120 inline bool operator!=(const DataElement
& a
, const DataElement
& b
) {
123 #endif // defined(UNIT_TEST)
125 } // namespace webkit_common
127 #endif // WEBKIT_COMMON_DATA_ELEMENT_H_