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 @@
#include <algorithm>
#include <limits>
#include <set>
#include <utility>
#include "base/auto_reset.h"
#include "base/logging.h"
......@@ -1187,7 +1188,7 @@ Status IndexedDBDatabase::GetAllOperation(
if (cursor_type == indexed_db::CURSOR_KEY_ONLY) {
// 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.
callbacks->OnSuccess(IndexedDBKey(found_keys));
callbacks->OnSuccess(IndexedDBKey(std::move(found_keys)));
} else {
callbacks->OnSuccessArray(&found_values);
}
......
......@@ -6,6 +6,7 @@
#include <iterator>
#include <limits>
#include <utility>
#include "base/logging.h"
#include "base/strings/string16.h"
......@@ -390,21 +391,21 @@ bool DecodeIDBKey(StringPiece* slice, std::unique_ptr<IndexedDBKey>* value) {
return false;
array.push_back(*key);
}
*value = std::make_unique<IndexedDBKey>(array);
*value = std::make_unique<IndexedDBKey>(std::move(array));
return true;
}
case kIndexedDBKeyBinaryTypeByte: {
std::string binary;
if (!DecodeBinary(slice, &binary))
return false;
*value = std::make_unique<IndexedDBKey>(binary);
*value = std::make_unique<IndexedDBKey>(std::move(binary));
return true;
}
case kIndexedDBKeyStringTypeByte: {
base::string16 s;
if (!DecodeStringWithLength(slice, &s))
return false;
*value = std::make_unique<IndexedDBKey>(s);
*value = std::make_unique<IndexedDBKey>(std::move(s));
return true;
}
case kIndexedDBKeyDateTypeByte: {
......
......@@ -8,6 +8,7 @@
#include <stdint.h>
#include <limits>
#include <utility>
#include <vector>
#include "base/macros.h"
......@@ -34,7 +35,7 @@ static IndexedDBKey CreateArrayIDBKey() {
static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1) {
IndexedDBKey::KeyArray array;
array.push_back(key1);
return IndexedDBKey(array);
return IndexedDBKey(std::move(array));
}
static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1,
......@@ -42,7 +43,7 @@ static IndexedDBKey CreateArrayIDBKey(const IndexedDBKey& key1,
IndexedDBKey::KeyArray array;
array.push_back(key1);
array.push_back(key2);
return IndexedDBKey(array);
return IndexedDBKey(std::move(array));
}
static std::string WrappedEncodeByte(char value) {
......@@ -591,7 +592,7 @@ TEST(IndexedDBLevelDBCodingTest, EncodeDecodeIDBKey) {
array.push_back(IndexedDBKey(ASCIIToUTF16("Hello World!")));
array.push_back(IndexedDBKey(std::string("\x01\x02")));
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) {
expected_key = test_cases[i];
......
......@@ -8,6 +8,7 @@
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "base/logging.h"
......@@ -75,7 +76,7 @@ IndexedDBKey IndexedDBKeyBuilder::Build(blink::WebIDBKeyView key) {
key_string.append(segment, segment_size);
return true;
});
return IndexedDBKey(key_string);
return IndexedDBKey(std::move(key_string));
}
case kWebIDBKeyTypeString:
return IndexedDBKey(key.String().Utf16());
......
......@@ -5,6 +5,7 @@
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
#include <string>
#include <utility>
namespace blink {
......@@ -40,16 +41,6 @@ int Compare(const T& a, const T& b) {
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
IndexedDBKey::IndexedDBKey()
......@@ -67,22 +58,22 @@ IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
DCHECK(type == kWebIDBKeyTypeNumber || type == kWebIDBKeyTypeDate);
}
IndexedDBKey::IndexedDBKey(const KeyArray& array)
IndexedDBKey::IndexedDBKey(KeyArray array)
: type_(kWebIDBKeyTypeArray),
array_(CopyKeyArray(array)),
size_estimate_(kOverheadSize + CalculateArraySize(array)) {}
array_(std::move(array)),
size_estimate_(kOverheadSize + CalculateArraySize(array_)) {}
IndexedDBKey::IndexedDBKey(const std::string& binary)
IndexedDBKey::IndexedDBKey(std::string binary)
: type_(kWebIDBKeyTypeBinary),
binary_(binary),
binary_(std::move(binary)),
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),
string_(string),
string_(std::move(string)),
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() = default;
......
......@@ -6,6 +6,7 @@
#include <stddef.h>
#include <utility>
#include <vector>
#include "base/strings/string16.h"
......@@ -34,7 +35,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) {
estimates.push_back(24u); // Overhead + sizeof(double).
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).
estimates.push_back(2064u);
......@@ -44,7 +45,7 @@ TEST(IndexedDBKeyTest, KeySizeEstimates) {
for (size_t i = 0; i < array_size; ++i) {
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)).
estimates.push_back(24592u);
......
......@@ -4,6 +4,8 @@
#include "third_party/blink/public/common/indexeddb/indexeddb_mojom_traits.h"
#include <utility>
#include "base/stl_util.h"
#include "mojo/public/cpp/base/string16_mojom_traits.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
......@@ -166,22 +168,21 @@ bool UnionTraits<blink::mojom::IDBKeyDataDataView, blink::IndexedDBKey>::Read(
std::vector<blink::IndexedDBKey> array;
if (!data.ReadKeyArray(&array))
return false;
*out = blink::IndexedDBKey(array);
*out = blink::IndexedDBKey(std::move(array));
return true;
}
case blink::mojom::IDBKeyDataDataView::Tag::BINARY: {
std::vector<uint8_t> binary;
if (!data.ReadBinary(&binary))
return false;
*out = blink::IndexedDBKey(
std::string(binary.data(), binary.data() + binary.size()));
ArrayDataView<uint8_t> bytes;
data.GetBinaryDataView(&bytes);
std::string binary(bytes.data(), bytes.data() + bytes.size());
*out = blink::IndexedDBKey(std::move(binary));
return true;
}
case blink::mojom::IDBKeyDataDataView::Tag::STRING: {
base::string16 string;
if (!data.ReadString(&string))
return false;
*out = blink::IndexedDBKey(string);
*out = blink::IndexedDBKey(std::move(string));
return true;
}
case blink::mojom::IDBKeyDataDataView::Tag::DATE:
......
......@@ -23,9 +23,9 @@ class BLINK_COMMON_EXPORT IndexedDBKey {
IndexedDBKey(); // Defaults to blink::WebIDBKeyTypeInvalid.
explicit IndexedDBKey(blink::WebIDBKeyType); // must be Null or Invalid
explicit IndexedDBKey(const KeyArray& array);
explicit IndexedDBKey(const std::string& binary);
explicit IndexedDBKey(const base::string16& string);
explicit IndexedDBKey(KeyArray array);
explicit IndexedDBKey(std::string binary);
explicit IndexedDBKey(base::string16 string);
IndexedDBKey(double number,
blink::WebIDBKeyType type); // must be date or number
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