Skip to content

Commit

Permalink
feat: expose setBackgroundThrottling api (#15104)
Browse files Browse the repository at this point in the history
  • Loading branch information
adill authored and MarshallOfSound committed Oct 19, 2018
1 parent 10db2bc commit 37f2113
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
33 changes: 33 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -1104,6 +1104,37 @@ void WebContents::NavigationEntryCommitted(
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 {
return web_contents()->GetMainFrame()->GetProcess()->GetID();
}
Expand Down Expand Up @@ -2011,6 +2042,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
prototype->SetClassName(mate::StringToV8(isolate, "WebContents"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setBackgroundThrottling",
&WebContents::SetBackgroundThrottling)
.SetMethod("getProcessId", &WebContents::GetProcessID)
.SetMethod("getOSProcessId", &WebContents::GetOSProcessID)
.SetMethod("equal", &WebContents::Equal)
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -101,6 +101,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Notifies to destroy any guest web contents before destroying self.
void DestroyWebContents(bool async);

void SetBackgroundThrottling(bool allowed);
int GetProcessID() const;
base::ProcessId GetOSProcessID() const;
Type GetType() const;
Expand Down
7 changes: 7 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -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`.

#### `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

#### `contents.id`
Expand Down
3 changes: 3 additions & 0 deletions lib/browser/api/browser-window.js
Expand Up @@ -221,6 +221,9 @@ Object.assign(BrowserWindow.prototype, {
},
setTouchBar (touchBar) {
electron.TouchBar._setOnWindow(touchBar, this)
},
setBackgroundThrottling (allowed) {
this.webContents.setBackgroundThrottling(allowed)
}
})

Expand Down
27 changes: 27 additions & 0 deletions spec/api-web-contents-spec.js
Expand Up @@ -905,4 +905,31 @@ describe('webContents module', () => {
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()
})
})
})

0 comments on commit 37f2113

Please sign in to comment.