-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdullahan_browser_client.cpp
More file actions
502 lines (414 loc) · 16 KB
/
Copy pathdullahan_browser_client.cpp
File metadata and controls
502 lines (414 loc) · 16 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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
@brief Dullahan - a headless browser rendering engine
based around the Chromium Embedded Framework
@author Callum Prentice 2017
Copyright (c) 2017, Linden Research, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define NOMINMAX
#include "cef_browser.h"
#include "wrapper/cef_helpers.h"
#include "dullahan_render_handler.h"
#include "dullahan_browser_client.h"
#include "dullahan_callback_manager.h"
#include "dullahan_impl.h"
#include <algorithm>
#include <chrono>
#include <thread>
dullahan_browser_client::dullahan_browser_client(dullahan_impl* parent,
scoped_refptr<dullahan_render_handler> render_handler) :
mParent(parent),
mRenderHandler(render_handler)
{
DLNOUT("dullahan_browser_client::dullahan_browser_client - parent ptr = " << parent);
}
dullahan_browser_client::~dullahan_browser_client()
{
mRenderHandler = nullptr;
}
// CefClient override
CefRefPtr<CefRenderHandler> dullahan_browser_client::GetRenderHandler()
{
return mRenderHandler;
}
// CefLifeSpanHandler override
bool dullahan_browser_client::OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int popup_id,
const CefString& target_url,
const CefString& target_frame_name,
CefLifeSpanHandler::WindowOpenDisposition target_disposition,
bool user_gesture,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
CefRefPtr<CefDictionaryValue>& extra_info,
bool* no_javascript_access)
{
CEF_REQUIRE_UI_THREAD();
std::string url = std::string(target_url);
std::string target = std::string(target_frame_name);
// target = "_blank" is the official W3 way to specify a new tab/window but clickong on a link
// arrives here with target_frame_name = "". I suspect there is more details I haven't worked out yet
// but for the moment, I'm going to manually insert the "_blank" string so links like this inside
// Second Life start working again.
if (target.length() == 0)
{
target = "_blank";
}
// tell the calling app a popup wants to open
mParent->getCallbackManager()->onOpenPopup(url, target);
// return true indicates CEF should not open the popup which is correct
// the app consuming this code is responsible for creating a new window
// and navigating to the popup URL itself
return true;
}
bool dullahan_browser_client::GetAudioParameters(CefRefPtr<CefBrowser> browser,
CefAudioParameters& params)
{
params.channel_layout = CEF_CHANNEL_LAYOUT_7_1;
params.sample_rate = 48000;
params.frames_per_buffer = 1024;
return true;
}
void dullahan_browser_client::OnAudioStreamStarted(CefRefPtr<CefBrowser> browser,
const CefAudioParameters& params,
int channels)
{
dullahan::dullahan_audio_stream_info info;
info.sample_rate = params.sample_rate;
info.channels = channels;
info.channel_layout = static_cast<int>(params.channel_layout);
info.frames_per_buffer = params.frames_per_buffer;
mParent->getCallbackManager()->onAudioStreamStarted(info);
}
void dullahan_browser_client::OnAudioStreamPacket(CefRefPtr<CefBrowser> browser,
const float** data,
int frames,
int64_t pts)
{
mParent->getCallbackManager()->onAudioStreamPacket(data, frames, pts);
}
void dullahan_browser_client::OnAudioStreamStopped(CefRefPtr<CefBrowser> browser)
{
mParent->getCallbackManager()->onAudioStreamStopped();
}
void dullahan_browser_client::OnAudioStreamError(CefRefPtr<CefBrowser> browser,
const CefString& message)
{
mParent->getCallbackManager()->onAudioStreamError(std::string(message));
}
// CefLifeSpanHandler override
void dullahan_browser_client::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
mBrowserList.push_back(browser);
}
// CefLifeSpanHandler override
void dullahan_browser_client::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
BrowserList::iterator bit = mBrowserList.begin();
for (; bit != mBrowserList.end(); ++bit)
{
if ((*bit)->IsSame(browser))
{
mBrowserList.erase(bit);
break;
}
}
if (mBrowserList.empty())
{
// necessary to enforce CEF finishes work before exit - writing cookies file for example.
// see: https://github.com/chromiumembedded/cef/blob/2773518869b5f57a848e807ddb2ee30adbf1c255/tests/shared/browser/main_message_loop_external_pump_win.cc#L91
// and: https://bitbucket.org/chromiumembedded/cef/issues/1805/
const int num_extra_cef_work_loops = 10;
const int sleep_time_between_calls = 50;
for (int i = 0; i < num_extra_cef_work_loops; ++i)
{
CefDoMessageLoopWork();
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time_between_calls));
}
mParent->getCallbackManager()->onRequestExit();
}
}
// CefLifeSpanHandler override
bool dullahan_browser_client::DoClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
return false;
}
// CefDisplayhandler override
void dullahan_browser_client::OnAddressChange(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, const CefString& url)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->onAddressChange(std::string(url));
}
// CefDisplayhandler override
bool dullahan_browser_client::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
cef_log_severity_t level, const CefString& message,
const CefString& source, int line)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->onConsoleMessage(std::string(message),
std::string(source), line);
return true;
}
// CefDisplayhandler override
void dullahan_browser_client::OnStatusMessage(CefRefPtr<CefBrowser> browser,
const CefString& value)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->onStatusMessage(std::string(value));
}
// CefDisplayhandler overrides
void dullahan_browser_client::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->onTitleChange(std::string(title));
}
// CefDisplayhandler overrides
bool dullahan_browser_client::OnTooltip(CefRefPtr<CefBrowser> browser,
CefString& text)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->OnTooltip(std::string(text));
return false;
}
// CefDisplayhandler overrides
bool dullahan_browser_client::OnCursorChange(CefRefPtr<CefBrowser> browser,
CefCursorHandle cursor, cef_cursor_type_t type,
const CefCursorInfo& custom_cursor_info)
{
CEF_REQUIRE_UI_THREAD();
mParent->getCallbackManager()->onCursorChanged((dullahan::ECursorType)type);
return false;
}
// CefLoadHandler override
void dullahan_browser_client::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading, bool canGoBack, bool canGoForward)
{
CEF_REQUIRE_UI_THREAD();
// Terrible hack but AFAICT, this is the only (and perhaps even official) way to
// establish a zoom across the browser - there ought to be a setting at startup to change this.
// Each time a page load starts/ends, this will re-request the zoom. The page zoom is reset
// between pages so the effect is very jarring but it's the best we can do right now.
mParent->requestPageZoom();
}
// CefLoadHandler override
void dullahan_browser_client::OnLoadStart(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
TransitionType transition_type)
{
CEF_REQUIRE_UI_THREAD();
if (frame->IsMain())
{
mParent->getCallbackManager()->onLoadStart();
}
}
// CefLoadHandler override
void dullahan_browser_client::OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, int httpStatusCode)
{
CEF_REQUIRE_UI_THREAD();
if (frame->IsMain())
{
const std::string url = frame->GetURL();
mParent->getCallbackManager()->onLoadEnd(httpStatusCode, url);
}
}
// CefLoadHandler override
void dullahan_browser_client::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, ErrorCode errorCode,
const CefString& errorText, const CefString& failedUrl)
{
CEF_REQUIRE_UI_THREAD();
if (errorCode == ERR_ABORTED)
{
return;
}
if (frame->IsMain())
{
mParent->getCallbackManager()->onLoadError(errorCode, std::string(errorText), std::string(failedUrl) );
}
}
// CefRequestHandler override
bool dullahan_browser_client::OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request,
bool user_gesture, bool isRedirect)
{
CEF_REQUIRE_UI_THREAD();
std::string url = request->GetURL();
// for comparison, use lowercase
std::transform(url.begin(), url.end(), url.begin(), [](char c)
{
return static_cast<char>(tolower(c));
});
std::vector<std::string>::iterator iter = mParent->getCustomSchemes().begin();
while (iter != mParent->getCustomSchemes().end())
{
if (url.substr(0, (*iter).length()) == (*iter))
{
// get URL again since we lower cased it for comparison
url = request->GetURL();
// also pass over the user_gesture and isRedirect flags - see the CefRequestHandler
// header file for details - the user_gesture tells us if a link was clicked
// or navigated to - something we care about deeply for the custom scheme support
mParent->getCallbackManager()->onCustomSchemeURL(url, user_gesture, isRedirect);
// don't continue with navigation
return true;
}
++iter;
}
return false;
}
// CefRequestHandler override
bool dullahan_browser_client::GetAuthCredentials(CefRefPtr<CefBrowser> browser, const CefString& origin_url, bool isProxy,
const CefString& host, int port, const CefString& realm,
const CefString& scheme, CefRefPtr<CefAuthCallback> callback)
{
CEF_REQUIRE_IO_THREAD();
std::string host_str = host;
std::string realm_str = realm;
std::string scheme_str = scheme;
std::string username = "";
std::string password = "";
bool proceed = mParent->getCallbackManager()->onHTTPAuth(host_str, realm_str, username, password);
if (proceed)
{
callback->Continue(username.c_str(), password.c_str());
return true; // continue with request
}
else
{
callback->Cancel();
return false; // cancel request
}
}
// CefDownloadHandler overrides
bool dullahan_browser_client::OnBeforeDownload(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback)
{
CEF_REQUIRE_UI_THREAD();
// this triggers display of file dialog then dullahan_browser_client::OnFileDialog(..)
// intercepts that and does the right thing.
bool show_file_dialog = true;
callback->Continue(suggested_name, show_file_dialog);
return true;
}
void dullahan_browser_client::OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
CefRefPtr<CefDownloadItemCallback> callback)
{
CEF_REQUIRE_UI_THREAD();
bool is_in_progress = download_item->IsInProgress();
int percent_complete = download_item->GetPercentComplete();
bool is_complete = download_item->IsComplete();
if (is_in_progress)
{
mParent->getCallbackManager()->onFileDownloadProgress(percent_complete, is_complete);
}
}
// CefDialogHandler orerrides
bool dullahan_browser_client::OnFileDialog(CefRefPtr<CefBrowser> browser,
FileDialogMode mode,
const CefString& title,
const CefString& default_file_path,
const std::vector<CefString>& accept_filters,
const std::vector<CefString>& accept_extensions,
const std::vector<CefString>& accept_descriptions,
CefRefPtr<CefFileDialogCallback> callback)
{
CEF_REQUIRE_UI_THREAD();
dullahan::EFileDialogType dialog_type = dullahan::FD_UNKNOWN;
if ((mode & 0x0f) == FileDialogMode::FILE_DIALOG_OPEN)
{
dialog_type = dullahan::FD_OPEN_FILE;
}
else if ((mode & 0x0f) == FileDialogMode::FILE_DIALOG_OPEN_FOLDER)
{
dialog_type = dullahan::FD_OPEN_FOLDER;
}
else if ((mode & 0x0f) == FileDialogMode::FILE_DIALOG_OPEN_MULTIPLE)
{
dialog_type = dullahan::FD_OPEN_MULTIPLE_FILES;
}
else if ((mode & 0x0f) == FileDialogMode::FILE_DIALOG_SAVE)
{
dialog_type = dullahan::FD_SAVE_FILE;
}
const std::string dialog_title = std::string(title);
std::string dialog_accept_filter = std::string();
if (accept_filters.size() > 0)
{
dialog_accept_filter = std::string(accept_filters[0]);
}
const std::string default_file = std::string(default_file_path);
bool use_default = true;
const std::vector<std::string> file_paths = mParent->getCallbackManager()->onFileDialog(dialog_type, dialog_title, default_file, dialog_accept_filter, use_default);
if (use_default)
{
return false;
}
if (file_paths.size())
{
std::vector<CefString> cef_file_paths;
for (std::vector<std::string>::const_iterator iter = file_paths.begin(); iter != file_paths.end(); ++iter)
{
cef_file_paths.push_back(*iter);
}
callback->Continue(cef_file_paths);
}
else
{
callback->Cancel();
}
return true;
}
// CefJSDialogHandler overrides
bool dullahan_browser_client::OnJSDialog(CefRefPtr<CefBrowser> browser,
const CefString& origin_url,
JSDialogType dialog_type,
const CefString& message_text,
const CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool& suppress_message)
{
suppress_message = mParent->getCallbackManager()->onJSDialogCallback(std::string(origin_url),
std::string(message_text),
std::string(default_prompt_text));
return false;
}
bool dullahan_browser_client::OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
const CefString& message_text,
bool is_reload,
CefRefPtr<CefJSDialogCallback> callback)
{
bool suppress_dialog = mParent->getCallbackManager()->onJSBeforeUnloadCallback();
if (suppress_dialog)
{
const CefString user_input("");
callback->Continue(true, user_input);
return true;
}
return false;
}