-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser-client.cpp
405 lines (333 loc) · 9.89 KB
/
browser-client.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
#include "browser-client.hpp"
#include "base64/base64.hpp"
#include "json11/json11.hpp"
#include <QApplication>
#include <QThread>
#include <QToolTip>
#include "GrpcBrowser.h"
#include "JavascriptApi.h"
#include "SlBrowser.h"
#include "WindowsFunctions.h"
#include <json11/json11.hpp>
using namespace json11;
inline bool BrowserClient::valid() const
{
return true;
}
CefRefPtr<CefLoadHandler> BrowserClient::GetLoadHandler()
{
return this;
}
CefRefPtr<CefRenderHandler> BrowserClient::GetRenderHandler()
{
return this;
}
CefRefPtr<CefDisplayHandler> BrowserClient::GetDisplayHandler()
{
return this;
}
CefRefPtr<CefLifeSpanHandler> BrowserClient::GetLifeSpanHandler()
{
return this;
}
CefRefPtr<CefContextMenuHandler> BrowserClient::GetContextMenuHandler()
{
return this;
}
CefRefPtr<CefAudioHandler> BrowserClient::GetAudioHandler()
{
return m_reroute_audio ? this : nullptr;
}
#if CHROME_VERSION_BUILD >= 4638
CefRefPtr<CefRequestHandler> BrowserClient::GetRequestHandler()
{
return this;
}
CefRefPtr<CefResourceRequestHandler> BrowserClient::GetResourceRequestHandler(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest> request, bool, bool, const CefString &, bool &)
{
if (request->GetHeaderByName("origin") == "null")
{
return this;
}
return nullptr;
}
CefResourceRequestHandler::ReturnValue BrowserClient::OnBeforeResourceLoad(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest>, CefRefPtr<CefCallback>)
{
return RV_CONTINUE;
}
#endif
bool BrowserClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, const CefString &, const CefString &, cef_window_open_disposition_t, bool, const CefPopupFeatures &, CefWindowInfo &, CefRefPtr<CefClient> &, CefBrowserSettings &, CefRefPtr<CefDictionaryValue> &, bool *)
{
/* block popups */
return true;
}
void BrowserClient::OnBeforeContextMenu(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefContextMenuParams>, CefRefPtr<CefMenuModel> model)
{
/* remove all context menu contributions */
model->Clear();
}
/*static*/
json11::Json convertCefValueToJSON(CefRefPtr<CefValue> value)
{
switch (value->GetType())
{
case VTYPE_NULL:
return nullptr;
case VTYPE_BOOL:
return value->GetBool();
case VTYPE_INT:
return value->GetInt();
case VTYPE_DOUBLE:
return value->GetDouble();
case VTYPE_STRING:
return value->GetString().ToString();
case VTYPE_LIST: {
const auto &list = value->GetList();
std::vector<json11::Json> jsonList;
for (size_t i = 0; i < list->GetSize(); ++i)
{
jsonList.push_back(convertCefValueToJSON(list->GetValue(i)));
}
return jsonList;
}
case VTYPE_DICTIONARY: {
const auto &dict = value->GetDictionary();
std::map<std::string, json11::Json> jsonMap;
CefDictionaryValue::KeyList keys;
dict->GetKeys(keys);
for (const auto &key : keys)
{
jsonMap[key] = convertCefValueToJSON(dict->GetValue(key));
}
return jsonMap;
}
default:
return nullptr;
}
}
/*static*/
std::string BrowserClient::cefListValueToJSONString(CefRefPtr<CefListValue> listValue)
{
std::map<std::string, json11::Json> jsonMap;
for (size_t i = 0; i < listValue->GetSize(); ++i)
{
// Convert index to string key like "param1", "param2", ...
std::string key = "param" + std::to_string(i + 1);
jsonMap[key] = convertCefValueToJSON(listValue->GetValue(i));
}
std::string json_str;
json_str = json11::Json(jsonMap).dump();
return json_str;
}
CefRefPtr<CefBrowser> BrowserClient::GetMostRecentRenderKnown()
{
std::lock_guard<std::recursive_mutex> grd(m_recursiveMutex);
return m_MostRecentRenderKnowOf;
}
void BrowserClient::RegisterCallback(const int functionId, CefRefPtr<CefBrowser> browser)
{
std::lock_guard<std::recursive_mutex> grd(m_recursiveMutex);
m_callbackDictionary[functionId] = browser;
m_MostRecentRenderKnowOf = browser;
}
CefRefPtr<CefBrowser> BrowserClient::PopCallback(const int functionId)
{
std::lock_guard<std::recursive_mutex> grd(m_recursiveMutex);
auto itr = m_callbackDictionary.find(functionId);
if (itr != m_callbackDictionary.end())
{
CefRefPtr<CefBrowser> browser = itr->second;
m_callbackDictionary.erase(itr);
return browser;
}
return nullptr;
}
bool BrowserClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>, CefProcessId processId, CefRefPtr<CefProcessMessage> message)
{
const std::string &name = message->GetName();
CefRefPtr<CefListValue> input_args = message->GetArgumentList();
if (!valid())
return false;
int funcid = input_args->GetInt(0);
if (JavascriptApi::isBrowserFunctionName(name))
{
std::string jsonOutput = "{}";
std::vector <CefRefPtr<CefValue>> argsWithoutFunc;
for (u_long l = 1; l < input_args->GetSize(); l++)
argsWithoutFunc.push_back(input_args->GetValue(l));
// Stuff done right here and now to the browser
// Put this into a sub function if it gets bigger
switch (JavascriptApi::getFunctionId(name))
{
case JavascriptApi::JS_BROWSER_RESIZE_BROWSER:
{
if (argsWithoutFunc.size() < 2)
{
jsonOutput = Json(Json::object({{"error", "Invalid parameters"}})).dump();
break;
}
int w = argsWithoutFunc[0]->GetInt();
int h = argsWithoutFunc[1]->GetInt();
if (w < 200 || h < 200 || w > 8096 || h > 8096)
{
jsonOutput = Json(Json::object({{"error", "Invalid parameters"}})).dump();
break;
}
SlBrowser::instance().m_widget->resize(w, h);
break;
}
case JavascriptApi::JS_BROWSER_BRING_FRONT:
{
HWND hwnd = HWND(SlBrowser::instance().m_widget->winId());
if (::IsIconic(hwnd))
::ShowWindow(hwnd, SW_RESTORE);
WindowsFunctions::ForceForegroundWindow(hwnd);
break;
}
case JavascriptApi::JS_BROWSER_SET_WINDOW_POSITION:
{
if (argsWithoutFunc.size() < 2)
{
jsonOutput = Json(Json::object({{"error", "Invalid parameters"}})).dump();
break;
}
int x = argsWithoutFunc[0]->GetInt();
int y = argsWithoutFunc[1]->GetInt();
SlBrowser::instance().m_widget->move(x, y);
break;
}
case JavascriptApi::JS_BROWSER_SET_ALLOW_HIDE_BROWSER:
{
if (argsWithoutFunc.size() < 1)
{
jsonOutput = Json(Json::object({{"error", "Invalid parameters"}})).dump();
break;
}
SlBrowser::instance().m_allowHideBrowser = argsWithoutFunc[0]->GetBool();
break;
}
case JavascriptApi::JS_BROWSER_SET_HIDDEN_STATE:
{
if (argsWithoutFunc.size() < 1)
{
jsonOutput = Json(Json::object({{"error", "Invalid parameters"}})).dump();
break;
}
SlBrowser::instance().m_widget->setHidden(argsWithoutFunc[0]->GetBool());
SlBrowser::instance().saveHiddenState(SlBrowser::instance().m_widget->isHidden());
if (!SlBrowser::instance().m_widget->isHidden())
{
HWND hwnd = HWND(SlBrowser::instance().m_widget->winId());
WindowsFunctions::ForceForegroundWindow(hwnd);
}
break;
}
}
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("executeCallback");
CefRefPtr<CefListValue> execute_args = msg->GetArgumentList();
execute_args->SetInt(0, funcid);
execute_args->SetString(1, jsonOutput);
SendBrowserProcessMessage(browser, PID_RENDERER, msg);
}
else
{
RegisterCallback(funcid, browser);
if (!GrpcBrowser::instance().getClient()->send_js_api(name, cefListValueToJSONString(input_args)))
{
// todo; handle
abort();
return false;
}
}
return true;
}
void BrowserClient::GetViewRect(CefRefPtr<CefBrowser>, CefRect &rect)
{
if (!valid())
{
rect.Set(0, 0, 16, 16);
return;
}
rect.Set(0, 0, 1920, 1080);
}
bool BrowserClient::OnTooltip(CefRefPtr<CefBrowser>, CefString &text)
{
std::string str_text = text;
QMetaObject::invokeMethod(QCoreApplication::instance()->thread(), [str_text]() { QToolTip::showText(QCursor::pos(), str_text.c_str()); });
return true;
}
void BrowserClient::OnPaint(CefRefPtr<CefBrowser>, PaintElementType type, const RectList &, const void *buffer, int width, int height)
{
if (type != PET_VIEW)
{
// TODO Overlay texture on top of bs->texture
return;
}
if (!valid())
{
return;
}
}
void BrowserClient::OnAudioStreamStarted(CefRefPtr<CefBrowser> browser, const CefAudioParameters ¶ms_, int channels_)
{
m_channels = channels_;
m_channel_layout = (ChannelLayout)params_.channel_layout;
m_sample_rate = params_.sample_rate;
m_frames_per_buffer = params_.frames_per_buffer;
}
void BrowserClient::OnAudioStreamPacket(CefRefPtr<CefBrowser> browser, const float **data, int frames, int64_t pts)
{
if (!valid())
{
return;
}
}
void BrowserClient::OnAudioStreamStopped(CefRefPtr<CefBrowser> browser) {}
void BrowserClient::OnAudioStreamError(CefRefPtr<CefBrowser> browser, const CefString &message) {}
static CefAudioHandler::ChannelLayout Convert2CEFSpeakerLayout(int channels)
{
switch (channels)
{
case 1:
return CEF_CHANNEL_LAYOUT_MONO;
case 2:
return CEF_CHANNEL_LAYOUT_STEREO;
case 3:
return CEF_CHANNEL_LAYOUT_2_1;
case 4:
return CEF_CHANNEL_LAYOUT_4_0;
case 5:
return CEF_CHANNEL_LAYOUT_4_1;
case 6:
return CEF_CHANNEL_LAYOUT_5_1;
case 8:
return CEF_CHANNEL_LAYOUT_7_1;
default:
return CEF_CHANNEL_LAYOUT_UNSUPPORTED;
}
}
bool BrowserClient::GetAudioParameters(CefRefPtr<CefBrowser> browser, CefAudioParameters ¶ms)
{
int channels = 2;
params.channel_layout = CEF_CHANNEL_LAYOUT_MONO;
params.sample_rate = 48000;
params.frames_per_buffer = kFramesPerBuffer;
return true;
}
void BrowserClient::OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, TransitionType transition_type)
{
SlBrowser::instance().setMainLoadingInProgress(true);
}
void BrowserClient::OnLoadEnd(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame> frame, int httpStatusCode)
{
if (httpStatusCode == 200)
SlBrowser::instance().setMainPageSuccess(true);
SlBrowser::instance().setMainLoadingInProgress(false);
}
void BrowserClient::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString &errorText, const CefString &failedUrl)
{
}
bool BrowserClient::OnConsoleMessage(CefRefPtr<CefBrowser>, cef_log_severity_t level, const CefString &message, const CefString &source, int line)
{
return false;
}