Skip to content

Commit

Permalink
Fix context menu for sandbox devtools (#11933)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsakh authored and alexeykuzmin committed Apr 27, 2018
1 parent 43650d4 commit b703290
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
7 changes: 5 additions & 2 deletions atom/renderer/atom_sandboxed_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ipc/ipc_message_macros.h"
#include "native_mate/converter.h"
#include "native_mate/dictionary.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
Expand All @@ -38,6 +39,10 @@ namespace {
const std::string kIpcKey = "ipcNative";
const std::string kModuleCacheKey = "native-module-cache";

bool IsDevTools(content::RenderFrame* render_frame) {
return render_frame->GetWebFrame()->GetDocument().Url()
.ProtocolIs("chrome-devtools");
}

v8::Local<v8::Object> GetModuleCache(v8::Isolate* isolate) {
mate::Dictionary global(isolate, isolate->GetCurrentContext()->Global());
Expand Down Expand Up @@ -158,8 +163,6 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
std::string preload_script = command_line->GetSwitchValueASCII(
switches::kPreloadScript);
if (preload_script.empty())
return;

auto isolate = context->GetIsolate();
v8::HandleScope handle_scope(isolate);
Expand Down
28 changes: 17 additions & 11 deletions lib/sandboxed_renderer/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const preloadModules = new Map([
['timers', require('timers')]
])

const preloadSrc = fs.readFileSync(preloadPath).toString()

// Pass different process object to the preload script(which should not have
// access to things like `process.atomBinding`).
const preloadProcess = new events.EventEmitter()
Expand All @@ -54,6 +52,11 @@ function preloadRequire (module) {
throw new Error('module not found')
}

if (window.location.protocol === 'chrome-devtools:') {
// Override some inspector APIs.
require('../renderer/inspector')
}

// Wrap the script into a function executed in global scope. It won't have
// access to the current scope, so we'll expose a few objects as arguments:
//
Expand All @@ -73,13 +76,16 @@ function preloadRequire (module) {
// and any `require('electron')` calls in `preload.js` will work as expected
// since browserify won't try to include `electron` in the bundle, falling back
// to the `preloadRequire` function above.
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate) {
${preloadSrc}
})`
if (preloadPath) {
const preloadSrc = fs.readFileSync(preloadPath).toString()
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate) {
${preloadSrc}
})`

// eval in window scope:
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
const geval = eval
const preloadFn = geval(preloadWrapperSrc)
const {setImmediate} = require('timers')
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate)
// eval in window scope:
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
const geval = eval
const preloadFn = geval(preloadWrapperSrc)
const {setImmediate} = require('timers')
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate)
}

0 comments on commit b703290

Please sign in to comment.