chore: migrate base::StringPiece to std::string_view (#40915)

* chore: migrate from base::StringPiece to std::string_view in keyboard_util.cc

* chore: migrate from base::StringPiece to std::string_view in error_thrower.cc

* chore: migrate from base::StringPiece to std::string_view in electron_api_web_contents.cc

* chore: migrate from base::StringPiece to std::string_view in gin_helper/dictionary.h

* chore: migrate from base::StringPiece to std::string_view in electron_api_url_loader.cc

* chore: phase out internal use of base:::StringPiece

`base::StringPiece` is being phased out upstream. Its code has been
removed upstream and it's just a typedef for `std::string_view`.

They haven't removed the typedef yet, so this PR tries to get ahead
of future breakage by migrating "internal" use (i.e. leaving alone the
places where the `base::StringPiece` name is coming from an upstream
method that we override).

Xref: https://bugs.chromium.org/p/chromium/issues/detail?id=691162

Xref: 4294483

Xref: https://docs.google.com/document/d/1d4RnD1uAE2t4iANR0nXy82ASIPGsPuw2mpO6v6T7JKs
This commit is contained in:
Charles Kerr 2024-01-10 19:00:37 -06:00 committed by GitHub
parent 892c9d78a3
commit f36ceae024
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 223 additions and 174 deletions

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#include <string_view>
#include "shell/common/gin_helper/arguments.h"
#include "v8/include/v8-exception.h"
@ -15,7 +17,7 @@ void Arguments::ThrowError() const {
gin::Arguments::ThrowError();
}
void Arguments::ThrowError(base::StringPiece message) const {
void Arguments::ThrowError(const std::string_view message) const {
isolate()->ThrowException(
v8::Exception::Error(gin::StringToV8(isolate(), message)));
}

View file

@ -5,6 +5,8 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
#include <string_view>
#include "gin/arguments.h"
namespace gin_helper {
@ -40,7 +42,7 @@ class Arguments : public gin::Arguments {
// Throw error with custom error message.
void ThrowError() const;
void ThrowError(base::StringPiece message) const;
void ThrowError(std::string_view message) const;
private:
// MUST NOT ADD ANY DATA MEMBER.

View file

@ -6,6 +6,7 @@
#define ELECTRON_SHELL_COMMON_GIN_HELPER_DICTIONARY_H_
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
@ -68,7 +69,7 @@ class Dictionary : public gin::Dictionary {
// Like normal Get but put result in an std::optional.
template <typename T>
bool GetOptional(base::StringPiece key, std::optional<T>* out) const {
bool GetOptional(const std::string_view key, std::optional<T>* out) const {
T ret;
if (Get(key, &ret)) {
out->emplace(std::move(ret));
@ -79,7 +80,7 @@ class Dictionary : public gin::Dictionary {
}
template <typename T>
bool GetHidden(base::StringPiece key, T* out) const {
bool GetHidden(std::string_view key, T* out) const {
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
v8::Local<v8::Private> privateKey =
v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key));
@ -92,7 +93,7 @@ class Dictionary : public gin::Dictionary {
}
template <typename T>
bool SetHidden(base::StringPiece key, T val) {
bool SetHidden(std::string_view key, T val) {
v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false;
@ -105,7 +106,7 @@ class Dictionary : public gin::Dictionary {
}
template <typename T>
bool SetMethod(base::StringPiece key, const T& callback) {
bool SetMethod(std::string_view key, const T& callback) {
auto context = isolate()->GetCurrentContext();
auto templ = CallbackTraits<T>::CreateTemplate(isolate(), callback);
return GetHandle()
@ -147,7 +148,7 @@ class Dictionary : public gin::Dictionary {
}
template <typename T>
bool SetReadOnly(base::StringPiece key, const T& val) {
bool SetReadOnly(std::string_view key, const T& val) {
v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false;
@ -160,7 +161,7 @@ class Dictionary : public gin::Dictionary {
// Note: If we plan to add more Set methods, consider adding an option instead
// of copying code.
template <typename T>
bool SetReadOnlyNonConfigurable(base::StringPiece key, T val) {
bool SetReadOnlyNonConfigurable(std::string_view key, T val) {
v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false;
@ -171,13 +172,13 @@ class Dictionary : public gin::Dictionary {
return !result.IsNothing() && result.FromJust();
}
bool Has(base::StringPiece key) const {
bool Has(std::string_view key) const {
v8::Maybe<bool> result = GetHandle()->Has(isolate()->GetCurrentContext(),
gin::StringToV8(isolate(), key));
return !result.IsNothing() && result.FromJust();
}
bool Delete(base::StringPiece key) {
bool Delete(std::string_view key) {
v8::Maybe<bool> result = GetHandle()->Delete(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key));
return !result.IsNothing() && result.FromJust();

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string_view>
#include "shell/common/gin_helper/error_thrower.h"
#include "gin/converter.h"
@ -15,27 +17,28 @@ ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {}
// costly to invoke
ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {}
void ErrorThrower::ThrowError(base::StringPiece err_msg) const {
void ErrorThrower::ThrowError(const std::string_view err_msg) const {
Throw(v8::Exception::Error, err_msg);
}
void ErrorThrower::ThrowTypeError(base::StringPiece err_msg) const {
void ErrorThrower::ThrowTypeError(const std::string_view err_msg) const {
Throw(v8::Exception::TypeError, err_msg);
}
void ErrorThrower::ThrowRangeError(base::StringPiece err_msg) const {
void ErrorThrower::ThrowRangeError(const std::string_view err_msg) const {
Throw(v8::Exception::RangeError, err_msg);
}
void ErrorThrower::ThrowReferenceError(base::StringPiece err_msg) const {
void ErrorThrower::ThrowReferenceError(const std::string_view err_msg) const {
Throw(v8::Exception::ReferenceError, err_msg);
}
void ErrorThrower::ThrowSyntaxError(base::StringPiece err_msg) const {
void ErrorThrower::ThrowSyntaxError(const std::string_view err_msg) const {
Throw(v8::Exception::SyntaxError, err_msg);
}
void ErrorThrower::Throw(ErrorGenerator gen, base::StringPiece err_msg) const {
void ErrorThrower::Throw(ErrorGenerator gen,
const std::string_view err_msg) const {
v8::Local<v8::Value> exception = gen(gin::StringToV8(isolate_, err_msg), {});
if (!isolate_->IsExecutionTerminating())
isolate_->ThrowException(exception);

View file

@ -5,8 +5,9 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/strings/string_piece.h"
#include "v8/include/v8.h"
namespace gin_helper {
@ -17,18 +18,18 @@ class ErrorThrower {
ErrorThrower();
~ErrorThrower() = default;
void ThrowError(base::StringPiece err_msg) const;
void ThrowTypeError(base::StringPiece err_msg) const;
void ThrowRangeError(base::StringPiece err_msg) const;
void ThrowReferenceError(base::StringPiece err_msg) const;
void ThrowSyntaxError(base::StringPiece err_msg) const;
void ThrowError(std::string_view err_msg) const;
void ThrowTypeError(std::string_view err_msg) const;
void ThrowRangeError(std::string_view err_msg) const;
void ThrowReferenceError(std::string_view err_msg) const;
void ThrowSyntaxError(std::string_view err_msg) const;
v8::Isolate* isolate() const { return isolate_; }
private:
using ErrorGenerator = v8::Local<v8::Value> (*)(v8::Local<v8::String> err_msg,
v8::Local<v8::Value> options);
void Throw(ErrorGenerator gen, base::StringPiece err_msg) const;
void Throw(ErrorGenerator gen, std::string_view err_msg) const;
raw_ptr<v8::Isolate> isolate_;
};

View file

@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
#include <string_view>
#include <utility>
#include <vector>
@ -38,7 +39,7 @@ class EventEmitter : public gin_helper::Wrappable<T> {
// this.emit(name, new Event(), args...);
template <typename... Args>
bool Emit(base::StringPiece name, Args&&... args) {
bool Emit(const std::string_view name, Args&&... args) {
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Object> wrapper = GetWrapper();
if (wrapper.IsEmpty())
@ -58,7 +59,7 @@ class EventEmitter : public gin_helper::Wrappable<T> {
private:
// this.emit(name, event, args...);
template <typename... Args>
bool EmitWithEvent(base::StringPiece name,
bool EmitWithEvent(const std::string_view name,
gin::Handle<gin_helper::internal::Event> event,
Args&&... args) {
// It's possible that |this| will be deleted by EmitEvent, so save anything

View file

@ -12,14 +12,14 @@ ObjectTemplateBuilder::ObjectTemplateBuilder(
: isolate_(isolate), template_(templ) {}
ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(
const base::StringPiece& name,
const std::string_view name,
v8::Local<v8::Data> val) {
template_->Set(gin::StringToSymbol(isolate_, name), val);
return *this;
}
ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl(
const base::StringPiece& name,
const std::string_view name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter) {
template_->SetAccessorProperty(gin::StringToSymbol(isolate_, name), getter,

View file

@ -5,6 +5,8 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "shell/common/gin_helper/function_template.h"
@ -28,7 +30,7 @@ class ObjectTemplateBuilder {
// poetic license here in order that all calls to Set() can be via the '.'
// operator and line up nicely.
template <typename T>
ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
ObjectTemplateBuilder& SetValue(const std::string_view name, T val) {
return SetImpl(name, ConvertToV8(isolate_, val));
}
@ -37,19 +39,19 @@ class ObjectTemplateBuilder {
// will want to use one of the first two options. Also see
// gin::CreateFunctionTemplate() for creating raw function templates.
template <typename T>
ObjectTemplateBuilder& SetMethod(const base::StringPiece& name,
ObjectTemplateBuilder& SetMethod(const std::string_view name,
const T& callback) {
return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback));
}
template <typename T>
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
ObjectTemplateBuilder& SetProperty(const std::string_view name,
const T& getter) {
return SetPropertyImpl(name,
CallbackTraits<T>::CreateTemplate(isolate_, getter),
v8::Local<v8::FunctionTemplate>());
}
template <typename T, typename U>
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
ObjectTemplateBuilder& SetProperty(const std::string_view name,
const T& getter,
const U& setter) {
return SetPropertyImpl(name,
@ -60,10 +62,10 @@ class ObjectTemplateBuilder {
v8::Local<v8::ObjectTemplate> Build();
private:
ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
ObjectTemplateBuilder& SetImpl(const std::string_view name,
v8::Local<v8::Data> val);
ObjectTemplateBuilder& SetPropertyImpl(
const base::StringPiece& name,
const std::string_view name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter);

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string_view>
#include "shell/common/gin_helper/promise.h"
namespace gin_helper {
@ -43,7 +45,8 @@ v8::Maybe<bool> PromiseBase::Reject(v8::Local<v8::Value> except) {
return GetInner()->Reject(GetContext(), except);
}
v8::Maybe<bool> PromiseBase::RejectWithErrorMessage(base::StringPiece message) {
v8::Maybe<bool> PromiseBase::RejectWithErrorMessage(
const std::string_view message) {
v8::HandleScope handle_scope(isolate());
gin_helper::MicrotasksScope microtasks_scope(
isolate(), GetContext()->GetMicrotaskQueue());

View file

@ -6,12 +6,12 @@
#define ELECTRON_SHELL_COMMON_GIN_HELPER_PROMISE_H_
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/strings/string_piece.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "shell/common/gin_converters/std_converter.h"
@ -47,19 +47,20 @@ class PromiseBase {
//
// Note: The parameter type is PromiseBase&& so it can take the instances of
// Promise<T> type.
static void RejectPromise(PromiseBase&& promise, base::StringPiece errmsg) {
static void RejectPromise(PromiseBase&& promise,
const std::string_view errmsg) {
if (electron::IsBrowserProcess() &&
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
// Note that this callback can not take StringPiece,
// Note that this callback can not take std::string_view,
// as StringPiece only references string internally and
// will blow when a temporary string is passed.
[](PromiseBase&& promise, std::string str) {
promise.RejectWithErrorMessage(str);
},
std::move(promise), std::string(errmsg.data(), errmsg.size())));
std::move(promise), std::string{errmsg}));
} else {
promise.RejectWithErrorMessage(errmsg);
}
@ -67,7 +68,7 @@ class PromiseBase {
v8::Maybe<bool> Reject();
v8::Maybe<bool> Reject(v8::Local<v8::Value> except);
v8::Maybe<bool> RejectWithErrorMessage(base::StringPiece message);
v8::Maybe<bool> RejectWithErrorMessage(std::string_view message);
v8::Local<v8::Context> GetContext() const;
v8::Local<v8::Promise> GetHandle() const;