Skip to content

Commit

Permalink
chore: Add new webFrame IsolatedWorldInfo API and deprecate
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsakh committed Feb 6, 2019
1 parent 84eef16 commit ea4d5af
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
41 changes: 38 additions & 3 deletions atom/renderer/api/atom_api_web_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,40 @@ void WebFrame::SetIsolatedWorldHumanReadableName(int world_id,
world_id, blink::WebString::FromUTF8(name));
}

void WebFrame::SetIsolatedWorldInfo(int world_id, mate::Arguments* args) {
if (args->Length() < 1) {
args->ThrowError("Invalid args");
return;
}

v8::Local<v8::Value> val;
args->GetNext(&val);

mate::Dictionary options;
if (!mate::ConvertFromV8(args->isolate(), val, &options)) {
args->ThrowError("Must pass valid object");
return;
}

std::string origin, csp, name;
options.Get("securityOrigin", &origin);
options.Get("csp", &csp);
options.Get("name", &name);

if (!csp.empty() && origin.empty()) {
args->ThrowError(
"If csp is spicified, securityOrigin should also be specified");
return;
}

if (!origin.empty())
SetIsolatedWorldSecurityOrigin(world_id, origin);
if (!csp.empty())
SetIsolatedWorldContentSecurityPolicy(world_id, csp);
if (!name.empty())
SetIsolatedWorldHumanReadableName(world_id, name);
}

// static
mate::Handle<WebFrame> WebFrame::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new WebFrame(isolate));
Expand Down Expand Up @@ -478,12 +512,13 @@ void WebFrame::BuildPrototype(v8::Isolate* isolate,
.SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript)
.SetMethod("executeJavaScriptInIsolatedWorld",
&WebFrame::ExecuteJavaScriptInIsolatedWorld)
.SetMethod("setIsolatedWorldSecurityOrigin",
.SetMethod("_setIsolatedWorldSecurityOrigin",
&WebFrame::SetIsolatedWorldSecurityOrigin)
.SetMethod("setIsolatedWorldContentSecurityPolicy",
.SetMethod("_setIsolatedWorldContentSecurityPolicy",
&WebFrame::SetIsolatedWorldContentSecurityPolicy)
.SetMethod("setIsolatedWorldHumanReadableName",
.SetMethod("_setIsolatedWorldHumanReadableName",
&WebFrame::SetIsolatedWorldHumanReadableName)
.SetMethod("setIsolatedWorldInfo", &WebFrame::SetIsolatedWorldInfo)
.SetMethod("getResourceUsage", &WebFrame::GetResourceUsage)
.SetMethod("clearCache", &WebFrame::ClearCache)
.SetMethod("getFrameForSelector", &WebFrame::GetFrameForSelector)
Expand Down
2 changes: 1 addition & 1 deletion atom/renderer/api/atom_api_web_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class WebFrame : public mate::Wrappable<WebFrame> {
int world_id,
const std::string& security_policy);
void SetIsolatedWorldHumanReadableName(int world_id, const std::string& name);

void SetIsolatedWorldInfo(int world_id, mate::Arguments* args);
// Resource related methods
blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate);
void ClearCache(v8::Isolate* isolate);
Expand Down
17 changes: 17 additions & 0 deletions docs/api/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ Child windows opened with the `nativeWindowOpen` option will always have Node.js
Renderer process APIs `webFrame.setRegisterURLSchemeAsPrivileged` and `webFrame.registerURLSchemeAsBypassingCSP` as well as browser process API `protocol.registerStandardSchemes` have been removed.
A new API, `protocol.registerSchemesAsPrivileged` has been added and should be used for registering custom schemes with the required privileges. Custom schemes are required to be registered before app ready.

## webFrame Isolated World APIs

```js
// Deprecated
webFrame.setIsolatedWorldContentSecurityPolicy(worldId, csp)
webFrame.setIsolatedWorldHumanReadableName(worldId, name)
webFrame.setIsolatedWorldSecurityOrigin(worldId, securityOrigin)
// Replace with
webFrame.setIsolatedWorldInfo(
worldId,
{
securityOrigin: 'some_origin',
name: 'human_readable_name',
csp: 'content_security_policy'
})
```

# Planned Breaking API Changes (4.0)

The following list includes the breaking API changes made in Electron 4.0.
Expand Down
10 changes: 10 additions & 0 deletions docs/api/web-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ Set the name of the isolated world. Useful in devtools.

Set the security origin of the isolated world.

### `webFrame.setIsolatedWorldInfo(worldId, info)`
* `worldId` Integer - The ID of the world to run the javascript in, `0` is the default world, `999` is the world used by Electrons `contextIsolation` feature. You can provide any integer here.
* `info` Object
* `securityOrigin` String (optional) - Security origin for the isolated world.
* `csp` String (optional) - Content Security Policy for the isolated world.
* `name` String (optional) - Name for isolated world. Useful in devtools.

Set the security origin, content security policy and name of the isolated world.
Note: If the `csp` is specified, then the `securityOrigin` also has to be specified.

### `webFrame.getResourceUsage()`

Returns `Object`:
Expand Down
15 changes: 15 additions & 0 deletions lib/renderer/api/web-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

const { EventEmitter } = require('events')
const { webFrame, WebFrame } = process.atomBinding('web_frame')
const { deprecate } = require('electron')

// WebFrame is an EventEmitter.
Object.setPrototypeOf(WebFrame.prototype, EventEmitter.prototype)
EventEmitter.call(webFrame)

// Lots of webview would subscribe to webFrame's events.
webFrame.setMaxListeners(0)
webFrame.setIsolatedWorldSecurityOrigin = (worldId, securityOrigin) => {
deprecate.warn('webFrame.setIsolatedWorldSecurityOrigin', 'webFrame.setIsolatedWorldInfo')
webFrame._setIsolatedWorldSecurityOrigin(worldId, securityOrigin)
}

webFrame.setIsolatedWorldContentSecurityPolicy = (worldId, csp) => {
deprecate.warn('webFrame.setIsolatedWorldContentSecurityPolicy', 'webFrame.setIsolatedWorldInfo')
webFrame._setIsolatedWorldContentSecurityPolicy(worldId, csp)
}

webFrame.setIsolatedWorldHumanReadableName = (worldId, name) => {
deprecate.warn('webFrame.setIsolatedWorldHumanReadableName', 'webFrame.setIsolatedWorldInfo')
webFrame._setIsolatedWorldHumanReadableName(worldId, name)
}

module.exports = webFrame

0 comments on commit ea4d5af

Please sign in to comment.