OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 WEBKIT_BASE_DATA_ELEMENT_H_ | 5 #ifndef WEBKIT_BASE_DATA_ELEMENT_H_ |
6 #define WEBKIT_BASE_DATA_ELEMENT_H_ | 6 #define WEBKIT_BASE_DATA_ELEMENT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
kinuko
2013/04/23 05:30:39
nit: #include <string>
(per lint)
michaeln
2013/04/23 19:27:16
Done.
michaeln
2013/04/23 19:27:16
Done.
| |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "webkit/base/webkit_base_export.h" | 15 #include "webkit/base/webkit_base_export.h" |
16 | 16 |
17 namespace webkit_base { | 17 namespace webkit_base { |
18 | 18 |
19 // Represents a base Web data element. This could be either one of | 19 // Represents a base Web data element. This could be either one of |
20 // bytes, file or blob data. | 20 // bytes, file or blob data. |
21 class WEBKIT_BASE_EXPORT DataElement { | 21 class WEBKIT_BASE_EXPORT DataElement { |
22 public: | 22 public: |
23 enum Type { | 23 enum Type { |
24 TYPE_UNKNOWN = -1, | 24 TYPE_UNKNOWN = -1, |
25 TYPE_BYTES, | 25 TYPE_BYTES, |
26 TYPE_FILE, | 26 TYPE_FILE, |
27 TYPE_BLOB, | 27 TYPE_BLOB, |
28 TYPE_FILE_FILESYSTEM, | 28 TYPE_FILE_FILESYSTEM, |
29 }; | 29 }; |
30 | 30 |
31 DataElement(); | 31 DataElement(); |
32 ~DataElement(); | 32 ~DataElement(); |
33 | 33 |
34 Type type() const { return type_; } | 34 Type type() const { return type_; } |
35 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; } | 35 const char* bytes() const { return bytes_ ? bytes_ : &buf_[0]; } |
36 const base::FilePath& path() const { return path_; } | 36 const base::FilePath& path() const { return path_; } |
37 const GURL& url() const { return url_; } | 37 const GURL& filesystem_url() const { return filesystem_url_; } |
38 | |
39 // TODO(michaeln): fully switch to using string uuids for blob identifiers. | |
kinuko
2013/04/23 05:30:39
nit: can you also link to the issue?
michaeln
2013/04/23 19:27:16
Done.
| |
40 // Note: Identifying blobs by url is being deprecated, but while transitioning | |
41 // there's a little of both going on in the project. | |
42 const std::string& blob_uuid() const { return blob_uuid_; } | |
43 const GURL& blob_url() const { return blob_url_; } | |
38 uint64 offset() const { return offset_; } | 44 uint64 offset() const { return offset_; } |
39 uint64 length() const { return length_; } | 45 uint64 length() const { return length_; } |
40 const base::Time& expected_modification_time() const { | 46 const base::Time& expected_modification_time() const { |
41 return expected_modification_time_; | 47 return expected_modification_time_; |
42 } | 48 } |
43 | 49 |
50 // TODO(michaeln): fixup callers to use filesytem_url() and blob_uuid(). | |
51 const GURL& url() const { | |
52 if (type_ == TYPE_FILE_FILESYSTEM) | |
53 return filesystem_url_; | |
54 return blob_url_; | |
55 } | |
56 | |
44 // Sets TYPE_BYTES data. This copies the given data into the element. | 57 // Sets TYPE_BYTES data. This copies the given data into the element. |
45 void SetToBytes(const char* bytes, int bytes_len) { | 58 void SetToBytes(const char* bytes, int bytes_len) { |
46 type_ = TYPE_BYTES; | 59 type_ = TYPE_BYTES; |
47 buf_.assign(bytes, bytes + bytes_len); | 60 buf_.assign(bytes, bytes + bytes_len); |
48 length_ = buf_.size(); | 61 length_ = buf_.size(); |
49 } | 62 } |
50 | 63 |
51 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller | 64 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller |
52 // should make sure the data is alive when this element is accessed. | 65 // should make sure the data is alive when this element is accessed. |
53 void SetToSharedBytes(const char* bytes, int bytes_len) { | 66 void SetToSharedBytes(const char* bytes, int bytes_len) { |
54 type_ = TYPE_BYTES; | 67 type_ = TYPE_BYTES; |
55 bytes_ = bytes; | 68 bytes_ = bytes; |
56 length_ = bytes_len; | 69 length_ = bytes_len; |
57 } | 70 } |
58 | 71 |
59 // Sets TYPE_FILE data. | 72 // Sets TYPE_FILE data. |
60 void SetToFilePath(const base::FilePath& path) { | 73 void SetToFilePath(const base::FilePath& path) { |
61 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 74 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
62 } | 75 } |
63 | 76 |
64 // Sets TYPE_BLOB data. | 77 // Sets TYPE_BLOB data. |
65 void SetToBlobUrl(const GURL& blob_url) { | 78 void SetToBlobUrl(const GURL& blob_url) { |
66 SetToBlobUrlRange(blob_url, 0, kuint64max); | 79 SetToBlobUrlRange(blob_url, 0, kuint64max); |
67 } | 80 } |
81 void SetToBlob(const std::string& uuid) { | |
82 SetToBlobRange(uuid, 0, kuint64max); | |
83 } | |
68 | 84 |
69 // Sets TYPE_FILE data with range. | 85 // Sets TYPE_FILE data with range. |
70 void SetToFilePathRange(const base::FilePath& path, | 86 void SetToFilePathRange(const base::FilePath& path, |
71 uint64 offset, uint64 length, | 87 uint64 offset, uint64 length, |
72 const base::Time& expected_modification_time); | 88 const base::Time& expected_modification_time); |
73 | 89 |
74 // Sets TYPE_BLOB data with range. | 90 // Sets TYPE_BLOB data with range. |
75 void SetToBlobUrlRange(const GURL& blob_url, | 91 void SetToBlobUrlRange(const GURL& blob_url, |
76 uint64 offset, uint64 length); | 92 uint64 offset, uint64 length); |
93 void SetToBlobRange(const std::string& blob_uuid, | |
94 uint64 offset, uint64 length); | |
77 | 95 |
78 // Sets TYPE_FILE_FILESYSTEM with range. | 96 // Sets TYPE_FILE_FILESYSTEM with range. |
79 void SetToFileSystemUrlRange(const GURL& filesystem_url, | 97 void SetToFileSystemUrlRange(const GURL& filesystem_url, |
80 uint64 offset, uint64 length, | 98 uint64 offset, uint64 length, |
81 const base::Time& expected_modification_time); | 99 const base::Time& expected_modification_time); |
82 | 100 |
83 private: | 101 private: |
84 Type type_; | 102 Type type_; |
85 std::vector<char> buf_; // For TYPE_BYTES. | 103 std::vector<char> buf_; // For TYPE_BYTES. |
86 const char* bytes_; // For TYPE_BYTES. | 104 const char* bytes_; // For TYPE_BYTES. |
87 base::FilePath path_; // For TYPE_FILE. | 105 base::FilePath path_; // For TYPE_FILE. |
88 GURL url_; // For TYPE_BLOB or TYPE_FILE_FILESYSTEM. | 106 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM. |
107 GURL blob_url_; | |
108 std::string blob_uuid_; | |
89 uint64 offset_; | 109 uint64 offset_; |
90 uint64 length_; | 110 uint64 length_; |
91 base::Time expected_modification_time_; | 111 base::Time expected_modification_time_; |
92 }; | 112 }; |
93 | 113 |
94 #if defined(UNIT_TEST) | 114 #if defined(UNIT_TEST) |
95 inline bool operator==(const DataElement& a, const DataElement& b) { | 115 inline bool operator==(const DataElement& a, const DataElement& b) { |
96 if (a.type() != b.type() || | 116 if (a.type() != b.type() || |
97 a.offset() != b.offset() || | 117 a.offset() != b.offset() || |
98 a.length() != b.length()) | 118 a.length() != b.length()) |
99 return false; | 119 return false; |
100 switch (a.type()) { | 120 switch (a.type()) { |
101 case DataElement::TYPE_BYTES: | 121 case DataElement::TYPE_BYTES: |
102 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; | 122 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; |
103 case DataElement::TYPE_FILE: | 123 case DataElement::TYPE_FILE: |
104 return a.path() == b.path() && | 124 return a.path() == b.path() && |
105 a.expected_modification_time() == b.expected_modification_time(); | 125 a.expected_modification_time() == b.expected_modification_time(); |
106 case DataElement::TYPE_BLOB: | 126 case DataElement::TYPE_BLOB: |
127 return a.blob_uuid().empty() ? (a.blob_url() == b.blob_url()) | |
128 : (a.blob_uuid() == b.blob_uuid()); | |
107 case DataElement::TYPE_FILE_FILESYSTEM: | 129 case DataElement::TYPE_FILE_FILESYSTEM: |
108 return a.url() == b.url(); | 130 return a.filesystem_url() == b.filesystem_url(); |
109 case DataElement::TYPE_UNKNOWN: | 131 case DataElement::TYPE_UNKNOWN: |
110 NOTREACHED(); | 132 NOTREACHED(); |
111 return false; | 133 return false; |
112 } | 134 } |
113 return false; | 135 return false; |
114 } | 136 } |
115 | 137 |
116 inline bool operator!=(const DataElement& a, const DataElement& b) { | 138 inline bool operator!=(const DataElement& a, const DataElement& b) { |
117 return !(a == b); | 139 return !(a == b); |
118 } | 140 } |
119 #endif // defined(UNIT_TEST) | 141 #endif // defined(UNIT_TEST) |
120 | 142 |
121 } // namespace webkit_base | 143 } // namespace webkit_base |
122 | 144 |
123 #endif // WEBKIT_BASE_DATA_ELEMENT_H_ | 145 #endif // WEBKIT_BASE_DATA_ELEMENT_H_ |
OLD | NEW |