From 1b353d1ed3dee226400560404ced3ee78c5c3964 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 13 Mar 2020 17:16:08 +0000 Subject: [PATCH] chore: revert deprecated WebContents properties (#22640) * chore: revert deprecated WebContents properties * Fix failing zoomFactor test --- docs/api/web-contents.md | 20 ----- lib/browser/api/web-contents.js | 34 ++++++-- .../browser/api/electron_api_web_contents.cc | 30 +++---- spec-main/api-browser-window-spec.ts | 26 +++--- spec-main/api-web-contents-spec.ts | 79 ++++++++++++++++++- 5 files changed, 124 insertions(+), 65 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 51bc5b2da269..fd2940ea802f 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -967,14 +967,10 @@ Returns `Boolean` - Whether the renderer process has crashed. Overrides the user agent for this web page. -**[Deprecated](modernization/property-updates.md)** - #### `contents.getUserAgent()` Returns `String` - The user agent for this web page. -**[Deprecated](modernization/property-updates.md)** - #### `contents.insertCSS(css[, options])` * `css` String @@ -1054,14 +1050,10 @@ Ignore application menu shortcuts while this web contents is focused. Mute the audio on the current web page. -**[Deprecated](modernization/property-updates.md)** - #### `contents.isAudioMuted()` Returns `Boolean` - Whether this page has been muted. -**[Deprecated](modernization/property-updates.md)** - #### `contents.isCurrentlyAudible()` Returns `Boolean` - Whether audio is currently playing. @@ -1073,14 +1065,10 @@ Returns `Boolean` - Whether audio is currently playing. Changes the zoom factor to the specified factor. Zoom factor is zoom percent divided by 100, so 300% = 3.0. -**[Deprecated](modernization/property-updates.md)** - #### `contents.getZoomFactor()` Returns `Number` - the current zoom factor. -**[Deprecated](modernization/property-updates.md)** - #### `contents.setZoomLevel(level)` * `level` Number - Zoom level. @@ -1090,14 +1078,10 @@ increment above or below represents zooming 20% larger or smaller to default limits of 300% and 50% of original size, respectively. The formula for this is `scale := 1.2 ^ level`. -**[Deprecated](modernization/property-updates.md)** - #### `contents.getZoomLevel()` Returns `Number` - the current zoom level. -**[Deprecated](modernization/property-updates.md)** - #### `contents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)` * `minimumLevel` Number @@ -1735,14 +1719,10 @@ Returns `Boolean` - If *offscreen rendering* is enabled returns whether it is cu If *offscreen rendering* is enabled sets the frame rate to the specified number. Only values between 1 and 60 are accepted. -**[Deprecated](modernization/property-updates.md)** - #### `contents.getFrameRate()` Returns `Integer` - If *offscreen rendering* is enabled returns the current frame rate. -**[Deprecated](modernization/property-updates.md)** - #### `contents.invalidate()` Schedules a full repaint of the window this web contents is in. diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 441120ac4313..04e308bfff56 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -549,14 +549,34 @@ WebContents.prototype._init = function () { const event = process.electronBinding('event').createEmpty() app.emit('web-contents-created', event, this) -} -// Deprecations -deprecate.fnToProperty(WebContents.prototype, 'audioMuted', '_isAudioMuted', '_setAudioMuted') -deprecate.fnToProperty(WebContents.prototype, 'userAgent', '_getUserAgent', '_setUserAgent') -deprecate.fnToProperty(WebContents.prototype, 'zoomLevel', '_getZoomLevel', '_setZoomLevel') -deprecate.fnToProperty(WebContents.prototype, 'zoomFactor', '_getZoomFactor', '_setZoomFactor') -deprecate.fnToProperty(WebContents.prototype, 'frameRate', '_getFrameRate', '_setFrameRate') + // Properties + + Object.defineProperty(this, 'audioMuted', { + get: () => this.isAudioMuted(), + set: (muted) => this.setAudioMuted(muted) + }) + + Object.defineProperty(this, 'userAgent', { + get: () => this.getUserAgent(), + set: (agent) => this.setUserAgent(agent) + }) + + Object.defineProperty(this, 'zoomLevel', { + get: () => this.getZoomLevel(), + set: (level) => this.setZoomLevel(level) + }) + + Object.defineProperty(this, 'zoomFactor', { + get: () => this.getZoomFactor(), + set: (factor) => this.setZoomFactor(factor) + }) + + Object.defineProperty(this, 'frameRate', { + get: () => this.getFrameRate(), + set: (rate) => this.setFrameRate(rate) + }) +} // JavaScript wrapper of Debugger. const { Debugger } = process.electronBinding('debugger') diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index fe1e4df22f96..8d1fa0f4e6f6 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2679,10 +2679,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("_goForward", &WebContents::GoForward) .SetMethod("_goToOffset", &WebContents::GoToOffset) .SetMethod("isCrashed", &WebContents::IsCrashed) - .SetMethod("_setUserAgent", &WebContents::SetUserAgent) - .SetMethod("_getUserAgent", &WebContents::GetUserAgent) - .SetProperty("userAgent", &WebContents::GetUserAgent, - &WebContents::SetUserAgent) + .SetMethod("setUserAgent", &WebContents::SetUserAgent) + .SetMethod("getUserAgent", &WebContents::GetUserAgent) .SetMethod("savePage", &WebContents::SavePage) .SetMethod("openDevTools", &WebContents::OpenDevTools) .SetMethod("closeDevTools", &WebContents::CloseDevTools) @@ -2693,10 +2691,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("toggleDevTools", &WebContents::ToggleDevTools) .SetMethod("inspectElement", &WebContents::InspectElement) .SetMethod("setIgnoreMenuShortcuts", &WebContents::SetIgnoreMenuShortcuts) - .SetMethod("_setAudioMuted", &WebContents::SetAudioMuted) - .SetMethod("_isAudioMuted", &WebContents::IsAudioMuted) - .SetProperty("audioMuted", &WebContents::IsAudioMuted, - &WebContents::SetAudioMuted) + .SetMethod("setAudioMuted", &WebContents::SetAudioMuted) + .SetMethod("isAudioMuted", &WebContents::IsAudioMuted) .SetMethod("isCurrentlyAudible", &WebContents::IsCurrentlyAudible) .SetMethod("undo", &WebContents::Undo) .SetMethod("redo", &WebContents::Redo) @@ -2728,20 +2724,14 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("startPainting", &WebContents::StartPainting) .SetMethod("stopPainting", &WebContents::StopPainting) .SetMethod("isPainting", &WebContents::IsPainting) - .SetMethod("_setFrameRate", &WebContents::SetFrameRate) - .SetMethod("_getFrameRate", &WebContents::GetFrameRate) - .SetProperty("frameRate", &WebContents::GetFrameRate, - &WebContents::SetFrameRate) + .SetMethod("setFrameRate", &WebContents::SetFrameRate) + .SetMethod("getFrameRate", &WebContents::GetFrameRate) #endif .SetMethod("invalidate", &WebContents::Invalidate) - .SetMethod("_setZoomLevel", &WebContents::SetZoomLevel) - .SetMethod("_getZoomLevel", &WebContents::GetZoomLevel) - .SetProperty("zoomLevel", &WebContents::GetZoomLevel, - &WebContents::SetZoomLevel) - .SetMethod("_setZoomFactor", &WebContents::SetZoomFactor) - .SetMethod("_getZoomFactor", &WebContents::GetZoomFactor) - .SetProperty("zoomFactor", &WebContents::GetZoomFactor, - &WebContents::SetZoomFactor) + .SetMethod("setZoomLevel", &WebContents::SetZoomLevel) + .SetMethod("getZoomLevel", &WebContents::GetZoomLevel) + .SetMethod("setZoomFactor", &WebContents::SetZoomFactor) + .SetMethod("getZoomFactor", &WebContents::GetZoomFactor) .SetMethod("getType", &WebContents::GetType) .SetMethod("_getPreloadPaths", &WebContents::GetPreloadPaths) .SetMethod("getWebPreferences", &WebContents::GetWebPreferences) diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 7273271762fe..0431203d5b69 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -3958,19 +3958,23 @@ describe('BrowserWindow module', () => { }) }) - // TODO(codebytere): remove in Electron v8.0.0 - describe('window.webContents.getFrameRate()', () => { - it('has default frame rate', (done) => { + describe('frameRate APIs', () => { + it('has default frame rate (functions)', (done) => { w.webContents.once('paint', function () { expect(w.webContents.getFrameRate()).to.equal(60) done() }) w.loadFile(path.join(fixtures, 'api', 'offscreen-rendering.html')) }) - }) - // TODO(codebytere): remove in Electron v8.0.0 - describe('window.webContents.setFrameRate(frameRate)', () => { + it('has default frame rate', (done) => { + w.webContents.once('paint', function () { + expect(w.webContents.frameRate).to.equal(60) + done() + }) + w.loadFile(path.join(fixtures, 'api', 'offscreen-rendering.html')) + }) + it('sets custom frame rate', (done) => { w.webContents.on('dom-ready', () => { w.webContents.setFrameRate(30) @@ -3981,16 +3985,6 @@ describe('BrowserWindow module', () => { }) w.loadFile(path.join(fixtures, 'api', 'offscreen-rendering.html')) }) - }) - - describe('window.webContents.FrameRate', () => { - it('has default frame rate', (done) => { - w.webContents.once('paint', function () { - expect(w.webContents.frameRate).to.equal(60) - done() - }) - w.loadFile(path.join(fixtures, 'api', 'offscreen-rendering.html')) - }) it('sets custom frame rate', (done) => { w.webContents.on('dom-ready', () => { diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index ca68fe612a0b..f84631fe9847 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -770,6 +770,52 @@ describe('webContents module', () => { }) }) + describe('userAgent APIs', () => { + it('can set the user agent (functions)', () => { + const w = new BrowserWindow({ show: false }) + const userAgent = w.webContents.getUserAgent() + + w.webContents.setUserAgent('my-user-agent') + expect(w.webContents.getUserAgent()).to.equal('my-user-agent') + + w.webContents.setUserAgent(userAgent) + expect(w.webContents.getUserAgent()).to.equal(userAgent) + }) + + it('can set the user agent (properties)', () => { + const w = new BrowserWindow({ show: false }) + const userAgent = w.webContents.userAgent + + w.webContents.userAgent = 'my-user-agent' + expect(w.webContents.userAgent).to.equal('my-user-agent') + + w.webContents.userAgent = userAgent + expect(w.webContents.userAgent).to.equal(userAgent) + }) + }) + + describe('audioMuted APIs', () => { + it('can set the audio mute level (functions)', () => { + const w = new BrowserWindow({ show: false }) + + w.webContents.setAudioMuted(true) + expect(w.webContents.isAudioMuted()).to.be.true() + + w.webContents.setAudioMuted(false) + expect(w.webContents.isAudioMuted()).to.be.false() + }) + + it('can set the audio mute level (functions)', () => { + const w = new BrowserWindow({ show: false }) + + w.webContents.audioMuted = true + expect(w.webContents.audioMuted).to.be.true() + + w.webContents.audioMuted = false + expect(w.webContents.audioMuted).to.be.false() + }) + }) + describe('zoom api', () => { const scheme = (global as any).standardScheme const hostZoomMap: Record = { @@ -800,7 +846,6 @@ describe('webContents module', () => { afterEach(closeAllWindows) - // TODO(codebytere): remove in Electron v8.0.0 it('can set the correct zoom level (functions)', async () => { const w = new BrowserWindow({ show: false }) try { @@ -815,7 +860,7 @@ describe('webContents module', () => { } }) - it('can set the correct zoom level', async () => { + it('can set the correct zoom level (properties)', async () => { const w = new BrowserWindow({ show: false }) try { await w.loadURL('about:blank') @@ -829,6 +874,36 @@ describe('webContents module', () => { } }) + it('can set the correct zoom factor (functions)', async () => { + const w = new BrowserWindow({ show: false }) + try { + await w.loadURL('about:blank') + const zoomFactor = w.webContents.getZoomFactor() + expect(zoomFactor).to.eql(1.0) + + w.webContents.setZoomFactor(0.5) + const newZoomFactor = w.webContents.getZoomFactor() + expect(newZoomFactor).to.eql(0.5) + } finally { + w.webContents.setZoomFactor(1.0) + } + }) + + it('can set the correct zoom factor (properties)', async () => { + const w = new BrowserWindow({ show: false }) + try { + await w.loadURL('about:blank') + const zoomFactor = w.webContents.zoomFactor + expect(zoomFactor).to.eql(1.0) + + w.webContents.zoomFactor = 0.5 + const newZoomFactor = w.webContents.zoomFactor + expect(newZoomFactor).to.eql(0.5) + } finally { + w.webContents.zoomFactor = 1.0 + } + }) + it('can persist zoom level across navigation', (done) => { const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, enableRemoteModule: true } }) let finalNavigation = false