diff --git a/atom.gyp b/atom.gyp index 6a79cb8c93a6..6bd28b25d90d 100644 --- a/atom.gyp +++ b/atom.gyp @@ -93,6 +93,8 @@ 'atom/browser/browser_mac.mm', 'atom/browser/browser_win.cc', 'atom/browser/browser_observer.h', + 'atom/browser/javascript_environment.cc', + 'atom/browser/javascript_environment.h', 'atom/browser/mac/atom_application.h', 'atom/browser/mac/atom_application.mm', 'atom/browser/mac/atom_application_delegate.h', diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index 86d8d6b1e745..d883367495bc 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -7,10 +7,10 @@ #include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/browser.h" +#include "atom/browser/javascript_environment.h" #include "atom/common/api/atom_bindings.h" #include "atom/common/node_bindings.h" #include "base/command_line.h" -#include "net/proxy/proxy_resolver_v8.h" #if defined(OS_WIN) #include "ui/gfx/win/dpi.h" @@ -28,14 +28,9 @@ namespace atom { AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL; AtomBrowserMainParts::AtomBrowserMainParts() - : atom_bindings_(new AtomBindings), - browser_(new Browser), + : browser_(new Browser), node_bindings_(NodeBindings::Create(true)), - isolate_(v8::Isolate::GetCurrent()), - locker_(isolate_), - handle_scope_(isolate_), - context_(isolate_, v8::Context::New(isolate_)), - context_scope_(v8::Local::New(isolate_, context_)) { + atom_bindings_(new AtomBindings) { DCHECK(!self_) << "Cannot have two AtomBrowserMainParts"; self_ = this; } @@ -53,21 +48,20 @@ brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() { return new AtomBrowserContext(); } -void AtomBrowserMainParts::InitProxyResolverV8() { - brightray::BrowserMainParts::InitProxyResolverV8(); -} - void AtomBrowserMainParts::PostEarlyInitialization() { brightray::BrowserMainParts::PostEarlyInitialization(); + // The ProxyResolverV8 has setup a complete V8 environment, in order to avoid + // conflicts we only initialize our V8 environment after that. + js_env_.reset(new JavascriptEnvironment); + node_bindings_->Initialize(); // Create the global environment. - global_env = node_bindings_->CreateEnvironment( - v8::Local::New(isolate_, context_)); + global_env = node_bindings_->CreateEnvironment(js_env_->context()); // Add atom-shell extended APIs. - atom_bindings_->BindTo(isolate_, global_env->process_object()); + atom_bindings_->BindTo(js_env_->isolate(), global_env->process_object()); } void AtomBrowserMainParts::PreMainMessageLoopRun() { diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index d9b94e8d8310..269e042b3768 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -6,12 +6,12 @@ #define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_ #include "brightray/browser/browser_main_parts.h" -#include "v8/include/v8.h" namespace atom { class AtomBindings; class Browser; +class JavascriptEnvironment; class NodeBindings; class AtomBrowserMainParts : public brightray::BrowserMainParts { @@ -26,7 +26,6 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { protected: // Implementations of brightray::BrowserMainParts. virtual brightray::BrowserContext* CreateBrowserContext() OVERRIDE; - virtual void InitProxyResolverV8() OVERRIDE; // Implementations of content::BrowserMainParts. virtual void PostEarlyInitialization() OVERRIDE; @@ -37,16 +36,10 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { #endif private: - scoped_ptr atom_bindings_; scoped_ptr browser_; + scoped_ptr js_env_; scoped_ptr node_bindings_; - - // The V8 environment of browser process. - v8::Isolate* isolate_; - v8::Locker locker_; - v8::HandleScope handle_scope_; - v8::UniquePersistent context_; - v8::Context::Scope context_scope_; + scoped_ptr atom_bindings_; static AtomBrowserMainParts* self_; diff --git a/atom/browser/javascript_environment.cc b/atom/browser/javascript_environment.cc new file mode 100644 index 000000000000..ddc29afcc453 --- /dev/null +++ b/atom/browser/javascript_environment.cc @@ -0,0 +1,17 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/javascript_environment.h" + +namespace atom { + +JavascriptEnvironment::JavascriptEnvironment() + : isolate_(v8::Isolate::GetCurrent()), + locker_(isolate_), + handle_scope_(isolate_), + context_(isolate_, v8::Context::New(isolate_)), + context_scope_(v8::Local::New(isolate_, context_)) { +} + +} // namespace atom diff --git a/atom/browser/javascript_environment.h b/atom/browser/javascript_environment.h new file mode 100644 index 000000000000..67cab80598f6 --- /dev/null +++ b/atom/browser/javascript_environment.h @@ -0,0 +1,34 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_ +#define ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_ + +#include "base/basictypes.h" +#include "v8/include/v8.h" + +namespace atom { + +class JavascriptEnvironment { + public: + JavascriptEnvironment(); + + v8::Isolate* isolate() const { return isolate_; } + v8::Local context() const { + return v8::Local::New(isolate_, context_); + } + + private: + v8::Isolate* isolate_; + v8::Locker locker_; + v8::HandleScope handle_scope_; + v8::UniquePersistent context_; + v8::Context::Scope context_scope_; + + DISALLOW_COPY_AND_ASSIGN(JavascriptEnvironment); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_