Skip to content

Commit

Permalink
refactor: implement isRemoteModuleEnabled via getLastWebPreferences()
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Aug 26, 2019
1 parent cddbddc commit 9e53610
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 30 deletions.
7 changes: 6 additions & 1 deletion lib/browser/rpc-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,16 @@ const unwrapArgs = function (sender, frameId, contextId, args) {
return args.map(metaToValue)
}

const isRemoteModuleEnabledImpl = function (contents) {
const webPreferences = contents.getLastWebPreferences() || {}
return !!webPreferences.enableRemoteModule
}

const isRemoteModuleEnabledCache = new WeakMap()

const isRemoteModuleEnabled = function (contents) {
if (!isRemoteModuleEnabledCache.has(contents)) {
isRemoteModuleEnabledCache.set(contents, contents._isRemoteModuleEnabled())
isRemoteModuleEnabledCache.set(contents, isRemoteModuleEnabledImpl(contents))
}

return isRemoteModuleEnabledCache.get(contents)
Expand Down
11 changes: 0 additions & 11 deletions shell/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2235,16 +2235,6 @@ v8::Local<v8::Value> WebContents::GetLastWebPreferences(
return mate::ConvertToV8(isolate, *web_preferences->last_preference());
}

bool WebContents::IsRemoteModuleEnabled() const {
if (web_contents()->GetVisibleURL().SchemeIs("devtools")) {
return false;
}
if (auto* web_preferences = WebContentsPreferences::From(web_contents())) {
return web_preferences->IsRemoteModuleEnabled();
}
return true;
}

v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() const {
if (owner_window())
return BrowserWindow::From(isolate(), owner_window());
Expand Down Expand Up @@ -2453,7 +2443,6 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("_getPreloadPaths", &WebContents::GetPreloadPaths)
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
.SetMethod("getLastWebPreferences", &WebContents::GetLastWebPreferences)
.SetMethod("_isRemoteModuleEnabled", &WebContents::IsRemoteModuleEnabled)
.SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
.SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
.SetMethod("inspectSharedWorker", &WebContents::InspectSharedWorker)
Expand Down
2 changes: 0 additions & 2 deletions shell/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate) const;
v8::Local<v8::Value> GetLastWebPreferences(v8::Isolate* isolate) const;

bool IsRemoteModuleEnabled() const;

// Returns the owner window.
v8::Local<v8::Value> GetOwnerBrowserWindow() const;

Expand Down
3 changes: 0 additions & 3 deletions shell/browser/atom_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,6 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(

content::WebContents* web_contents = GetWebContentsFromProcessID(process_id);
if (web_contents) {
if (web_contents->GetVisibleURL().SchemeIs("devtools")) {
command_line->AppendSwitch(switches::kDisableRemoteModule);
}
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences)
web_preferences->AppendCommandLineSwitches(
Expand Down
11 changes: 4 additions & 7 deletions shell/browser/web_contents_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ WebContentsPreferences::WebContentsPreferences(
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
SetDefaultBoolIfUndefined(options::kSandbox, false);
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
SetDefaultBoolIfUndefined(options::kEnableRemoteModule, true);
SetDefaultBoolIfUndefined(options::kContextIsolation, false);
SetDefaultBoolIfUndefined(options::kJavaScript, true);
SetDefaultBoolIfUndefined(options::kImages, true);
Expand Down Expand Up @@ -171,6 +170,8 @@ WebContentsPreferences::~WebContentsPreferences() {
}

void WebContentsPreferences::SetDefaults() {
SetDefaultBoolIfUndefined(options::kEnableRemoteModule, true);

if (IsEnabled(options::kSandbox)) {
SetBool(options::kNativeWindowOpen, true);
}
Expand Down Expand Up @@ -220,10 +221,6 @@ bool WebContentsPreferences::GetPreference(base::StringPiece name,
return GetAsString(&preference_, name, value);
}

bool WebContentsPreferences::IsRemoteModuleEnabled() const {
return IsEnabled(options::kEnableRemoteModule, true);
}

bool WebContentsPreferences::GetPreloadPath(
base::FilePath::StringType* path) const {
DCHECK(path);
Expand Down Expand Up @@ -327,8 +324,8 @@ void WebContentsPreferences::AppendCommandLineSwitches(
}

// Whether to enable the remote module
if (!IsRemoteModuleEnabled())
command_line->AppendSwitch(switches::kDisableRemoteModule);
if (IsEnabled(options::kEnableRemoteModule))
command_line->AppendSwitch(switches::kEnableRemoteModule);

// Run Electron APIs and preload script in isolated world
if (IsEnabled(options::kContextIsolation))
Expand Down
3 changes: 0 additions & 3 deletions shell/browser/web_contents_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class WebContentsPreferences
// Return true if the particular preference value exists.
bool GetPreference(base::StringPiece name, std::string* value) const;

// Whether to enable the remote module
bool IsRemoteModuleEnabled() const;

// Returns the preload script path.
bool GetPreloadPath(base::FilePath::StringType* path) const;

Expand Down
2 changes: 1 addition & 1 deletion shell/common/options_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const char kBackgroundColor[] = "background-color";
const char kPreloadScript[] = "preload";
const char kPreloadScripts[] = "preload-scripts";
const char kNodeIntegration[] = "node-integration";
const char kDisableRemoteModule[] = "disable-remote-module";
const char kEnableRemoteModule[] = "enable-remote-module";
const char kContextIsolation[] = "context-isolation";
const char kGuestInstanceID[] = "guest-instance-id";
const char kOpenerID[] = "opener-id";
Expand Down
2 changes: 1 addition & 1 deletion shell/common/options_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ extern const char kBackgroundColor[];
extern const char kPreloadScript[];
extern const char kPreloadScripts[];
extern const char kNodeIntegration[];
extern const char kDisableRemoteModule[];
extern const char kEnableRemoteModule[];
extern const char kContextIsolation[];
extern const char kGuestInstanceID[];
extern const char kOpenerID[];
Expand Down
2 changes: 1 addition & 1 deletion shell/renderer/renderer_client_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void RendererClientBase::DidCreateScriptContext(

auto* command_line = base::CommandLine::ForCurrentProcess();
bool enableRemoteModule =
!command_line->HasSwitch(switches::kDisableRemoteModule);
command_line->HasSwitch(switches::kEnableRemoteModule);
global.SetHidden("enableRemoteModule", enableRemoteModule);
}

Expand Down

0 comments on commit 9e53610

Please sign in to comment.