2017-03-31 13:01:33 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/renderer/atom_render_frame_observer.h"
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
#include <string>
|
2017-08-31 16:51:40 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "atom/common/api/api_messages.h"
|
2018-03-09 03:22:44 +00:00
|
|
|
#include "atom/common/api/event_emitter_caller.h"
|
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
|
|
|
#include "atom/common/node_includes.h"
|
2019-03-07 22:46:57 +00:00
|
|
|
#include "atom/common/options_switches.h"
|
|
|
|
#include "base/command_line.h"
|
2018-03-09 03:22:44 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/trace_event/trace_event.h"
|
2017-03-31 13:01:33 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2017-08-31 16:51:40 +00:00
|
|
|
#include "content/public/renderer/render_view.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "electron/atom/common/api/api.mojom.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "ipc/ipc_message_macros.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "mojo/public/cpp/bindings/associated_binding.h"
|
2018-03-09 03:22:44 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "net/base/net_module.h"
|
|
|
|
#include "net/grit/net_resources.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
2019-01-21 20:45:15 +00:00
|
|
|
#include "third_party/blink/public/platform/web_isolated_world_info.h"
|
2018-09-18 18:00:31 +00:00
|
|
|
#include "third_party/blink/public/web/blink.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
|
|
#include "third_party/blink/public/web/web_draggable_region.h"
|
|
|
|
#include "third_party/blink/public/web/web_element.h"
|
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
|
|
#include "third_party/blink/public/web/web_script_source.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
2017-03-31 13:01:33 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
base::StringPiece NetResourceProvider(int key) {
|
|
|
|
if (key == IDR_DIR_HEADER_HTML) {
|
|
|
|
base::StringPiece html_data =
|
|
|
|
ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
|
|
|
|
IDR_DIR_HEADER_HTML);
|
|
|
|
return html_data;
|
|
|
|
}
|
|
|
|
return base::StringPiece();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-03-31 13:01:33 +00:00
|
|
|
AtomRenderFrameObserver::AtomRenderFrameObserver(
|
|
|
|
content::RenderFrame* frame,
|
2017-04-08 14:27:19 +00:00
|
|
|
RendererClientBase* renderer_client)
|
2018-03-09 09:31:09 +00:00
|
|
|
: content::RenderFrameObserver(frame),
|
|
|
|
render_frame_(frame),
|
2018-05-21 22:18:38 +00:00
|
|
|
renderer_client_(renderer_client) {
|
2018-03-09 09:31:09 +00:00
|
|
|
// Initialise resource for directory listing.
|
|
|
|
net::NetModule::SetResourceProvider(NetResourceProvider);
|
|
|
|
}
|
2017-03-31 13:01:33 +00:00
|
|
|
|
|
|
|
void AtomRenderFrameObserver::DidClearWindowObject() {
|
|
|
|
renderer_client_->DidClearWindowObject(render_frame_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomRenderFrameObserver::DidCreateScriptContext(
|
|
|
|
v8::Handle<v8::Context> context,
|
|
|
|
int world_id) {
|
|
|
|
if (ShouldNotifyClient(world_id))
|
|
|
|
renderer_client_->DidCreateScriptContext(context, render_frame_);
|
|
|
|
|
2019-03-07 22:46:57 +00:00
|
|
|
bool use_context_isolation = renderer_client_->isolated_world();
|
|
|
|
bool is_main_world = IsMainWorld(world_id);
|
|
|
|
bool is_main_frame = render_frame_->IsMainFrame();
|
|
|
|
bool is_not_opened = !render_frame_->GetWebFrame()->Opener();
|
|
|
|
bool allow_node_in_sub_frames =
|
|
|
|
base::CommandLine::ForCurrentProcess()->HasSwitch(
|
|
|
|
switches::kNodeIntegrationInSubFrames);
|
|
|
|
bool should_create_isolated_context =
|
|
|
|
use_context_isolation && is_main_world &&
|
|
|
|
(is_main_frame || allow_node_in_sub_frames) && is_not_opened;
|
|
|
|
|
|
|
|
if (should_create_isolated_context) {
|
2017-03-31 13:01:33 +00:00
|
|
|
CreateIsolatedWorldContext();
|
2018-12-13 21:25:12 +00:00
|
|
|
renderer_client_->SetupMainWorldOverrides(context, render_frame_);
|
2017-03-31 13:01:33 +00:00
|
|
|
}
|
2019-03-11 23:27:57 +00:00
|
|
|
|
|
|
|
if (world_id >= World::ISOLATED_WORLD_EXTENSIONS &&
|
|
|
|
world_id <= World::ISOLATED_WORLD_EXTENSIONS_END) {
|
|
|
|
renderer_client_->SetupExtensionWorldOverrides(context, render_frame_,
|
|
|
|
world_id);
|
|
|
|
}
|
2017-03-31 13:01:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:51:40 +00:00
|
|
|
void AtomRenderFrameObserver::DraggableRegionsChanged() {
|
|
|
|
blink::WebVector<blink::WebDraggableRegion> webregions =
|
|
|
|
render_frame_->GetWebFrame()->GetDocument().DraggableRegions();
|
|
|
|
std::vector<DraggableRegion> regions;
|
|
|
|
for (auto& webregion : webregions) {
|
|
|
|
DraggableRegion region;
|
|
|
|
render_frame_->GetRenderView()->ConvertViewportToWindowViaWidget(
|
|
|
|
&webregion.bounds);
|
|
|
|
region.bounds = webregion.bounds;
|
|
|
|
region.draggable = webregion.draggable;
|
|
|
|
regions.push_back(region);
|
|
|
|
}
|
2018-01-01 17:24:02 +00:00
|
|
|
Send(new AtomFrameHostMsg_UpdateDraggableRegions(routing_id(), regions));
|
2017-08-31 16:51:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:01:33 +00:00
|
|
|
void AtomRenderFrameObserver::WillReleaseScriptContext(
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
int world_id) {
|
|
|
|
if (ShouldNotifyClient(world_id))
|
|
|
|
renderer_client_->WillReleaseScriptContext(context, render_frame_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomRenderFrameObserver::OnDestruct() {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomRenderFrameObserver::CreateIsolatedWorldContext() {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* frame = render_frame_->GetWebFrame();
|
2019-01-21 20:45:15 +00:00
|
|
|
blink::WebIsolatedWorldInfo info;
|
2017-03-31 13:01:33 +00:00
|
|
|
// This maps to the name shown in the context combo box in the Console tab
|
|
|
|
// of the dev tools.
|
2019-01-21 20:45:15 +00:00
|
|
|
info.human_readable_name =
|
|
|
|
blink::WebString::FromUTF8("Electron Isolated Context");
|
2017-03-31 13:01:33 +00:00
|
|
|
// Setup document's origin policy in isolated world
|
2019-01-21 20:45:15 +00:00
|
|
|
info.security_origin = frame->GetDocument().GetSecurityOrigin();
|
|
|
|
frame->SetIsolatedWorldInfo(World::ISOLATED_WORLD, info);
|
2017-03-31 13:01:33 +00:00
|
|
|
|
|
|
|
// Create initial script context in isolated world
|
|
|
|
blink::WebScriptSource source("void 0");
|
2018-09-15 00:21:19 +00:00
|
|
|
frame->ExecuteScriptInIsolatedWorld(World::ISOLATED_WORLD, source);
|
2017-03-31 13:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomRenderFrameObserver::IsMainWorld(int world_id) {
|
|
|
|
return world_id == World::MAIN_WORLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomRenderFrameObserver::IsIsolatedWorld(int world_id) {
|
|
|
|
return world_id == World::ISOLATED_WORLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomRenderFrameObserver::ShouldNotifyClient(int world_id) {
|
2019-03-07 22:46:57 +00:00
|
|
|
bool allow_node_in_sub_frames =
|
|
|
|
base::CommandLine::ForCurrentProcess()->HasSwitch(
|
|
|
|
switches::kNodeIntegrationInSubFrames);
|
|
|
|
if (renderer_client_->isolated_world() &&
|
|
|
|
(render_frame_->IsMainFrame() || allow_node_in_sub_frames))
|
2017-03-31 13:01:33 +00:00
|
|
|
return IsIsolatedWorld(world_id);
|
|
|
|
else
|
|
|
|
return IsMainWorld(world_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|