Skip to content

Commit

Permalink
feat: add WebFrameMain.visibilityState
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Apr 16, 2021
1 parent 6df2680 commit ea5d842
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/api/web-frame-main.md
Expand Up @@ -182,3 +182,7 @@ This is not the same as the OS process ID; to read that use `frame.osProcessId`.
An `Integer` representing the unique frame id in the current renderer process.
Distinct `WebFrameMain` instances that refer to the same underlying frame will
have the same `routingId`.

#### `frame.visibilityState` _Readonly_

A `string` representing the [visibility state](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) of the frame.
4 changes: 2 additions & 2 deletions script/spec-runner.js
Expand Up @@ -59,8 +59,8 @@ async function main () {
(lastSpecInstallHash !== currentSpecInstallHash);

if (somethingChanged) {
await installSpecModules(path.resolve(__dirname, '..', 'spec'));
await installSpecModules(path.resolve(__dirname, '..', 'spec-main'));
// await installSpecModules(path.resolve(__dirname, '..', 'spec'));
// await installSpecModules(path.resolve(__dirname, '..', 'spec-main'));
await getSpecHash().then(saveSpecHash);
}

Expand Down
29 changes: 29 additions & 0 deletions shell/browser/api/electron_api_web_frame_main.cc
Expand Up @@ -30,6 +30,28 @@
#include "shell/common/node_includes.h"
#include "shell/common/v8_value_serializer.h"

namespace gin {

template <>
struct Converter<blink::mojom::PageVisibilityState> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
blink::mojom::PageVisibilityState val) {
std::string visibility;
switch (val) {
case blink::mojom::PageVisibilityState::kVisible:
visibility = "visible";
break;
case blink::mojom::PageVisibilityState::kHidden:
case blink::mojom::PageVisibilityState::kHiddenButPainting:
visibility = "hidden";
break;
}
return gin::ConvertToV8(isolate, visibility);
}
};

} // namespace gin

namespace electron {

namespace api {
Expand Down Expand Up @@ -228,6 +250,12 @@ GURL WebFrameMain::URL() const {
return render_frame_->GetLastCommittedURL();
}

blink::mojom::PageVisibilityState WebFrameMain::VisibilityState() const {
if (!CheckRenderFrame())
return blink::mojom::PageVisibilityState::kHidden;
return render_frame_->GetVisibilityState();
}

content::RenderFrameHost* WebFrameMain::Top() const {
if (!CheckRenderFrame())
return nullptr;
Expand Down Expand Up @@ -331,6 +359,7 @@ v8::Local<v8::ObjectTemplate> WebFrameMain::FillObjectTemplate(
.SetProperty("processId", &WebFrameMain::ProcessID)
.SetProperty("routingId", &WebFrameMain::RoutingID)
.SetProperty("url", &WebFrameMain::URL)
.SetProperty("visibilityState", &WebFrameMain::VisibilityState)
.SetProperty("top", &WebFrameMain::Top)
.SetProperty("parent", &WebFrameMain::Parent)
.SetProperty("frames", &WebFrameMain::Frames)
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/api/electron_api_web_frame_main.h
Expand Up @@ -15,6 +15,7 @@
#include "gin/wrappable.h"
#include "shell/common/gin_helper/constructible.h"
#include "shell/common/gin_helper/pinnable.h"
#include "third_party/blink/public/mojom/page/page_visibility_state.mojom-forward.h"

class GURL;

Expand Down Expand Up @@ -95,6 +96,7 @@ class WebFrameMain : public gin::Wrappable<WebFrameMain>,
int ProcessID() const;
int RoutingID() const;
GURL URL() const;
blink::mojom::PageVisibilityState VisibilityState() const;

content::RenderFrameHost* Top() const;
content::RenderFrameHost* Parent() const;
Expand Down
12 changes: 12 additions & 0 deletions spec-main/api-web-frame-main-spec.ts
Expand Up @@ -135,6 +135,18 @@ describe('webFrameMain module', () => {
});
});

describe('WebFrame.visibilityState', () => {
it('should match window state', async () => {
const w = new BrowserWindow({ show: true });
await w.loadURL('about:blank');
const webFrame = w.webContents.mainFrame;

expect(webFrame.visibilityState).to.equal('visible');
w.hide();
expect(webFrame.visibilityState).to.equal('hidden');
});
});

describe('WebFrame.executeJavaScript', () => {
it('can inject code into any subframe', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
Expand Down

0 comments on commit ea5d842

Please sign in to comment.