1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/indexeddb/idb_value.h"
#include <memory>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-blink.h"
#include "third_party/blink/public/platform/web_blob_info.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value_wrapping.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "v8/include/v8.h"
namespace blink {
IDBValue::IDBValue() = default;
IDBValue::~IDBValue() {
if (decompression_count_ > 0) {
base::UmaHistogramCounts100("IndexedDB.ValueDecompressionCount",
decompression_count_);
}
if (isolate_) {
external_memory_accounter_.Clear(isolate_.get());
}
}
scoped_refptr<SerializedScriptValue> IDBValue::CreateSerializedValue() const {
if (base::FeatureList::IsEnabled(kIdbDecompressValuesInPlace)) {
SerializedScriptValue::DataBufferPtr decompressed;
if (IDBValueUnwrapper::Decompress(Data(), /*out_buffer=*/nullptr,
/*out_buffer_in_place=*/&decompressed)) {
const_cast<IDBValue*>(this)->decompression_count_++;
return SerializedScriptValue::Create(std::move(decompressed));
}
} else {
Vector<char> decompressed;
if (IDBValueUnwrapper::Decompress(Data(),
/*out_buffer=*/&decompressed,
/*out_buffer_in_place=*/nullptr)) {
const_cast<IDBValue*>(this)->decompression_count_++;
const_cast<IDBValue*>(this)->SetData(std::move(decompressed));
}
}
return SerializedScriptValue::Create(Data());
}
void IDBValue::SetIsolate(v8::Isolate* isolate) {
DCHECK(isolate);
DCHECK(!isolate_) << "SetIsolate must be called at most once";
isolate_ = isolate;
size_t external_allocated_size = Data().size();
if (external_allocated_size) {
external_memory_accounter_.Increase(isolate_.get(),
external_allocated_size);
}
}
base::span<const uint8_t> IDBValue::Data() const {
if (!data_from_mojo_.empty()) {
return base::as_byte_span(data_from_mojo_);
}
return data_.as_span();
}
void IDBValue::SetBlobInfo(Vector<WebBlobInfo> blob_info) {
blob_info_ = std::move(blob_info);
}
void IDBValue::SetData(Vector<char> new_data) {
if (isolate_) {
external_memory_accounter_.Set(isolate_.get(), new_data.size());
}
data_from_mojo_ = std::move(new_data);
}
void IDBValue::SetData(SerializedScriptValue::DataBufferPtr new_data) {
if (isolate_) {
external_memory_accounter_.Set(isolate_.get(), new_data.size());
}
data_ = std::move(new_data);
}
scoped_refptr<BlobDataHandle> IDBValue::TakeLastBlob() {
DCHECK_GT(blob_info_.size(), 0U)
<< "The IDBValue does not have any attached Blob";
scoped_refptr<BlobDataHandle> return_value =
blob_info_.back().GetBlobHandle();
blob_info_.pop_back();
return return_value;
}
// static
std::unique_ptr<IDBValue> IDBValue::ConvertReturnValue(
const mojom::blink::IDBReturnValuePtr& input) {
if (!input) {
return std::make_unique<IDBValue>();
}
std::unique_ptr<IDBValue> output = std::move(input->value);
output->SetInjectedPrimaryKey(std::move(input->primary_key), input->key_path);
return output;
}
} // namespace blink
|