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/process/process_metrics_iocounters.h"
#include "base/sys_info.h" #include "base/sys_info.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "brightray/common/application_info.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
namespace atom { namespace atom {
@ -74,6 +75,11 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local<v8::Object> process) {
dict.Set("mas", true); dict.Set("mas", true);
#endif #endif
#if defined(OS_WIN)
if (brightray::IsRunningInDesktopBridge())
dict.Set("windowsStore", true);
#endif
mate::Dictionary versions; mate::Dictionary versions;
if (dict.Get("versions", &versions)) { if (dict.Get("versions", &versions)) {
// TODO(kevinsawicki): Make read-only in 2.0 to match node // 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/files/file_path.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
#include "brightray/common/application_info.h"
#include "chrome/renderer/printing/print_web_view_helper.h" #include "chrome/renderer/printing/print_web_view_helper.h"
#include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_frame.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -80,10 +81,6 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
return exports; return exports;
} }
base::CommandLine::StringVector GetArgv() {
return base::CommandLine::ForCurrentProcess()->argv();
}
base::FilePath::StringType GetExecPath() { base::FilePath::StringType GetExecPath() {
base::FilePath path; base::FilePath path;
PathService::Get(base::FILE_EXE, &path); PathService::Get(base::FILE_EXE, &path);
@ -146,17 +143,34 @@ void AtomSandboxedRendererClient::InitializeBindings(
mate::Dictionary b(isolate, binding); mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding); b.SetMethod("get", GetBinding);
b.SetMethod("createPreloadScript", CreatePreloadScript); b.SetMethod("createPreloadScript", CreatePreloadScript);
b.SetMethod("crash", AtomBindings::Crash);
b.SetMethod("hang", AtomBindings::Hang); mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate);
b.SetMethod("getArgv", GetArgv); b.Set("process", process);
b.SetMethod("getExecPath", GetExecPath);
b.SetMethod("getPid", &base::GetCurrentProcId); process.SetMethod("crash", AtomBindings::Crash);
b.SetMethod("getResourcesPath", &NodeBindings::GetHelperResourcesPath); process.SetMethod("hang", AtomBindings::Hang);
b.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics); process.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics);
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo); process.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
b.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, process.SetMethod(
base::Unretained(metrics_.get()))); "getCPUUsage",
b.SetMethod("getIOCounters", &AtomBindings::GetIOCounters); 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 // Pass in CLI flags needed to setup the renderer
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 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` - `type`
- `version` - `version`
- `versions` - `versions`
- `mas`
- `windowsStore`
## Events ## Events

View file

@ -50,13 +50,4 @@ if (process.platform === 'win32') {
process.__defineGetter__('stdin', function () { process.__defineGetter__('stdin', function () {
return stdin 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" */ /* eslint no-eval: "off" */
/* global binding, Buffer */ /* global binding, Buffer */
const events = require('events') const events = require('events')
const { EventEmitter } = events
process.atomBinding = require('@electron/internal/common/atom-binding-setup')(binding.get, 'renderer') 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) v8Util.setHiddenValue(global, 'Buffer', Buffer)
// The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the // The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the
// "ipc" hidden value // "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 process object created by browserify is not an event emitter, fix it so
// the API is more compatible with non-sandboxed renderers. // 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)) { if (process.hasOwnProperty(prop)) {
delete process[prop] delete process[prop]
} }
} }
Object.setPrototypeOf(process, events.EventEmitter.prototype) Object.setPrototypeOf(process, EventEmitter.prototype)
const remoteModules = new Set([ const remoteModules = new Set([
'child_process', '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 // Pass different process object to the preload script(which should not have
// access to things like `process.atomBinding`). // access to things like `process.atomBinding`).
const preloadProcess = new events.EventEmitter() const preloadProcess = new 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'
})
Object.assign(preloadProcess, binding.process)
Object.assign(preloadProcess, processProps) Object.assign(preloadProcess, processProps)
Object.assign(process, binding.process)
Object.assign(process, processProps) Object.assign(process, processProps)
process.on('exit', () => preloadProcess.emit('exit')) process.on('exit', () => preloadProcess.emit('exit'))