]> khamidou — GitHub mirrors - electron.git/commitdiff
Wrap callbacks with v8::Locker.
authorCheng Zhao <zcbenz@gmail.com>
Mon, 27 Jan 2014 07:14:11 +0000 (15:14 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 27 Jan 2014 07:14:11 +0000 (15:14 +0800)
In browser process, because of the use of v8::Locker in ProxyResolverV8,
creating HandleScope requires entering locker first, so in browser
process we should just use v8::Locker whenever we need to use V8 in a
callback called from non-V8 places (like dialog's callback, work pushed
to MessageLoop).

But also notice that the renderer process doesn't use v8::Locker, so we
have to be careful not to use it, otherwise blink would crash when
creating HandleScope.

browser/api/atom_api_dialog.cc
browser/api/atom_api_menu.cc
browser/api/atom_api_protocol.cc
browser/api/atom_api_window.cc
browser/api/atom_browser_bindings.cc
browser/atom_browser_main_parts.cc
common/api/atom_api_event_emitter.cc
common/node_bindings.cc

index b35f67af7682d96c2344515af5be8b791b8c0933..2eb555ccd5f95eff94789c83748d505ad0fb9187 100644 (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);
index c86a1ce727cafe60b36baf48a0c5c62cfcee0fe5..cc3a514d5690d404467f26545557c44c7e78ed4d 100644 (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,6 +123,7 @@ 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);
 }
index 01dc0de4bac0ff3fbff7c2c2ef42a3446811d3d3..107477d0eb2984525e49ee209beebce38b20d1a2 100644 (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.
index 0052a28c6c7cb5c0bfb7b74bb83a965d2886e4e8..d5b6296831a7958b6b9216f8af8b3a1dc6a3a405 100644 (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(
index 4b3e79f59f32482373b13e68da2f678cd84927ed..a7753f77aea89e94e592d06cc8696f766d502906 100644 (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);
index 165e76b98c4a800ff9e64f166ec1f1feead7857b..0cb05dd1a1eba75469a87c7e0ea83298a8748268 100644 (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);
 
index f175810ad45f29a5c03794116a21869a89493b01..998876b05270af5704cf2339c81cebbea453349d 100644 (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();
index e36ba9baff4df8f0505cd12c8e6c49f0c206349b..3d2d5ebcc4f7b7a2fa3e24fd16e5d57f56fd7365 100644 (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