refactor: simplify process object initialization for sandboxed renderers (#14878)

Also fix `process.windowsStore`.
This commit is contained in:
Milan Burda 2018-09-30 23:24:00 +02:00 committed by Alexey Kuzmin
parent 0127bbc8e8
commit ce38be74df
5 changed files with 45 additions and 43 deletions

View file

@ -20,6 +20,7 @@
#include "base/process/process_metrics_iocounters.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "brightray/common/application_info.h"
#include "native_mate/dictionary.h"
namespace atom {
@ -74,6 +75,11 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local<v8::Object> process) {
dict.Set("mas", true);
#endif
#if defined(OS_WIN)
if (brightray::IsRunningInDesktopBridge())
dict.Set("windowsStore", true);
#endif
mate::Dictionary versions;
if (dict.Get("versions", &versions)) {
// TODO(kevinsawicki): Make read-only in 2.0 to match node

View file

@ -17,6 +17,7 @@
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/process/process_handle.h"
#include "brightray/common/application_info.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
#include "content/public/renderer/render_frame.h"
#include "native_mate/dictionary.h"
@ -80,10 +81,6 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
return exports;
}
base::CommandLine::StringVector GetArgv() {
return base::CommandLine::ForCurrentProcess()->argv();
}
base::FilePath::StringType GetExecPath() {
base::FilePath path;
PathService::Get(base::FILE_EXE, &path);
@ -146,17 +143,34 @@ void AtomSandboxedRendererClient::InitializeBindings(
mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding);
b.SetMethod("createPreloadScript", CreatePreloadScript);
b.SetMethod("crash", AtomBindings::Crash);
b.SetMethod("hang", AtomBindings::Hang);
b.SetMethod("getArgv", GetArgv);
b.SetMethod("getExecPath", GetExecPath);
b.SetMethod("getPid", &base::GetCurrentProcId);
b.SetMethod("getResourcesPath", &NodeBindings::GetHelperResourcesPath);
b.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics);
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
b.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage,
base::Unretained(metrics_.get())));
b.SetMethod("getIOCounters", &AtomBindings::GetIOCounters);
mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate);
b.Set("process", process);
process.SetMethod("crash", AtomBindings::Crash);
process.SetMethod("hang", AtomBindings::Hang);
process.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics);
process.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
process.SetMethod(
"getCPUUsage",
base::Bind(&AtomBindings::GetCPUUsage, base::Unretained(metrics_.get())));
process.SetMethod("getIOCounters", &AtomBindings::GetIOCounters);
process.Set("argv", base::CommandLine::ForCurrentProcess()->argv());
process.Set("execPath", GetExecPath());
process.Set("pid", base::GetCurrentProcId());
process.Set("resourcesPath", NodeBindings::GetHelperResourcesPath());
process.Set("sandboxed", true);
process.Set("type", "renderer");
#if defined(MAS_BUILD)
process.Set("mas", true);
#endif
#if defined(OS_WIN)
if (brightray::IsRunningInDesktopBridge())
process.Set("windowsStore", true);
#endif
// Pass in CLI flags needed to setup the renderer
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

View file

@ -28,6 +28,8 @@ In sandboxed renderers the `process` object contains only a subset of the APIs:
- `type`
- `version`
- `versions`
- `mas`
- `windowsStore`
## Events

View file

@ -50,13 +50,4 @@ if (process.platform === 'win32') {
process.__defineGetter__('stdin', function () {
return stdin
})
// If we're running as a Windows Store app, __dirname will be set
// to C:/Program Files/WindowsApps.
//
// Nobody else get's to install there, changing the path is forbidden
// We can therefore say that we're running as appx
if (__dirname.includes('\\WindowsApps\\')) {
process.windowsStore = true
}
}

View file

@ -3,6 +3,7 @@
/* eslint no-eval: "off" */
/* global binding, Buffer */
const events = require('events')
const { EventEmitter } = events
process.atomBinding = require('@electron/internal/common/atom-binding-setup')(binding.get, 'renderer')
@ -15,15 +16,15 @@ const v8Util = process.atomBinding('v8_util')
v8Util.setHiddenValue(global, 'Buffer', Buffer)
// The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the
// "ipc" hidden value
v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter())
v8Util.setHiddenValue(global, 'ipc', new EventEmitter())
// The process object created by browserify is not an event emitter, fix it so
// the API is more compatible with non-sandboxed renderers.
for (let prop of Object.keys(events.EventEmitter.prototype)) {
for (let prop of Object.keys(EventEmitter.prototype)) {
if (process.hasOwnProperty(prop)) {
delete process[prop]
}
}
Object.setPrototypeOf(process, events.EventEmitter.prototype)
Object.setPrototypeOf(process, EventEmitter.prototype)
const remoteModules = new Set([
'child_process',
@ -47,24 +48,12 @@ require('@electron/internal/renderer/web-frame-init')()
// Pass different process object to the preload script(which should not have
// access to things like `process.atomBinding`).
const preloadProcess = new events.EventEmitter()
preloadProcess.crash = () => binding.crash()
preloadProcess.hang = () => binding.hang()
preloadProcess.getHeapStatistics = () => binding.getHeapStatistics()
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
preloadProcess.getCPUUsage = () => binding.getCPUUsage()
preloadProcess.getIOCounters = () => binding.getIOCounters()
Object.assign(processProps, {
argv: binding.getArgv(),
execPath: binding.getExecPath(),
pid: binding.getPid(),
resourcesPath: binding.getResourcesPath(),
sandboxed: true,
type: 'renderer'
})
const preloadProcess = new EventEmitter()
Object.assign(preloadProcess, binding.process)
Object.assign(preloadProcess, processProps)
Object.assign(process, binding.process)
Object.assign(process, processProps)
process.on('exit', () => preloadProcess.emit('exit'))