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.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/renderer/electron_render_frame_observer.h"
|
2017-03-31 13:01:33 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
#include <string>
|
2019-06-03 17:43:04 +00:00
|
|
|
#include <utility>
|
2017-08-31 16:51:40 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-03-07 22:46:57 +00:00
|
|
|
#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-07-31 21:25:41 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "electron/shell/common/api/api.mojom.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "ipc/ipc_message_macros.h"
|
|
|
|
#include "net/base/net_module.h"
|
|
|
|
#include "net/grit/net_resources.h"
|
2019-06-03 17:43:04 +00:00
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/options_switches.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
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-03-31 13:01:33 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
scoped_refptr<base::RefCountedMemory> NetResourceProvider(int key) {
|
2018-03-09 09:31:09 +00:00
|
|
|
if (key == IDR_DIR_HEADER_HTML) {
|
2019-10-28 22:12:35 +00:00
|
|
|
return ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
|
|
|
|
IDR_DIR_HEADER_HTML);
|
2018-03-09 09:31:09 +00:00
|
|
|
}
|
2019-10-28 22:12:35 +00:00
|
|
|
return nullptr;
|
2018-03-09 09:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronRenderFrameObserver::ElectronRenderFrameObserver(
|
2017-03-31 13:01:33 +00:00
|
|
|
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
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronRenderFrameObserver::DidClearWindowObject() {
|
2017-03-31 13:01:33 +00:00
|
|
|
renderer_client_->DidClearWindowObject(render_frame_);
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:45:13 +00:00
|
|
|
void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
|
2017-03-31 13:01:33 +00:00
|
|
|
v8::Handle<v8::Context> context,
|
|
|
|
int world_id) {
|
|
|
|
if (ShouldNotifyClient(world_id))
|
|
|
|
renderer_client_->DidCreateScriptContext(context, render_frame_);
|
|
|
|
|
2019-08-05 19:50:51 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
|
2019-03-07 22:46:57 +00:00
|
|
|
bool use_context_isolation = renderer_client_->isolated_world();
|
2020-02-14 11:25:39 +00:00
|
|
|
// This logic matches the EXPLAINED logic in electron_renderer_client.cc
|
2019-08-05 19:50:51 +00:00
|
|
|
// to avoid explaining it twice go check that implementation in
|
|
|
|
// DidCreateScriptContext();
|
2019-03-07 22:46:57 +00:00
|
|
|
bool is_main_world = IsMainWorld(world_id);
|
|
|
|
bool is_main_frame = render_frame_->IsMainFrame();
|
2019-08-05 19:50:51 +00:00
|
|
|
bool reuse_renderer_processes_enabled =
|
|
|
|
command_line->HasSwitch(switches::kDisableElectronSiteInstanceOverrides);
|
|
|
|
bool is_not_opened =
|
|
|
|
!render_frame_->GetWebFrame()->Opener() ||
|
|
|
|
command_line->HasSwitch(switches::kEnableNodeLeakageInRenderers);
|
2019-03-07 22:46:57 +00:00
|
|
|
bool allow_node_in_sub_frames =
|
2019-08-05 19:50:51 +00:00
|
|
|
command_line->HasSwitch(switches::kNodeIntegrationInSubFrames);
|
2019-03-07 22:46:57 +00:00
|
|
|
bool should_create_isolated_context =
|
|
|
|
use_context_isolation && is_main_world &&
|
2019-08-05 19:50:51 +00:00
|
|
|
(is_main_frame || allow_node_in_sub_frames) &&
|
|
|
|
(is_not_opened || reuse_renderer_processes_enabled);
|
2019-03-07 22:46:57 +00:00
|
|
|
|
|
|
|
if (should_create_isolated_context) {
|
2017-03-31 13:01:33 +00:00
|
|
|
CreateIsolatedWorldContext();
|
2019-07-17 00:13:05 +00:00
|
|
|
if (!renderer_client_->IsWebViewFrame(context, render_frame_))
|
|
|
|
renderer_client_->SetupMainWorldOverrides(context, render_frame_);
|
2017-03-31 13:01:33 +00:00
|
|
|
}
|
2019-03-11 23:27:57 +00:00
|
|
|
|
2019-07-31 21:25:41 +00:00
|
|
|
#if !BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
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);
|
|
|
|
}
|
2019-07-31 21:25:41 +00:00
|
|
|
#endif
|
2017-03-31 13:01:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronRenderFrameObserver::DraggableRegionsChanged() {
|
2017-08-31 16:51:40 +00:00
|
|
|
blink::WebVector<blink::WebDraggableRegion> webregions =
|
|
|
|
render_frame_->GetWebFrame()->GetDocument().DraggableRegions();
|
2019-06-03 17:43:04 +00:00
|
|
|
std::vector<mojom::DraggableRegionPtr> regions;
|
2017-08-31 16:51:40 +00:00
|
|
|
for (auto& webregion : webregions) {
|
2019-06-03 17:43:04 +00:00
|
|
|
auto region = mojom::DraggableRegion::New();
|
2019-10-28 22:12:35 +00:00
|
|
|
render_frame_->ConvertViewportToWindow(&webregion.bounds);
|
2019-06-03 17:43:04 +00:00
|
|
|
region->bounds = webregion.bounds;
|
|
|
|
region->draggable = webregion.draggable;
|
|
|
|
regions.push_back(std::move(region));
|
2017-08-31 16:51:40 +00:00
|
|
|
}
|
2019-06-03 17:43:04 +00:00
|
|
|
|
|
|
|
mojom::ElectronBrowserPtr browser_ptr;
|
|
|
|
render_frame_->GetRemoteInterfaces()->GetInterface(
|
|
|
|
mojo::MakeRequest(&browser_ptr));
|
|
|
|
browser_ptr->UpdateDraggableRegions(std::move(regions));
|
2017-08-31 16:51:40 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronRenderFrameObserver::WillReleaseScriptContext(
|
2017-03-31 13:01:33 +00:00
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
int world_id) {
|
|
|
|
if (ShouldNotifyClient(world_id))
|
|
|
|
renderer_client_->WillReleaseScriptContext(context, render_frame_);
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronRenderFrameObserver::OnDestruct() {
|
2017-03-31 13:01:33 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronRenderFrameObserver::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
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronRenderFrameObserver::IsMainWorld(int world_id) {
|
2017-03-31 13:01:33 +00:00
|
|
|
return world_id == World::MAIN_WORLD;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronRenderFrameObserver::IsIsolatedWorld(int world_id) {
|
2017-03-31 13:01:33 +00:00
|
|
|
return world_id == World::ISOLATED_WORLD;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronRenderFrameObserver::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);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|