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
This commit is contained in:
Charles Kerr 2024-01-30 14:48:47 -06:00 коммит произвёл GitHub
Родитель 08615b2d4e
Коммит 0e0a0bf724
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 8 добавлений и 23 удалений

Просмотреть файл

@ -15,6 +15,7 @@
#include "base/json/string_escape.h" #include "base/json/string_escape.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/ranges/algorithm.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/pattern.h" #include "base/strings/pattern.h"
#include "base/strings/string_number_conversions.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) { gfx::Rect DictionaryToRect(const base::Value::Dict& dict) {
const base::Value* found = dict.Find("x"); return gfx::Rect{dict.FindInt("x").value_or(0), dict.FindInt("y").value_or(0),
int x = found ? found->GetInt() : 0; dict.FindInt("width").value_or(800),
dict.FindInt("height").value_or(600)};
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());
} }
bool IsPointInScreen(const gfx::Point& point) { bool IsPointInScreen(const gfx::Point& point) {
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { return base::ranges::any_of(display::Screen::GetScreen()->GetAllDisplays(),
if (IsPointInRect(point, display.bounds())) [&point](auto const& display) {
return true; return display.bounds().Contains(point);
} });
return false;
} }
void SetZoomLevelForWebContents(content::WebContents* web_contents, void SetZoomLevelForWebContents(content::WebContents* web_contents,