| 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 <string> |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "third_party/WebKit/public/platform/WebIDBKey.h" | |
| 10 #include "third_party/WebKit/public/platform/WebString.h" | |
| 11 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 12 | 9 |
| 13 namespace content { | 10 namespace content { |
| 14 | 11 |
| 15 using WebKit::WebIDBKey; | 12 using WebKit::WebIDBKey; |
| 16 using WebKit::WebIDBKeyType; | 13 using WebKit::WebIDBKeyType; |
| 17 using WebKit::WebIDBKeyTypeArray; | 14 using WebKit::WebIDBKeyTypeArray; |
| 18 using WebKit::WebIDBKeyTypeDate; | 15 using WebKit::WebIDBKeyTypeDate; |
| 19 using WebKit::WebIDBKeyTypeInvalid; | 16 using WebKit::WebIDBKeyTypeInvalid; |
| 20 using WebKit::WebIDBKeyTypeMin; | 17 using WebKit::WebIDBKeyTypeMin; |
| 21 using WebKit::WebIDBKeyTypeNull; | 18 using WebKit::WebIDBKeyTypeNull; |
| 22 using WebKit::WebIDBKeyTypeNumber; | 19 using WebKit::WebIDBKeyTypeNumber; |
| 23 using WebKit::WebIDBKeyTypeString; | 20 using WebKit::WebIDBKeyTypeString; |
| 24 using WebKit::WebVector; | |
| 25 | 21 |
| 26 namespace { | 22 namespace { |
| 27 | 23 |
| 28 // Very rough estimate of minimum key size overhead. | 24 // Very rough estimate of minimum key size overhead. |
| 29 const size_t kOverheadSize = 16; | 25 const size_t kOverheadSize = 16; |
| 30 | 26 |
| 31 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) { | 27 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) { |
| 32 size_t size(0); | 28 size_t size(0); |
| 33 for (size_t i = 0; i < keys.size(); ++i) | 29 for (size_t i = 0; i < keys.size(); ++i) |
| 34 size += keys[i].size_estimate(); | 30 size += keys[i].size_estimate(); |
| 35 return size; | 31 return size; |
| 36 } | 32 } |
| 37 | 33 |
| 38 static size_t CalculateKeySize(const WebIDBKey& key) { | |
| 39 switch (key.keyType()) { | |
| 40 case WebIDBKeyTypeArray: { | |
| 41 const WebVector<WebIDBKey>& array = key.array(); | |
| 42 size_t total = 0; | |
| 43 for (size_t i = 0; i < array.size(); ++i) | |
| 44 total += CalculateKeySize(array[i]); | |
| 45 return kOverheadSize + total; | |
| 46 } | |
| 47 case WebIDBKeyTypeString: | |
| 48 return kOverheadSize + | |
| 49 (key.string().length() * sizeof(string16::value_type)); | |
| 50 | |
| 51 case WebIDBKeyTypeDate: | |
| 52 case WebIDBKeyTypeNumber: | |
| 53 return kOverheadSize + sizeof(double); | |
| 54 | |
| 55 default: | |
| 56 return kOverheadSize; | |
| 57 } | |
| 58 NOTREACHED(); | |
| 59 return 0; | |
| 60 } | |
| 61 | |
| 62 template <typename T> | 34 template <typename T> |
| 63 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) { | 35 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) { |
| 64 IndexedDBKey::KeyArray result; | 36 IndexedDBKey::KeyArray result; |
| 65 result.reserve(array.size()); | 37 result.reserve(array.size()); |
| 66 for (size_t i = 0; i < array.size(); ++i) { | 38 for (size_t i = 0; i < array.size(); ++i) { |
| 67 result.push_back(IndexedDBKey(array[i])); | 39 result.push_back(IndexedDBKey(array[i])); |
| 68 } | 40 } |
| 69 return result; | 41 return result; |
| 70 } | 42 } |
| 71 | 43 |
| 72 static IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) { | |
| 73 IndexedDBKey::KeyArray result; | |
| 74 if (other.keyType() == WebIDBKeyTypeArray) { | |
| 75 result = CopyKeyArray(other.array()); | |
| 76 } | |
| 77 return result; | |
| 78 } | |
| 79 } // namespace | 44 } // namespace |
| 80 | 45 |
| 81 IndexedDBKey::IndexedDBKey() | 46 IndexedDBKey::IndexedDBKey() |
| 82 : type_(WebIDBKeyTypeNull), | 47 : type_(WebIDBKeyTypeNull), |
| 83 date_(0), | 48 date_(0), |
| 84 number_(0), | 49 number_(0), |
| 85 size_estimate_(kOverheadSize) {} | 50 size_estimate_(kOverheadSize) {} |
| 86 | 51 |
| 87 IndexedDBKey::IndexedDBKey(WebIDBKeyType type) | 52 IndexedDBKey::IndexedDBKey(WebIDBKeyType type) |
| 88 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { | 53 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 date_(0), | 68 date_(0), |
| 104 number_(0), | 69 number_(0), |
| 105 size_estimate_(kOverheadSize + CalculateArraySize(keys)) {} | 70 size_estimate_(kOverheadSize + CalculateArraySize(keys)) {} |
| 106 | 71 |
| 107 IndexedDBKey::IndexedDBKey(const string16& key) | 72 IndexedDBKey::IndexedDBKey(const string16& key) |
| 108 : type_(WebIDBKeyTypeString), | 73 : type_(WebIDBKeyTypeString), |
| 109 string_(key), | 74 string_(key), |
| 110 size_estimate_(kOverheadSize + | 75 size_estimate_(kOverheadSize + |
| 111 (key.length() * sizeof(string16::value_type))) {} | 76 (key.length() * sizeof(string16::value_type))) {} |
| 112 | 77 |
| 113 IndexedDBKey::IndexedDBKey(const WebIDBKey& key) | |
| 114 : type_(key.keyType()), | |
| 115 array_(CopyKeyArray(key)), | |
| 116 string_(key.keyType() == WebIDBKeyTypeString | |
| 117 ? static_cast<string16>(key.string()) | |
| 118 : string16()), | |
| 119 date_(key.keyType() == WebIDBKeyTypeDate ? key.date() : 0), | |
| 120 number_(key.keyType() == WebIDBKeyTypeNumber ? key.number() : 0), | |
| 121 size_estimate_(CalculateKeySize(key)) {} | |
| 122 | |
| 123 IndexedDBKey::~IndexedDBKey() {} | 78 IndexedDBKey::~IndexedDBKey() {} |
| 124 | 79 |
| 125 int IndexedDBKey::Compare(const IndexedDBKey& other) const { | 80 int IndexedDBKey::Compare(const IndexedDBKey& other) const { |
| 126 DCHECK(IsValid()); | 81 DCHECK(IsValid()); |
| 127 DCHECK(other.IsValid()); | 82 DCHECK(other.IsValid()); |
| 128 if (type_ != other.type_) | 83 if (type_ != other.type_) |
| 129 return type_ > other.type_ ? -1 : 1; | 84 return type_ > other.type_ ? -1 : 1; |
| 130 | 85 |
| 131 switch (type_) { | 86 switch (type_) { |
| 132 case WebIDBKeyTypeArray: | 87 case WebIDBKeyTypeArray: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 if (type_ == WebIDBKeyTypeArray) { | 125 if (type_ == WebIDBKeyTypeArray) { |
| 171 for (size_t i = 0; i < array_.size(); i++) { | 126 for (size_t i = 0; i < array_.size(); i++) { |
| 172 if (!array_[i].IsValid()) | 127 if (!array_[i].IsValid()) |
| 173 return false; | 128 return false; |
| 174 } | 129 } |
| 175 } | 130 } |
| 176 | 131 |
| 177 return true; | 132 return true; |
| 178 } | 133 } |
| 179 | 134 |
| 180 IndexedDBKey::operator WebIDBKey() const { | |
| 181 switch (type_) { | |
| 182 case WebIDBKeyTypeArray: | |
| 183 return WebIDBKey::createArray(array_); | |
| 184 case WebIDBKeyTypeString: | |
| 185 return WebIDBKey::createString(string_); | |
| 186 case WebIDBKeyTypeDate: | |
| 187 return WebIDBKey::createDate(date_); | |
| 188 case WebIDBKeyTypeNumber: | |
| 189 return WebIDBKey::createNumber(number_); | |
| 190 case WebIDBKeyTypeInvalid: | |
| 191 return WebIDBKey::createInvalid(); | |
| 192 case WebIDBKeyTypeNull: | |
| 193 return WebIDBKey::createNull(); | |
| 194 case WebIDBKeyTypeMin: | |
| 195 NOTREACHED(); | |
| 196 return WebIDBKey::createInvalid(); | |
| 197 } | |
| 198 NOTREACHED(); | |
| 199 return WebIDBKey::createInvalid(); | |
| 200 } | |
| 201 | |
| 202 } // namespace content | 135 } // namespace content |
| OLD | NEW |