electron/shell/common/gin_converters/value_converter.cc
Charles Kerr 10fd0ba655
refactor: use base::Value::Take*() (#41169)
* perf: avoid temporary strings in Converter<net::HttpRequestHeaders>::FromV8()

* perf: take strings instead of copying them in ToResponseHead()

* refactor: prefer base::Value::Take*() where appropriate

As per the base::Value docs: "prefer over `std::move(value.Get...())`
so clang-tidy can warn about potential use-after-move mistakes."
2024-05-13 15:58:52 +02:00

63 lines
1.9 KiB
C++

// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_converters/value_converter.h"
#include <memory>
#include <utility>
#include "content/public/renderer/v8_value_converter.h"
namespace gin {
bool Converter<base::Value::Dict>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value::Dict* out) {
std::unique_ptr<base::Value> value =
content::V8ValueConverter::Create()->FromV8Value(
val, isolate->GetCurrentContext());
if (value && value->is_dict()) {
*out = std::move(*value).TakeDict();
return true;
} else {
return false;
}
}
bool Converter<base::Value>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value* out) {
std::unique_ptr<base::Value> value =
content::V8ValueConverter::Create()->FromV8Value(
val, isolate->GetCurrentContext());
if (value) {
*out = std::move(*value);
return true;
} else {
return false;
}
}
v8::Local<v8::Value> Converter<base::ValueView>::ToV8(
v8::Isolate* isolate,
const base::ValueView val) {
return content::V8ValueConverter::Create()->ToV8Value(
val, isolate->GetCurrentContext());
}
bool Converter<base::Value::List>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value::List* out) {
std::unique_ptr<base::Value> value =
content::V8ValueConverter::Create()->FromV8Value(
val, isolate->GetCurrentContext());
if (value && value->is_list()) {
*out = std::move(*value).TakeList();
return true;
} else {
return false;
}
}
} // namespace gin