From 0e0a0bf724064400c117ba68e761818fd5b2ad30 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 30 Jan 2024 14:48:47 -0600 Subject: [PATCH] fix: avoid potential `CHECK()` failure in `DictionaryToRect()` (#41160) refactor: use gfx::Rect::Contains() instead of reinventing the wheel perf: use base::Value::FindInt() to avoid redundant map lookups --- shell/browser/ui/inspectable_web_contents.cc | 31 +++++--------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/shell/browser/ui/inspectable_web_contents.cc b/shell/browser/ui/inspectable_web_contents.cc index 3bb01208a9..b69628e328 100644 --- a/shell/browser/ui/inspectable_web_contents.cc +++ b/shell/browser/ui/inspectable_web_contents.cc @@ -15,6 +15,7 @@ #include "base/json/string_escape.h" #include "base/memory/raw_ptr.h" #include "base/metrics/histogram.h" +#include "base/ranges/algorithm.h" #include "base/stl_util.h" #include "base/strings/pattern.h" #include "base/strings/string_number_conversions.h" @@ -108,32 +109,16 @@ base::Value::Dict RectToDictionary(const gfx::Rect& bounds) { } gfx::Rect DictionaryToRect(const base::Value::Dict& dict) { - const base::Value* found = dict.Find("x"); - int x = found ? found->GetInt() : 0; - - found = dict.Find("y"); - int y = found ? found->GetInt() : 0; - - found = dict.Find("width"); - int width = found ? found->GetInt() : 800; - - found = dict.Find("height"); - int height = found ? found->GetInt() : 600; - - return gfx::Rect(x, y, width, height); -} - -bool IsPointInRect(const gfx::Point& point, const gfx::Rect& rect) { - return point.x() > rect.x() && point.x() < (rect.width() + rect.x()) && - point.y() > rect.y() && point.y() < (rect.height() + rect.y()); + return gfx::Rect{dict.FindInt("x").value_or(0), dict.FindInt("y").value_or(0), + dict.FindInt("width").value_or(800), + dict.FindInt("height").value_or(600)}; } bool IsPointInScreen(const gfx::Point& point) { - for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { - if (IsPointInRect(point, display.bounds())) - return true; - } - return false; + return base::ranges::any_of(display::Screen::GetScreen()->GetAllDisplays(), + [&point](auto const& display) { + return display.bounds().Contains(point); + }); } void SetZoomLevelForWebContents(content::WebContents* web_contents,