electron/shell/renderer/electron_render_frame_observer.cc

177 lines
6.4 KiB
C++
Raw Normal View History

// 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 "shell/renderer/electron_render_frame_observer.h"
2018-03-09 09:31:09 +00:00
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/trace_event/trace_event.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "electron/buildflags/buildflags.h"
#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"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/common/options_switches.h"
#include "shell/common/world_ids.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/web/blink.h"
#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"
namespace electron {
2018-03-09 09:31:09 +00:00
namespace {
scoped_refptr<base::RefCountedMemory> NetResourceProvider(int key) {
2018-03-09 09:31:09 +00:00
if (key == IDR_DIR_HEADER_HTML) {
return ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
IDR_DIR_HEADER_HTML);
2018-03-09 09:31:09 +00:00
}
return nullptr;
2018-03-09 09:31:09 +00:00
}
} // namespace
ElectronRenderFrameObserver::ElectronRenderFrameObserver(
content::RenderFrame* frame,
RendererClientBase* renderer_client)
2018-03-09 09:31:09 +00:00
: content::RenderFrameObserver(frame),
render_frame_(frame),
renderer_client_(renderer_client) {
2018-03-09 09:31:09 +00:00
// Initialise resource for directory listing.
net::NetModule::SetResourceProvider(NetResourceProvider);
}
void ElectronRenderFrameObserver::DidClearWindowObject() {
renderer_client_->DidClearWindowObject(render_frame_);
}
void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
v8::Handle<v8::Context> context,
int world_id) {
if (ShouldNotifyClient(world_id))
renderer_client_->DidCreateScriptContext(context, render_frame_);
auto* command_line = base::CommandLine::ForCurrentProcess();
bool use_context_isolation = renderer_client_->isolated_world();
// This logic matches the EXPLAINED logic in electron_renderer_client.cc
// to avoid explaining it twice go check that implementation in
// DidCreateScriptContext();
bool is_main_world = IsMainWorld(world_id);
bool is_main_frame = render_frame_->IsMainFrame();
bool reuse_renderer_processes_enabled =
command_line->HasSwitch(switches::kDisableElectronSiteInstanceOverrides);
bool is_not_opened =
!render_frame_->GetWebFrame()->Opener() ||
command_line->HasSwitch(switches::kEnableNodeLeakageInRenderers);
bool allow_node_in_sub_frames =
command_line->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 || reuse_renderer_processes_enabled);
if (should_create_isolated_context) {
CreateIsolatedWorldContext();
if (!renderer_client_->IsWebViewFrame(context, render_frame_))
renderer_client_->SetupMainWorldOverrides(context, render_frame_);
}
#if !BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
if (world_id >= WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS &&
world_id <= WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS_END) {
renderer_client_->SetupExtensionWorldOverrides(context, render_frame_,
world_id);
}
#endif
}
void ElectronRenderFrameObserver::DraggableRegionsChanged() {
blink::WebVector<blink::WebDraggableRegion> webregions =
render_frame_->GetWebFrame()->GetDocument().DraggableRegions();
std::vector<mojom::DraggableRegionPtr> regions;
for (auto& webregion : webregions) {
auto region = mojom::DraggableRegion::New();
render_frame_->ConvertViewportToWindow(&webregion.bounds);
region->bounds = webregion.bounds;
region->draggable = webregion.draggable;
regions.push_back(std::move(region));
}
chore: bump chromium to a84d34e372b8fb5e9a94b1b4b447e (master) (#23908) * chore: bump chromium in DEPS to a7249f73ae05d456c04487ef1693325f719556dd * chore: bump chromium in DEPS to 202466fa40b58f0bb9c9a76a037d1c50154c099e * chore: bump chromium in DEPS to 2dd1b25c8d794b50fb0dd911e0c4e909ff39f145 * Update patches * update patches * Revert "[printing] Mojofy PrintHostMsg_CheckForCancel" https://chromium-review.googlesource.com/c/chromium/src/+/2226002 * chore: bump chromium in DEPS to 8c1542e7dd36854fdf4abd1a8021eeb65a6a2e2b * chore: bump chromium in DEPS to 078bc6d796334fb403acd8975b99d1c8ecd028e8 * chore: bump chromium in DEPS to d96e9f16ae852ec9dbd15bf17df3d440402413bb * update patches * chore: update patches * Use ExtensionSystem::is_ready() instead of ExtensionService::is_ready() https://chromium-review.googlesource.com/c/chromium/src/+/2207499 * Remove WebImeTextSpan https://chromium-review.googlesource.com/c/chromium/src/+/2225240 * Remove PDFAnnotations flag altogether. https://chromium-review.googlesource.com/c/chromium/src/+/2229317 * Rework find-from-selection so it's synchronous -- fixes flaky tests https://chromium-review.googlesource.com/c/chromium/src/+/2181570 * fixup! Revert "[printing] Mojofy PrintHostMsg_CheckForCancel" * chore: bump chromium in DEPS to a8a280835830c65145ed8573a9a09f36d3920418 * update sysroots * update patches * update patches * Take RFH as a parameter for DidUpdateFavicon/ManifestURL https://chromium-review.googlesource.com/c/chromium/src/+/2224745 * chore: bump chromium in DEPS to b6149cb5a5e32caf8eab67b97ef3072b72521ca8 * Update patches * Rename net::cookie_util::StripStatuses to StripAccessResults https://chromium-review.googlesource.com/c/chromium/src/+/2212697 * use net::CookieAccessResultList instead of net::CookieAccessResultList * fix mas_no_private_api patch https://chromium-review.googlesource.com/c/chromium/src/+/2230281 * chore: bump chromium in DEPS to a27feee1643d952e48f77c92d8c03aedea14b720 * update patches * fix: add new navigation state REUSE_SITE_INSTANCE To fix the new set of state checks added in https://chromium-review.googlesource.com/c/chromium/src/+/2215141 * chore: bump chromium in DEPS to ff4559a4c13d20888202474e4ab9917dbdad8a9a * update patches * Cleanup usages of old mojo types and remove unused code https://chromium-review.googlesource.com/c/chromium/src/+/2235699 * chore: bump chromium in DEPS to 05279845f76eb22900a8b0d1a11d4fd339a8e53b * chore: bump chromium in DEPS to 821558279767cffec90e3b5b947865f90089fed3 * chore: bump chromium in DEPS to 1aef04e6486be337d3dd820b2d64d6320a1b9c13 * chore: bump chromium in DEPS to dc86386e8fdd796a0f7577e91e42a7f8b7e9bc78 * chore: bump chromium in DEPS to 64f2360794f14643764092ba3e58e2ed8f9fee12 * chore: update patches * refactor: MessageLoop, you are terminated \o/ Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2246173 * refactor: plumb DownloadSchedule to DownloadItem Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2242202 * chore: fix variable typo in IPC * chore: s/BindPipeAndPassReceiver/BindNewPipeAndPassReceiver * chore: update patches * chore: XEvent becomes x11::Event Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2240355 * fixup! refactor: MessageLoop, you are terminated \o/ * fixup! chore: XEvent becomes x11::Event * build: update v8 headers * chore: fix windows build * chore: disable SameSite-by-default changes https://chromium-review.googlesource.com/c/chromium/src/+/2231445 * update printing.patch * chore: bump chromium DEPS to 9ae03ef8f7d4f6ac663f725bcfe70311987652f3 * Convert WidgetHostMsg_SelectionBoundsChanged/TextInputStateChanged https://chromium-review.googlesource.com/c/chromium/src/+/2243531 * chore: update v8 patches * [XProto] Replace usages of XID and ::Window with x11::Window https://chromium-review.googlesource.com/c/chromium/src/+/2249389 * Update VideoFrameMetadata to use base::Optionals https://chromium-review.googlesource.com/c/chromium/src/+/2231706 https://chromium-review.googlesource.com/c/chromium/src/+/2238361 * --disable-dev-shm-usage for gpu process crash * [v8] Allow for 4GB TypedArrays https://chromium-review.googlesource.com/c/v8/v8/+/2249668 * update lib_src_switch_buffer_kmaxlength_to_size_t.patch * disable app.getGPUInfo spec on linux * update patches Co-authored-by: John Kleinschmidt <jkleinsc@github.com> Co-authored-by: Electron Bot <anonymous@electronjs.org> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
2020-06-22 17:35:10 +00:00
mojo::Remote<mojom::ElectronBrowser> browser_remote;
render_frame_->GetRemoteInterfaces()->GetInterface(
chore: bump chromium to a84d34e372b8fb5e9a94b1b4b447e (master) (#23908) * chore: bump chromium in DEPS to a7249f73ae05d456c04487ef1693325f719556dd * chore: bump chromium in DEPS to 202466fa40b58f0bb9c9a76a037d1c50154c099e * chore: bump chromium in DEPS to 2dd1b25c8d794b50fb0dd911e0c4e909ff39f145 * Update patches * update patches * Revert "[printing] Mojofy PrintHostMsg_CheckForCancel" https://chromium-review.googlesource.com/c/chromium/src/+/2226002 * chore: bump chromium in DEPS to 8c1542e7dd36854fdf4abd1a8021eeb65a6a2e2b * chore: bump chromium in DEPS to 078bc6d796334fb403acd8975b99d1c8ecd028e8 * chore: bump chromium in DEPS to d96e9f16ae852ec9dbd15bf17df3d440402413bb * update patches * chore: update patches * Use ExtensionSystem::is_ready() instead of ExtensionService::is_ready() https://chromium-review.googlesource.com/c/chromium/src/+/2207499 * Remove WebImeTextSpan https://chromium-review.googlesource.com/c/chromium/src/+/2225240 * Remove PDFAnnotations flag altogether. https://chromium-review.googlesource.com/c/chromium/src/+/2229317 * Rework find-from-selection so it's synchronous -- fixes flaky tests https://chromium-review.googlesource.com/c/chromium/src/+/2181570 * fixup! Revert "[printing] Mojofy PrintHostMsg_CheckForCancel" * chore: bump chromium in DEPS to a8a280835830c65145ed8573a9a09f36d3920418 * update sysroots * update patches * update patches * Take RFH as a parameter for DidUpdateFavicon/ManifestURL https://chromium-review.googlesource.com/c/chromium/src/+/2224745 * chore: bump chromium in DEPS to b6149cb5a5e32caf8eab67b97ef3072b72521ca8 * Update patches * Rename net::cookie_util::StripStatuses to StripAccessResults https://chromium-review.googlesource.com/c/chromium/src/+/2212697 * use net::CookieAccessResultList instead of net::CookieAccessResultList * fix mas_no_private_api patch https://chromium-review.googlesource.com/c/chromium/src/+/2230281 * chore: bump chromium in DEPS to a27feee1643d952e48f77c92d8c03aedea14b720 * update patches * fix: add new navigation state REUSE_SITE_INSTANCE To fix the new set of state checks added in https://chromium-review.googlesource.com/c/chromium/src/+/2215141 * chore: bump chromium in DEPS to ff4559a4c13d20888202474e4ab9917dbdad8a9a * update patches * Cleanup usages of old mojo types and remove unused code https://chromium-review.googlesource.com/c/chromium/src/+/2235699 * chore: bump chromium in DEPS to 05279845f76eb22900a8b0d1a11d4fd339a8e53b * chore: bump chromium in DEPS to 821558279767cffec90e3b5b947865f90089fed3 * chore: bump chromium in DEPS to 1aef04e6486be337d3dd820b2d64d6320a1b9c13 * chore: bump chromium in DEPS to dc86386e8fdd796a0f7577e91e42a7f8b7e9bc78 * chore: bump chromium in DEPS to 64f2360794f14643764092ba3e58e2ed8f9fee12 * chore: update patches * refactor: MessageLoop, you are terminated \o/ Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2246173 * refactor: plumb DownloadSchedule to DownloadItem Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2242202 * chore: fix variable typo in IPC * chore: s/BindPipeAndPassReceiver/BindNewPipeAndPassReceiver * chore: update patches * chore: XEvent becomes x11::Event Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2240355 * fixup! refactor: MessageLoop, you are terminated \o/ * fixup! chore: XEvent becomes x11::Event * build: update v8 headers * chore: fix windows build * chore: disable SameSite-by-default changes https://chromium-review.googlesource.com/c/chromium/src/+/2231445 * update printing.patch * chore: bump chromium DEPS to 9ae03ef8f7d4f6ac663f725bcfe70311987652f3 * Convert WidgetHostMsg_SelectionBoundsChanged/TextInputStateChanged https://chromium-review.googlesource.com/c/chromium/src/+/2243531 * chore: update v8 patches * [XProto] Replace usages of XID and ::Window with x11::Window https://chromium-review.googlesource.com/c/chromium/src/+/2249389 * Update VideoFrameMetadata to use base::Optionals https://chromium-review.googlesource.com/c/chromium/src/+/2231706 https://chromium-review.googlesource.com/c/chromium/src/+/2238361 * --disable-dev-shm-usage for gpu process crash * [v8] Allow for 4GB TypedArrays https://chromium-review.googlesource.com/c/v8/v8/+/2249668 * update lib_src_switch_buffer_kmaxlength_to_size_t.patch * disable app.getGPUInfo spec on linux * update patches Co-authored-by: John Kleinschmidt <jkleinsc@github.com> Co-authored-by: Electron Bot <anonymous@electronjs.org> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
2020-06-22 17:35:10 +00:00
browser_remote.BindNewPipeAndPassReceiver());
browser_remote->UpdateDraggableRegions(std::move(regions));
}
void ElectronRenderFrameObserver::WillReleaseScriptContext(
v8::Local<v8::Context> context,
int world_id) {
if (ShouldNotifyClient(world_id))
renderer_client_->WillReleaseScriptContext(context, render_frame_);
}
void ElectronRenderFrameObserver::OnDestruct() {
delete this;
}
void ElectronRenderFrameObserver::DidMeaningfulLayout(
blink::WebMeaningfulLayout layout_type) {
if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
mojo::Remote<mojom::ElectronBrowser> browser_remote;
render_frame_->GetRemoteInterfaces()->GetInterface(
browser_remote.BindNewPipeAndPassReceiver());
browser_remote->OnFirstNonEmptyLayout();
}
}
void ElectronRenderFrameObserver::CreateIsolatedWorldContext() {
auto* frame = render_frame_->GetWebFrame();
blink::WebIsolatedWorldInfo info;
// This maps to the name shown in the context combo box in the Console tab
// of the dev tools.
info.human_readable_name =
blink::WebString::FromUTF8("Electron Isolated Context");
// Setup document's origin policy in isolated world
info.security_origin = frame->GetDocument().GetSecurityOrigin();
blink::SetIsolatedWorldInfo(WorldIDs::ISOLATED_WORLD_ID, info);
// Create initial script context in isolated world
blink::WebScriptSource source("void 0");
frame->ExecuteScriptInIsolatedWorld(WorldIDs::ISOLATED_WORLD_ID, source);
}
bool ElectronRenderFrameObserver::IsMainWorld(int world_id) {
return world_id == WorldIDs::MAIN_WORLD_ID;
}
bool ElectronRenderFrameObserver::IsIsolatedWorld(int world_id) {
return world_id == WorldIDs::ISOLATED_WORLD_ID;
}
bool ElectronRenderFrameObserver::ShouldNotifyClient(int world_id) {
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))
return IsIsolatedWorld(world_id);
else
return IsMainWorld(world_id);
}
} // namespace electron