| 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/Source/Platform/chromium/public/WebString.h" | 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
| 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" | 10 #include "third_party/WebKit/Source/Platform/chromium/public/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 } |
| 133 } | 136 } |
| 134 | 137 |
| 135 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { | 138 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const { |
| 136 return Compare(other) < 0; | 139 return Compare(other) < 0; |
| 137 } | 140 } |
| 138 | 141 |
| 139 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { | 142 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const { |
| 140 return !Compare(other); | 143 return !Compare(other); |
| 141 } | 144 } |
| 142 | 145 |
| 146 bool IndexedDBKey::IsValid() const { |
| 147 if (type_ == WebIDBKey::InvalidType || type_ == WebIDBKey::NullType) |
| 148 return false; |
| 149 |
| 150 if (type_ == WebIDBKey::ArrayType) { |
| 151 for (size_t i = 0; i < array_.size(); i++) { |
| 152 if (!array_[i].IsValid()) |
| 153 return false; |
| 154 } |
| 155 } |
| 156 |
| 157 return true; |
| 158 } |
| 159 |
| 143 IndexedDBKey::operator WebIDBKey() const { | 160 IndexedDBKey::operator WebIDBKey() const { |
| 144 switch (type_) { | 161 switch (type_) { |
| 145 case WebIDBKey::ArrayType: | 162 case WebIDBKey::ArrayType: |
| 146 return WebIDBKey::createArray(array_); | 163 return WebIDBKey::createArray(array_); |
| 147 case WebIDBKey::StringType: | 164 case WebIDBKey::StringType: |
| 148 return WebIDBKey::createString(string_); | 165 return WebIDBKey::createString(string_); |
| 149 case WebIDBKey::DateType: | 166 case WebIDBKey::DateType: |
| 150 return WebIDBKey::createDate(date_); | 167 return WebIDBKey::createDate(date_); |
| 151 case WebIDBKey::NumberType: | 168 case WebIDBKey::NumberType: |
| 152 return WebIDBKey::createNumber(number_); | 169 return WebIDBKey::createNumber(number_); |
| 153 case WebIDBKey::InvalidType: | 170 case WebIDBKey::InvalidType: |
| 154 return WebIDBKey::createInvalid(); | 171 return WebIDBKey::createInvalid(); |
| 155 case WebIDBKey::NullType: | 172 case WebIDBKey::NullType: |
| 156 return WebIDBKey::createNull(); | 173 return WebIDBKey::createNull(); |
| 157 default: | 174 case WebIDBKey::MinType: |
| 158 // This is a placeholder for WebKit::WebIDBKey::MinType | |
| 159 NOTREACHED(); | 175 NOTREACHED(); |
| 160 return WebIDBKey::createInvalid(); | 176 return WebIDBKey::createInvalid(); |
| 161 } | 177 } |
| 162 } | 178 } |
| 163 | 179 |
| 164 } // namespace content | 180 } // namespace content |
| OLD | NEW |