-
Notifications
You must be signed in to change notification settings - Fork 6
/
cefengine.cpp
467 lines (397 loc) · 23.5 KB
/
cefengine.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "cefengine.h"
extern QVariantMap settingsData;
extern QVariantMap notificationsData;
extern CefString historyData;
extern SignalBroker* signalBroker;
CefEngine::CefEngine() : CefApp()
{
}
void CefEngine::AddRef() const {
CefRefCount::AddRef();
}
bool CefEngine::Release() const {
return CefRefCount::Release();
}
bool CefEngine::HasOneRef() const {
return CefRefCount::HasOneRef();
}
void CefEngine::OnContextInitialized() {
CefRegisterSchemeHandlerFactory("theweb", "theweb", new theWebSchemeHandler());
}
void CefEngine::OnContextCreated(Browser browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) {
//Get browser settings
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("theWebSettings_get");
browser.get()->SendProcessMessage(PID_BROWSER, message);
if (QUrl(QString::fromStdString(frame.get()->GetURL().ToString())).scheme() == "theweb") {
{
//Register the theWebSettingsObject JavaScript object
CefRefPtr<theWebSettingsAccessor> accessor = new theWebSettingsAccessor(browser);
CefRefPtr<CefV8Value> JsObject = CefV8Value::CreateObject(accessor, new V8Interceptor);
JsObject.get()->SetValue("dnt", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("home", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("toolbar", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("tabText", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("malwareProtect", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("tabPreview", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("history", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("resetBrowser", CefV8Value::CreateFunction("resetBrowser", new V8Function([=]() {
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("confirm(\"Reset theWeb?:Here\'s what we\'ll do.\\n"
"- Reset all settings back to defaults\\n"
"- Erase all history items\\n"
"- Clear the cache\\n"
"- Erase all cookies\\n"
"\\n"
"Are you sure that you want to reset theWeb?:Continue:Cancel\")", "", 0, returnVal, exception);
if (returnVal.get()->GetBoolValue()) {
context.get()->Eval("confirm(\"Reset theWeb?:We're ready to reset theWeb. Once this is done, theWeb will exit. Is this OK?:Reset:Cancel\")", "", 0, returnVal, exception);
}
})), V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("proxySettings", CefV8Value::CreateFunction("proxySettings", new V8Function([=]() {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("showProxy");
browser.get()->SendProcessMessage(PID_BROWSER, message);
})), V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("clearData", CefV8Value::CreateFunction("clearData", new V8Function([=](CefV8ValueList &arguments) {
QVector<CefRefPtr<CefV8Value>> args = QVector<CefRefPtr<CefV8Value>>::fromStdVector(arguments);
if (args.size() == 3) {
//Erase History
if (args[0].get()->GetBoolValue()) {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("clearData_History");
browser.get()->SendProcessMessage(PID_BROWSER, message);
}
//Erase Cache
if (args[1].get()->GetBoolValue()) {
qDebug() << "Erase Cache";
}
//Erase Cookies
if (args[2].get()->GetBoolValue()) {
qDebug() << "Erase Cookies";
}
}
})), V8_PROPERTY_ATTRIBUTE_NONE);
JsObject.get()->SetValue("goToCredits", CefV8Value::CreateFunction("goToCredits", new V8Function([=]() {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("goToCredits");
browser.get()->SendProcessMessage(PID_BROWSER, message);
})), V8_PROPERTY_ATTRIBUTE_NONE);
context.get()->GetGlobal()->SetValue("theWebSettingsObject", JsObject, V8_PROPERTY_ATTRIBUTE_NONE);
}
}
{
{
//Register the Notifications JavaScript Object
CefRefPtr<CefV8Value> notificationObject = CefV8Value::CreateFunction("NotificationConstructor", new V8Function([=](CefV8ValueList &arguments) {
QVector<CefRefPtr<CefV8Value>> args = QVector<CefRefPtr<CefV8Value>>::fromStdVector(arguments);
if (args.count() < 1) {
throw new std::exception;
}
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("jsNotifications_post");
message.get()->GetArgumentList().get()->SetString(0, QUrl(QString::fromStdString(frame.get()->GetURL().ToString())).host().toStdString());
message.get()->GetArgumentList().get()->SetString(1, args.at(0).get()->GetStringValue());
browser.get()->SendProcessMessage(PID_BROWSER, message);
}));
notificationObject.get()->SetValue("permission", CefV8Value::CreateString(notificationsData.value(QUrl(QString::fromStdString(frame.get()->GetURL().ToString())).host(), "default").toString().toStdString()), V8_PROPERTY_ATTRIBUTE_NONE);
notificationObject.get()->SetValue("requestPermission", CefV8Value::CreateFunction("requestPermission", new V8Function((std::function<CefRefPtr<CefV8Value>()>) [=]() {
if (notificationObject.get()->GetValue("permission").get()->GetStringValue() == "default") {
if (notificationRequestPromise == NULL) {
qDebug() << context.get()->GetGlobal().get()->SetValue("theWebNotificationRequest", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("new Promise(function(resolve, reject) {"
"theWebNotificationRequest = resolve;"
"});", "", 0, notificationRequestPromise, exception);
notificationRequestResolver = context.get()->GetGlobal().get()->GetValue("theWebNotificationRequest");
//Ask user permission to show notifications
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("jsNotificationRequest");
message.get()->GetArgumentList().get()->SetString(0, QUrl(QString::fromStdString(frame.get()->GetURL().ToString())).host().toStdString());
browser.get()->SendProcessMessage(PID_BROWSER, message);
notificationRequestFrame = frame;
return notificationRequestPromise;
} else {
return CefV8Value::CreateNull();
}
} else {
return CefV8Value::CreateNull();
}
})), V8_PROPERTY_ATTRIBUTE_NONE);
context.get()->GetGlobal().get()->SetValue("Notification", notificationObject, V8_PROPERTY_ATTRIBUTE_NONE);
}
}
}
void CefEngine::OnContextReleased(Browser browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) {
if (this->videoFrame.get() != NULL) {
//Stop MPRIS because the browser just closed
if (this->videoFrame.get()->GetIdentifier() == frame.get()->GetIdentifier()) {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisDoStop");
browser.get()->SendProcessMessage(PID_BROWSER, message);
this->videoFrame = NULL;
}
}
}
bool CefEngine::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) {
if (message.get()->GetName() == "theWebSettings_reply") {
CefRefPtr<CefListValue> args = message.get()->GetArgumentList();
{
CefRefPtr<CefDictionaryValue> settingsDictionary = args.get()->GetDictionary(0);
std::vector<CefString> keys;
settingsDictionary.get()->GetKeys(keys);
for (CefString key : keys) {
//Convert CEF types into Qt types
if (settingsDictionary.get()->GetType(key) == VTYPE_BOOL) {
settingsData.insert(QString::fromStdString(key.ToString()), settingsDictionary.get()->GetBool(key));
} else if (settingsDictionary.get()->GetType(key) == VTYPE_STRING) {
settingsData.insert(QString::fromStdString(key.ToString()), QString::fromStdString(settingsDictionary.get()->GetString(key).ToString()));
}
}
historyData = args.get()->GetString(2);
}
{
CefRefPtr<CefDictionaryValue> notificationsDictionary = args.get()->GetDictionary(1);
std::vector<CefString> keys;
notificationsDictionary.get()->GetKeys(keys);
for (CefString key : keys) {
//Convert CEF types into Qt types
notificationsData.insert(QString::fromStdString(key.ToString()), QString::fromStdString(notificationsDictionary.get()->GetString(key).ToString()));
}
}
} else if (message.get()->GetName() == "mprisCheck") {
//Iterate over all frames
std::vector<int64> frameIdentifiers;
browser.get()->GetFrameIdentifiers(frameIdentifiers);
for (unsigned long i = 0; i < frameIdentifiers.size(); i++) {
//Check if we should check the browser
bool doCheck = false;
if (this->videoFrame == NULL) { //If there is no browser playing media, check the browser
doCheck = true;
} else if (this->videoFrame.get()->GetIdentifier() == browser.get()->GetFrame(frameIdentifiers[i]).get()->GetIdentifier() &&
browser.get()->GetIdentifier() == this->videoFrame.get()->GetBrowser().get()->GetIdentifier()) {
//If this is the same browser playing media, check the browser
doCheck = true;
}
if (doCheck) {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = browser.get()->GetFrame(frameIdentifiers[i]).get()->GetV8Context();
context.get()->Enter();
//Get all <video> elements
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("document.getElementsByTagName('video').length;", "", 0, returnVal, exception);
if (returnVal.get() == NULL) {
mprisElementTagType = "";
} else {
if (returnVal.get()->GetIntValue() > 0) {
//Video found.
mprisElementTagType = "video";
} else {
//No <video> elements. Get all <audio> elements
context.get()->Eval("document.getElementsByTagName('audio').length;", "", 0, returnVal, exception);
if (returnVal.get()->GetIntValue() > 0) {
//Audio found.
mprisElementTagType = "audio";
} else {
mprisElementTagType = "";
}
}
}
if (mprisElementTagType != "") {
this->videoContext = context;
this->videoFrame = browser.get()->GetFrame(frameIdentifiers[i]);
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisStart");
browser.get()->SendProcessMessage(PID_BROWSER, message);
//Get if the video is paused
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].paused", "", 0, returnVal, exception);
videoPlaying = !returnVal.get()->GetBoolValue();
CefString title, artist, album;
//Provide extra information for videos from youtube.com
if (QUrl(QString::fromStdString(videoFrame.get()->GetURL().ToString())).host().startsWith("www.youtube.com")) {
context.get()->Eval("document.getElementById('eow-title').innerHTML", "", 0, returnVal, exception);
if (returnVal.get() != NULL) {
title = QString::fromStdString(returnVal.get()->GetStringValue().ToString()).trimmed().replace("&", "&")
.replace("<", "<").replace(">", ">").toStdString();
}
context.get()->Eval("document.getElementById('watch7-user-header').childNodes[3].childNodes[1].innerHTML", "", 0, returnVal, exception);
if (returnVal.get() != NULL) {
artist = QString::fromStdString(returnVal.get()->GetStringValue().ToString()).replace("&", "&")
.replace("<", "<").replace(">", ">").toStdString();
}
album = QUrl(QString::fromStdString(videoFrame.get()->GetURL().ToString())).host().toStdString();
}
{
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisData");
message.get()->GetArgumentList().get()->SetBool(0, videoPlaying);
message.get()->GetArgumentList().get()->SetString(1, title);
message.get()->GetArgumentList().get()->SetString(2, artist);
message.get()->GetArgumentList().get()->SetString(3, album);
browser.get()->SendProcessMessage(PID_BROWSER, message);
}
} else {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisStop");
//message.get()->GetArgumentList().get()->SetBool(0, browser.get()->GetFrame(frameIdentifiers[i]).get()->IsMain());
browser.get()->SendProcessMessage(PID_BROWSER, message);
this->videoFrame = NULL;
}
//Exit the V8 Context
context.get()->Exit();
}
}
} else if (message.get()->GetName() == "mprisPlayPause") {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = videoFrame.get()->GetV8Context();
context.get()->Enter();
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
if (videoPlaying) {
//The media is playing, pause the media.
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].pause()", "", 0, returnVal, exception);
} else {
//The media is paused, play the media.
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].play()", "", 0, returnVal, exception);
}
//Exit the V8 Context
context.get()->Exit();
//Do MPRIS Check
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisCheck");
browser.get()->SendProcessMessage(PID_RENDERER, message);
} else if (message.get()->GetName() == "mprisPlay") {
if (!videoPlaying) {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = videoFrame.get()->GetV8Context();
context.get()->Enter();
//The media is paused, play the media.
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].play()", "", 0, returnVal, exception);
//Exit the V8 Context
context.get()->Exit();
}
//Do MPRIS Check
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisCheck");
browser.get()->SendProcessMessage(PID_RENDERER, message);
} else if (message.get()->GetName() == "mprisPause") {
if (videoPlaying) {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = videoFrame.get()->GetV8Context();
context.get()->Enter();
//The media is playing, pause the media.
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].pause()", "", 0, returnVal, exception);
//Exit the V8 Context
context.get()->Exit();
}
//Do MPRIS Check
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisCheck");
browser.get()->SendProcessMessage(PID_RENDERER, message);
} else if (message.get()->GetName() == "mprisBack") {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = videoFrame.get()->GetV8Context();
context.get()->Enter();
//Set the media to 0 seconds
CefRefPtr<CefV8Value> returnVal;
CefRefPtr<CefV8Exception> exception;
context.get()->Eval("document.getElementsByTagName('" + mprisElementTagType.toStdString() + "')[0].currentTime = 0", "", 0, returnVal, exception);
//Exit the V8 Context
context.get()->Exit();
//Do MPRIS Check
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("mprisCheck");
browser.get()->SendProcessMessage(PID_RENDERER, message);
} else if (message.get()->GetName() == "jsNotificationRequest_Reply") {
//Enter the V8 Context
CefRefPtr<CefV8Context> context = notificationRequestFrame->GetV8Context();
context.get()->Enter();
CefRefPtr<CefProcessMessage> setMessage = CefProcessMessage::Create("jsNotifications_set");
setMessage.get()->GetArgumentList().get()->SetString(0, QUrl(QString::fromStdString(notificationRequestFrame.get()->GetURL().ToString())).host().toStdString());
CefV8ValueList args;
if (message.get()->GetArgumentList().get()->GetBool(0)) {
args.push_back(CefV8Value::CreateString("granted"));
context.get()->GetGlobal().get()->GetValue("Notification").get()->SetValue("permission", CefV8Value::CreateString("granted"), V8_PROPERTY_ATTRIBUTE_NONE);
notificationsData.insert(QUrl(QString::fromStdString(notificationRequestFrame.get()->GetURL().ToString())).host(), "granted");
setMessage.get()->GetArgumentList().get()->SetString(1, "granted");
} else {
args.push_back(CefV8Value::CreateString("denied"));
context.get()->GetGlobal().get()->GetValue("Notification").get()->SetValue("permission", CefV8Value::CreateString("denied"), V8_PROPERTY_ATTRIBUTE_NONE);
notificationsData.insert(QUrl(QString::fromStdString(notificationRequestFrame.get()->GetURL().ToString())).host(), "denied");
setMessage.get()->GetArgumentList().get()->SetString(1, "denied");
}
notificationRequestResolver.get()->ExecuteFunction(NULL, args);
browser.get()->SendProcessMessage(PID_RENDERER, setMessage);
//notificationRequestPromise.get()->GetValue("resolve").get()->ExecuteFunction(NULL, args);
/*if (message.get()->GetArgumentList().get()->GetBool(0)) {
browser.get()->GetMainFrame().get()->ExecuteJavaScript("theWebNotificationRequest(\"granted\")", "", 0);
} else {
browser.get()->GetMainFrame().get()->ExecuteJavaScript("theWebNotificationRequest(\"denied\")", "", 0);
}*/
//Exit the V8 Context
context.get()->Exit();
notificationRequestPromise = NULL;
}
return true;
}
bool CefEngine::OnPrintDialog(bool has_selection, CefRefPtr<CefPrintDialogCallback> callback) {
emit signalBroker->PrintDialog(this->printingBrowser, this->printer, has_selection, callback);
return true;
}
void CefEngine::OnPrintStart(Browser browser) {
if (printer == NULL) {
printer = new QPrinter;
}
this->printingBrowser = browser;
}
void CefEngine::OnPrintSettings(CefRefPtr<CefPrintSettings> settings, bool get_defaults) {
if (get_defaults) {
getCefPrinterSettings(this->printer, settings);
} else {
printer->setCollateCopies(settings.get()->WillCollate());
printer->setOrientation(settings.get()->IsLandscape() ? QPrinter::Landscape : QPrinter::Portrait);
}
}
void CefEngine::OnPrintReset() {
this->printingBrowser = NULL;
}
bool CefEngine::OnPrintJob(const CefString &document_name, const CefString &pdf_file_path, CefRefPtr<CefPrintJobCallback> callback) {
QString path = QString::fromStdString(pdf_file_path);
//Set up the printer
printer->setDocName(QString::fromStdString(document_name));
Poppler::Document *document = Poppler::Document::load(QString::fromStdString(pdf_file_path));
//Make the output look a bit better
document->setRenderHint(Poppler::Document::Antialiasing);
document->setRenderHint(Poppler::Document::TextAntialiasing);
//Start to print the document
QPrinter::Margins margins = printer->margins();
QPainter* painter = new QPainter(this->printer);
//Iterate over all pages
for (int i = 0; i < document->numPages(); i++) {
//Render page as an image
QImage render = document->page(i)->renderToImage(printer->resolution(), printer->resolution());
//render.save("/home/victor/printTest.png");
//document->page(i)->renderToPainter(painter, printer->resolution(), printer->resolution());
//Draw image onto printer
painter->drawImage(margins.left, margins.top, render);
//Check if we need to feed a new page
if (i != document->numPages() - 1) {
//Feed a new page
printer->newPage();
}
}
//Complete the printing
painter->end();
//Tell CEF to continue
callback.get()->Continue();
return true;
}
CefRefPtr<CefPrintSettings> CefEngine::getCefPrinterSettings(QPrinter* printer, CefRefPtr<CefPrintSettings> currentSettings) {
currentSettings.get()->SetDeviceName(printer->printerName().toStdString());
currentSettings.get()->SetCollate(printer->collateCopies());
currentSettings.get()->SetOrientation(printer->orientation() == QPrinter::Landscape);
currentSettings.get()->SetDPI(printer->resolution());
currentSettings.get()->SetCopies(printer->numCopies());
currentSettings.get()->SetColorModel(printer->colorMode() == QPrinter::Color ? COLOR_MODEL_COLOR : COLOR_MODEL_GRAY);
currentSettings.get()->SetDuplexMode(printer->doubleSidedPrinting() ? DUPLEX_MODE_LONG_EDGE : DUPLEX_MODE_SIMPLEX);
CefSize size;
CefRect area;
QRect paperSize = printer->paperRect();
size.Set(paperSize.width(), paperSize.height());
QPrinter::Margins margins = printer->margins();
area.Set(margins.left, margins.top, paperSize.width() - margins.left - margins.right, paperSize.height() - margins.top - margins.bottom);
currentSettings.get()->SetPrinterPrintableArea(size, area, true);
return currentSettings;
}