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),
exit_code_(nullptr),
browser_(new Browser),
node_bindings_(NodeBindings::Create(true)),
node_bindings_(NodeBindings::Create(NodeBindings::BROWSER)),
atom_bindings_(new AtomBindings),
gc_timer_(true, true) {
DCHECK(!self_) << "Cannot have two AtomBrowserMainParts";

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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