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

@ -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();