Merge pull request #166 from atom/cz-v8-locker

Wrap callbacks with v8 locker in browser process
This commit is contained in:
Cheng Zhao 2014-01-27 00:15:22 -08:00
commit 96cb4fd6d7
19 changed files with 32 additions and 40 deletions

View file

@ -174,8 +174,6 @@ void App::DockGetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void App::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Application"));

View file

@ -47,8 +47,6 @@ void AutoUpdater::ReadyForUpdateOnQuit(const std::string& version,
// static
void AutoUpdater::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
@ -102,8 +100,6 @@ void AutoUpdater::QuitAndInstall(
// static
void AutoUpdater::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
v8::Local<v8::FunctionTemplate> t(
v8::FunctionTemplate::New(AutoUpdater::New));
t->InstanceTemplate()->SetInternalFieldCount(1);

View file

@ -19,7 +19,9 @@ namespace {
template<typename T>
void CallV8Function(const RefCountedV8Function& callback, T arg) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> value = ToV8Value(arg);
callback->NewHandle(node_isolate)->Call(
v8::Context::GetCurrent()->Global(), 1, &value);
@ -34,8 +36,6 @@ void CallV8Function2(const RefCountedV8Function& callback, bool result, T arg) {
}
void Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
NODE_SET_METHOD(target, "showMessageBox", ShowMessageBox);
NODE_SET_METHOD(target, "showOpenDialog", ShowOpenDialog);
NODE_SET_METHOD(target, "showSaveDialog", ShowSaveDialog);

View file

@ -24,6 +24,7 @@ v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
v8::Handle<v8::Object> menu,
const char* method,
int command_id) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> delegate = menu->Get(v8::String::New("delegate"));
@ -52,6 +53,7 @@ Menu::~Menu() {
}
bool Menu::IsCommandIdChecked(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(),
@ -60,6 +62,7 @@ bool Menu::IsCommandIdChecked(int command_id) const {
}
bool Menu::IsCommandIdEnabled(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(),
const_cast<Menu*>(this)->handle(),
@ -68,6 +71,7 @@ bool Menu::IsCommandIdEnabled(int command_id) const {
}
bool Menu::IsCommandIdVisible(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::True(),
const_cast<Menu*>(this)->handle(),
@ -77,6 +81,7 @@ bool Menu::IsCommandIdVisible(int command_id) const {
bool Menu::GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(),
handle(),
@ -91,6 +96,7 @@ bool Menu::GetAcceleratorForCommandId(int command_id,
}
bool Menu::IsItemForCommandIdDynamic(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(),
@ -99,6 +105,7 @@ bool Menu::IsItemForCommandIdDynamic(int command_id) const {
}
string16 Menu::GetLabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return FromV8Value(CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(),
@ -107,6 +114,7 @@ string16 Menu::GetLabelForCommandId(int command_id) const {
}
string16 Menu::GetSublabelForCommandId(int command_id) const {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
return FromV8Value(CallDelegate(v8::False(),
const_cast<Menu*>(this)->handle(),
@ -115,14 +123,13 @@ string16 Menu::GetSublabelForCommandId(int command_id) const {
}
void Menu::ExecuteCommand(int command_id, int event_flags) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
CallDelegate(v8::False(), handle(), "executeCommand", command_id);
}
// static
void Menu::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
@ -307,8 +314,6 @@ void Menu::Popup(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void Menu::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
v8::Local<v8::FunctionTemplate> t(v8::FunctionTemplate::New(Menu::New));
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Menu"));

View file

@ -74,8 +74,6 @@ void MenuMac::SendActionToFirstResponder(const std::string& action) {
// static
void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
if (!args[0]->IsObject())
return node::ThrowTypeError("Bad argument");
@ -94,8 +92,6 @@ void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void Menu::SendActionToFirstResponder(
const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
std::string action;
if (!FromV8Arguments(args, &action))
return node::ThrowTypeError("Bad argument");

View file

@ -31,8 +31,6 @@ void MenuWin::Popup(NativeWindow* native_window) {
// static
void Menu::AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
Menu* self = ObjectWrap::Unwrap<Menu>(args.This());
if (self == NULL)
return node::ThrowError("Menu is already destroyed");

View file

@ -39,8 +39,6 @@ void PowerMonitor::OnResume() {
// static
void PowerMonitor::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
@ -49,8 +47,6 @@ void PowerMonitor::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void PowerMonitor::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
#if defined(OS_MACOSX)
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
#endif

View file

@ -35,6 +35,7 @@ static const char* kEarlyUseProtocolError = "This method can only be used"
// Emit an event for the protocol module.
void EmitEventInUI(const std::string& event, const std::string& parameter) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Value> argv[] = {
@ -72,6 +73,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
virtual void GetJobTypeInUI() OVERRIDE {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
// Call the JS handler.

View file

@ -92,6 +92,7 @@ void Window::OnRendererCrashed() {
void Window::OnCapturePageDone(const RefCountedV8Function& callback,
const std::vector<unsigned char>& data) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Local<v8::Value> buffer = node::Buffer::New(
@ -103,8 +104,6 @@ void Window::OnCapturePageDone(const RefCountedV8Function& callback,
// static
void Window::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
@ -625,8 +624,6 @@ void Window::ReloadIgnoringCache(
// static
void Window::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(Window::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("BrowserWindow"));

View file

@ -25,6 +25,7 @@ void AtomBrowserBindings::OnRendererMessage(int process_id,
int routing_id,
const string16& channel,
const base::ListValue& args) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
@ -58,6 +59,7 @@ void AtomBrowserBindings::OnRendererMessageSync(
const base::ListValue& args,
NativeWindow* sender,
IPC::Message* message) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);

View file

@ -51,6 +51,7 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
v8::V8::Initialize();
// Create context.
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);
v8::Local<v8::Context> context = v8::Context::New(node_isolate);

View file

@ -74,8 +74,6 @@ void Clipboard::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void Clipboard::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
NODE_SET_METHOD(target, "has", Has);
NODE_SET_METHOD(target, "read", Read);
NODE_SET_METHOD(target, "readText", ReadText);

View file

@ -21,6 +21,11 @@ EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
}
EventEmitter::~EventEmitter() {
// Use Locker in browser process.
scoped_ptr<v8::Locker> locker;
if (node::g_standalone_mode)
locker.reset(new v8::Locker(node_isolate));
// Clear the aligned pointer, it should have been done by ObjectWrap but
// somehow node v0.11.x changed this behaviour.
v8::HandleScope handle_scope(node_isolate);
@ -33,6 +38,11 @@ bool EventEmitter::Emit(const std::string& name) {
}
bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
// Use Locker in browser process.
scoped_ptr<v8::Locker> locker;
if (node::g_standalone_mode)
locker.reset(new v8::Locker(node_isolate));
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Context> context = v8::Context::GetCurrent();

View file

@ -122,8 +122,6 @@ void IDWeakMap::Remove(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void IDWeakMap::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("IDWeakMap"));

View file

@ -67,8 +67,6 @@ void Screen::GetPrimaryDisplay(
// static
void Screen::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Screen"));

View file

@ -60,8 +60,6 @@ void Shell::Beep(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void Shell::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
NODE_SET_METHOD(target, "showItemInFolder", ShowItemInFolder);
NODE_SET_METHOD(target, "openItem", OpenItem);
NODE_SET_METHOD(target, "openExternal", OpenExternal);

View file

@ -46,8 +46,6 @@ void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args) {
} // namespace
void InitializeV8Util(v8::Handle<v8::Object> target) {
v8::HandleScope handle_scope(node_isolate);
NODE_SET_METHOD(target, "createObjectWithName", CreateObjectWithName);
NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue);
NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue);

View file

@ -90,8 +90,6 @@ AtomBindings::~AtomBindings() {
}
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
v8::HandleScope handle_scope(node_isolate);
NODE_SET_METHOD(process, "atomBinding", Binding);
NODE_SET_METHOD(process, "crash", Crash);
NODE_SET_METHOD(process, "activateUvLoop", ActivateUVLoop);
@ -170,8 +168,6 @@ void AtomBindings::Log(const v8::FunctionCallbackInfo<v8::Value>& args) {
// static
void AtomBindings::GetCurrentStackTrace(
const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
int stack_limit = kMaxCallStackSize;
FromV8Arguments(args, &stack_limit);

View file

@ -194,6 +194,11 @@ void NodeBindings::RunMessageLoop() {
void NodeBindings::UvRunOnce() {
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
// Use Locker in browser process.
scoped_ptr<v8::Locker> locker;
if (is_browser_)
locker.reset(new v8::Locker(node_isolate));
v8::HandleScope handle_scope(node_isolate);
// Enter node context while dealing with uv events, by default the global