|
@@ -16,16 +16,15 @@
|
|
|
#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/atom_render_view_observer.h"
|
|
|
#include "atom/renderer/node_array_buffer_bridge.h"
|
|
|
#include "atom/renderer/web_worker_observer.h"
|
|
|
#include "base/command_line.h"
|
|
|
#include "content/public/renderer/render_frame.h"
|
|
|
-#include "content/public/renderer/render_frame_observer.h"
|
|
|
#include "native_mate/dictionary.h"
|
|
|
#include "third_party/WebKit/public/web/WebDocument.h"
|
|
|
#include "third_party/WebKit/public/web/WebLocalFrame.h"
|
|
|
-#include "third_party/WebKit/public/web/WebScriptSource.h"
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
@@ -33,127 +32,6 @@ namespace atom {
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
-enum World {
|
|
|
- MAIN_WORLD = 0,
|
|
|
- // Use a high number far away from 0 to not collide with any other world
|
|
|
- // IDs created internally by Chrome.
|
|
|
- ISOLATED_WORLD = 999
|
|
|
-};
|
|
|
-
|
|
|
-enum ExtensionGroup {
|
|
|
- MAIN_GROUP = 1
|
|
|
-};
|
|
|
-
|
|
|
-// Helper class to forward the messages to the client.
|
|
|
-class AtomRenderFrameObserver : public content::RenderFrameObserver {
|
|
|
- public:
|
|
|
- AtomRenderFrameObserver(content::RenderFrame* frame,
|
|
|
- AtomRendererClient* renderer_client)
|
|
|
- : content::RenderFrameObserver(frame),
|
|
|
- render_frame_(frame),
|
|
|
- renderer_client_(renderer_client) {}
|
|
|
-
|
|
|
- // content::RenderFrameObserver:
|
|
|
- void DidClearWindowObject() override {
|
|
|
- renderer_client_->DidClearWindowObject(render_frame_);
|
|
|
- }
|
|
|
-
|
|
|
- void CreateIsolatedWorldContext() {
|
|
|
- auto frame = render_frame_->GetWebFrame();
|
|
|
-
|
|
|
- // This maps to the name shown in the context combo box in the Console tab
|
|
|
- // of the dev tools.
|
|
|
- frame->setIsolatedWorldHumanReadableName(
|
|
|
- World::ISOLATED_WORLD,
|
|
|
- blink::WebString::fromUTF8("Electron Isolated Context"));
|
|
|
-
|
|
|
- // Setup document's origin policy in isolated world
|
|
|
- frame->setIsolatedWorldSecurityOrigin(
|
|
|
- World::ISOLATED_WORLD, frame->document().getSecurityOrigin());
|
|
|
-
|
|
|
- // Create initial script context in isolated world
|
|
|
- blink::WebScriptSource source("void 0");
|
|
|
- frame->executeScriptInIsolatedWorld(
|
|
|
- World::ISOLATED_WORLD, &source, 1, ExtensionGroup::MAIN_GROUP);
|
|
|
- }
|
|
|
-
|
|
|
- void SetupMainWorldOverrides(v8::Handle<v8::Context> context) {
|
|
|
- // Setup window overrides in the main world context
|
|
|
- v8::Isolate* isolate = context->GetIsolate();
|
|
|
-
|
|
|
- // Wrap the bundle into a function that receives the binding object as
|
|
|
- // an argument.
|
|
|
- std::string bundle(node::isolated_bundle_data,
|
|
|
- node::isolated_bundle_data + sizeof(node::isolated_bundle_data));
|
|
|
- std::string wrapper = "(function (binding, require) {\n" + bundle + "\n})";
|
|
|
- auto script = v8::Script::Compile(
|
|
|
- mate::ConvertToV8(isolate, wrapper)->ToString());
|
|
|
- auto func = v8::Handle<v8::Function>::Cast(
|
|
|
- script->Run(context).ToLocalChecked());
|
|
|
-
|
|
|
- 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));
|
|
|
-
|
|
|
- v8::Local<v8::Value> args[] = { binding };
|
|
|
- ignore_result(func->Call(context, v8::Null(isolate), 1, args));
|
|
|
- }
|
|
|
-
|
|
|
- bool IsMainWorld(int world_id) {
|
|
|
- return world_id == World::MAIN_WORLD;
|
|
|
- }
|
|
|
-
|
|
|
- bool IsIsolatedWorld(int world_id) {
|
|
|
- return world_id == World::ISOLATED_WORLD;
|
|
|
- }
|
|
|
-
|
|
|
- bool ShouldNotifyClient(int world_id) {
|
|
|
- if (renderer_client_->isolated_world() && render_frame_->IsMainFrame())
|
|
|
- return IsIsolatedWorld(world_id);
|
|
|
- else
|
|
|
- return IsMainWorld(world_id);
|
|
|
- }
|
|
|
-
|
|
|
- void DidCreateScriptContext(v8::Handle<v8::Context> context,
|
|
|
- int extension_group,
|
|
|
- int world_id) override {
|
|
|
- if (ShouldNotifyClient(world_id))
|
|
|
- renderer_client_->DidCreateScriptContext(context, render_frame_);
|
|
|
-
|
|
|
- if (renderer_client_->isolated_world() && IsMainWorld(world_id)
|
|
|
- && render_frame_->IsMainFrame()) {
|
|
|
- CreateIsolatedWorldContext();
|
|
|
- SetupMainWorldOverrides(context);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void WillReleaseScriptContext(v8::Local<v8::Context> context,
|
|
|
- int world_id) override {
|
|
|
- if (ShouldNotifyClient(world_id))
|
|
|
- renderer_client_->WillReleaseScriptContext(context, render_frame_);
|
|
|
- }
|
|
|
-
|
|
|
- void OnDestruct() override {
|
|
|
- delete this;
|
|
|
- }
|
|
|
-
|
|
|
- private:
|
|
|
- content::RenderFrame* render_frame_;
|
|
|
- AtomRendererClient* renderer_client_;
|
|
|
-
|
|
|
- DISALLOW_COPY_AND_ASSIGN(AtomRenderFrameObserver);
|
|
|
-};
|
|
|
-
|
|
|
bool IsDevToolsExtension(content::RenderFrame* render_frame) {
|
|
|
return static_cast<GURL>(render_frame->GetWebFrame()->document().url())
|
|
|
.SchemeIs("chrome-extension");
|
|
@@ -180,7 +58,6 @@ void AtomRendererClient::RenderThreadStarted() {
|
|
|
|
|
|
void AtomRendererClient::RenderFrameCreated(
|
|
|
content::RenderFrame* render_frame) {
|
|
|
- new AtomRenderFrameObserver(render_frame, this);
|
|
|
RendererClientBase::RenderFrameCreated(render_frame);
|
|
|
}
|
|
|
|
|
@@ -189,12 +66,6 @@ void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
|
|
|
RendererClientBase::RenderViewCreated(render_view);
|
|
|
}
|
|
|
|
|
|
-void AtomRendererClient::DidClearWindowObject(
|
|
|
- content::RenderFrame* render_frame) {
|
|
|
- // Make sure every page will get a script context created.
|
|
|
- render_frame->GetWebFrame()->executeScript(blink::WebScriptSource("void 0"));
|
|
|
-}
|
|
|
-
|
|
|
void AtomRendererClient::RunScriptsAtDocumentStart(
|
|
|
content::RenderFrame* render_frame) {
|
|
|
// Inform the document start pharse.
|
|
@@ -307,4 +178,38 @@ v8::Local<v8::Context> AtomRendererClient::GetContext(
|
|
|
return frame->mainWorldScriptContext();
|
|
|
}
|
|
|
|
|
|
+void AtomRendererClient::SetupMainWorldOverrides(
|
|
|
+ v8::Handle<v8::Context> context) {
|
|
|
+ // Setup window overrides in the main world context
|
|
|
+ v8::Isolate* isolate = context->GetIsolate();
|
|
|
+
|
|
|
+ // Wrap the bundle into a function that receives the binding object as
|
|
|
+ // an argument.
|
|
|
+ std::string bundle(node::isolated_bundle_data,
|
|
|
+ node::isolated_bundle_data + sizeof(node::isolated_bundle_data));
|
|
|
+ std::string wrapper = "(function (binding, require) {\n" + bundle + "\n})";
|
|
|
+ auto script = v8::Script::Compile(
|
|
|
+ mate::ConvertToV8(isolate, wrapper)->ToString());
|
|
|
+ auto func = v8::Handle<v8::Function>::Cast(
|
|
|
+ script->Run(context).ToLocalChecked());
|
|
|
+
|
|
|
+ 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));
|
|
|
+
|
|
|
+ v8::Local<v8::Value> args[] = { binding };
|
|
|
+ ignore_result(func->Call(context, v8::Null(isolate), 1, args));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
} // namespace atom
|