From a4c4c86b9dcd394855249b9c63314cbd00fe3682 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 13 Mar 2020 23:13:05 +0000 Subject: [PATCH] fix: crash on invalid zoomFactor (#22673) --- docs/api/web-contents.md | 4 +++- docs/api/web-frame.md | 4 +++- shell/browser/api/electron_api_web_contents.cc | 9 ++++++++- shell/browser/api/electron_api_web_contents.h | 2 +- shell/renderer/api/electron_api_web_frame.cc | 10 +++++++++- spec-main/api-web-contents-spec.ts | 13 +++++++++++++ 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index fd2940ea802f..a44cdf279f6e 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1060,11 +1060,13 @@ Returns `Boolean` - Whether audio is currently playing. #### `contents.setZoomFactor(factor)` -* `factor` Number - Zoom factor. +* `factor` Double - Zoom factor; default is 1.0. Changes the zoom factor to the specified factor. Zoom factor is zoom percent divided by 100, so 300% = 3.0. +The factor must be greater than 0.0. + #### `contents.getZoomFactor()` Returns `Number` - the current zoom factor. diff --git a/docs/api/web-frame.md b/docs/api/web-frame.md index 779434d086c2..6d2e5b5fdfcb 100644 --- a/docs/api/web-frame.md +++ b/docs/api/web-frame.md @@ -22,11 +22,13 @@ The `WebFrame` class has the following instance methods: ### `webFrame.setZoomFactor(factor)` -* `factor` Number - Zoom factor. +* `factor` Double - Zoom factor; default is 1.0. Changes the zoom factor to the specified factor. Zoom factor is zoom percent divided by 100, so 300% = 3.0. +The factor must be greater than 0.0. + ### `webFrame.getZoomFactor()` Returns `Number` - The current zoom factor. diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 8d1fa0f4e6f6..d12a2702a474 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -4,6 +4,7 @@ #include "shell/browser/api/electron_api_web_contents.h" +#include #include #include #include @@ -2489,7 +2490,13 @@ double WebContents::GetZoomLevel() const { return zoom_controller_->GetZoomLevel(); } -void WebContents::SetZoomFactor(double factor) { +void WebContents::SetZoomFactor(gin_helper::ErrorThrower thrower, + double factor) { + if (factor < std::numeric_limits::epsilon()) { + thrower.ThrowError("'zoomFactor' must be a double greater than 0.0"); + return; + } + auto level = blink::PageZoomFactorToZoomLevel(factor); SetZoomLevel(level); } diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 84ae2b50036f..7f32581787df 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -297,7 +297,7 @@ class WebContents : public gin_helper::TrackableObject, // Methods for zoom handling. void SetZoomLevel(double level); double GetZoomLevel() const; - void SetZoomFactor(double factor); + void SetZoomFactor(gin_helper::ErrorThrower thrower, double factor); double GetZoomFactor() const; // Callback triggered on permission response. diff --git a/shell/renderer/api/electron_api_web_frame.cc b/shell/renderer/api/electron_api_web_frame.cc index 924bddd73322..708b8da95cc4 100644 --- a/shell/renderer/api/electron_api_web_frame.cc +++ b/shell/renderer/api/electron_api_web_frame.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include #include #include #include @@ -275,7 +276,14 @@ double GetZoomLevel(v8::Local window) { return result; } -void SetZoomFactor(v8::Local window, double factor) { +void SetZoomFactor(gin_helper::ErrorThrower thrower, + v8::Local window, + double factor) { + if (factor < std::numeric_limits::epsilon()) { + thrower.ThrowError("'zoomFactor' must be a double greater than 0.0"); + return; + } + SetZoomLevel(window, blink::PageZoomFactorToZoomLevel(factor)); } diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index f84631fe9847..80cb0164fe2b 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -846,6 +846,19 @@ describe('webContents module', () => { afterEach(closeAllWindows) + it('throws on an invalid zoomFactor', async () => { + const w = new BrowserWindow({ show: false }) + await w.loadURL('about:blank') + + expect(() => { + w.webContents.setZoomFactor(0.0) + }).to.throw(/'zoomFactor' must be a double greater than 0.0/) + + expect(() => { + w.webContents.setZoomFactor(-2.0) + }).to.throw(/'zoomFactor' must be a double greater than 0.0/) + }) + it('can set the correct zoom level (functions)', async () => { const w = new BrowserWindow({ show: false }) try {