Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: storage/common/data_element.h

Issue 1288373002: [BlobAsync] Patch 2: Common Constants (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@async1
Patch Set: removed messages Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 19 matching lines...) Expand all
111 std::vector<char> buf_; // For TYPE_BYTES. 135 std::vector<char> buf_; // For TYPE_BYTES.
112 const char* bytes_; // For TYPE_BYTES. 136 const char* bytes_; // For TYPE_BYTES.
113 base::FilePath path_; // For TYPE_FILE. 137 base::FilePath path_; // For TYPE_FILE.
114 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM. 138 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM.
115 std::string blob_uuid_; 139 std::string blob_uuid_;
116 uint64 offset_; 140 uint64 offset_;
117 uint64 length_; 141 uint64 length_;
118 base::Time expected_modification_time_; 142 base::Time expected_modification_time_;
119 }; 143 };
120 144
145 STORAGE_COMMON_EXPORT std::ostream& operator<<(std::ostream& os,
146 const DataElement& x);
147
121 #if defined(UNIT_TEST) 148 #if defined(UNIT_TEST)
122 inline bool operator==(const DataElement& a, const DataElement& b) { 149 inline bool operator==(const DataElement& a, const DataElement& b) {
123 if (a.type() != b.type() || 150 if (a.type() != b.type() ||
124 a.offset() != b.offset() || 151 a.offset() != b.offset() ||
125 a.length() != b.length()) 152 a.length() != b.length())
126 return false; 153 return false;
127 switch (a.type()) { 154 switch (a.type()) {
128 case DataElement::TYPE_BYTES: 155 case DataElement::TYPE_BYTES:
129 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; 156 return memcmp(a.bytes(), b.bytes(), b.length()) == 0;
130 case DataElement::TYPE_FILE: 157 case DataElement::TYPE_FILE:
131 return a.path() == b.path() && 158 return a.path() == b.path() &&
132 a.expected_modification_time() == b.expected_modification_time(); 159 a.expected_modification_time() == b.expected_modification_time();
133 case DataElement::TYPE_BLOB: 160 case DataElement::TYPE_BLOB:
134 return a.blob_uuid() == b.blob_uuid(); 161 return a.blob_uuid() == b.blob_uuid();
135 case DataElement::TYPE_FILE_FILESYSTEM: 162 case DataElement::TYPE_FILE_FILESYSTEM:
136 return a.filesystem_url() == b.filesystem_url(); 163 return a.filesystem_url() == b.filesystem_url();
137 case DataElement::TYPE_DISK_CACHE_ENTRY: 164 case DataElement::TYPE_DISK_CACHE_ENTRY:
138 // We compare only length and offset; we trust the entry itself was 165 // We compare only length and offset; we trust the entry itself was
139 // compared at some higher level such as in BlobDataItem. 166 // compared at some higher level such as in BlobDataItem.
140 return true; 167 return true;
168 case DataElement::TYPE_BYTES_DESCRIPTION:
169 return a.length() == b.length();
michaeln 2015/10/20 01:16:14 you can return true here since lengths are compare
dmurph 2015/10/20 18:28:27 Nice catch.
141 case DataElement::TYPE_UNKNOWN: 170 case DataElement::TYPE_UNKNOWN:
142 NOTREACHED(); 171 NOTREACHED();
143 return false; 172 return false;
144 } 173 }
145 return false; 174 return false;
146 } 175 }
147 176
148 inline bool operator!=(const DataElement& a, const DataElement& b) { 177 inline bool operator!=(const DataElement& a, const DataElement& b) {
149 return !(a == b); 178 return !(a == b);
150 } 179 }
180
151 #endif // defined(UNIT_TEST) 181 #endif // defined(UNIT_TEST)
152 182
153 } // namespace storage 183 } // namespace storage
154 184
155 #endif // STORAGE_COMMON_DATA_ELEMENT_H_ 185 #endif // STORAGE_COMMON_DATA_ELEMENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698