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

@ -7,6 +7,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@ -157,7 +158,7 @@ struct Converter<JumpListItem::Type> {
private:
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, JumpListItem::Type>({
base::MakeFixedFlatMap<std::string_view, JumpListItem::Type>({
{"file", JumpListItem::Type::kFile},
{"separator", JumpListItem::Type::kSeparator},
{"task", JumpListItem::Type::kTask},
@ -248,7 +249,7 @@ struct Converter<JumpListCategory::Type> {
private:
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, JumpListCategory::Type>({
base::MakeFixedFlatMap<std::string_view, JumpListCategory::Type>({
{"custom", JumpListCategory::Type::kCustom},
{"frequent", JumpListCategory::Type::kFrequent},
{"recent", JumpListCategory::Type::kRecent},
@ -414,7 +415,7 @@ struct Converter<net::SecureDnsMode> {
v8::Local<v8::Value> val,
net::SecureDnsMode* out) {
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, net::SecureDnsMode>({
base::MakeFixedFlatMap<std::string_view, net::SecureDnsMode>({
{"automatic", net::SecureDnsMode::kAutomatic},
{"off", net::SecureDnsMode::kOff},
{"secure", net::SecureDnsMode::kSecure},
@ -440,9 +441,9 @@ IconLoader::IconSize GetIconSizeByString(const std::string& size) {
}
// Return the path constant from string.
int GetPathConstant(base::StringPiece name) {
int GetPathConstant(std::string_view name) {
// clang-format off
constexpr auto Lookup = base::MakeFixedFlatMap<base::StringPiece, int>({
constexpr auto Lookup = base::MakeFixedFlatMap<std::string_view, int>({
{"appData", DIR_APP_DATA},
#if BUILDFLAG(IS_POSIX)
{"cache", base::DIR_CACHE},

View file

@ -9,6 +9,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "content/public/browser/browser_task_traits.h"
@ -249,7 +250,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void RemoveFromParentChildWindows();
template <typename... Args>
void EmitEventSoon(base::StringPiece eventName) {
void EmitEventSoon(std::string_view eventName) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(base::IgnoreResult(&BaseWindow::Emit<Args...>),

View file

@ -4,6 +4,8 @@
#include "shell/browser/api/electron_api_cookies.h"
#include <string>
#include <string_view>
#include <utility>
#include "base/time/time.h"
@ -169,7 +171,7 @@ base::Time ParseTimeProperty(const std::optional<double>& value) {
return base::Time::FromSecondsSinceUnixEpoch(*value);
}
base::StringPiece InclusionStatusToString(net::CookieInclusionStatus status) {
std::string_view InclusionStatusToString(net::CookieInclusionStatus status) {
if (status.HasExclusionReason(net::CookieInclusionStatus::EXCLUDE_HTTP_ONLY))
return "Failed to create httponly cookie";
if (status.HasExclusionReason(

View file

@ -4,6 +4,7 @@
#include "shell/browser/api/electron_api_protocol.h"
#include <string_view>
#include <vector>
#include "base/command_line.h"
@ -193,7 +194,7 @@ const char* const kBuiltinSchemes[] = {
};
// Convert error code to string.
constexpr base::StringPiece ErrorCodeToString(ProtocolError error) {
constexpr std::string_view ErrorCodeToString(ProtocolError error) {
switch (error) {
case ProtocolError::kRegistered:
return "The scheme has been registered";

View file

@ -5,6 +5,7 @@
#include "shell/browser/api/electron_api_screen.h"
#include <string>
#include <string_view>
#include "base/functional/bind.h"
#include "gin/dictionary.h"
@ -49,13 +50,13 @@ std::vector<std::string> MetricsToArray(uint32_t metrics) {
}
void DelayEmit(Screen* screen,
base::StringPiece name,
const std::string_view name,
const display::Display& display) {
screen->Emit(name, display);
}
void DelayEmitWithMetrics(Screen* screen,
base::StringPiece name,
const std::string_view name,
const display::Display& display,
const std::vector<std::string>& metrics) {
screen->Emit(name, display, metrics);

View file

@ -4,6 +4,7 @@
#include "shell/browser/api/electron_api_service_worker_context.h"
#include <string_view>
#include <utility>
#include "chrome/browser/browser_process.h"
@ -24,7 +25,7 @@ namespace electron::api {
namespace {
constexpr base::StringPiece MessageSourceToString(
constexpr std::string_view MessageSourceToString(
const blink::mojom::ConsoleMessageSource source) {
switch (source) {
case blink::mojom::ConsoleMessageSource::kXml:

View file

@ -2,10 +2,12 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <iomanip>
#include <string_view>
#include <dwmapi.h>
#include <windows.devices.enumeration.h>
#include <wrl/client.h>
#include <iomanip>
#include "shell/browser/api/electron_api_system_preferences.h"
@ -98,39 +100,38 @@ std::string SystemPreferences::GetAccentColor() {
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
const std::string& color) {
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, int>({
{"3d-dark-shadow", COLOR_3DDKSHADOW},
{"3d-face", COLOR_3DFACE},
{"3d-highlight", COLOR_3DHIGHLIGHT},
{"3d-light", COLOR_3DLIGHT},
{"3d-shadow", COLOR_3DSHADOW},
{"active-border", COLOR_ACTIVEBORDER},
{"active-caption", COLOR_ACTIVECAPTION},
{"active-caption-gradient", COLOR_GRADIENTACTIVECAPTION},
{"app-workspace", COLOR_APPWORKSPACE},
{"button-text", COLOR_BTNTEXT},
{"caption-text", COLOR_CAPTIONTEXT},
{"desktop", COLOR_DESKTOP},
{"disabled-text", COLOR_GRAYTEXT},
{"highlight", COLOR_HIGHLIGHT},
{"highlight-text", COLOR_HIGHLIGHTTEXT},
{"hotlight", COLOR_HOTLIGHT},
{"inactive-border", COLOR_INACTIVEBORDER},
{"inactive-caption", COLOR_INACTIVECAPTION},
{"inactive-caption-gradient", COLOR_GRADIENTINACTIVECAPTION},
{"inactive-caption-text", COLOR_INACTIVECAPTIONTEXT},
{"info-background", COLOR_INFOBK},
{"info-text", COLOR_INFOTEXT},
{"menu", COLOR_MENU},
{"menu-highlight", COLOR_MENUHILIGHT},
{"menu-text", COLOR_MENUTEXT},
{"menubar", COLOR_MENUBAR},
{"scrollbar", COLOR_SCROLLBAR},
{"window", COLOR_WINDOW},
{"window-frame", COLOR_WINDOWFRAME},
{"window-text", COLOR_WINDOWTEXT},
});
static constexpr auto Lookup = base::MakeFixedFlatMap<std::string_view, int>({
{"3d-dark-shadow", COLOR_3DDKSHADOW},
{"3d-face", COLOR_3DFACE},
{"3d-highlight", COLOR_3DHIGHLIGHT},
{"3d-light", COLOR_3DLIGHT},
{"3d-shadow", COLOR_3DSHADOW},
{"active-border", COLOR_ACTIVEBORDER},
{"active-caption", COLOR_ACTIVECAPTION},
{"active-caption-gradient", COLOR_GRADIENTACTIVECAPTION},
{"app-workspace", COLOR_APPWORKSPACE},
{"button-text", COLOR_BTNTEXT},
{"caption-text", COLOR_CAPTIONTEXT},
{"desktop", COLOR_DESKTOP},
{"disabled-text", COLOR_GRAYTEXT},
{"highlight", COLOR_HIGHLIGHT},
{"highlight-text", COLOR_HIGHLIGHTTEXT},
{"hotlight", COLOR_HOTLIGHT},
{"inactive-border", COLOR_INACTIVEBORDER},
{"inactive-caption", COLOR_INACTIVECAPTION},
{"inactive-caption-gradient", COLOR_GRADIENTINACTIVECAPTION},
{"inactive-caption-text", COLOR_INACTIVECAPTIONTEXT},
{"info-background", COLOR_INFOBK},
{"info-text", COLOR_INFOTEXT},
{"menu", COLOR_MENU},
{"menu-highlight", COLOR_MENUHILIGHT},
{"menu-text", COLOR_MENUTEXT},
{"menubar", COLOR_MENUBAR},
{"scrollbar", COLOR_SCROLLBAR},
{"window", COLOR_WINDOW},
{"window-frame", COLOR_WINDOWFRAME},
{"window-text", COLOR_WINDOWTEXT},
});
if (const auto* iter = Lookup.find(color); iter != Lookup.end())
return ToRGBAHex(color_utils::GetSysSkColor(iter->second));

View file

@ -5,6 +5,7 @@
#include "shell/browser/api/electron_api_tray.h"
#include <string>
#include <string_view>
#include "base/containers/fixed_flat_map.h"
#include "gin/dictionary.h"
@ -32,7 +33,7 @@ struct Converter<electron::TrayIcon::IconType> {
electron::TrayIcon::IconType* out) {
using Val = electron::TrayIcon::IconType;
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, Val>({
base::MakeFixedFlatMap<std::string_view, Val>({
{"custom", Val::kCustom},
{"error", Val::kError},
{"info", Val::kInfo},

View file

@ -9,6 +9,7 @@
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@ -205,7 +206,7 @@ struct Converter<printing::mojom::MarginType> {
printing::mojom::MarginType* out) {
using Val = printing::mojom::MarginType;
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, Val>({
base::MakeFixedFlatMap<std::string_view, Val>({
{"custom", Val::kCustomMargins},
{"default", Val::kDefaultMargins},
{"none", Val::kNoMargins},
@ -222,7 +223,7 @@ struct Converter<printing::mojom::DuplexMode> {
printing::mojom::DuplexMode* out) {
using Val = printing::mojom::DuplexMode;
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, Val>({
base::MakeFixedFlatMap<std::string_view, Val>({
{"longEdge", Val::kLongEdge},
{"shortEdge", Val::kShortEdge},
{"simplex", Val::kSimplex},
@ -284,7 +285,7 @@ struct Converter<content::SavePageType> {
content::SavePageType* out) {
using Val = content::SavePageType;
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, Val>({
base::MakeFixedFlatMap<std::string_view, Val>({
{"htmlcomplete", Val::SAVE_PAGE_TYPE_AS_COMPLETE_HTML},
{"htmlonly", Val::SAVE_PAGE_TYPE_AS_ONLY_HTML},
{"mhtml", Val::SAVE_PAGE_TYPE_AS_MHTML},
@ -329,7 +330,7 @@ struct Converter<electron::api::WebContents::Type> {
electron::api::WebContents::Type* out) {
using Val = electron::api::WebContents::Type;
static constexpr auto Lookup =
base::MakeFixedFlatMap<base::StringPiece, Val>({
base::MakeFixedFlatMap<std::string_view, Val>({
{"backgroundPage", Val::kBackgroundPage},
{"browserView", Val::kBrowserView},
{"offscreen", Val::kOffScreen},
@ -357,7 +358,7 @@ namespace electron::api {
namespace {
constexpr base::StringPiece CursorTypeToString(
constexpr std::string_view CursorTypeToString(
ui::mojom::CursorType cursor_type) {
switch (cursor_type) {
case ui::mojom::CursorType::kPointer:

View file

@ -9,6 +9,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@ -371,7 +372,7 @@ class WebContents : public ExclusiveAccessContext,
// this.emit(name, new Event(sender, message), args...);
template <typename... Args>
bool EmitWithSender(base::StringPiece name,
bool EmitWithSender(const std::string_view name,
content::RenderFrameHost* frame,
electron::mojom::ElectronApiIPC::InvokeCallback callback,
Args&&... args) {

View file

@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "base/containers/contains.h"
@ -33,7 +34,7 @@
#include "shell/common/gin_helper/dictionary.h"
static constexpr auto ResourceTypes =
base::MakeFixedFlatMap<base::StringPiece,
base::MakeFixedFlatMap<std::string_view,
extensions::WebRequestResourceType>({
{"cspReport", extensions::WebRequestResourceType::CSP_REPORT},
{"font", extensions::WebRequestResourceType::FONT},
@ -77,7 +78,7 @@ struct UserData : public base::SupportsUserData::Data {
raw_ptr<WebRequest> data;
};
extensions::WebRequestResourceType ParseResourceType(base::StringPiece value) {
extensions::WebRequestResourceType ParseResourceType(std::string_view value) {
if (const auto* iter = ResourceTypes.find(value); iter != ResourceTypes.end())
return iter->second;