-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbrowser.cc
More file actions
236 lines (182 loc) · 6.78 KB
/
browser.cc
File metadata and controls
236 lines (182 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <node_version.h>
#include "browser.h"
#include "top_v8.h"
using namespace v8;
Persistent<Function> Browser::constructor;
Browser::Browser(QString userAgent, QString libraryCode, QString cookies, bool disableImages) {
userAgent_ = userAgent;
libraryCode_ = libraryCode;
cookies_ = cookies;
disableImages_ = disableImages;
chimera_ = 0;
}
Browser::~Browser() {
delete chimera_;
}
void Browser::Initialize(Handle<Object> target) {
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("Browser"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->PrototypeTemplate()->Set(String::NewSymbol("open"), FunctionTemplate::New(Open)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("close"), FunctionTemplate::New(Close)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("capture"), FunctionTemplate::New(Capture)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("cookies"), FunctionTemplate::New(Cookies)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("setCookies"), FunctionTemplate::New(SetCookies)->GetFunction());
tpl->PrototypeTemplate()->Set(String::NewSymbol("setProxy"), FunctionTemplate::New(SetProxy)->GetFunction());
constructor = Persistent<Function>::New(
tpl->GetFunction());
target->Set(String::NewSymbol("Browser"), constructor);
}
Handle<Value> Browser::New(const Arguments& args) {
HandleScope scope;
QString userAgent = top_v8::ToQString(args[0]->ToString());
QString libraryCode = top_v8::ToQString(args[1]->ToString());
QString cookies = top_v8::ToQString(args[2]->ToString());
bool disableImages = args[3]->BooleanValue();
Browser* w = new Browser(userAgent, libraryCode, cookies, disableImages);
w->Wrap(args.This());
return args.This();
}
void AsyncWork(uv_work_t* req) {
BWork* work = static_cast<BWork*>(req->data);
work->chimera->wait();
work->result = work->chimera->getResult();
work->errorResult = work->chimera->getError();
}
#if NODE_MINOR_VERSION >= 10
void AsyncAfter(uv_work_t* req, int /*status*/) {
#else
void AsyncAfter(uv_work_t* req) {
#endif
HandleScope scope;
BWork* work = static_cast<BWork*>(req->data);
if (work->error) {
Local<Value> err = Exception::Error(String::New(work->error_message.c_str()));
const unsigned argc = 1;
Local<Value> argv[argc] = { err };
TryCatch try_catch;
work->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
} else {
const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Local<Value>::New(top_v8::FromQString(work->result))
};
TryCatch try_catch;
work->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
uv_queue_work(uv_default_loop(), &work->request, AsyncWork, AsyncAfter);
// TODO: we need to figure out where to dispose the callback & free up work
// work->callback.Dispose();
// delete work;
}
Handle<Value> Browser::Cookies(const Arguments& args) {
HandleScope scope;
QString cookies = "";
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
cookies = chimera->getCookies();
}
return scope.Close(top_v8::FromQString(cookies));
}
Handle<Value> Browser::SetCookies(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a cookie string")));
}
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
chimera->setCookies(top_v8::ToQString(args[0]->ToString()));
}
return scope.Close(Undefined());
}
Handle<Value> Browser::SetProxy(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("First argument must be the type of proxy (socks/http)")));
}
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
chimera->setProxy(top_v8::ToQString(args[0]->ToString()), top_v8::ToQString(args[1]->ToString()), args[2]->Int32Value(),
top_v8::ToQString(args[3]->ToString()), top_v8::ToQString(args[4]->ToString()));
}
return scope.Close(Undefined());
}
Handle<Value> Browser::Capture(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a filename string")));
}
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
chimera->capture(top_v8::ToQString(args[0]->ToString()));
}
return scope.Close(Undefined());
}
Handle<Value> Browser::Close(const Arguments& args) {
HandleScope scope;
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
chimera->exit(1);
w->setChimera(0);
chimera->deleteLater();
}
return scope.Close(Undefined());
}
Handle<Value> Browser::Open(const Arguments& args) {
HandleScope scope;
if (!args[1]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("Second argument must be a javascript string")));
}
if (!args[2]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("Third argument must be a callback function")));
}
Local<Function> callback = Local<Function>::Cast(args[2]);
BWork* work = new BWork();
work->error = false;
work->request.data = work;
work->callback = Persistent<Function>::New(callback);
Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {
work->chimera = chimera;
} else {
work->chimera = new Chimera();
work->chimera->setUserAgent(w->userAgent());
work->chimera->setLibraryCode(w->libraryCode());
work->chimera->setCookies(w->cookies());
if (w->disableImages_) {
work->chimera->disableImages();
}
w->setChimera(work->chimera);
}
work->chimera->setEmbedScript(top_v8::ToQString(args[1]->ToString()));
if (args[0]->IsString()) {
work->url = top_v8::ToQString(args[0]->ToString());
work->chimera->open(work->url);
} else {
std::cout << "debug -- about to call execute" << std::endl;
work->chimera->execute();
}
int status = uv_queue_work(uv_default_loop(), &work->request, AsyncWork, AsyncAfter);
assert(status == 0);
return Undefined();
}