refactor: constexpr lookup tables (#38771)
* refactor: use a constexpr lookup table in GetPathConstant() * refactor: use a constexpr lookup table in SystemPreferences::GetColor() * refactor: use a constexpr lookup table in SimpleURLLoaderWrapper::Create()
This commit is contained in:
parent
bb4e9b27b5
commit
bf1ba4a857
3 changed files with 106 additions and 160 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
|
#include "base/containers/fixed_flat_map.h"
|
||||||
#include "base/containers/span.h"
|
#include "base/containers/span.h"
|
||||||
#include "base/environment.h"
|
#include "base/environment.h"
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
|
@ -473,51 +474,38 @@ IconLoader::IconSize GetIconSizeByString(const std::string& size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the path constant from string.
|
// Return the path constant from string.
|
||||||
int GetPathConstant(const std::string& name) {
|
constexpr int GetPathConstant(base::StringPiece name) {
|
||||||
if (name == "appData")
|
// clang-format off
|
||||||
return DIR_APP_DATA;
|
constexpr auto Lookup = base::MakeFixedFlatMapSorted<base::StringPiece, int>({
|
||||||
else if (name == "sessionData")
|
{"appData", DIR_APP_DATA},
|
||||||
return DIR_SESSION_DATA;
|
|
||||||
else if (name == "userData")
|
|
||||||
return chrome::DIR_USER_DATA;
|
|
||||||
else if (name == "cache")
|
|
||||||
#if BUILDFLAG(IS_POSIX)
|
#if BUILDFLAG(IS_POSIX)
|
||||||
return base::DIR_CACHE;
|
{"cache", base::DIR_CACHE},
|
||||||
#else
|
#else
|
||||||
return base::DIR_ROAMING_APP_DATA;
|
{"cache", base::DIR_ROAMING_APP_DATA},
|
||||||
#endif
|
#endif
|
||||||
else if (name == "userCache")
|
{"crashDumps", DIR_CRASH_DUMPS},
|
||||||
return DIR_USER_CACHE;
|
{"desktop", base::DIR_USER_DESKTOP},
|
||||||
else if (name == "logs")
|
{"documents", chrome::DIR_USER_DOCUMENTS},
|
||||||
return DIR_APP_LOGS;
|
{"downloads", chrome::DIR_DEFAULT_DOWNLOADS},
|
||||||
else if (name == "crashDumps")
|
{"exe", base::FILE_EXE},
|
||||||
return DIR_CRASH_DUMPS;
|
{"home", base::DIR_HOME},
|
||||||
else if (name == "home")
|
{"logs", DIR_APP_LOGS},
|
||||||
return base::DIR_HOME;
|
{"module", base::FILE_MODULE},
|
||||||
else if (name == "temp")
|
{"music", chrome::DIR_USER_MUSIC},
|
||||||
return base::DIR_TEMP;
|
{"pictures", chrome::DIR_USER_PICTURES},
|
||||||
else if (name == "userDesktop" || name == "desktop")
|
|
||||||
return base::DIR_USER_DESKTOP;
|
|
||||||
else if (name == "exe")
|
|
||||||
return base::FILE_EXE;
|
|
||||||
else if (name == "module")
|
|
||||||
return base::FILE_MODULE;
|
|
||||||
else if (name == "documents")
|
|
||||||
return chrome::DIR_USER_DOCUMENTS;
|
|
||||||
else if (name == "downloads")
|
|
||||||
return chrome::DIR_DEFAULT_DOWNLOADS;
|
|
||||||
else if (name == "music")
|
|
||||||
return chrome::DIR_USER_MUSIC;
|
|
||||||
else if (name == "pictures")
|
|
||||||
return chrome::DIR_USER_PICTURES;
|
|
||||||
else if (name == "videos")
|
|
||||||
return chrome::DIR_USER_VIDEOS;
|
|
||||||
#if BUILDFLAG(IS_WIN)
|
#if BUILDFLAG(IS_WIN)
|
||||||
else if (name == "recent")
|
{"recent", electron::DIR_RECENT},
|
||||||
return electron::DIR_RECENT;
|
|
||||||
#endif
|
#endif
|
||||||
else
|
{"sessionData", DIR_SESSION_DATA},
|
||||||
return -1;
|
{"temp", base::DIR_TEMP},
|
||||||
|
{"userCache", DIR_USER_CACHE},
|
||||||
|
{"userData", chrome::DIR_USER_DATA},
|
||||||
|
{"userDesktop", base::DIR_USER_DESKTOP},
|
||||||
|
{"videos", chrome::DIR_USER_VIDEOS},
|
||||||
|
});
|
||||||
|
// clang-format on
|
||||||
|
const auto* iter = Lookup.find(name);
|
||||||
|
return iter != Lookup.end() ? iter->second : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NotificationCallbackWrapper(
|
bool NotificationCallbackWrapper(
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "shell/browser/api/electron_api_system_preferences.h"
|
#include "shell/browser/api/electron_api_system_preferences.h"
|
||||||
|
|
||||||
|
#include "base/containers/fixed_flat_map.h"
|
||||||
#include "base/win/core_winrt_util.h"
|
#include "base/win/core_winrt_util.h"
|
||||||
#include "base/win/windows_types.h"
|
#include "base/win/windows_types.h"
|
||||||
#include "base/win/wrapped_window_proc.h"
|
#include "base/win/wrapped_window_proc.h"
|
||||||
|
@ -97,73 +98,45 @@ std::string SystemPreferences::GetAccentColor() {
|
||||||
|
|
||||||
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
|
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
|
||||||
const std::string& color) {
|
const std::string& color) {
|
||||||
int id;
|
static constexpr auto Lookup =
|
||||||
if (color == "3d-dark-shadow") {
|
base::MakeFixedFlatMapSorted<base::StringPiece, int>({
|
||||||
id = COLOR_3DDKSHADOW;
|
{"3d-dark-shadow", COLOR_3DDKSHADOW},
|
||||||
} else if (color == "3d-face") {
|
{"3d-face", COLOR_3DFACE},
|
||||||
id = COLOR_3DFACE;
|
{"3d-highlight", COLOR_3DHIGHLIGHT},
|
||||||
} else if (color == "3d-highlight") {
|
{"3d-light", COLOR_3DLIGHT},
|
||||||
id = COLOR_3DHIGHLIGHT;
|
{"3d-shadow", COLOR_3DSHADOW},
|
||||||
} else if (color == "3d-light") {
|
{"active-border", COLOR_ACTIVEBORDER},
|
||||||
id = COLOR_3DLIGHT;
|
{"active-caption", COLOR_ACTIVECAPTION},
|
||||||
} else if (color == "3d-shadow") {
|
{"active-caption-gradient", COLOR_GRADIENTACTIVECAPTION},
|
||||||
id = COLOR_3DSHADOW;
|
{"app-workspace", COLOR_APPWORKSPACE},
|
||||||
} else if (color == "active-border") {
|
{"button-text", COLOR_BTNTEXT},
|
||||||
id = COLOR_ACTIVEBORDER;
|
{"caption-text", COLOR_CAPTIONTEXT},
|
||||||
} else if (color == "active-caption") {
|
{"desktop", COLOR_DESKTOP},
|
||||||
id = COLOR_ACTIVECAPTION;
|
{"disabled-text", COLOR_GRAYTEXT},
|
||||||
} else if (color == "active-caption-gradient") {
|
{"highlight", COLOR_HIGHLIGHT},
|
||||||
id = COLOR_GRADIENTACTIVECAPTION;
|
{"highlight-text", COLOR_HIGHLIGHTTEXT},
|
||||||
} else if (color == "app-workspace") {
|
{"hotlight", COLOR_HOTLIGHT},
|
||||||
id = COLOR_APPWORKSPACE;
|
{"inactive-border", COLOR_INACTIVEBORDER},
|
||||||
} else if (color == "button-text") {
|
{"inactive-caption", COLOR_INACTIVECAPTION},
|
||||||
id = COLOR_BTNTEXT;
|
{"inactive-caption-gradient", COLOR_GRADIENTINACTIVECAPTION},
|
||||||
} else if (color == "caption-text") {
|
{"inactive-caption-text", COLOR_INACTIVECAPTIONTEXT},
|
||||||
id = COLOR_CAPTIONTEXT;
|
{"info-background", COLOR_INFOBK},
|
||||||
} else if (color == "desktop") {
|
{"info-text", COLOR_INFOTEXT},
|
||||||
id = COLOR_DESKTOP;
|
{"menu", COLOR_MENU},
|
||||||
} else if (color == "disabled-text") {
|
{"menu-highlight", COLOR_MENUHILIGHT},
|
||||||
id = COLOR_GRAYTEXT;
|
{"menu-text", COLOR_MENUTEXT},
|
||||||
} else if (color == "highlight") {
|
{"menubar", COLOR_MENUBAR},
|
||||||
id = COLOR_HIGHLIGHT;
|
{"scrollbar", COLOR_SCROLLBAR},
|
||||||
} else if (color == "highlight-text") {
|
{"window", COLOR_WINDOW},
|
||||||
id = COLOR_HIGHLIGHTTEXT;
|
{"window-frame", COLOR_WINDOWFRAME},
|
||||||
} else if (color == "hotlight") {
|
{"window-text", COLOR_WINDOWTEXT},
|
||||||
id = COLOR_HOTLIGHT;
|
});
|
||||||
} else if (color == "inactive-border") {
|
|
||||||
id = COLOR_INACTIVEBORDER;
|
|
||||||
} else if (color == "inactive-caption") {
|
|
||||||
id = COLOR_INACTIVECAPTION;
|
|
||||||
} else if (color == "inactive-caption-gradient") {
|
|
||||||
id = COLOR_GRADIENTINACTIVECAPTION;
|
|
||||||
} else if (color == "inactive-caption-text") {
|
|
||||||
id = COLOR_INACTIVECAPTIONTEXT;
|
|
||||||
} else if (color == "info-background") {
|
|
||||||
id = COLOR_INFOBK;
|
|
||||||
} else if (color == "info-text") {
|
|
||||||
id = COLOR_INFOTEXT;
|
|
||||||
} else if (color == "menu") {
|
|
||||||
id = COLOR_MENU;
|
|
||||||
} else if (color == "menu-highlight") {
|
|
||||||
id = COLOR_MENUHILIGHT;
|
|
||||||
} else if (color == "menubar") {
|
|
||||||
id = COLOR_MENUBAR;
|
|
||||||
} else if (color == "menu-text") {
|
|
||||||
id = COLOR_MENUTEXT;
|
|
||||||
} else if (color == "scrollbar") {
|
|
||||||
id = COLOR_SCROLLBAR;
|
|
||||||
} else if (color == "window") {
|
|
||||||
id = COLOR_WINDOW;
|
|
||||||
} else if (color == "window-frame") {
|
|
||||||
id = COLOR_WINDOWFRAME;
|
|
||||||
} else if (color == "window-text") {
|
|
||||||
id = COLOR_WINDOWTEXT;
|
|
||||||
} else {
|
|
||||||
thrower.ThrowError("Unknown color: " + color);
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return ToRGBHex(color_utils::GetSysSkColor(id));
|
if (const auto* iter = Lookup.find(color); iter != Lookup.end())
|
||||||
|
return ToRGBHex(color_utils::GetSysSkColor(iter->second));
|
||||||
|
|
||||||
|
thrower.ThrowError("Unknown color: " + color);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SystemPreferences::GetMediaAccessStatus(
|
std::string SystemPreferences::GetMediaAccessStatus(
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "base/containers/fixed_flat_map.h"
|
||||||
#include "base/memory/raw_ptr.h"
|
#include "base/memory/raw_ptr.h"
|
||||||
#include "base/no_destructor.h"
|
#include "base/no_destructor.h"
|
||||||
#include "gin/handle.h"
|
#include "gin/handle.h"
|
||||||
|
@ -561,62 +562,46 @@ gin::Handle<SimpleURLLoaderWrapper> SimpleURLLoaderWrapper::Create(
|
||||||
request->trusted_params->has_user_activation = has_user_activation;
|
request->trusted_params->has_user_activation = has_user_activation;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string mode;
|
if (std::string mode; opts.Get("mode", &mode)) {
|
||||||
if (opts.Get("mode", &mode) && !mode.empty()) {
|
using Val = network::mojom::RequestMode;
|
||||||
if (mode == "navigate") {
|
static constexpr auto Lookup =
|
||||||
request->mode = network::mojom::RequestMode::kNavigate;
|
base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
|
||||||
} else if (mode == "cors") {
|
{"cors", Val::kCors},
|
||||||
request->mode = network::mojom::RequestMode::kCors;
|
{"navigate", Val::kNavigate},
|
||||||
} else if (mode == "no-cors") {
|
{"no-cors", Val::kNoCors},
|
||||||
request->mode = network::mojom::RequestMode::kNoCors;
|
{"same-origin", Val::kSameOrigin},
|
||||||
} else if (mode == "same-origin") {
|
});
|
||||||
request->mode = network::mojom::RequestMode::kSameOrigin;
|
if (auto* iter = Lookup.find(mode); iter != Lookup.end())
|
||||||
}
|
request->mode = iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string destination;
|
if (std::string destination; opts.Get("destination", &destination)) {
|
||||||
if (opts.Get("destination", &destination) && !destination.empty()) {
|
using Val = network::mojom::RequestDestination;
|
||||||
if (destination == "empty") {
|
static constexpr auto Lookup =
|
||||||
request->destination = network::mojom::RequestDestination::kEmpty;
|
base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
|
||||||
} else if (destination == "audio") {
|
{"audio", Val::kAudio},
|
||||||
request->destination = network::mojom::RequestDestination::kAudio;
|
{"audioworklet", Val::kAudioWorklet},
|
||||||
} else if (destination == "audioworklet") {
|
{"document", Val::kDocument},
|
||||||
request->destination = network::mojom::RequestDestination::kAudioWorklet;
|
{"embed", Val::kEmbed},
|
||||||
} else if (destination == "document") {
|
{"empty", Val::kEmpty},
|
||||||
request->destination = network::mojom::RequestDestination::kDocument;
|
{"font", Val::kFont},
|
||||||
} else if (destination == "embed") {
|
{"frame", Val::kFrame},
|
||||||
request->destination = network::mojom::RequestDestination::kEmbed;
|
{"iframe", Val::kIframe},
|
||||||
} else if (destination == "font") {
|
{"image", Val::kImage},
|
||||||
request->destination = network::mojom::RequestDestination::kFont;
|
{"manifest", Val::kManifest},
|
||||||
} else if (destination == "frame") {
|
{"object", Val::kObject},
|
||||||
request->destination = network::mojom::RequestDestination::kFrame;
|
{"paintworklet", Val::kPaintWorklet},
|
||||||
} else if (destination == "iframe") {
|
{"report", Val::kReport},
|
||||||
request->destination = network::mojom::RequestDestination::kIframe;
|
{"script", Val::kScript},
|
||||||
} else if (destination == "image") {
|
{"serviceworker", Val::kServiceWorker},
|
||||||
request->destination = network::mojom::RequestDestination::kImage;
|
{"style", Val::kStyle},
|
||||||
} else if (destination == "manifest") {
|
{"track", Val::kTrack},
|
||||||
request->destination = network::mojom::RequestDestination::kManifest;
|
{"video", Val::kVideo},
|
||||||
} else if (destination == "object") {
|
{"worker", Val::kWorker},
|
||||||
request->destination = network::mojom::RequestDestination::kObject;
|
{"xslt", Val::kXslt},
|
||||||
} else if (destination == "paintworklet") {
|
});
|
||||||
request->destination = network::mojom::RequestDestination::kPaintWorklet;
|
if (auto* iter = Lookup.find(destination); iter != Lookup.end())
|
||||||
} else if (destination == "report") {
|
request->destination = iter->second;
|
||||||
request->destination = network::mojom::RequestDestination::kReport;
|
|
||||||
} else if (destination == "script") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kScript;
|
|
||||||
} else if (destination == "serviceworker") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kServiceWorker;
|
|
||||||
} else if (destination == "style") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kStyle;
|
|
||||||
} else if (destination == "track") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kTrack;
|
|
||||||
} else if (destination == "video") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kVideo;
|
|
||||||
} else if (destination == "worker") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kWorker;
|
|
||||||
} else if (destination == "xslt") {
|
|
||||||
request->destination = network::mojom::RequestDestination::kXslt;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool credentials_specified =
|
bool credentials_specified =
|
||||||
|
|
Loading…
Reference in a new issue