refactor: use gin_helper::Dictionary::ValueOrDefault() (#46982)

* refactor: use ValueOrDefault() in electron_api_web_contents.cc

* refactor: use ValueOrDefault() in electron_api_url_loader.cc

* refactor: use ValueOrDefault() in electron_download_manager_delegate.cc

* refactor: use ValueOrDefault() in electron_touch_bar.mm

* refactor: use ValueOrDefault() in electron_url_loader_factory.cc

* refactor: use ValueOrDefault() in electron_browser_context.cc

* refactor: use ValueOrDefault() in electron_touch_bar.mm

* refactor: use ValueOrDefault() in blink_converter.cc

* feat: add ValueOrDefault() to PersistentDictionary

* empty commit

* refactor: use ValueOrDefault() in blink_converter.cc

* refactor: inline the rectangle base::Value::Dict

* refactor: remove has_scroll temporary

---------

Co-authored-by: Deepak Mohan <hop2deep@gmail.com>
This commit is contained in:
Charles Kerr 2025-05-08 14:17:22 -05:00 committed by GitHub
commit 2cbd968da5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 76 additions and 114 deletions

View file

@ -822,8 +822,7 @@ WebContents::WebContents(v8::Isolate* isolate,
// Whether to enable DevTools.
options.Get("devTools", &enable_devtools_);
bool initially_shown = true;
options.Get(options::kShow, &initially_shown);
const bool initially_shown = options.ValueOrDefault(options::kShow, true);
// Obtain the session.
std::string partition;
@ -2332,9 +2331,7 @@ void WebContents::LoadURL(const GURL& url,
params.load_type = content::NavigationController::LOAD_TYPE_DATA;
}
bool reload_ignoring_cache = false;
if (options.Get("reloadIgnoringCache", &reload_ignoring_cache) &&
reload_ignoring_cache) {
if (options.ValueOrDefault("reloadIgnoringCache", false)) {
params.reload_type = content::ReloadType::BYPASSING_CACHE;
}
@ -3011,13 +3008,10 @@ void WebContents::Print(gin::Arguments* args) {
}
// Set optional silent printing.
bool silent = false;
options.Get("silent", &silent);
settings.Set("silent", silent);
settings.Set("silent", options.ValueOrDefault("silent", false));
bool print_background = false;
options.Get("printBackground", &print_background);
settings.Set(printing::kSettingShouldPrintBackgrounds, print_background);
settings.Set(printing::kSettingShouldPrintBackgrounds,
options.ValueOrDefault("printBackground", false));
// Set custom margin settings
auto margins = gin_helper::Dictionary::CreateEmpty(args->isolate());
@ -3028,20 +3022,16 @@ void WebContents::Print(gin::Arguments* args) {
settings.Set(printing::kSettingMarginsType, static_cast<int>(margin_type));
if (margin_type == printing::mojom::MarginType::kCustomMargins) {
base::Value::Dict custom_margins;
int top = 0;
margins.Get("top", &top);
custom_margins.Set(printing::kSettingMarginTop, top);
int bottom = 0;
margins.Get("bottom", &bottom);
custom_margins.Set(printing::kSettingMarginBottom, bottom);
int left = 0;
margins.Get("left", &left);
custom_margins.Set(printing::kSettingMarginLeft, left);
int right = 0;
margins.Get("right", &right);
custom_margins.Set(printing::kSettingMarginRight, right);
settings.Set(printing::kSettingMarginsCustom, std::move(custom_margins));
settings.Set(printing::kSettingMarginsCustom,
base::Value::Dict{}
.Set(printing::kSettingMarginTop,
margins.ValueOrDefault("top", 0))
.Set(printing::kSettingMarginBottom,
margins.ValueOrDefault("bottom", 0))
.Set(printing::kSettingMarginLeft,
margins.ValueOrDefault("left", 0))
.Set(printing::kSettingMarginRight,
margins.ValueOrDefault("right", 0)));
}
} else {
settings.Set(
@ -3050,46 +3040,37 @@ void WebContents::Print(gin::Arguments* args) {
}
// Set whether to print color or greyscale
bool print_color = true;
options.Get("color", &print_color);
auto const color_model = print_color ? printing::mojom::ColorModel::kColor
: printing::mojom::ColorModel::kGray;
settings.Set(printing::kSettingColor, static_cast<int>(color_model));
settings.Set(printing::kSettingColor,
static_cast<int>(options.ValueOrDefault("color", true)
? printing::mojom::ColorModel::kColor
: printing::mojom::ColorModel::kGray));
// Is the orientation landscape or portrait.
bool landscape = false;
options.Get("landscape", &landscape);
settings.Set(printing::kSettingLandscape, landscape);
settings.Set(printing::kSettingLandscape,
options.ValueOrDefault("landscape", false));
// We set the default to the system's default printer and only update
// if at the Chromium level if the user overrides.
// Printer device name as opened by the OS.
std::u16string device_name;
options.Get("deviceName", &device_name);
const auto device_name =
options.ValueOrDefault("deviceName", std::u16string{});
int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
settings.Set(printing::kSettingScaleFactor, scale_factor);
settings.Set(printing::kSettingScaleFactor,
options.ValueOrDefault("scaleFactor", 100));
int pages_per_sheet = 1;
options.Get("pagesPerSheet", &pages_per_sheet);
settings.Set(printing::kSettingPagesPerSheet, pages_per_sheet);
settings.Set(printing::kSettingPagesPerSheet,
options.ValueOrDefault("pagesPerSheet", 1));
// True if the user wants to print with collate.
bool collate = true;
options.Get("collate", &collate);
settings.Set(printing::kSettingCollate, collate);
settings.Set(printing::kSettingCollate,
options.ValueOrDefault("collate", true));
// The number of individual copies to print
int copies = 1;
options.Get("copies", &copies);
settings.Set(printing::kSettingCopies, copies);
settings.Set(printing::kSettingCopies, options.ValueOrDefault("copies", 1));
// Strings to be printed as headers and footers if requested by the user.
std::string header;
options.Get("header", &header);
std::string footer;
options.Get("footer", &footer);
const auto header = options.ValueOrDefault("header", std::string{});
const auto footer = options.ValueOrDefault("footer", std::string{});
if (!(header.empty() && footer.empty())) {
settings.Set(printing::kSettingHeaderFooterEnabled, true);
@ -3128,9 +3109,8 @@ void WebContents::Print(gin::Arguments* args) {
}
// Duplex type user wants to use.
printing::mojom::DuplexMode duplex_mode =
printing::mojom::DuplexMode::kSimplex;
options.Get("duplexMode", &duplex_mode);
const auto duplex_mode = options.ValueOrDefault(
"duplexMode", printing::mojom::DuplexMode::kSimplex);
settings.Set(printing::kSettingDuplexMode, static_cast<int>(duplex_mode));
base::Value::Dict media_size;
@ -3150,14 +3130,11 @@ void WebContents::Print(gin::Arguments* args) {
}
// Set custom dots per inch (dpi)
gin_helper::Dictionary dpi_settings;
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.Set(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.Set(printing::kSettingDpiVertical, vertical);
if (gin_helper::Dictionary dpi; options.Get("dpi", &dpi)) {
settings.Set(printing::kSettingDpiHorizontal,
dpi.ValueOrDefault("horizontal", 72));
settings.Set(printing::kSettingDpiVertical,
dpi.ValueOrDefault("vertical", 72));
}
print_task_runner_->PostTaskAndReplyWithResult(