Use JavaScript to open the main window.

This commit is contained in:
Cheng Zhao 2013-04-17 20:05:43 +08:00
parent b313d94a2f
commit 60528e53ee
11 changed files with 479 additions and 25 deletions

View file

@ -7,7 +7,8 @@
#include "base/values.h"
#include "browser/atom_browser_context.h"
#include "browser/native_window.h"
#include "content/public/renderer/v8_value_converter.h"
#include "common/v8_value_converter_impl.h"
#include "content/public/browser/web_contents.h"
using content::V8ValueConverter;
@ -18,6 +19,12 @@ namespace api {
Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
: EventEmitter(wrapper),
window_(NativeWindow::Create(AtomBrowserContext::Get(), options)) {
window_->InitFromOptions(options);
window_->GetWebContents()->GetController().LoadURL(
GURL("https://github.com"),
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
Window::~Window() {
@ -30,7 +37,7 @@ v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
if (!args[0]->IsObject())
return node::ThrowTypeError("Bad argument");
scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
scoped_ptr<V8ValueConverter> converter(new V8ValueConverterImpl());
scoped_ptr<base::Value> options(
converter->FromV8Value(args[0], v8::Context::GetCurrent()));

View file

@ -4,6 +4,7 @@
#include "browser/api/atom_bindings.h"
#include "base/logging.h"
#include "vendor/node/src/node.h"
#include "vendor/node/src/node_internals.h"
@ -31,6 +32,21 @@ void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
node::SetMethod(process, "atom_binding", Binding);
}
void AtomBindings::AfterLoad() {
v8::HandleScope scope;
v8::Context::Scope context_scope(node::g_context);
v8::Handle<v8::Object> global = node::g_context->Global();
v8::Handle<v8::Object> atom =
global->Get(v8::String::New("__atom"))->ToObject();
DCHECK(!atom.IsEmpty());
browser_main_parts_ = v8::Persistent<v8::Object>::New(
node_isolate,
atom->Get(v8::String::New("browserMainParts"))->ToObject());
DCHECK(!browser_main_parts_.IsEmpty());
}
// static
v8::Handle<v8::Value> AtomBindings::Binding(const v8::Arguments& args) {
v8::HandleScope scope;

View file

@ -17,13 +17,23 @@ class AtomBindings {
// Add process.atom_binding function, which behaves like process.binding but
// load native code from atom-shell instead.
void BindTo(v8::Handle<v8::Object> process);
virtual void BindTo(v8::Handle<v8::Object> process);
// Called when the node.js main script has been loaded.
virtual void AfterLoad();
// The require('atom').browserMainParts object.
v8::Handle<v8::Object> browser_main_parts() {
return browser_main_parts_;
}
private:
static v8::Handle<v8::Value> Binding(const v8::Arguments& args);
static v8::Persistent<v8::Object> binding_cache_;
v8::Persistent<v8::Object> browser_main_parts_;
DISALLOW_COPY_AND_ASSIGN(AtomBindings);
};

View file

@ -0,0 +1,3 @@
Window = process.atom_binding('window').Window
module.exports = Window

View file

@ -4,14 +4,11 @@
#include "browser/atom_browser_main_parts.h"
#include "base/values.h"
#include "browser/api/atom_bindings.h"
#include "browser/atom_browser_context.h"
#include "browser/native_window.h"
#include "brightray/browser/browser_context.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "common/node_bindings.h"
#include "vendor/node/src/node.h"
#include "vendor/node/src/node_internals.h"
namespace atom {
@ -36,6 +33,8 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
atom_bindings_->BindTo(node::process);
node_bindings_->Load();
atom_bindings_->AfterLoad();
}
void AtomBrowserMainParts::PreMainMessageLoopStart() {
@ -47,22 +46,17 @@ void AtomBrowserMainParts::PreMainMessageLoopStart() {
void AtomBrowserMainParts::PreMainMessageLoopRun() {
brightray::BrowserMainParts::PreMainMessageLoopRun();
{
v8::HandleScope scope;
v8::Context::Scope context_scope(node::g_context);
v8::Handle<v8::Value> args;
node::MakeCallback(atom_bindings_->browser_main_parts(),
"preMainMessageLoopRun",
0, &args);
}
node_bindings_->RunMessageLoop();
scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue);
options->SetInteger("width", 800);
options->SetInteger("height", 600);
options->SetString("title", "Atom");
// FIXME: Leak object here.
NativeWindow* window = NativeWindow::Create(browser_context(), options.get());
window->InitFromOptions(options.get());
window->GetWebContents()->GetController().LoadURL(
GURL("http://localhost"),
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
} // namespace atom

View file

@ -6,7 +6,6 @@
#define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_
#include "brightray/browser/browser_main_parts.h"
#include "common/node_bindings.h"
namespace atom {

View file

@ -1,5 +1,8 @@
var atom = require('atom');
var Window = require('window');
var mainWindow = null;
atom.browserMainParts.preMainMessageLoopRun = function() {
console.log('Create new window');
mainWindow = new Window({ width: 800, height: 600 });
}