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> | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 8 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" | 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" |
11 | 10 |
12 namespace content { | 11 namespace content { |
13 | 12 |
14 using WebKit::WebIDBKey; | 13 using WebKit::WebIDBKey; |
15 using WebKit::WebVector; | 14 using WebKit::WebVector; |
16 | 15 |
17 namespace { | 16 namespace { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 return result; | 53 return result; |
55 } | 54 } |
56 | 55 |
57 static IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) { | 56 static IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) { |
58 IndexedDBKey::KeyArray result; | 57 IndexedDBKey::KeyArray result; |
59 if (other.type() == WebIDBKey::ArrayType) { | 58 if (other.type() == WebIDBKey::ArrayType) { |
60 result = CopyKeyArray(other.array()); | 59 result = CopyKeyArray(other.array()); |
61 } | 60 } |
62 return result; | 61 return result; |
63 } | 62 } |
64 } // namespace | 63 } // namespace |
65 | 64 |
66 IndexedDBKey::IndexedDBKey() | 65 IndexedDBKey::IndexedDBKey() |
67 : type_(WebIDBKey::NullType), | 66 : type_(WebIDBKey::NullType), |
68 date_(0), | 67 date_(0), |
69 number_(0), | 68 number_(0), |
70 size_estimate_(kOverheadSize) {} | 69 size_estimate_(kOverheadSize) {} |
71 | 70 |
72 IndexedDBKey::IndexedDBKey(WebIDBKey::Type type) | 71 IndexedDBKey::IndexedDBKey(WebIDBKey::Type type) |
73 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { | 72 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { |
74 DCHECK(type == WebIDBKey::NullType || type == WebIDBKey::InvalidType); | 73 DCHECK(type == WebIDBKey::NullType || type == WebIDBKey::InvalidType); |
(...skipping 23 matching lines...) Expand all Loading... |
98 date_(key.type() == WebIDBKey::DateType ? key.date() : 0), | 97 date_(key.type() == WebIDBKey::DateType ? key.date() : 0), |
99 number_(key.type() == WebIDBKey::NumberType ? key.number() : 0), | 98 number_(key.type() == WebIDBKey::NumberType ? key.number() : 0), |
100 size_estimate_(kOverheadSize + CalculateKeySize(key)) {} | 99 size_estimate_(kOverheadSize + CalculateKeySize(key)) {} |
101 | 100 |
102 IndexedDBKey::IndexedDBKey(const string16& key) | 101 IndexedDBKey::IndexedDBKey(const string16& key) |
103 : type_(WebIDBKey::StringType), string_(key) {} | 102 : type_(WebIDBKey::StringType), string_(key) {} |
104 | 103 |
105 IndexedDBKey::~IndexedDBKey() {} | 104 IndexedDBKey::~IndexedDBKey() {} |
106 | 105 |
107 int IndexedDBKey::Compare(const IndexedDBKey& other) const { | 106 int IndexedDBKey::Compare(const IndexedDBKey& other) const { |
108 DCHECK(IsValid()); | |
109 DCHECK(other.IsValid()); | |
110 if (type_ != other.type_) | 107 if (type_ != other.type_) |
111 return type_ > other.type_ ? -1 : 1; | 108 return type_ > other.type_ ? -1 : 1; |
112 | 109 |
113 switch (type_) { | 110 switch (type_) { |
114 case WebIDBKey::ArrayType: | 111 case WebIDBKey::ArrayType: |
115 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) { | 112 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) { |
116 if (int result = array_[i].Compare(other.array_[i])) | 113 if (int result = array_[i].Compare(other.array_[i])) |
117 return result; | 114 return result; |
118 } | 115 } |
119 if (array_.size() < other.array_.size()) | 116 if (array_.size() < other.array_.size()) |
120 return -1; | 117 return -1; |
121 if (array_.size() > other.array_.size()) | 118 if (array_.size() > other.array_.size()) |
122 return 1; | 119 return 1; |
123 return 0; | 120 return 0; |
124 case WebIDBKey::StringType: | 121 case WebIDBKey::StringType: |
125 return -other.string_.compare(string_); | 122 return -other.string_.compare(string_); |
126 case WebIDBKey::DateType: | 123 case WebIDBKey::DateType: |
127 return (date_ < other.date_) ? -1 : (date_ > other.date_) ? 1 : 0; | |
128 case WebIDBKey::NumberType: | 124 case WebIDBKey::NumberType: |
129 return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0; | 125 return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0; |
130 case WebIDBKey::InvalidType: | 126 case WebIDBKey::InvalidType: |
131 case WebIDBKey::NullType: | 127 case WebIDBKey::NullType: |
132 case WebIDBKey::MinType: | 128 default: |
| 129 // This is a placeholder for WebKit::WebIDBKey::MinType |
133 NOTREACHED(); | 130 NOTREACHED(); |
134 return 0; | 131 return 0; |
135 } | 132 } |
136 NOTREACHED(); | |
137 return 0; | |
138 } | 133 } |
139 | 134 |
140 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { | 135 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { |
141 return Compare(other) < 0; | 136 return Compare(other) < 0; |
142 } | 137 } |
143 | 138 |
144 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { | 139 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { |
145 return !Compare(other); | 140 return !Compare(other); |
146 } | 141 } |
147 | 142 |
148 bool IndexedDBKey::IsValid() const { | |
149 if (type_ == WebIDBKey::InvalidType || type_ == WebIDBKey::NullType) | |
150 return false; | |
151 | |
152 if (type_ == WebIDBKey::ArrayType) { | |
153 for (size_t i = 0; i < array_.size(); i++) { | |
154 if (!array_[i].IsValid()) | |
155 return false; | |
156 } | |
157 } | |
158 | |
159 return true; | |
160 } | |
161 | |
162 IndexedDBKey::operator WebIDBKey() const { | 143 IndexedDBKey::operator WebIDBKey() const { |
163 switch (type_) { | 144 switch (type_) { |
164 case WebIDBKey::ArrayType: | 145 case WebIDBKey::ArrayType: |
165 return WebIDBKey::createArray(array_); | 146 return WebIDBKey::createArray(array_); |
166 case WebIDBKey::StringType: | 147 case WebIDBKey::StringType: |
167 return WebIDBKey::createString(string_); | 148 return WebIDBKey::createString(string_); |
168 case WebIDBKey::DateType: | 149 case WebIDBKey::DateType: |
169 return WebIDBKey::createDate(date_); | 150 return WebIDBKey::createDate(date_); |
170 case WebIDBKey::NumberType: | 151 case WebIDBKey::NumberType: |
171 return WebIDBKey::createNumber(number_); | 152 return WebIDBKey::createNumber(number_); |
172 case WebIDBKey::InvalidType: | 153 case WebIDBKey::InvalidType: |
173 return WebIDBKey::createInvalid(); | 154 return WebIDBKey::createInvalid(); |
174 case WebIDBKey::NullType: | 155 case WebIDBKey::NullType: |
175 return WebIDBKey::createNull(); | 156 return WebIDBKey::createNull(); |
176 case WebIDBKey::MinType: | 157 default: |
| 158 // This is a placeholder for WebKit::WebIDBKey::MinType |
177 NOTREACHED(); | 159 NOTREACHED(); |
178 return WebIDBKey::createInvalid(); | 160 return WebIDBKey::createInvalid(); |
179 } | 161 } |
180 NOTREACHED(); | |
181 return WebIDBKey::createInvalid(); | |
182 } | 162 } |
183 | 163 |
184 } // namespace content | 164 } // namespace content |
OLD | NEW |