Commit c07c4f39 authored by Chase Phillips's avatar Chase Phillips Committed by Commit Bot

IndexedDB: Use ArrayDataView to convert IDBKey binary to std::string

As part of this change, also update the IndexedDBKey
constructors to support std::move().

Bug: 902498
Bug: 717812
Change-Id: Ifd94d01a525850c6293fbfaf28624d3614a419a6
Reviewed-on: https://chromium-review.googlesource.com/c/1321164
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607347}
parent 30c82c22
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <set> #include <set>
#include <utility>
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -1187,7 +1188,7 @@ Status IndexedDBDatabase::GetAllOperation( ...@@ -1187,7 +1188,7 @@ Status IndexedDBDatabase::GetAllOperation(
if (cursor_type == indexed_db::CURSOR_KEY_ONLY) { if (cursor_type == indexed_db::CURSOR_KEY_ONLY) {
// IndexedDBKey already supports an array of values so we can leverage this // IndexedDBKey already supports an array of values so we can leverage this
// to return an array of keys - no need to create our own array of keys. // to return an array of keys - no need to create our own array of keys.
callbacks->OnSuccess(IndexedDBKey(found_keys)); callbacks->OnSuccess(IndexedDBKey(std::move(found_keys)));
} else { } else {
callbacks->OnSuccessArray(&found_values); callbacks->OnSuccessArray(&found_values);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <utility>
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
...@@ -390,21 +391,21 @@ bool DecodeIDBKey(StringPiece* slice, std::unique_ptr<IndexedDBKey>* value) { ...@@ -390,21 +391,21 @@ bool DecodeIDBKey(StringPiece* slice, std::unique_ptr<IndexedDBKey>* value) {
return false; return false;
array.push_back(*key); array.push_back(*key);
} }
*value = std::make_unique<IndexedDBKey>(array); *value = std::make_unique<IndexedDBKey>(std::move(array));
return true; return true;
} }
case kIndexedDBKeyBinaryTypeByte: { case kIndexedDBKeyBinaryTypeByte: {
std::string binary; std::string binary;
if (!DecodeBinary(slice, &binary)) if (!DecodeBinary(slice, &binary))
return false; return false;
*value = std::make_unique<IndexedDBKey>(binary); *value = std::make_unique<IndexedDBKey>(std::move(binary));
return true; return true;
} }
case kIndexedDBKeyStringTypeByte: { case kIndexedDBKeyStringTypeByte: {
base::string16 s; base::string16 s;
if (!DecodeStringWithLength(slice, &s)) if (!DecodeStringWithLength(slice, &s))
return false; return false;
*value = std::make_unique<IndexedDBKey>(s); *value = std::make_unique<IndexedDBKey>(std::move(s));
return true; return true;
} }
case kIndexedDBKeyDateTypeByte: { case kIndexedDBKeyDateTypeByte: {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <stdint.h> #include <stdint.h>
#include <limits> #include <limits>
#include <utility>
#include <vector> #include <vector>
#include "base/macros.h" #include "base/macros.h"
...@@ -34,7 +35,7 @@ static IndexedDBKey CreateArrayIDBKey() { ...@@ -34,7 +35,7 @@ static IndexedDBKey CreateArrayIDBKey() {
static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1) { static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1) {
IndexedDBKey::KeyArray array; IndexedDBKey::KeyArray array;
array.push_back(key1); array.push_back(key1);
return IndexedDBKey(array); return IndexedDBKey(std::move(array));
} }
static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1, static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1,
...@@ -42,7 +43,7 @@ static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1, ...@@ -42,7 +43,7 @@ static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1,
IndexedDBKey::KeyArray array; IndexedDBKey::KeyArray array;
array.push_back(key1); array.push_back(key1);
array.push_back(key2); array.push_back(key2);
return IndexedDBKey(array); return IndexedDBKey(std::move(array));
} }
static std::string WrappedEncodeByte(char value) { static std::string WrappedEncodeByte(char value) {
...@@ -591,7 +592,7 @@ TEST(IndexedDBLevelDBCodingTest, EncodeDecodeIDBKey) { ...@@ -591,7 +592,7 @@ TEST(IndexedDBLevelDBCodingTest, EncodeDecodeIDBKey) {
array.push_back(IndexedDBKey(ASCIIToUTF16("Hello World!"))); array.push_back(IndexedDBKey(ASCIIToUTF16("Hello World!")));
array.push_back(IndexedDBKey(std::string("\x01\x02"))); array.push_back(IndexedDBKey(std::string("\x01\x02")));
array.push_back(IndexedDBKey(IndexedDBKey::KeyArray())); array.push_back(IndexedDBKey(IndexedDBKey::KeyArray()));
test_cases.push_back(IndexedDBKey(array)); test_cases.push_back(IndexedDBKey(std::move(array)));
for (size_t i = 0; i < test_cases.size(); ++i) { for (size_t i = 0; i < test_cases.size(); ++i) {
expected_key = test_cases[i]; expected_key = test_cases[i];
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "base/logging.h" #include "base/logging.h"
...@@ -75,7 +76,7 @@ IndexedDBKey IndexedDBKeyBuilder::Build(blink::WebIDBKeyView key) { ...@@ -75,7 +76,7 @@ IndexedDBKey IndexedDBKeyBuilder::Build(blink::WebIDBKeyView key) {
key_string.append(segment, segment_size); key_string.append(segment, segment_size);
return true; return true;
}); });
return IndexedDBKey(key_string); return IndexedDBKey(std::move(key_string));
} }
case kWebIDBKeyTypeString: case kWebIDBKeyTypeString:
return IndexedDBKey(key.String().Utf16()); return IndexedDBKey(key.String().Utf16());
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h" #include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include <string> #include <string>
#include <utility>
namespace blink { namespace blink {
...@@ -40,16 +41,6 @@ int Compare(const T& a, const T& b) { ...@@ -40,16 +41,6 @@ int Compare(const T& a, const T& b) {
return (b < a) ? 1 : 0; return (b < a) ? 1 : 0;
} }
template <typename T>
static IndexedDBKey::KeyArray CopyKeyArray(const T& array) {
IndexedDBKey::KeyArray result;
result.reserve(array.size());
for (size_t i = 0; i < array.size(); ++i) {
result.push_back(IndexedDBKey(array[i]));
}
return result;
}
} // namespace } // namespace
IndexedDBKey::IndexedDBKey() IndexedDBKey::IndexedDBKey()
...@@ -67,22 +58,22 @@ IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type) ...@@ -67,22 +58,22 @@ IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
DCHECK(type == kWebIDBKeyTypeNumber || type == kWebIDBKeyTypeDate); DCHECK(type == kWebIDBKeyTypeNumber || type == kWebIDBKeyTypeDate);
} }
IndexedDBKey::IndexedDBKey(const KeyArray& array) IndexedDBKey::IndexedDBKey(KeyArray array)
: type_(kWebIDBKeyTypeArray), : type_(kWebIDBKeyTypeArray),
array_(CopyKeyArray(array)), array_(std::move(array)),
size_estimate_(kOverheadSize + CalculateArraySize(array)) {} size_estimate_(kOverheadSize + CalculateArraySize(array_)) {}
IndexedDBKey::IndexedDBKey(const std::string& binary) IndexedDBKey::IndexedDBKey(std::string binary)
: type_(kWebIDBKeyTypeBinary), : type_(kWebIDBKeyTypeBinary),
binary_(binary), binary_(std::move(binary)),
size_estimate_(kOverheadSize + size_estimate_(kOverheadSize +
(binary.length() * sizeof(std::string::value_type))) {} (binary_.length() * sizeof(std::string::value_type))) {}
IndexedDBKey::IndexedDBKey(const base::string16& string) IndexedDBKey::IndexedDBKey(base::string16 string)
: type_(kWebIDBKeyTypeString), : type_(kWebIDBKeyTypeString),
string_(string), string_(std::move(string)),
size_estimate_(kOverheadSize + size_estimate_(kOverheadSize +
(string.length() * sizeof(base::string16::value_type))) {} (string_.length() * sizeof(base::string16::value_type))) {}
IndexedDBKey::IndexedDBKey(const IndexedDBKey& other) = default; IndexedDBKey::IndexedDBKey(const IndexedDBKey& other) = default;
IndexedDBKey::~IndexedDBKey() = default; IndexedDBKey::~IndexedDBKey() = default;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <stddef.h> #include <stddef.h>
#include <utility>
#include <vector> #include <vector>
#include "base/strings/string16.h" #include "base/strings/string16.h"
...@@ -34,7 +35,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) { ...@@ -34,7 +35,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) {
estimates.push_back(24u); // Overhead + sizeof(double). estimates.push_back(24u); // Overhead + sizeof(double).
const base::string16 string(1024, static_cast<base::char16>('X')); const base::string16 string(1024, static_cast<base::char16>('X'));
keys.push_back(IndexedDBKey(string)); keys.push_back(IndexedDBKey(std::move(string)));
// Overhead + string length * sizeof(base::char16). // Overhead + string length * sizeof(base::char16).
estimates.push_back(2064u); estimates.push_back(2064u);
...@@ -44,7 +45,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) { ...@@ -44,7 +45,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) {
for (size_t i = 0; i < array_size; ++i) { for (size_t i = 0; i < array_size; ++i) {
array.push_back(IndexedDBKey(value, blink::kWebIDBKeyTypeNumber)); array.push_back(IndexedDBKey(value, blink::kWebIDBKeyTypeNumber));
} }
keys.push_back(IndexedDBKey(array)); keys.push_back(IndexedDBKey(std::move(array)));
// Overhead + array length * (Overhead + sizeof(double)). // Overhead + array length * (Overhead + sizeof(double)).
estimates.push_back(24592u); estimates.push_back(24592u);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "third_party/blink/public/common/indexeddb/indexeddb_mojom_traits.h" #include "third_party/blink/public/common/indexeddb/indexeddb_mojom_traits.h"
#include <utility>
#include "base/stl_util.h" #include "base/stl_util.h"
#include "mojo/public/cpp/base/string16_mojom_traits.h" #include "mojo/public/cpp/base/string16_mojom_traits.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h" #include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
...@@ -166,22 +168,21 @@ bool UnionTraits<blink::mojom::IDBKeyDataDataView, blink::IndexedDBKey>::Read( ...@@ -166,22 +168,21 @@ bool UnionTraits<blink::mojom::IDBKeyDataDataView, blink::IndexedDBKey>::Read(
std::vector<blink::IndexedDBKey> array; std::vector<blink::IndexedDBKey> array;
if (!data.ReadKeyArray(&array)) if (!data.ReadKeyArray(&array))
return false; return false;
*out = blink::IndexedDBKey(array); *out = blink::IndexedDBKey(std::move(array));
return true; return true;
} }
case blink::mojom::IDBKeyDataDataView::Tag::BINARY: { case blink::mojom::IDBKeyDataDataView::Tag::BINARY: {
std::vector<uint8_t> binary; ArrayDataView<uint8_t> bytes;
if (!data.ReadBinary(&binary)) data.GetBinaryDataView(&bytes);
return false; std::string binary(bytes.data(), bytes.data() + bytes.size());
*out = blink::IndexedDBKey( *out = blink::IndexedDBKey(std::move(binary));
std::string(binary.data(), binary.data() + binary.size()));
return true; return true;
} }
case blink::mojom::IDBKeyDataDataView::Tag::STRING: { case blink::mojom::IDBKeyDataDataView::Tag::STRING: {
base::string16 string; base::string16 string;
if (!data.ReadString(&string)) if (!data.ReadString(&string))
return false; return false;
*out = blink::IndexedDBKey(string); *out = blink::IndexedDBKey(std::move(string));
return true; return true;
} }
case blink::mojom::IDBKeyDataDataView::Tag::DATE: case blink::mojom::IDBKeyDataDataView::Tag::DATE:
......
...@@ -23,9 +23,9 @@ class BLINK_COMMON_EXPORT IndexedDBKey { ...@@ -23,9 +23,9 @@ class BLINK_COMMON_EXPORT IndexedDBKey {
IndexedDBKey(); // Defaults to blink::WebIDBKeyTypeInvalid. IndexedDBKey(); // Defaults to blink::WebIDBKeyTypeInvalid.
explicit IndexedDBKey(blink::WebIDBKeyType); // must be Null or Invalid explicit IndexedDBKey(blink::WebIDBKeyType); // must be Null or Invalid
explicit IndexedDBKey(const KeyArray& array); explicit IndexedDBKey(KeyArray array);
explicit IndexedDBKey(const std::string& binary); explicit IndexedDBKey(std::string binary);
explicit IndexedDBKey(const base::string16& string); explicit IndexedDBKey(base::string16 string);
IndexedDBKey(double number, IndexedDBKey(double number,
blink::WebIDBKeyType type); // must be date or number blink::WebIDBKeyType type); // must be date or number
IndexedDBKey(const IndexedDBKey& other); IndexedDBKey(const IndexedDBKey& other);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment