fix: -Wunsafe-buffer-usage warnings in GetNextZoomLevel() (#43803)

This commit is contained in:
Charles Kerr 2024-09-21 17:21:02 -05:00 committed by GitHub
parent f89bd745d5
commit e894839709
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,6 +6,7 @@
#include "shell/browser/ui/inspectable_web_contents.h" #include "shell/browser/ui/inspectable_web_contents.h"
#include <algorithm> #include <algorithm>
#include <array>
#include <memory> #include <memory>
#include <string_view> #include <string_view>
#include <utility> #include <utility>
@ -69,10 +70,6 @@ namespace electron {
namespace { namespace {
const double kPresetZoomFactors[] = {0.25, 0.333, 0.5, 0.666, 0.75, 0.9,
1.0, 1.1, 1.25, 1.5, 1.75, 2.0,
2.5, 3.0, 4.0, 5.0};
const char kChromeUIDevToolsURL[] = const char kChromeUIDevToolsURL[] =
"devtools://devtools/bundled/devtools_app.html?" "devtools://devtools/bundled/devtools_app.html?"
"remoteBase=%s&" "remoteBase=%s&"
@ -121,16 +118,20 @@ void SetZoomLevelForWebContents(content::WebContents* web_contents,
content::HostZoomMap::SetZoomLevel(web_contents, level); content::HostZoomMap::SetZoomLevel(web_contents, level);
} }
double GetNextZoomLevel(double level, bool out) { double GetNextZoomLevel(const double level, const bool out) {
double factor = blink::ZoomLevelToZoomFactor(level); static constexpr std::array<double, 16U> kPresetFactors{
size_t size = std::size(kPresetZoomFactors); 0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, 1.1,
for (size_t i = 0; i < size; ++i) { 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0};
if (!blink::ZoomValuesEqual(kPresetZoomFactors[i], factor)) static constexpr auto kBegin = kPresetFactors.begin();
continue; static constexpr auto kEnd = kPresetFactors.end();
if (out && i > 0)
return blink::ZoomFactorToZoomLevel(kPresetZoomFactors[i - 1]); const double factor = blink::ZoomLevelToZoomFactor(level);
if (!out && i != size - 1) auto matches = [=](auto val) { return blink::ZoomValuesEqual(factor, val); };
return blink::ZoomFactorToZoomLevel(kPresetZoomFactors[i + 1]); if (auto iter = std::find_if(kBegin, kEnd, matches); iter != kEnd) {
if (out && iter != kBegin)
return blink::ZoomFactorToZoomLevel(*--iter);
if (!out && ++iter != kEnd)
return blink::ZoomFactorToZoomLevel(*iter);
} }
return level; return level;
} }