fix: crash on invalid zoomFactor (#22673)
This commit is contained in:
parent
9c5874306d
commit
a4c4c86b9d
6 changed files with 37 additions and 5 deletions
|
@ -1060,11 +1060,13 @@ Returns `Boolean` - Whether audio is currently playing.
|
||||||
|
|
||||||
#### `contents.setZoomFactor(factor)`
|
#### `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
|
Changes the zoom factor to the specified factor. Zoom factor is
|
||||||
zoom percent divided by 100, so 300% = 3.0.
|
zoom percent divided by 100, so 300% = 3.0.
|
||||||
|
|
||||||
|
The factor must be greater than 0.0.
|
||||||
|
|
||||||
#### `contents.getZoomFactor()`
|
#### `contents.getZoomFactor()`
|
||||||
|
|
||||||
Returns `Number` - the current zoom factor.
|
Returns `Number` - the current zoom factor.
|
||||||
|
|
|
@ -22,11 +22,13 @@ The `WebFrame` class has the following instance methods:
|
||||||
|
|
||||||
### `webFrame.setZoomFactor(factor)`
|
### `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
|
Changes the zoom factor to the specified factor. Zoom factor is
|
||||||
zoom percent divided by 100, so 300% = 3.0.
|
zoom percent divided by 100, so 300% = 3.0.
|
||||||
|
|
||||||
|
The factor must be greater than 0.0.
|
||||||
|
|
||||||
### `webFrame.getZoomFactor()`
|
### `webFrame.getZoomFactor()`
|
||||||
|
|
||||||
Returns `Number` - The current zoom factor.
|
Returns `Number` - The current zoom factor.
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "shell/browser/api/electron_api_web_contents.h"
|
#include "shell/browser/api/electron_api_web_contents.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -2489,7 +2490,13 @@ double WebContents::GetZoomLevel() const {
|
||||||
return zoom_controller_->GetZoomLevel();
|
return zoom_controller_->GetZoomLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetZoomFactor(double factor) {
|
void WebContents::SetZoomFactor(gin_helper::ErrorThrower thrower,
|
||||||
|
double factor) {
|
||||||
|
if (factor < std::numeric_limits<double>::epsilon()) {
|
||||||
|
thrower.ThrowError("'zoomFactor' must be a double greater than 0.0");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto level = blink::PageZoomFactorToZoomLevel(factor);
|
auto level = blink::PageZoomFactorToZoomLevel(factor);
|
||||||
SetZoomLevel(level);
|
SetZoomLevel(level);
|
||||||
}
|
}
|
||||||
|
|
|
@ -297,7 +297,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
|
||||||
// Methods for zoom handling.
|
// Methods for zoom handling.
|
||||||
void SetZoomLevel(double level);
|
void SetZoomLevel(double level);
|
||||||
double GetZoomLevel() const;
|
double GetZoomLevel() const;
|
||||||
void SetZoomFactor(double factor);
|
void SetZoomFactor(gin_helper::ErrorThrower thrower, double factor);
|
||||||
double GetZoomFactor() const;
|
double GetZoomFactor() const;
|
||||||
|
|
||||||
// Callback triggered on permission response.
|
// Callback triggered on permission response.
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Use of this source code is governed by the MIT license that can be
|
// Use of this source code is governed by the MIT license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -275,7 +276,14 @@ double GetZoomLevel(v8::Local<v8::Value> window) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetZoomFactor(v8::Local<v8::Value> window, double factor) {
|
void SetZoomFactor(gin_helper::ErrorThrower thrower,
|
||||||
|
v8::Local<v8::Value> window,
|
||||||
|
double factor) {
|
||||||
|
if (factor < std::numeric_limits<double>::epsilon()) {
|
||||||
|
thrower.ThrowError("'zoomFactor' must be a double greater than 0.0");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SetZoomLevel(window, blink::PageZoomFactorToZoomLevel(factor));
|
SetZoomLevel(window, blink::PageZoomFactorToZoomLevel(factor));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -846,6 +846,19 @@ describe('webContents module', () => {
|
||||||
|
|
||||||
afterEach(closeAllWindows)
|
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 () => {
|
it('can set the correct zoom level (functions)', async () => {
|
||||||
const w = new BrowserWindow({ show: false })
|
const w = new BrowserWindow({ show: false })
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue