Add a new type of NodeBindings

This commit is contained in:
Cheng Zhao 2017-03-08 17:33:44 +09:00
parent aac934b34e
commit b467c3939e
11 changed files with 57 additions and 38 deletions

View file

@ -60,7 +60,7 @@ AtomBrowserMainParts::AtomBrowserMainParts()
: fake_browser_process_(new BrowserProcess), : fake_browser_process_(new BrowserProcess),
exit_code_(nullptr), exit_code_(nullptr),
browser_(new Browser), browser_(new Browser),
node_bindings_(NodeBindings::Create(true)), node_bindings_(NodeBindings::Create(NodeBindings::BROWSER)),
atom_bindings_(new AtomBindings), atom_bindings_(new AtomBindings),
gc_timer_(true, true) { gc_timer_(true, true) {
DCHECK(!self_) << "Cannot have two AtomBrowserMainParts"; DCHECK(!self_) << "Cannot have two AtomBrowserMainParts";

View file

@ -98,8 +98,8 @@ base::FilePath GetResourcesPath(bool is_browser) {
} // namespace } // namespace
NodeBindings::NodeBindings(bool is_browser) NodeBindings::NodeBindings(BrowserEnvironment browser_env)
: is_browser_(is_browser), : browser_env_(browser_env),
uv_loop_(uv_default_loop()), uv_loop_(uv_default_loop()),
embed_closed_(false), embed_closed_(false),
uv_env_(nullptr), uv_env_(nullptr),
@ -122,12 +122,12 @@ NodeBindings::~NodeBindings() {
void NodeBindings::Initialize() { void NodeBindings::Initialize() {
// Open node's error reporting system for browser process. // Open node's error reporting system for browser process.
node::g_standalone_mode = is_browser_; node::g_standalone_mode = browser_env_ == BROWSER;
node::g_upstream_node_mode = false; node::g_upstream_node_mode = false;
#if defined(OS_LINUX) #if defined(OS_LINUX)
// Get real command line in renderer process forked by zygote. // Get real command line in renderer process forked by zygote.
if (!is_browser_) if (browser_env_ != BROWSER)
AtomCommandLine::InitializeFromCommandLine(); AtomCommandLine::InitializeFromCommandLine();
#endif #endif
@ -139,7 +139,7 @@ void NodeBindings::Initialize() {
// uv_init overrides error mode to suppress the default crash dialog, bring // uv_init overrides error mode to suppress the default crash dialog, bring
// it back if user wants to show it. // it back if user wants to show it.
std::unique_ptr<base::Environment> env(base::Environment::Create()); std::unique_ptr<base::Environment> env(base::Environment::Create());
if (is_browser_ || env->HasVar("ELECTRON_DEFAULT_ERROR_MODE")) if (browser_env_ == BROWSER || env->HasVar("ELECTRON_DEFAULT_ERROR_MODE"))
SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX); SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX);
#endif #endif
} }
@ -149,9 +149,19 @@ node::Environment* NodeBindings::CreateEnvironment(
auto args = AtomCommandLine::argv(); auto args = AtomCommandLine::argv();
// Feed node the path to initialization script. // Feed node the path to initialization script.
base::FilePath::StringType process_type = is_browser_ ? base::FilePath::StringType process_type;
FILE_PATH_LITERAL("browser") : FILE_PATH_LITERAL("renderer"); switch (browser_env_) {
base::FilePath resources_path = GetResourcesPath(is_browser_); case BROWSER:
process_type = FILE_PATH_LITERAL("browser");
break;
case RENDERER:
process_type = FILE_PATH_LITERAL("renderer");
break;
case WORKER:
process_type = FILE_PATH_LITERAL("worker");
break;
}
base::FilePath resources_path = GetResourcesPath(browser_env_ == BROWSER);
base::FilePath script_path = base::FilePath script_path =
resources_path.Append(FILE_PATH_LITERAL("electron.asar")) resources_path.Append(FILE_PATH_LITERAL("electron.asar"))
.Append(process_type) .Append(process_type)
@ -164,7 +174,7 @@ node::Environment* NodeBindings::CreateEnvironment(
new node::IsolateData(context->GetIsolate(), uv_default_loop()), context, new node::IsolateData(context->GetIsolate(), uv_default_loop()), context,
args.size(), c_argv.get(), 0, nullptr); args.size(), c_argv.get(), 0, nullptr);
if (is_browser_) { if (browser_env_ == BROWSER) {
// SetAutorunMicrotasks is no longer called in node::CreateEnvironment // SetAutorunMicrotasks is no longer called in node::CreateEnvironment
// so instead call it here to match expected node behavior // so instead call it here to match expected node behavior
context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit); context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
@ -178,7 +188,7 @@ node::Environment* NodeBindings::CreateEnvironment(
process.Set("type", process_type); process.Set("type", process_type);
process.Set("resourcesPath", resources_path); process.Set("resourcesPath", resources_path);
// Do not set DOM globals for renderer process. // Do not set DOM globals for renderer process.
if (!is_browser_) if (browser_env_ != BROWSER)
process.Set("_noBrowserGlobals", resources_path); process.Set("_noBrowserGlobals", resources_path);
// The path to helper app. // The path to helper app.
base::FilePath helper_exec_path; base::FilePath helper_exec_path;
@ -187,7 +197,7 @@ node::Environment* NodeBindings::CreateEnvironment(
// Set process._debugWaitConnect if --debug-brk was specified to stop // Set process._debugWaitConnect if --debug-brk was specified to stop
// the debugger on the first line // the debugger on the first line
if (is_browser_ && if (browser_env_ == BROWSER &&
base::CommandLine::ForCurrentProcess()->HasSwitch("debug-brk")) base::CommandLine::ForCurrentProcess()->HasSwitch("debug-brk"))
process.Set("_debugWaitConnect", true); process.Set("_debugWaitConnect", true);
@ -200,7 +210,8 @@ void NodeBindings::LoadEnvironment(node::Environment* env) {
} }
void NodeBindings::PrepareMessageLoop() { void NodeBindings::PrepareMessageLoop() {
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(browser_env_ != BROWSER ||
BrowserThread::CurrentlyOn(BrowserThread::UI));
// Add dummy handle for libuv, otherwise libuv would quit when there is // Add dummy handle for libuv, otherwise libuv would quit when there is
// nothing to do. // nothing to do.
@ -212,7 +223,8 @@ void NodeBindings::PrepareMessageLoop() {
} }
void NodeBindings::RunMessageLoop() { void NodeBindings::RunMessageLoop() {
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(browser_env_ != BROWSER ||
BrowserThread::CurrentlyOn(BrowserThread::UI));
// The MessageLoop should have been created, remember the one in main thread. // The MessageLoop should have been created, remember the one in main thread.
task_runner_ = base::ThreadTaskRunnerHandle::Get(); task_runner_ = base::ThreadTaskRunnerHandle::Get();
@ -222,7 +234,8 @@ void NodeBindings::RunMessageLoop() {
} }
void NodeBindings::UvRunOnce() { void NodeBindings::UvRunOnce() {
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(browser_env_ != BROWSER ||
BrowserThread::CurrentlyOn(BrowserThread::UI));
node::Environment* env = uv_env(); node::Environment* env = uv_env();
@ -237,13 +250,13 @@ void NodeBindings::UvRunOnce() {
v8::MicrotasksScope script_scope(env->isolate(), v8::MicrotasksScope script_scope(env->isolate(),
v8::MicrotasksScope::kRunMicrotasks); v8::MicrotasksScope::kRunMicrotasks);
if (!is_browser_) if (browser_env_ != BROWSER)
TRACE_EVENT_BEGIN0("devtools.timeline", "FunctionCall"); TRACE_EVENT_BEGIN0("devtools.timeline", "FunctionCall");
// Deal with uv events. // Deal with uv events.
int r = uv_run(uv_loop_, UV_RUN_NOWAIT); int r = uv_run(uv_loop_, UV_RUN_NOWAIT);
if (!is_browser_) if (browser_env_ != BROWSER)
TRACE_EVENT_END0("devtools.timeline", "FunctionCall"); TRACE_EVENT_END0("devtools.timeline", "FunctionCall");
if (r == 0) if (r == 0)

View file

@ -23,7 +23,13 @@ namespace atom {
class NodeBindings { class NodeBindings {
public: public:
static NodeBindings* Create(bool is_browser); enum BrowserEnvironment {
BROWSER,
RENDERER,
WORKER,
};
static NodeBindings* Create(BrowserEnvironment browser_env);
virtual ~NodeBindings(); virtual ~NodeBindings();
@ -47,7 +53,7 @@ class NodeBindings {
node::Environment* uv_env() const { return uv_env_; } node::Environment* uv_env() const { return uv_env_; }
protected: protected:
explicit NodeBindings(bool is_browser); explicit NodeBindings(BrowserEnvironment browser_env);
// Called to poll events in new thread. // Called to poll events in new thread.
virtual void PollEvents() = 0; virtual void PollEvents() = 0;
@ -61,8 +67,8 @@ class NodeBindings {
// Interrupt the PollEvents. // Interrupt the PollEvents.
void WakeupEmbedThread(); void WakeupEmbedThread();
// Are we running in browser. // Which environment we are running.
bool is_browser_; BrowserEnvironment browser_env_;
// Main thread's MessageLoop. // Main thread's MessageLoop.
scoped_refptr<base::SingleThreadTaskRunner> task_runner_; scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

View file

@ -8,8 +8,8 @@
namespace atom { namespace atom {
NodeBindingsLinux::NodeBindingsLinux(bool is_browser) NodeBindingsLinux::NodeBindingsLinux(BrowserEnvironment browser_env)
: NodeBindings(is_browser), : NodeBindings(browser_env),
epoll_(epoll_create(1)) { epoll_(epoll_create(1)) {
int backend_fd = uv_backend_fd(uv_loop_); int backend_fd = uv_backend_fd(uv_loop_);
struct epoll_event ev = { 0 }; struct epoll_event ev = { 0 };
@ -50,8 +50,8 @@ void NodeBindingsLinux::PollEvents() {
} }
// static // static
NodeBindings* NodeBindings::Create(bool is_browser) { NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
return new NodeBindingsLinux(is_browser); return new NodeBindingsLinux(browser_env);
} }
} // namespace atom } // namespace atom

View file

@ -12,7 +12,7 @@ namespace atom {
class NodeBindingsLinux : public NodeBindings { class NodeBindingsLinux : public NodeBindings {
public: public:
explicit NodeBindingsLinux(bool is_browser); explicit NodeBindingsLinux(BrowserEnvironment browser_env);
virtual ~NodeBindingsLinux(); virtual ~NodeBindingsLinux();
void RunMessageLoop() override; void RunMessageLoop() override;

View file

@ -14,8 +14,8 @@
namespace atom { namespace atom {
NodeBindingsMac::NodeBindingsMac(bool is_browser) NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)
: NodeBindings(is_browser) { : NodeBindings(browser_env) {
} }
NodeBindingsMac::~NodeBindingsMac() { NodeBindingsMac::~NodeBindingsMac() {
@ -60,8 +60,8 @@ void NodeBindingsMac::PollEvents() {
} }
// static // static
NodeBindings* NodeBindings::Create(bool is_browser) { NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
return new NodeBindingsMac(is_browser); return new NodeBindingsMac(browser_env);
} }
} // namespace atom } // namespace atom

View file

@ -12,7 +12,7 @@ namespace atom {
class NodeBindingsMac : public NodeBindings { class NodeBindingsMac : public NodeBindings {
public: public:
explicit NodeBindingsMac(bool is_browser); explicit NodeBindingsMac(BrowserEnvironment browser_env);
virtual ~NodeBindingsMac(); virtual ~NodeBindingsMac();
void RunMessageLoop() override; void RunMessageLoop() override;

View file

@ -14,8 +14,8 @@ extern "C" {
namespace atom { namespace atom {
NodeBindingsWin::NodeBindingsWin(bool is_browser) NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env)
: NodeBindings(is_browser) { : NodeBindings(browser_env) {
} }
NodeBindingsWin::~NodeBindingsWin() { NodeBindingsWin::~NodeBindingsWin() {
@ -45,8 +45,8 @@ void NodeBindingsWin::PollEvents() {
} }
// static // static
NodeBindings* NodeBindings::Create(bool is_browser) { NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
return new NodeBindingsWin(is_browser); return new NodeBindingsWin(browser_env);
} }
} // namespace atom } // namespace atom

View file

@ -12,7 +12,7 @@ namespace atom {
class NodeBindingsWin : public NodeBindings { class NodeBindingsWin : public NodeBindings {
public: public:
explicit NodeBindingsWin(bool is_browser); explicit NodeBindingsWin(BrowserEnvironment browser_env);
virtual ~NodeBindingsWin(); virtual ~NodeBindingsWin();
private: private:

View file

@ -216,7 +216,7 @@ std::vector<std::string> ParseSchemesCLISwitch(const char* switch_name) {
AtomRendererClient::AtomRendererClient() AtomRendererClient::AtomRendererClient()
: node_integration_initialized_(false), : node_integration_initialized_(false),
node_bindings_(NodeBindings::Create(false)), node_bindings_(NodeBindings::Create(NodeBindings::RENDERER)),
atom_bindings_(new AtomBindings) { atom_bindings_(new AtomBindings) {
isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch( isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kContextIsolation); switches::kContextIsolation);

View file

@ -28,7 +28,7 @@ WebWorkerObserver* WebWorkerObserver::GetCurrent() {
} }
WebWorkerObserver::WebWorkerObserver() WebWorkerObserver::WebWorkerObserver()
: node_bindings_(NodeBindings::Create(false)), : node_bindings_(NodeBindings::Create(NodeBindings::WORKER)),
atom_bindings_(new AtomBindings) { atom_bindings_(new AtomBindings) {
lazy_tls.Pointer()->Set(this); lazy_tls.Pointer()->Set(this);
} }