X Tutup
Skip to content

Commit 8cc0435

Browse files
fix(extensions): set lowest isolated world id (electron#22212)
* fix(extensions): set lowest isolated world id * refactor: move world IDs into separate header file Several files are including electron_render_frame_observer.h just for the world IDs.
1 parent 68c6d53 commit 8cc0435

File tree

7 files changed

+45
-33
lines changed

7 files changed

+45
-33
lines changed

filenames.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ filenames = {
558558
"shell/common/skia_util.h",
559559
"shell/common/v8_value_converter.cc",
560560
"shell/common/v8_value_converter.h",
561+
"shell/common/world_ids.h",
561562
"shell/renderer/api/context_bridge/render_frame_context_bridge_store.cc",
562563
"shell/renderer/api/context_bridge/render_frame_context_bridge_store.h",
563564
"shell/renderer/api/electron_api_context_bridge.cc",

shell/common/world_ids.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2020 Samuel Maddock
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_COMMON_WORLD_IDS_H_
6+
#define SHELL_COMMON_WORLD_IDS_H_
7+
8+
#include "third_party/blink/public/platform/web_isolated_world_ids.h"
9+
10+
namespace electron {
11+
12+
enum WorldIDs : int32_t {
13+
MAIN_WORLD_ID = 0,
14+
15+
// Use a high number far away from 0 to not collide with any other world
16+
// IDs created internally by Chrome.
17+
ISOLATED_WORLD_ID = 999,
18+
19+
// Numbers for isolated worlds for extensions are set in
20+
// lib/renderer/content-script-injector.ts, and are greater than or equal to
21+
// this number, up to ISOLATED_WORLD_ID_EXTENSIONS_END.
22+
ISOLATED_WORLD_ID_EXTENSIONS = 1 << 20,
23+
24+
// Last valid isolated world ID.
25+
ISOLATED_WORLD_ID_EXTENSIONS_END =
26+
blink::IsolatedWorldId::kEmbedderWorldIdLimit - 1
27+
};
28+
29+
} // namespace electron
30+
31+
#endif // SHELL_COMMON_WORLD_IDS_H_

shell/renderer/api/electron_api_context_bridge.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "shell/common/gin_helper/dictionary.h"
2121
#include "shell/common/gin_helper/promise.h"
2222
#include "shell/common/node_includes.h"
23+
#include "shell/common/world_ids.h"
2324
#include "shell/renderer/api/context_bridge/render_frame_context_bridge_store.h"
24-
#include "shell/renderer/electron_render_frame_observer.h"
2525
#include "third_party/blink/public/web/web_local_frame.h"
2626

2727
namespace electron {
@@ -477,7 +477,7 @@ void ExposeAPIInMainWorld(const std::string& key,
477477
}
478478

479479
v8::Local<v8::Context> isolated_context =
480-
frame->WorldScriptContext(args->isolate(), World::ISOLATED_WORLD);
480+
frame->WorldScriptContext(args->isolate(), WorldIDs::ISOLATED_WORLD_ID);
481481

482482
v8::Context::Scope main_context_scope(main_context);
483483
{

shell/renderer/electron_render_frame_observer.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "net/grit/net_resources.h"
2121
#include "services/service_manager/public/cpp/interface_provider.h"
2222
#include "shell/common/options_switches.h"
23+
#include "shell/common/world_ids.h"
2324
#include "third_party/blink/public/platform/web_isolated_world_info.h"
2425
#include "third_party/blink/public/web/blink.h"
2526
#include "third_party/blink/public/web/web_document.h"
@@ -90,8 +91,8 @@ void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
9091
}
9192

9293
#if !BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
93-
if (world_id >= World::ISOLATED_WORLD_EXTENSIONS &&
94-
world_id <= World::ISOLATED_WORLD_EXTENSIONS_END) {
94+
if (world_id >= WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS &&
95+
world_id <= WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS_END) {
9596
renderer_client_->SetupExtensionWorldOverrides(context, render_frame_,
9697
world_id);
9798
}
@@ -136,19 +137,19 @@ void ElectronRenderFrameObserver::CreateIsolatedWorldContext() {
136137
blink::WebString::FromUTF8("Electron Isolated Context");
137138
// Setup document's origin policy in isolated world
138139
info.security_origin = frame->GetDocument().GetSecurityOrigin();
139-
frame->SetIsolatedWorldInfo(World::ISOLATED_WORLD, info);
140+
frame->SetIsolatedWorldInfo(WorldIDs::ISOLATED_WORLD_ID, info);
140141

141142
// Create initial script context in isolated world
142143
blink::WebScriptSource source("void 0");
143-
frame->ExecuteScriptInIsolatedWorld(World::ISOLATED_WORLD, source);
144+
frame->ExecuteScriptInIsolatedWorld(WorldIDs::ISOLATED_WORLD_ID, source);
144145
}
145146

146147
bool ElectronRenderFrameObserver::IsMainWorld(int world_id) {
147-
return world_id == World::MAIN_WORLD;
148+
return world_id == WorldIDs::MAIN_WORLD_ID;
148149
}
149150

150151
bool ElectronRenderFrameObserver::IsIsolatedWorld(int world_id) {
151-
return world_id == World::ISOLATED_WORLD;
152+
return world_id == WorldIDs::ISOLATED_WORLD_ID;
152153
}
153154

154155
bool ElectronRenderFrameObserver::ShouldNotifyClient(int world_id) {

shell/renderer/electron_render_frame_observer.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "content/public/renderer/render_frame_observer.h"
1212
#include "ipc/ipc_platform_file.h"
1313
#include "shell/renderer/renderer_client_base.h"
14-
#include "third_party/blink/public/platform/web_isolated_world_ids.h"
1514
#include "third_party/blink/public/web/web_local_frame.h"
1615

1716
namespace base {
@@ -20,23 +19,6 @@ class ListValue;
2019

2120
namespace electron {
2221

23-
enum World {
24-
MAIN_WORLD = 0,
25-
26-
// Use a high number far away from 0 to not collide with any other world
27-
// IDs created internally by Chrome.
28-
ISOLATED_WORLD = 999,
29-
30-
// Numbers for isolated worlds for extensions are set in
31-
// lib/renderer/content-script-injector.ts, and are greater than or equal to
32-
// this number, up to ISOLATED_WORLD_EXTENSIONS_END.
33-
ISOLATED_WORLD_EXTENSIONS = 1 << 20,
34-
35-
// Last valid isolated world ID.
36-
ISOLATED_WORLD_EXTENSIONS_END =
37-
blink::IsolatedWorldId::kEmbedderWorldIdLimit - 1
38-
};
39-
4022
// Helper class to forward the messages to the client.
4123
class ElectronRenderFrameObserver : public content::RenderFrameObserver {
4224
public:

shell/renderer/extensions/electron_extensions_renderer_client.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "content/public/renderer/render_thread.h"
88
#include "extensions/renderer/dispatcher.h"
9+
#include "shell/common/world_ids.h"
910
#include "shell/renderer/extensions/electron_extensions_dispatcher_delegate.h"
1011

1112
namespace electron {
@@ -24,11 +25,7 @@ bool ElectronExtensionsRendererClient::IsIncognitoProcess() const {
2425
}
2526

2627
int ElectronExtensionsRendererClient::GetLowestIsolatedWorldId() const {
27-
// app_shell doesn't need to reserve world IDs for anything other than
28-
// extensions, so we always return 1. Note that 0 is reserved for the global
29-
// world.
30-
// TODO(samuelmaddock): skip electron worlds
31-
return 10;
28+
return WorldIDs::ISOLATED_WORLD_ID_EXTENSIONS;
3229
}
3330

3431
extensions::Dispatcher* ElectronExtensionsRendererClient::GetDispatcher() {

shell/renderer/renderer_client_base.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#include "shell/common/color_util.h"
2525
#include "shell/common/gin_helper/dictionary.h"
2626
#include "shell/common/options_switches.h"
27+
#include "shell/common/world_ids.h"
2728
#include "shell/renderer/browser_exposed_renderer_interfaces.h"
2829
#include "shell/renderer/content_settings_observer.h"
2930
#include "shell/renderer/electron_api_service_impl.h"
3031
#include "shell/renderer/electron_autofill_agent.h"
31-
#include "shell/renderer/electron_render_frame_observer.h"
3232
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
3333
#include "third_party/blink/public/web/blink.h"
3434
#include "third_party/blink/public/web/web_custom_element.h" // NOLINT(build/include_alpha)
@@ -424,7 +424,7 @@ v8::Local<v8::Context> RendererClientBase::GetContext(
424424
blink::WebLocalFrame* frame,
425425
v8::Isolate* isolate) const {
426426
if (isolated_world())
427-
return frame->WorldScriptContext(isolate, World::ISOLATED_WORLD);
427+
return frame->WorldScriptContext(isolate, WorldIDs::ISOLATED_WORLD_ID);
428428
else
429429
return frame->MainWorldScriptContext();
430430
}

0 commit comments

Comments
 (0)
X Tutup