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/file_path.h" | 12 #include "base/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 const std::string& blob_uuid() const { return blob_uuid_; } |
38 uint64 offset() const { return offset_; } | 40 uint64 offset() const { return offset_; } |
39 uint64 length() const { return length_; } | 41 uint64 length() const { return length_; } |
40 const base::Time& expected_modification_time() const { | 42 const base::Time& expected_modification_time() const { |
41 return expected_modification_time_; | 43 return expected_modification_time_; |
42 } | 44 } |
43 | 45 |
44 // Sets TYPE_BYTES data. This copies the given data into the element. | 46 // Sets TYPE_BYTES data. This copies the given data into the element. |
45 void SetToBytes(const char* bytes, int bytes_len) { | 47 void SetToBytes(const char* bytes, int bytes_len) { |
46 type_ = TYPE_BYTES; | 48 type_ = TYPE_BYTES; |
47 buf_.assign(bytes, bytes + bytes_len); | 49 buf_.assign(bytes, bytes + bytes_len); |
48 length_ = buf_.size(); | 50 length_ = buf_.size(); |
49 } | 51 } |
50 | 52 |
51 // Sets TYPE_BYTES data. This does NOT copy the given data and the caller | 53 // 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. | 54 // should make sure the data is alive when this element is accessed. |
53 void SetToSharedBytes(const char* bytes, int bytes_len) { | 55 void SetToSharedBytes(const char* bytes, int bytes_len) { |
54 type_ = TYPE_BYTES; | 56 type_ = TYPE_BYTES; |
55 bytes_ = bytes; | 57 bytes_ = bytes; |
56 length_ = bytes_len; | 58 length_ = bytes_len; |
57 } | 59 } |
58 | 60 |
59 // Sets TYPE_FILE data. | 61 // Sets TYPE_FILE data. |
60 void SetToFilePath(const base::FilePath& path) { | 62 void SetToFilePath(const base::FilePath& path) { |
61 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 63 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
62 } | 64 } |
63 | 65 |
64 // Sets TYPE_BLOB data. | 66 // Sets TYPE_BLOB data. |
65 void SetToBlobUrl(const GURL& blob_url) { | 67 void SetToBlob(const std::string& uuid) { |
66 SetToBlobUrlRange(blob_url, 0, kuint64max); | 68 SetToBlobRange(uuid, 0, kuint64max); |
67 } | 69 } |
68 | 70 |
69 // Sets TYPE_FILE data with range. | 71 // Sets TYPE_FILE data with range. |
70 void SetToFilePathRange(const base::FilePath& path, | 72 void SetToFilePathRange(const base::FilePath& path, |
71 uint64 offset, uint64 length, | 73 uint64 offset, uint64 length, |
72 const base::Time& expected_modification_time); | 74 const base::Time& expected_modification_time); |
73 | |
74 // Sets TYPE_BLOB data with range. | 75 // Sets TYPE_BLOB data with range. |
75 void SetToBlobUrlRange(const GURL& blob_url, | 76 void SetToBlobRange(const std::string& uuid, |
76 uint64 offset, uint64 length); | 77 uint64 offset, uint64 length); |
77 | 78 |
78 // Sets TYPE_FILE_FILESYSTEM with range. | 79 // Sets TYPE_FILE_FILESYSTEM with range. |
79 void SetToFileSystemUrlRange(const GURL& filesystem_url, | 80 void SetToFileSystemUrlRange(const GURL& filesystem_url, |
80 uint64 offset, uint64 length, | 81 uint64 offset, uint64 length, |
81 const base::Time& expected_modification_time); | 82 const base::Time& expected_modification_time); |
82 | 83 |
83 private: | 84 private: |
84 Type type_; | 85 Type type_; |
85 std::vector<char> buf_; // For TYPE_BYTES. | 86 std::vector<char> buf_; // For TYPE_BYTES. |
86 const char* bytes_; // For TYPE_BYTES. | 87 const char* bytes_; // For TYPE_BYTES when we don't own the buffer. |
87 base::FilePath path_; // For TYPE_FILE. | 88 base::FilePath path_; // For TYPE_FILE. |
88 GURL url_; // For TYPE_BLOB or TYPE_FILE_FILESYSTEM. | 89 GURL filesystem_url_; // For TYPE_FILE_FILESYSTEM. |
| 90 std::string blob_uuid_; // For TYPE_BLOB |
89 uint64 offset_; | 91 uint64 offset_; |
90 uint64 length_; | 92 uint64 length_; |
91 base::Time expected_modification_time_; | 93 base::Time expected_modification_time_; |
92 }; | 94 }; |
93 | 95 |
94 #if defined(UNIT_TEST) | 96 #if defined(UNIT_TEST) |
95 inline bool operator==(const DataElement& a, const DataElement& b) { | 97 inline bool operator==(const DataElement& a, const DataElement& b) { |
96 if (a.type() != b.type() || | 98 if (a.type() != b.type() || |
97 a.offset() != b.offset() || | 99 a.offset() != b.offset() || |
98 a.length() != b.length()) | 100 a.length() != b.length()) |
99 return false; | 101 return false; |
100 switch (a.type()) { | 102 switch (a.type()) { |
101 case DataElement::TYPE_BYTES: | 103 case DataElement::TYPE_BYTES: |
102 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; | 104 return memcmp(a.bytes(), b.bytes(), b.length()) == 0; |
103 case DataElement::TYPE_FILE: | 105 case DataElement::TYPE_FILE: |
104 return a.path() == b.path() && | 106 return a.path() == b.path() && |
105 a.expected_modification_time() == b.expected_modification_time(); | 107 a.expected_modification_time() == b.expected_modification_time(); |
106 case DataElement::TYPE_BLOB: | 108 case DataElement::TYPE_BLOB: |
| 109 return a.blob_uuid() == b.blob_uuid(); |
107 case DataElement::TYPE_FILE_FILESYSTEM: | 110 case DataElement::TYPE_FILE_FILESYSTEM: |
108 return a.url() == b.url(); | 111 return a.filesystem_url() == b.filesystem_url(); |
109 case DataElement::TYPE_UNKNOWN: | 112 case DataElement::TYPE_UNKNOWN: |
110 NOTREACHED(); | 113 NOTREACHED(); |
111 return false; | 114 return false; |
112 } | 115 } |
113 return false; | 116 return false; |
114 } | 117 } |
115 | 118 |
116 inline bool operator!=(const DataElement& a, const DataElement& b) { | 119 inline bool operator!=(const DataElement& a, const DataElement& b) { |
117 return !(a == b); | 120 return !(a == b); |
118 } | 121 } |
119 #endif // defined(UNIT_TEST) | 122 #endif // defined(UNIT_TEST) |
120 | 123 |
121 } // namespace webkit_base | 124 } // namespace webkit_base |
122 | 125 |
123 #endif // WEBKIT_BASE_DATA_ELEMENT_H_ | 126 #endif // WEBKIT_BASE_DATA_ELEMENT_H_ |
OLD | NEW |