Skip to content

Commit 553cc23

Browse files
committed
context wrapper to receive the webkit context
1 parent 95085d3 commit 553cc23

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

lib/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ nwebkit.init = function(options) {
2929
});
3030
};
3131

32-
global.require = require;
33-
global.process = process;
32+
nwebkit.context = new nwebkit.Context();
33+
34+
global.__nwebkit = nwebkit; //FIX ME: is there any other way to get this from WebKit context?
35+
nwebkit.require = require;
36+
nwebkit.process = process;
37+
nwebkit.global = global;
38+
nwebkit.console = console;
39+
3440
module.exports = nwebkit;

src/context_wrap.cc

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
#include <assert.h>
23+
24+
#include "context_wrap.h"
25+
26+
using v8::Context;
27+
using v8::Script;
28+
using v8::Value;
29+
using v8::Handle;
30+
using v8::HandleScope;
31+
using v8::Object;
32+
using v8::Arguments;
33+
using v8::ThrowException;
34+
using v8::TryCatch;
35+
using v8::String;
36+
using v8::Exception;
37+
using v8::Local;
38+
using v8::Array;
39+
using v8::Persistent;
40+
using v8::Integer;
41+
using v8::Function;
42+
using v8::FunctionTemplate;
43+
44+
namespace nwebkit {
45+
46+
Persistent<FunctionTemplate> ContextWrap::constructor_template;
47+
48+
void ContextWrap::Initialize(Handle<Object> target) {
49+
HandleScope scope;
50+
51+
Local<FunctionTemplate> t = FunctionTemplate::New(ContextWrap::New);
52+
constructor_template = Persistent<FunctionTemplate>::New(t);
53+
constructor_template->InstanceTemplate()->SetInternalFieldCount(2);
54+
constructor_template->SetClassName(String::NewSymbol("Context"));
55+
56+
NODE_SET_PROTOTYPE_METHOD(constructor_template,
57+
"onContextCreationFromWebKit",
58+
ContextWrap::OnContextCreationFromWebKit);
59+
60+
NODE_SET_METHOD(constructor_template,
61+
"onContextCreationFromWebKit",
62+
ContextWrap::OnContextCreationFromWebKit);
63+
64+
target->Set(String::NewSymbol("Context"),
65+
constructor_template->GetFunction());
66+
}
67+
68+
69+
bool ContextWrap::InstanceOf(Handle<Value> value) {
70+
return !value.IsEmpty() && constructor_template->HasInstance(value);
71+
}
72+
73+
74+
Handle<Value> ContextWrap::New(const Arguments& args) {
75+
HandleScope scope;
76+
77+
ContextWrap *t = new ContextWrap();
78+
t->Wrap(args.This());
79+
80+
return args.This();
81+
}
82+
83+
84+
ContextWrap::ContextWrap() : ObjectWrap() {
85+
context_valid = false;
86+
}
87+
88+
89+
ContextWrap::~ContextWrap() {
90+
}
91+
92+
93+
Local<Object> ContextWrap::NewInstance() {
94+
Local<Object> wrapper = constructor_template->GetFunction()->NewInstance();
95+
return wrapper;
96+
}
97+
98+
99+
Persistent<Context> ContextWrap::GetV8Context() {
100+
return context_;
101+
}
102+
103+
Handle<Value> ContextWrap::OnContextCreationFromWebKit(const Arguments& args) {
104+
HandleScope scope;
105+
106+
if (args.Length() > 0) {
107+
Local<Object> sandbox = args[0]->ToObject();
108+
}
109+
110+
ContextWrap* context_wrap = ObjectWrap::Unwrap<ContextWrap> (args.Holder());
111+
v8::Context* p = static_cast<v8::Context*>(args.Holder()->GetPointerFromInternalField (1));
112+
context_wrap->context_ = p;
113+
context_wrap->context_valid = true;
114+
115+
return v8::Undefined();
116+
}
117+
118+
}

src/context_wrap.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef _NWEBKIT_CONTEXT_WRAP
2+
#define _NWEBKIT_CONTEXT_WRAP
3+
4+
#include <node.h>
5+
6+
namespace nwebkit {
7+
class ContextWrap : node::ObjectWrap {
8+
public:
9+
static void Initialize(v8::Handle<v8::Object> target);
10+
static v8::Handle<v8::Value> New(const v8::Arguments& args);
11+
12+
v8::Persistent<v8::Context> GetV8Context();
13+
static v8::Local<v8::Object> NewInstance();
14+
static bool InstanceOf(v8::Handle<v8::Value> value);
15+
static v8::Handle<v8::Value> OnContextCreationFromWebKit(const v8::Arguments& args);
16+
protected:
17+
18+
static v8::Persistent<v8::FunctionTemplate> constructor_template;
19+
20+
ContextWrap();
21+
~ContextWrap();
22+
23+
v8::Persistent<v8::Context> context_;
24+
bool context_valid;
25+
};
26+
}
27+
#endif

wscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ def build(bld):
1919
obj.source = """
2020
src/nwebkit.cc
2121
src/main.cc
22+
src/context_wrap.cc
2223
"""
2324
obj.uselib = "GTK GLIB WEBKIT"

0 commit comments

Comments
 (0)