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