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 #include "content/common/indexed_db/indexed_db_key.h" | 5 #include "content/common/indexed_db/indexed_db_key.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "third_party/WebKit/public/platform/WebIDBKey.h" |
9 #include "third_party/WebKit/public/platform/WebString.h" | 10 #include "third_party/WebKit/public/platform/WebString.h" |
10 #include "third_party/WebKit/public/platform/WebVector.h" | 11 #include "third_party/WebKit/public/platform/WebVector.h" |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 | 14 |
14 using WebKit::WebIDBKey; | 15 using WebKit::WebIDBKey; |
| 16 using WebKit::WebIDBKeyType; |
| 17 using WebKit::WebIDBKeyTypeArray; |
| 18 using WebKit::WebIDBKeyTypeDate; |
| 19 using WebKit::WebIDBKeyTypeInvalid; |
| 20 using WebKit::WebIDBKeyTypeMin; |
| 21 using WebKit::WebIDBKeyTypeNull; |
| 22 using WebKit::WebIDBKeyTypeNumber; |
| 23 using WebKit::WebIDBKeyTypeString; |
15 using WebKit::WebVector; | 24 using WebKit::WebVector; |
16 | 25 |
17 namespace { | 26 namespace { |
18 | 27 |
19 // Very rough estimate of minimum key size overhead. | 28 // Very rough estimate of minimum key size overhead. |
20 const size_t kOverheadSize = 16; | 29 const size_t kOverheadSize = 16; |
21 | 30 |
22 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) { | 31 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) { |
23 size_t size(0); | 32 size_t size(0); |
24 for (size_t i = 0; i < keys.size(); ++i) | 33 for (size_t i = 0; i < keys.size(); ++i) |
25 size += keys[i].size_estimate(); | 34 size += keys[i].size_estimate(); |
26 return size; | 35 return size; |
27 } | 36 } |
28 | 37 |
29 static size_t CalculateKeySize(const WebIDBKey& key) { | 38 static size_t CalculateKeySize(const WebIDBKey& key) { |
30 switch (key.type()) { | 39 switch (key.keyType()) { |
31 case WebIDBKey::ArrayType: { | 40 case WebIDBKeyTypeArray: { |
32 const WebVector<WebIDBKey>& array = key.array(); | 41 const WebVector<WebIDBKey>& array = key.array(); |
33 size_t total = 0; | 42 size_t total = 0; |
34 for (size_t i = 0; i < array.size(); ++i) | 43 for (size_t i = 0; i < array.size(); ++i) |
35 total += CalculateKeySize(array[i]); | 44 total += CalculateKeySize(array[i]); |
36 return kOverheadSize + total; | 45 return kOverheadSize + total; |
37 } | 46 } |
38 case WebIDBKey::StringType: | 47 case WebIDBKeyTypeString: |
39 return kOverheadSize + | 48 return kOverheadSize + |
40 (key.string().length() * sizeof(string16::value_type)); | 49 (key.string().length() * sizeof(string16::value_type)); |
41 | 50 |
42 case WebIDBKey::DateType: | 51 case WebIDBKeyTypeDate: |
43 case WebIDBKey::NumberType: | 52 case WebIDBKeyTypeNumber: |
44 return kOverheadSize + sizeof(double); | 53 return kOverheadSize + sizeof(double); |
45 | 54 |
46 default: | 55 default: |
47 return kOverheadSize; | 56 return kOverheadSize; |
48 } | 57 } |
49 NOTREACHED(); | 58 NOTREACHED(); |
50 return 0; | 59 return 0; |
51 } | 60 } |
52 | 61 |
53 template <typename T> | 62 template <typename T> |
54 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) { | 63 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) { |
55 IndexedDBKey::KeyArray result; | 64 IndexedDBKey::KeyArray result; |
56 result.reserve(array.size()); | 65 result.reserve(array.size()); |
57 for (size_t i = 0; i < array.size(); ++i) { | 66 for (size_t i = 0; i < array.size(); ++i) { |
58 result.push_back(IndexedDBKey(array[i])); | 67 result.push_back(IndexedDBKey(array[i])); |
59 } | 68 } |
60 return result; | 69 return result; |
61 } | 70 } |
62 | 71 |
63 static IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) { | 72 static IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) { |
64 IndexedDBKey::KeyArray result; | 73 IndexedDBKey::KeyArray result; |
65 if (other.type() == WebIDBKey::ArrayType) { | 74 if (other.keyType() == WebIDBKeyTypeArray) { |
66 result = CopyKeyArray(other.array()); | 75 result = CopyKeyArray(other.array()); |
67 } | 76 } |
68 return result; | 77 return result; |
69 } | 78 } |
70 } // namespace | 79 } // namespace |
71 | 80 |
72 IndexedDBKey::IndexedDBKey() | 81 IndexedDBKey::IndexedDBKey() |
73 : type_(WebIDBKey::NullType), | 82 : type_(WebIDBKeyTypeNull), |
74 date_(0), | 83 date_(0), |
75 number_(0), | 84 number_(0), |
76 size_estimate_(kOverheadSize) {} | 85 size_estimate_(kOverheadSize) {} |
77 | 86 |
78 IndexedDBKey::IndexedDBKey(WebIDBKey::Type type) | 87 IndexedDBKey::IndexedDBKey(WebIDBKeyType type) |
79 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { | 88 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { |
80 DCHECK(type == WebIDBKey::NullType || type == WebIDBKey::InvalidType); | 89 DCHECK(type == WebIDBKeyTypeNull || type == WebIDBKeyTypeInvalid); |
81 } | 90 } |
82 | 91 |
83 IndexedDBKey::IndexedDBKey(double number, WebIDBKey::Type type) | 92 IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type) |
84 : type_(type), | 93 : type_(type), |
85 date_(number), | 94 date_(number), |
86 number_(number), | 95 number_(number), |
87 size_estimate_(kOverheadSize + sizeof(number)) { | 96 size_estimate_(kOverheadSize + sizeof(number)) { |
88 DCHECK(type == WebIDBKey::NumberType || type == WebIDBKey::DateType); | 97 DCHECK(type == WebIDBKeyTypeNumber || type == WebIDBKeyTypeDate); |
89 } | 98 } |
90 | 99 |
91 IndexedDBKey::IndexedDBKey(const KeyArray& keys) | 100 IndexedDBKey::IndexedDBKey(const KeyArray& keys) |
92 : type_(WebIDBKey::ArrayType), | 101 : type_(WebIDBKeyTypeArray), |
93 array_(CopyKeyArray(keys)), | 102 array_(CopyKeyArray(keys)), |
94 date_(0), | 103 date_(0), |
95 number_(0), | 104 number_(0), |
96 size_estimate_(kOverheadSize + CalculateArraySize(keys)) {} | 105 size_estimate_(kOverheadSize + CalculateArraySize(keys)) {} |
97 | 106 |
98 IndexedDBKey::IndexedDBKey(const string16& key) | 107 IndexedDBKey::IndexedDBKey(const string16& key) |
99 : type_(WebIDBKey::StringType), | 108 : type_(WebIDBKeyTypeString), |
100 string_(key), | 109 string_(key), |
101 size_estimate_(kOverheadSize + | 110 size_estimate_(kOverheadSize + |
102 (key.length() * sizeof(string16::value_type))) {} | 111 (key.length() * sizeof(string16::value_type))) {} |
103 | 112 |
104 IndexedDBKey::IndexedDBKey(const WebIDBKey& key) | 113 IndexedDBKey::IndexedDBKey(const WebIDBKey& key) |
105 : type_(key.type()), | 114 : type_(key.keyType()), |
106 array_(CopyKeyArray(key)), | 115 array_(CopyKeyArray(key)), |
107 string_(key.type() == WebIDBKey::StringType | 116 string_(key.keyType() == WebIDBKeyTypeString |
108 ? static_cast<string16>(key.string()) | 117 ? static_cast<string16>(key.string()) |
109 : string16()), | 118 : string16()), |
110 date_(key.type() == WebIDBKey::DateType ? key.date() : 0), | 119 date_(key.keyType() == WebIDBKeyTypeDate ? key.date() : 0), |
111 number_(key.type() == WebIDBKey::NumberType ? key.number() : 0), | 120 number_(key.keyType() == WebIDBKeyTypeNumber ? key.number() : 0), |
112 size_estimate_(CalculateKeySize(key)) {} | 121 size_estimate_(CalculateKeySize(key)) {} |
113 | 122 |
114 IndexedDBKey::~IndexedDBKey() {} | 123 IndexedDBKey::~IndexedDBKey() {} |
115 | 124 |
116 int IndexedDBKey::Compare(const IndexedDBKey& other) const { | 125 int IndexedDBKey::Compare(const IndexedDBKey& other) const { |
117 DCHECK(IsValid()); | 126 DCHECK(IsValid()); |
118 DCHECK(other.IsValid()); | 127 DCHECK(other.IsValid()); |
119 if (type_ != other.type_) | 128 if (type_ != other.type_) |
120 return type_ > other.type_ ? -1 : 1; | 129 return type_ > other.type_ ? -1 : 1; |
121 | 130 |
122 switch (type_) { | 131 switch (type_) { |
123 case WebIDBKey::ArrayType: | 132 case WebIDBKeyTypeArray: |
124 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) { | 133 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) { |
125 if (int result = array_[i].Compare(other.array_[i])) | 134 if (int result = array_[i].Compare(other.array_[i])) |
126 return result; | 135 return result; |
127 } | 136 } |
128 if (array_.size() < other.array_.size()) | 137 if (array_.size() < other.array_.size()) |
129 return -1; | 138 return -1; |
130 if (array_.size() > other.array_.size()) | 139 if (array_.size() > other.array_.size()) |
131 return 1; | 140 return 1; |
132 return 0; | 141 return 0; |
133 case WebIDBKey::StringType: | 142 case WebIDBKeyTypeString: |
134 return -other.string_.compare(string_); | 143 return -other.string_.compare(string_); |
135 case WebIDBKey::DateType: | 144 case WebIDBKeyTypeDate: |
136 return (date_ < other.date_) ? -1 : (date_ > other.date_) ? 1 : 0; | 145 return (date_ < other.date_) ? -1 : (date_ > other.date_) ? 1 : 0; |
137 case WebIDBKey::NumberType: | 146 case WebIDBKeyTypeNumber: |
138 return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0; | 147 return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0; |
139 case WebIDBKey::InvalidType: | 148 case WebIDBKeyTypeInvalid: |
140 case WebIDBKey::NullType: | 149 case WebIDBKeyTypeNull: |
141 case WebIDBKey::MinType: | 150 case WebIDBKeyTypeMin: |
142 NOTREACHED(); | 151 NOTREACHED(); |
143 return 0; | 152 return 0; |
144 } | 153 } |
145 NOTREACHED(); | 154 NOTREACHED(); |
146 return 0; | 155 return 0; |
147 } | 156 } |
148 | 157 |
149 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { | 158 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { |
150 return Compare(other) < 0; | 159 return Compare(other) < 0; |
151 } | 160 } |
152 | 161 |
153 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { | 162 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { |
154 return !Compare(other); | 163 return !Compare(other); |
155 } | 164 } |
156 | 165 |
157 bool IndexedDBKey::IsValid() const { | 166 bool IndexedDBKey::IsValid() const { |
158 if (type_ == WebIDBKey::InvalidType || type_ == WebIDBKey::NullType) | 167 if (type_ == WebIDBKeyTypeInvalid || type_ == WebIDBKeyTypeNull) |
159 return false; | 168 return false; |
160 | 169 |
161 if (type_ == WebIDBKey::ArrayType) { | 170 if (type_ == WebIDBKeyTypeArray) { |
162 for (size_t i = 0; i < array_.size(); i++) { | 171 for (size_t i = 0; i < array_.size(); i++) { |
163 if (!array_[i].IsValid()) | 172 if (!array_[i].IsValid()) |
164 return false; | 173 return false; |
165 } | 174 } |
166 } | 175 } |
167 | 176 |
168 return true; | 177 return true; |
169 } | 178 } |
170 | 179 |
171 IndexedDBKey::operator WebIDBKey() const { | 180 IndexedDBKey::operator WebIDBKey() const { |
172 switch (type_) { | 181 switch (type_) { |
173 case WebIDBKey::ArrayType: | 182 case WebIDBKeyTypeArray: |
174 return WebIDBKey::createArray(array_); | 183 return WebIDBKey::createArray(array_); |
175 case WebIDBKey::StringType: | 184 case WebIDBKeyTypeString: |
176 return WebIDBKey::createString(string_); | 185 return WebIDBKey::createString(string_); |
177 case WebIDBKey::DateType: | 186 case WebIDBKeyTypeDate: |
178 return WebIDBKey::createDate(date_); | 187 return WebIDBKey::createDate(date_); |
179 case WebIDBKey::NumberType: | 188 case WebIDBKeyTypeNumber: |
180 return WebIDBKey::createNumber(number_); | 189 return WebIDBKey::createNumber(number_); |
181 case WebIDBKey::InvalidType: | 190 case WebIDBKeyTypeInvalid: |
182 return WebIDBKey::createInvalid(); | 191 return WebIDBKey::createInvalid(); |
183 case WebIDBKey::NullType: | 192 case WebIDBKeyTypeNull: |
184 return WebIDBKey::createNull(); | 193 return WebIDBKey::createNull(); |
185 case WebIDBKey::MinType: | 194 case WebIDBKeyTypeMin: |
186 NOTREACHED(); | 195 NOTREACHED(); |
187 return WebIDBKey::createInvalid(); | 196 return WebIDBKey::createInvalid(); |
188 } | 197 } |
189 NOTREACHED(); | 198 NOTREACHED(); |
190 return WebIDBKey::createInvalid(); | 199 return WebIDBKey::createInvalid(); |
191 } | 200 } |
192 | 201 |
193 } // namespace content | 202 } // namespace content |
OLD | NEW |