Skip to content

Commit

Permalink
chore: revert deprecated WebContents properties (#22640)
Browse files Browse the repository at this point in the history
* chore: revert deprecated WebContents properties

* Fix failing zoomFactor test
  • Loading branch information
codebytere committed Mar 13, 2020
1 parent 0c02d79 commit 1b353d1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 65 deletions.
20 changes: 0 additions & 20 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 27 additions & 7 deletions lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
30 changes: 10 additions & 20 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 10 additions & 16 deletions spec-main/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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', () => {
Expand Down
79 changes: 77 additions & 2 deletions spec-main/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> = {
Expand Down Expand Up @@ -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 {
Expand All @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 1b353d1

Please sign in to comment.