feat: expose setBackgroundThrottling api (#15104)

This commit is contained in:
Andy Dill 2018-10-18 17:32:22 -07:00 committed by Samuel Attard
parent 10db2bce4e
commit 37f2113032
5 changed files with 71 additions and 0 deletions

View file

@ -1104,6 +1104,37 @@ void WebContents::NavigationEntryCommitted(
details.is_same_document, details.did_replace_entry); details.is_same_document, details.did_replace_entry);
} }
void WebContents::SetBackgroundThrottling(bool allowed) {
background_throttling_ = allowed;
const auto* contents = web_contents();
if (!contents) {
return;
}
const auto* render_view_host = contents->GetRenderViewHost();
if (!render_view_host) {
return;
}
const auto* render_process_host = render_view_host->GetProcess();
if (!render_process_host) {
return;
}
auto* render_widget_host_impl = content::RenderWidgetHostImpl::FromID(
render_process_host->GetID(), render_view_host->GetRoutingID());
if (!render_widget_host_impl) {
return;
}
render_widget_host_impl->disable_hidden_ = !background_throttling_;
if (render_widget_host_impl->is_hidden()) {
render_widget_host_impl->WasShown(false);
}
}
int WebContents::GetProcessID() const { int WebContents::GetProcessID() const {
return web_contents()->GetMainFrame()->GetProcess()->GetID(); return web_contents()->GetMainFrame()->GetProcess()->GetID();
} }
@ -2011,6 +2042,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
prototype->SetClassName(mate::StringToV8(isolate, "WebContents")); prototype->SetClassName(mate::StringToV8(isolate, "WebContents"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable() .MakeDestroyable()
.SetMethod("setBackgroundThrottling",
&WebContents::SetBackgroundThrottling)
.SetMethod("getProcessId", &WebContents::GetProcessID) .SetMethod("getProcessId", &WebContents::GetProcessID)
.SetMethod("getOSProcessId", &WebContents::GetOSProcessID) .SetMethod("getOSProcessId", &WebContents::GetOSProcessID)
.SetMethod("equal", &WebContents::Equal) .SetMethod("equal", &WebContents::Equal)

View file

@ -101,6 +101,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Notifies to destroy any guest web contents before destroying self. // Notifies to destroy any guest web contents before destroying self.
void DestroyWebContents(bool async); void DestroyWebContents(bool async);
void SetBackgroundThrottling(bool allowed);
int GetProcessID() const; int GetProcessID() const;
base::ProcessId GetOSProcessID() const; base::ProcessId GetOSProcessID() const;
Type GetType() const; Type GetType() const;

View file

@ -1527,6 +1527,13 @@ Returns `Promise<void>` - Indicates whether the snapshot has been created succes
Takes a V8 heap snapshot and saves it to `filePath`. Takes a V8 heap snapshot and saves it to `filePath`.
#### `contents.setBackgroundThrottling(allowed)`
* `allowed` Boolean
Controls whether or not this WebContents will throttle animations and timers
when the page becomes backgrounded. This also affects the Page Visibility API.
### Instance Properties ### Instance Properties
#### `contents.id` #### `contents.id`

View file

@ -221,6 +221,9 @@ Object.assign(BrowserWindow.prototype, {
}, },
setTouchBar (touchBar) { setTouchBar (touchBar) {
electron.TouchBar._setOnWindow(touchBar, this) electron.TouchBar._setOnWindow(touchBar, this)
},
setBackgroundThrottling (allowed) {
this.webContents.setBackgroundThrottling(allowed)
} }
}) })

View file

@ -905,4 +905,31 @@ describe('webContents module', () => {
return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed') return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
}) })
}) })
describe('setBackgroundThrottling()', () => {
it('does not crash when allowing', (done) => {
w.webContents.setBackgroundThrottling(true)
done()
})
it('does not crash when disallowing', (done) => {
w.destroy()
w = new BrowserWindow({
show: false,
width: 400,
height: 400,
webPreferences: {
backgroundThrottling: true
}
})
w.webContents.setBackgroundThrottling(false)
done()
})
it('does not crash when called via BrowserWindow', (done) => {
w.setBackgroundThrottling(true)
done()
})
})
}) })