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