Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getBackgroundThrottling() + backgroundThrottling property #21036

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/modernization/property-updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The Electron team is currently undergoing an initiative to convert separate gett
* `fullscreenable`
* `movable`
* `closable`
* `backgroundThrottling`
* `NativeImage`
* `isMacTemplateImage`
* `SystemPreferences` module
Expand Down
10 changes: 10 additions & 0 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,11 @@ Returns `Promise<void>` - Indicates whether the snapshot has been created succes

Takes a V8 heap snapshot and saves it to `filePath`.

#### `contents.getBackgroundThrottling()`

Returns `Boolean` - whether or not this WebContents will throttle animations and timers
when the page becomes backgrounded. This also affects the Page Visibility API.

#### `contents.setBackgroundThrottling(allowed)`

* `allowed` Boolean
Expand Down Expand Up @@ -1879,3 +1884,8 @@ A [`Debugger`](debugger.md) instance for this webContents.
[event-emitter]: https://nodejs.org/api/events.html#events_class_eventemitter
[SCA]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
[`postMessage`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

#### `contents.backgroundThrottling`

A `Boolean` property that determines whether or not this WebContents will throttle animations and timers
when the page becomes backgrounded. This also affects the Page Visibility API.
3 changes: 3 additions & 0 deletions lib/browser/api/browser-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ Object.assign(BrowserWindow.prototype, {
setTouchBar (touchBar) {
electron.TouchBar._setOnWindow(touchBar, this);
},
getBackgroundThrottling () {
return this.webContents.getBackgroundThrottling();
},
setBackgroundThrottling (allowed) {
this.webContents.setBackgroundThrottling(allowed);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ WebContents.prototype._init = function () {
get: () => this.getFrameRate(),
set: (rate) => this.setFrameRate(rate)
});

Object.defineProperty(this, 'backgroundThrottling', {
get: () => this.getBackgroundThrottling(),
set: (allowed) => this.setBackgroundThrottling(allowed)
});
};

// Public APIs.
Expand Down
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,10 @@ void WebContents::NavigationEntryCommitted(
details.is_same_document, details.did_replace_entry);
}

bool WebContents::GetBackgroundThrottling() const {
return background_throttling_;
}

void WebContents::SetBackgroundThrottling(bool allowed) {
background_throttling_ = allowed;

Expand Down Expand Up @@ -2719,6 +2723,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
prototype->SetClassName(gin::StringToV8(isolate, "WebContents"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("getBackgroundThrottling",
&WebContents::GetBackgroundThrottling)
.SetMethod("setBackgroundThrottling",
&WebContents::SetBackgroundThrottling)
.SetMethod("getProcessId", &WebContents::GetProcessID)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
// See https://github.com/electron/electron/issues/15133.
void DestroyWebContents(bool async);

bool GetBackgroundThrottling() const;
void SetBackgroundThrottling(bool allowed);
int GetProcessID() const;
base::ProcessId GetOSProcessID() const;
Expand Down
33 changes: 33 additions & 0 deletions spec-main/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,39 @@ describe('webContents module', () => {
});
});

describe('getBackgroundThrottling()', () => {
miniak marked this conversation as resolved.
Show resolved Hide resolved
afterEach(closeAllWindows);
it('works via getter', () => {
miniak marked this conversation as resolved.
Show resolved Hide resolved
const w = new BrowserWindow({ show: false });

w.webContents.setBackgroundThrottling(false);
expect(w.webContents.getBackgroundThrottling()).to.equal(false);

w.webContents.setBackgroundThrottling(true);
expect(w.webContents.getBackgroundThrottling()).to.equal(true);
});

it('works via property', () => {
const w = new BrowserWindow({ show: false });

w.webContents.backgroundThrottling = false;
expect(w.webContents.backgroundThrottling).to.equal(false);

w.webContents.backgroundThrottling = true;
expect(w.webContents.backgroundThrottling).to.equal(true);
});

it('works via BrowserWindow', () => {
const w = new BrowserWindow({ show: false });

(w as any).setBackgroundThrottling(false);
expect((w as any).getBackgroundThrottling()).to.equal(false);

(w as any).setBackgroundThrottling(true);
expect((w as any).getBackgroundThrottling()).to.equal(true);
});
});

ifdescribe(features.isPrintingEnabled())('getPrinters()', () => {
afterEach(closeAllWindows);
it('can get printer list', async () => {
Expand Down