Skip to content

Commit ae7b969

Browse files
Hugo Manozcbenz
authored andcommitted
Add webFrame.setIsolatedWorldSecurityOrigin
Move vector to cc file Map executed javascript in isolated world to url Some 💅 Documentation Use WebSource[] as argument in executeJavaScriptInIsolatedWorld Refactor and lint with @poiru’s comments Remove duplicate call Typo Lint
1 parent 389edb6 commit ae7b969

4 files changed

Lines changed: 83 additions & 14 deletions

File tree

atom/renderer/api/atom_api_web_frame.cc

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace mate {
3333
template <>
3434
struct Converter<blink::WebLocalFrame::ScriptExecutionType> {
3535
static bool FromV8(v8::Isolate* isolate,
36-
v8::Handle<v8::Value> val,
36+
v8::Local<v8::Value> val,
3737
blink::WebLocalFrame::ScriptExecutionType* out) {
3838
std::string execution_type;
3939
if (!ConvertFromV8(isolate, val, &execution_type))
@@ -51,6 +51,7 @@ struct Converter<blink::WebLocalFrame::ScriptExecutionType> {
5151
return true;
5252
}
5353
};
54+
5455
} // namespace mate
5556

5657
namespace atom {
@@ -251,17 +252,34 @@ void WebFrame::ExecuteJavaScript(const base::string16& code,
251252
callback.release());
252253
}
253254

254-
void WebFrame::ExecuteJavaScriptInIsolatedWorld(int world_id,
255-
const base::string16& code,
256-
mate::Arguments* args) {
255+
void WebFrame::ExecuteJavaScriptInIsolatedWorld(
256+
int world_id,
257+
const std::vector<mate::Dictionary>& scripts,
258+
mate::Arguments* args) {
257259
std::vector<blink::WebScriptSource> sources;
258-
sources.push_back(blink::WebScriptSource(blink::WebString::FromUTF16(code)));
260+
261+
for (const auto& script : scripts) {
262+
base::string16 code;
263+
base::string16 url;
264+
int start_line = 1;
265+
script.Get("url", &url);
266+
script.Get("startLine", &start_line);
267+
268+
if (!script.Get("code", &code)) {
269+
args->ThrowError("Invalid 'code'");
270+
return;
271+
}
272+
273+
sources.emplace_back(blink::WebScriptSource(
274+
blink::WebString::FromUTF16(code),
275+
blink::WebURL(GURL(url)), start_line));
276+
}
259277

260278
bool has_user_gesture = false;
261279
args->GetNext(&has_user_gesture);
262280

263281
blink::WebLocalFrame::ScriptExecutionType scriptExecutionType =
264-
blink::WebLocalFrame::kSynchronous;
282+
blink::WebLocalFrame::kSynchronous;
265283
args->GetNext(&scriptExecutionType);
266284

267285
ScriptExecutionCallback::CompletionCallback completion_callback;
@@ -274,6 +292,14 @@ void WebFrame::ExecuteJavaScriptInIsolatedWorld(int world_id,
274292
scriptExecutionType, callback.release());
275293
}
276294

295+
void WebFrame::SetIsolatedWorldSecurityOrigin(int world_id,
296+
const std::string& origin_url) {
297+
web_frame_->SetIsolatedWorldSecurityOrigin(
298+
world_id,
299+
blink::WebSecurityOrigin::CreateFromString(
300+
blink::WebString::FromUTF8(origin_url)));
301+
}
302+
277303
void WebFrame::SetIsolatedWorldContentSecurityPolicy(int world_id,
278304
const std::string& security_policy) {
279305
web_frame_->SetIsolatedWorldContentSecurityPolicy(
@@ -337,6 +363,8 @@ void WebFrame::BuildPrototype(
337363
.SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript)
338364
.SetMethod("executeJavaScriptInIsolatedWorld",
339365
&WebFrame::ExecuteJavaScriptInIsolatedWorld)
366+
.SetMethod("setIsolatedWorldSecurityOrigin",
367+
&WebFrame::SetIsolatedWorldSecurityOrigin)
340368
.SetMethod("setIsolatedWorldContentSecurityPolicy",
341369
&WebFrame::SetIsolatedWorldContentSecurityPolicy)
342370
.SetMethod("setIsolatedWorldHumanReadableName",

atom/renderer/api/atom_api_web_frame.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class WebLocalFrame;
1919
}
2020

2121
namespace mate {
22+
class Dictionary;
2223
class Arguments;
2324
}
2425

@@ -72,15 +73,19 @@ class WebFrame : public mate::Wrappable<WebFrame> {
7273
void InsertText(const std::string& text);
7374
void InsertCSS(const std::string& css);
7475

75-
// Excecuting scripts.
76+
// Executing scripts.
7677
void ExecuteJavaScript(const base::string16& code, mate::Arguments* args);
77-
void ExecuteJavaScriptInIsolatedWorld(int world_id,
78-
const base::string16& code,
79-
mate::Arguments* args);
80-
81-
void SetIsolatedWorldContentSecurityPolicy(int world_id,
82-
const std::string& security_policy);
83-
78+
void ExecuteJavaScriptInIsolatedWorld(
79+
int world_id,
80+
const std::vector<mate::Dictionary>& scripts,
81+
mate::Arguments* args);
82+
83+
// Isolated world related methods
84+
void SetIsolatedWorldSecurityOrigin(int world_id,
85+
const std::string& origin_url);
86+
void SetIsolatedWorldContentSecurityPolicy(
87+
int world_id,
88+
const std::string& security_policy);
8489
void SetIsolatedWorldHumanReadableName(int world_id,
8590
const std::string& name);
8691

docs/api/structures/web-source.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# WebSource Object
2+
3+
* `code` String
4+
* `url` String (optional)
5+
* `startLine` Integer (optional) - Default is 1.

docs/api/web-frame.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,37 @@ In the browser window some HTML APIs like `requestFullScreen` can only be
145145
invoked by a gesture from the user. Setting `userGesture` to `true` will remove
146146
this limitation.
147147

148+
### `webFrame.executeJavaScriptInIsolatedWorld(worldId, scripts[, userGesture, callback])`
149+
150+
* `worldId` Integer
151+
* `scripts` [WebSource[]](structures/web-source.md)
152+
* `userGesture` Boolean (optional) - Default is `false`.
153+
* `callback` Function (optional) - Called after script has been executed.
154+
* `result` Any
155+
156+
Work like `executeJavaScript` but evaluates `scripts` in isolated context.
157+
158+
### `webFrame.setIsolatedWorldContentSecurityPolicy(worldId, csp)`
159+
160+
* `worldId` Integer
161+
* `csp` String
162+
163+
Set the content security policy of the isolated world.
164+
165+
### `webFrame.setIsolatedWorldHumanReadableName(worldId, name)`
166+
167+
* `worldId` Integer
168+
* `name` String
169+
170+
Set the name of the isolated world. Useful in devtools.
171+
172+
### `webFrame.setIsolatedWorldSecurityOrigin(worldId, securityOrigin)`
173+
174+
* `worldId` Integer
175+
* `securityOrigin` String
176+
177+
Set the security origin of the isolated world.
178+
148179
### `webFrame.getResourceUsage()`
149180

150181
Returns `Object`:

0 commit comments

Comments
 (0)