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