Reland "Be explicit about forcing TouchSelectionController updates"
[chromium-blink-merge.git] / content / common / indexed_db / indexed_db_key.cc
blobc23d3e44e2fef76604685e0ab01996683f9b874f
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
3 // found in the LICENSE file.
5 #include "content/common/indexed_db/indexed_db_key.h"
7 #include <string>
9 namespace content {
11 using blink::WebIDBKey;
12 using blink::WebIDBKeyType;
13 using blink::WebIDBKeyTypeArray;
14 using blink::WebIDBKeyTypeBinary;
15 using blink::WebIDBKeyTypeDate;
16 using blink::WebIDBKeyTypeInvalid;
17 using blink::WebIDBKeyTypeMin;
18 using blink::WebIDBKeyTypeNull;
19 using blink::WebIDBKeyTypeNumber;
20 using blink::WebIDBKeyTypeString;
22 namespace {
24 // Very rough estimate of minimum key size overhead.
25 const size_t kOverheadSize = 16;
27 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) {
28 size_t size(0);
29 for (size_t i = 0; i < keys.size(); ++i)
30 size += keys[i].size_estimate();
31 return size;
34 template<typename T>
35 int Compare(const T& a, const T& b) {
36 // Using '<' for both comparisons here is as generic as possible (for e.g.
37 // objects which only define operator<() and not operator>() or operator==())
38 // and also allows e.g. floating point NaNs to compare equal.
39 if (a < b)
40 return -1;
41 return (b < a) ? 1 : 0;
44 template <typename T>
45 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) {
46 IndexedDBKey::KeyArray result;
47 result.reserve(array.size());
48 for (size_t i = 0; i < array.size(); ++i) {
49 result.push_back(IndexedDBKey(array[i]));
51 return result;
54 } // namespace
56 IndexedDBKey::IndexedDBKey()
57 : type_(WebIDBKeyTypeNull),
58 size_estimate_(kOverheadSize) {}
60 IndexedDBKey::IndexedDBKey(WebIDBKeyType type)
61 : type_(type), size_estimate_(kOverheadSize) {
62 DCHECK(type == WebIDBKeyTypeNull || type == WebIDBKeyTypeInvalid);
65 IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
66 : type_(type),
67 number_(number),
68 size_estimate_(kOverheadSize + sizeof(number)) {
69 DCHECK(type == WebIDBKeyTypeNumber || type == WebIDBKeyTypeDate);
72 IndexedDBKey::IndexedDBKey(const KeyArray& array)
73 : type_(WebIDBKeyTypeArray),
74 array_(CopyKeyArray(array)),
75 size_estimate_(kOverheadSize + CalculateArraySize(array)) {}
77 IndexedDBKey::IndexedDBKey(const std::string& binary)
78 : type_(WebIDBKeyTypeBinary),
79 binary_(binary),
80 size_estimate_(kOverheadSize +
81 (binary.length() * sizeof(std::string::value_type))) {}
83 IndexedDBKey::IndexedDBKey(const base::string16& string)
84 : type_(WebIDBKeyTypeString),
85 string_(string),
86 size_estimate_(kOverheadSize +
87 (string.length() * sizeof(base::string16::value_type))) {}
89 IndexedDBKey::IndexedDBKey(const IndexedDBKey& other) = default;
90 IndexedDBKey::~IndexedDBKey() = default;
91 IndexedDBKey& IndexedDBKey::operator=(const IndexedDBKey& other) = default;
93 bool IndexedDBKey::IsValid() const {
94 if (type_ == WebIDBKeyTypeInvalid || type_ == WebIDBKeyTypeNull)
95 return false;
97 if (type_ == WebIDBKeyTypeArray) {
98 for (size_t i = 0; i < array_.size(); i++) {
99 if (!array_[i].IsValid())
100 return false;
104 return true;
107 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const {
108 return CompareTo(other) < 0;
111 bool IndexedDBKey::Equals(const IndexedDBKey& other) const {
112 return !CompareTo(other);
115 int IndexedDBKey::CompareTo(const IndexedDBKey& other) const {
116 DCHECK(IsValid());
117 DCHECK(other.IsValid());
118 if (type_ != other.type_)
119 return type_ > other.type_ ? -1 : 1;
121 switch (type_) {
122 case WebIDBKeyTypeArray:
123 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) {
124 int result = array_[i].CompareTo(other.array_[i]);
125 if (result != 0)
126 return result;
128 return Compare(array_.size(), other.array_.size());
129 case WebIDBKeyTypeBinary:
130 return binary_.compare(other.binary_);
131 case WebIDBKeyTypeString:
132 return string_.compare(other.string_);
133 case WebIDBKeyTypeDate:
134 case WebIDBKeyTypeNumber:
135 return Compare(number_, other.number_);
136 case WebIDBKeyTypeInvalid:
137 case WebIDBKeyTypeNull:
138 case WebIDBKeyTypeMin:
139 default:
140 NOTREACHED();
141 return 0;
145 } // namespace content