Browse Source

Make the node environment constrained in browser_main_parts.

Cheng Zhao 10 years ago
parent
commit
6a891be0e4

+ 2 - 4
atom/app/atom_main_delegate.cc

@@ -14,7 +14,6 @@
 #include "base/path_service.h"
 #include "content/public/common/content_switches.h"
 #include "ui/base/resource/resource_bundle.h"
-#include "vendor/brightray/common/content_client.h"
 
 namespace atom {
 
@@ -49,13 +48,12 @@ bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
   base::debug::EnableInProcessStackDumping();
 #endif
 
-  content_client_.reset(new brightray::ContentClient);
-  SetContentClient(content_client_.get());
-
 #if defined(OS_MACOSX)
   OverrideChildProcessPath();
   OverrideFrameworkBundlePath();
 #endif
+
+  SetContentClient(&content_client_);
   return false;
 }
 

+ 2 - 1
atom/app/atom_main_delegate.h

@@ -6,6 +6,7 @@
 #define ATOM_APP_ATOM_MAIN_DELEGATE_H_
 
 #include "brightray/common/main_delegate.h"
+#include "brightray/common/content_client.h"
 
 namespace atom {
 
@@ -30,7 +31,7 @@ class AtomMainDelegate : public brightray::MainDelegate {
   virtual content::ContentRendererClient*
       CreateContentRendererClient() OVERRIDE;
 
-  scoped_ptr<brightray::ContentClient> content_client_;
+  brightray::ContentClient content_client_;
   scoped_ptr<content::ContentBrowserClient> browser_client_;
   scoped_ptr<content::ContentRendererClient> renderer_client_;
 

+ 1 - 0
atom/browser/atom_browser_client.cc

@@ -125,6 +125,7 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
 
 brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
     const content::MainFunctionParams&) {
+  v8::V8::Initialize();  // Init V8 before creating main parts.
   return new AtomBrowserMainParts;
 }
 

+ 9 - 14
atom/browser/atom_browser_main_parts.cc

@@ -25,7 +25,12 @@ AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL;
 AtomBrowserMainParts::AtomBrowserMainParts()
     : atom_bindings_(new AtomBindings),
       browser_(new Browser),
-      node_bindings_(NodeBindings::Create(true)) {
+      node_bindings_(NodeBindings::Create(true)),
+      isolate_(v8::Isolate::GetCurrent()),
+      locker_(isolate_),
+      handle_scope_(isolate_),
+      context_(isolate_, v8::Context::New(isolate_)),
+      context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) {
   DCHECK(!self_) << "Cannot have two AtomBrowserMainParts";
   self_ = this;
 }
@@ -48,22 +53,12 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
 
   node_bindings_->Initialize();
 
-  v8::V8::Initialize();
-
-  // Create context.
-  v8::Isolate* isolate = v8::Isolate::GetCurrent();
-  v8::Locker locker(isolate);
-  v8::HandleScope handle_scope(isolate);
-  v8::Local<v8::Context> context = v8::Context::New(isolate);
-
   // Create the global environment.
-  global_env = node_bindings_->CreateEnvironment(context);
-
-  // Wrap whole process in one global context.
-  context->Enter();
+  global_env = node_bindings_->CreateEnvironment(
+      v8::Local<v8::Context>::New(isolate_, context_));
 
   // Add atom-shell extended APIs.
-  atom_bindings_->BindTo(isolate, global_env->process_object());
+  atom_bindings_->BindTo(isolate_, global_env->process_object());
 }
 
 void AtomBrowserMainParts::PreMainMessageLoopRun() {

+ 8 - 0
atom/browser/atom_browser_main_parts.h

@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
 
 #include "brightray/browser/browser_main_parts.h"
+#include "v8/include/v8.h"
 
 namespace atom {
 
@@ -40,6 +41,13 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
   scoped_ptr<Browser> browser_;
   scoped_ptr<NodeBindings> node_bindings_;
 
+  // The V8 environment of browser process.
+  v8::Isolate* isolate_;
+  v8::Locker locker_;
+  v8::HandleScope handle_scope_;
+  v8::UniquePersistent<v8::Context> context_;
+  v8::Context::Scope context_scope_;
+
   static AtomBrowserMainParts* self_;
 
   DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);

+ 2 - 2
atom/common/node_bindings.cc

@@ -35,10 +35,10 @@ void SetupProcessObject(Environment*, int, const char* const*, int,
 // DSO constructors, see http://git.io/DRIqCg.
 #if defined(OS_WIN)
 #define REFERENCE_MODULE(name) \
-  __pragma(comment (linker, "/export:_register_" #name))
+  __pragma(comment(linker, "/export:_register_" #name))
 #else
 #define REFERENCE_MODULE(name) \
-  extern "C" void _register_ ## name (void); \
+  extern "C" void _register_ ## name(void); \
   void (*fp_register_ ## name)(void) = _register_ ## name
 #endif
 // Node's builtin modules.