Skip to content

Commit c5eda60

Browse files
committed
support nwsnapshot
1 parent fbb9040 commit c5eda60

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

nw.gypi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
],
103103
'include_dirs': [
104104
'<(DEPTH)',
105+
'<(DEPTH)/v8',
105106
'<(DEPTH)/third_party',
106107
'<(DEPTH)/third_party/WebKit/Source',
107108
'<(DEPTH)/third_party/WebKit/public/web',
@@ -206,6 +207,7 @@
206207
'src/api/screen/screen.cc',
207208
'src/api/window_bindings.cc',
208209
'src/api/window_bindings.h',
210+
'src/api/v8_internal_helper.cc',
209211
'src/api/menu/menu.cc',
210212
'src/api/menu/menu.h',
211213
'src/api/menu/menu_delegate.cc',
@@ -866,7 +868,7 @@
866868
{
867869
'action_name': 'strip_nw_binaries',
868870
'inputs': [
869-
#'<(PRODUCT_DIR)/nwsnapshot',
871+
'<(PRODUCT_DIR)/nwsnapshot',
870872
'<(PRODUCT_DIR)/chromedriver',
871873
],
872874
'outputs': [
@@ -878,7 +880,7 @@
878880
},
879881
],
880882
'dependencies': [
881-
#'<(DEPTH)/v8/tools/gyp/v8.gyp:nwsnapshot',
883+
'<(DEPTH)/v8/tools/gyp/v8.gyp:nwsnapshot',
882884
'<(DEPTH)/chrome/chrome.gyp:chromedriver',
883885
],
884886
}],

src/api/v8_internal_helper.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "v8/src/v8.h"
2+
3+
using namespace v8;
4+
5+
6+
void FixSourceNWBin(Isolate* v8_isolate, Handle<UnboundScript> script) {
7+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
8+
i::Handle<i::HeapObject> obj =
9+
i::Handle<i::HeapObject>::cast(v8::Utils::OpenHandle(*script));
10+
i::Handle<i::SharedFunctionInfo>
11+
function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate());
12+
reinterpret_cast<i::Script*>(function_info->script())->set_source(isolate->heap()->undefined_value());
13+
}

src/api/window_bindings.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "content/nw/src/api/dispatcher.h"
2828
#include "content/renderer/render_view_impl.h"
2929
#include "grit/nw_resources.h"
30+
3031
#undef LOG
3132
using namespace blink;
3233
#if defined(OS_WIN)
@@ -55,6 +56,8 @@ using namespace blink;
5556
#undef CHECK
5657
#include "V8HTMLIFrameElement.h"
5758

59+
extern void FixSourceNWBin(v8::Isolate* v8_isolate, v8::Handle<v8::UnboundScript> script);
60+
5861
using blink::WebScriptSource;
5962
using blink::WebFrame;
6063
//using blink::InstrumentingAgents;
@@ -117,6 +120,8 @@ WindowBindings::AllocateId(const v8::FunctionCallbackInfo<v8::Value>& args) {
117120
void
118121
WindowBindings::CallObjectMethod(const v8::FunctionCallbackInfo<v8::Value>& args) {
119122
v8::Isolate* isolate = v8::Isolate::GetCurrent();
123+
v8::EscapableHandleScope scope(isolate);
124+
120125
v8::Local<v8::Object> self = args[0]->ToObject();
121126
int routing_id = self->Get(v8::String::NewFromUtf8(isolate, "routing_id"))->Int32Value();
122127
int object_id = self->Get(v8::String::NewFromUtf8(isolate, "id"))->Int32Value();
@@ -153,6 +158,43 @@ WindowBindings::CallObjectMethod(const v8::FunctionCallbackInfo<v8::Value>& args
153158
}
154159
args.GetReturnValue().Set(result);
155160
return;
161+
} else if (method == "EvaluateNWBin") {
162+
base::FilePath path(*v8::String::Utf8Value(args[3]));
163+
base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
164+
if (file.IsValid()) {
165+
int64 length = file.GetLength();
166+
if (length > 0 && length < INT_MAX) {
167+
int size = static_cast<int>(length);
168+
std::vector<unsigned char> raw_data;
169+
raw_data.resize(size);
170+
uint8_t* data = reinterpret_cast<uint8_t*>(&(raw_data.front()));
171+
if (file.ReadAtCurrentPos((char*)data, size) == length) {
172+
v8::Handle<v8::String> source_string = v8::String::NewFromUtf8(isolate, "");
173+
v8::ScriptCompiler::CachedData* cache;
174+
cache = new v8::ScriptCompiler::CachedData(
175+
data, length, v8::ScriptCompiler::CachedData::BufferNotOwned);
176+
v8::ScriptCompiler::Source source(source_string, cache);
177+
v8::Local<v8::UnboundScript> script;
178+
script = v8::ScriptCompiler::CompileUnbound(
179+
isolate, &source, v8::ScriptCompiler::kConsumeCodeCache);
180+
ASSERT(!cache->rejected);
181+
v8::Handle<v8::Value> result;
182+
v8::Handle<v8::Object> frm = v8::Handle<v8::Object>::Cast(args[2]);
183+
WebFrame* web_frame = NULL;
184+
if (frm->IsNull()) {
185+
web_frame = main_frame;
186+
}else{
187+
blink::HTMLIFrameElement* iframe = blink::V8HTMLIFrameElement::toImpl(frm);
188+
web_frame = blink::WebFrame::fromFrame(iframe->contentFrame());
189+
}
190+
v8::Context::Scope cscope (web_frame->mainWorldScriptContext());
191+
FixSourceNWBin(isolate, script);
192+
result = script->BindToCurrentContext()->Run();
193+
args.GetReturnValue().Set(result);
194+
}
195+
}
196+
}
197+
return;
156198
} else if (method == "setDevToolsJail") {
157199
v8::Handle<v8::Object> frm = v8::Handle<v8::Object>::Cast(args[2]);
158200
if (frm->IsNull()) {

src/api/window_bindings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ Window.prototype.eval = function(frame, script) {
509509
return CallObjectMethod(this, 'EvaluateScript', frame, script);
510510
};
511511

512+
Window.prototype.evalNWBin = function(frame, script) {
513+
return CallObjectMethod(this, 'EvaluateNWBin', frame, script);
514+
};
515+
512516
Window.prototype.disableCache = function(flag) {
513517
return CallObjectMethod(this, 'setCacheDisabled', flag);
514518
};

tools/package_binaries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def generate_target_nw(platform_name, arch, version):
118118
'credits.html',
119119
'libffmpegsumo.so',
120120
'nw.pak',
121-
# 'nwsnapshot',
121+
'nwsnapshot',
122122
'nw',
123123
'icudtl.dat',
124124
'locales',
@@ -134,13 +134,13 @@ def generate_target_nw(platform_name, arch, version):
134134
'nw.exe',
135135
'nw.pak',
136136
'locales',
137-
# 'nwsnapshot.exe',
137+
'nwsnapshot.exe',
138138
'credits.html',
139139
]
140140
elif platform_name == 'osx':
141141
target['input'] = [
142142
'nwjs.app',
143-
# 'nwsnapshot',
143+
'nwsnapshot',
144144
'credits.html',
145145
]
146146
else:

0 commit comments

Comments
 (0)