chore: simplify main world setup of contextIsolation (#16046)

This commit is contained in:
Cheng Zhao 2018-12-14 06:25:12 +09:00 коммит произвёл John Kleinschmidt
Родитель d6d245d0c3
Коммит 897c6e93cb
7 изменённых файлов: 34 добавлений и 49 удалений

Просмотреть файл

@ -96,9 +96,10 @@ void AtomRenderFrameObserver::DidCreateScriptContext(
renderer_client_->DidCreateScriptContext(context, render_frame_);
if (renderer_client_->isolated_world() && IsMainWorld(world_id) &&
render_frame_->IsMainFrame()) {
// Only the top window's main frame has isolated world.
render_frame_->IsMainFrame() && !render_frame_->GetWebFrame()->Opener()) {
CreateIsolatedWorldContext();
renderer_client_->SetupMainWorldOverrides(context);
renderer_client_->SetupMainWorldOverrides(context, render_frame_);
}
}

Просмотреть файл

@ -12,7 +12,6 @@
#include "atom/common/asar/asar_util.h"
#include "atom/common/node_bindings.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/api/atom_api_renderer_ipc.h"
#include "atom/renderer/atom_render_frame_observer.h"
#include "atom/renderer/web_worker_observer.h"
#include "base/command_line.h"
@ -187,15 +186,16 @@ void AtomRendererClient::WillDestroyWorkerContextOnWorkerThread(
}
void AtomRendererClient::SetupMainWorldOverrides(
v8::Handle<v8::Context> context) {
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) {
// Setup window overrides in the main world context
v8::Isolate* isolate = context->GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
// Wrap the bundle into a function that receives the binding object as
// Wrap the bundle into a function that receives the isolatedWorld as
// an argument.
std::string left = "(function (binding, require) {\n";
std::string left = "(function (nodeProcess, isolatedWorld) {\n";
std::string right = "\n})";
auto source = v8::String::Concat(
isolate, mate::ConvertToV8(isolate, left)->ToString(isolate),
@ -203,28 +203,14 @@ void AtomRendererClient::SetupMainWorldOverrides(
node::isolated_bundle_value.ToStringChecked(isolate),
mate::ConvertToV8(isolate, right)->ToString(isolate)));
auto result = RunScript(context, source);
DCHECK(result->IsFunction());
auto binding = v8::Object::New(isolate);
api::Initialize(binding, v8::Null(isolate), context, nullptr);
// Pass in CLI flags needed to setup window
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
mate::Dictionary dict(isolate, binding);
if (command_line->HasSwitch(switches::kGuestInstanceID))
dict.Set(options::kGuestInstanceID,
command_line->GetSwitchValueASCII(switches::kGuestInstanceID));
if (command_line->HasSwitch(switches::kOpenerID))
dict.Set(options::kOpenerID,
command_line->GetSwitchValueASCII(switches::kOpenerID));
dict.Set("hiddenPage", command_line->HasSwitch(switches::kHiddenPage));
dict.Set(options::kNativeWindowOpen,
command_line->HasSwitch(switches::kNativeWindowOpen));
v8::Local<v8::Value> args[] = {binding};
ignore_result(
result.As<v8::Function>()->Call(context, v8::Null(isolate), 1, args));
v8::Local<v8::Value> args[] = {
GetEnvironment(render_frame)->process_object(),
GetContext(render_frame->GetWebFrame(), isolate)->Global(),
};
ignore_result(result.As<v8::Function>()->Call(context, v8::Null(isolate),
node::arraysize(args), args));
}
node::Environment* AtomRendererClient::GetEnvironment(

Просмотреть файл

@ -31,7 +31,8 @@ class AtomRendererClient : public RendererClientBase {
content::RenderFrame* render_frame) override;
void WillReleaseScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) override;
void SetupMainWorldOverrides(v8::Handle<v8::Context> context) override;
void SetupMainWorldOverrides(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) override;
private:
enum NodeIntegration {

Просмотреть файл

@ -28,7 +28,8 @@ class AtomSandboxedRendererClient : public RendererClientBase {
content::RenderFrame* render_frame) override;
void WillReleaseScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) override;
void SetupMainWorldOverrides(v8::Handle<v8::Context> context) override {}
void SetupMainWorldOverrides(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) override {}
// content::ContentRendererClient:
void RenderFrameCreated(content::RenderFrame*) override;
void RenderViewCreated(content::RenderView*) override;

Просмотреть файл

@ -32,7 +32,8 @@ class RendererClientBase : public content::ContentRendererClient {
virtual void WillReleaseScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) = 0;
virtual void DidClearWindowObject(content::RenderFrame* render_frame);
virtual void SetupMainWorldOverrides(v8::Handle<v8::Context> context) = 0;
virtual void SetupMainWorldOverrides(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) = 0;
bool isolated_world() const { return isolated_world_; }

Просмотреть файл

@ -1,25 +1,11 @@
'use strict'
/* global binding */
/* global nodeProcess, isolatedWorld */
const { send, sendSync } = binding
// Note: Don't use "process", as it will be replaced by browserify's one.
const v8Util = nodeProcess.atomBinding('v8_util')
const ipcRenderer = {
send (...args) {
return send('ipc-internal-message', args)
},
const isolatedWorldArgs = v8Util.getHiddenValue(isolatedWorld, 'isolated-world-args')
const { ipcRenderer, guestInstanceId, hiddenPage, openerId, usesNativeWindowOpen } = isolatedWorldArgs
sendSync (...args) {
return sendSync('ipc-internal-message-sync', args)[0]
},
// No-ops since events aren't received
on () {},
once () {}
}
let { guestInstanceId, hiddenPage, openerId, nativeWindowOpen } = binding
if (guestInstanceId != null) guestInstanceId = parseInt(guestInstanceId)
if (openerId != null) openerId = parseInt(openerId)
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, hiddenPage, nativeWindowOpen)
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, hiddenPage, usesNativeWindowOpen)

Просмотреть файл

@ -33,6 +33,7 @@ require('@electron/internal/renderer/web-frame-init')()
// Process command line arguments.
let nodeIntegration = false
let webviewTag = false
let contextIsolation = false
let preloadScript = null
let preloadScripts = []
let isBackgroundPage = false
@ -56,6 +57,8 @@ for (const arg of process.argv) {
appPath = arg.substr(arg.indexOf('=') + 1)
} else if (arg.indexOf('--webview-tag=') === 0) {
webviewTag = arg.substr(arg.indexOf('=') + 1) === 'true'
} else if (arg === '--context-isolation') {
contextIsolation = true
} else if (arg.indexOf('--preload-scripts') === 0) {
preloadScripts = arg.substr(arg.indexOf('=') + 1).split(path.delimiter)
}
@ -69,6 +72,12 @@ if (preloadScript) {
preloadScripts.push(preloadScript)
}
// Pass the arguments to isolatedWorld.
if (contextIsolation) {
const isolatedWorldArgs = { ipcRenderer, guestInstanceId, hiddenPage, openerId, usesNativeWindowOpen }
v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs)
}
if (window.location.protocol === 'chrome-devtools:') {
// Override some inspector APIs.
require('@electron/internal/renderer/inspector')