CEFInterfaceBrowserHandler.cpp
23.3 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFBrowserHandler.cpp
#include "CEF/CEFInterfaceBrowserHandler.h"
#include "HAL/PlatformApplicationMisc.h"
#if WITH_CEF3
#include "WebInterfaceBrowserModule.h"
#include "CEFInterfaceBrowserClosureTask.h"
#include "IWebInterfaceBrowserSingleton.h"
#include "WebInterfaceBrowserSingleton.h"
#include "CEFInterfaceBrowserPopupFeatures.h"
#include "CEFWebInterfaceBrowserWindow.h"
#include "CEFInterfaceBrowserByteResource.h"
#include "Framework/Application/SlateApplication.h"
#include "HAL/ThreadingBase.h"
#include "PlatformHttp.h"
#define LOCTEXT_NAMESPACE "WebInterfaceBrowserHandler"
// Used to force returning custom content instead of performing a request.
const FString CustomContentMethod(TEXT("X-GET-CUSTOM-CONTENT"));
FCEFInterfaceBrowserHandler::FCEFInterfaceBrowserHandler(bool InUseTransparency, const TArray<FString>& InAltRetryDomains)
: bUseTransparency(InUseTransparency),
AltRetryDomains(InAltRetryDomains)
{ }
void FCEFInterfaceBrowserHandler::OnTitleChange(CefRefPtr<CefBrowser> Browser, const CefString& Title)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->SetTitle(Title);
}
}
void FCEFInterfaceBrowserHandler::OnAddressChange(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, const CefString& Url)
{
if (Frame->IsMain())
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->SetUrl(Url);
}
}
}
bool FCEFInterfaceBrowserHandler::OnTooltip(CefRefPtr<CefBrowser> Browser, CefString& Text)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->SetToolTip(Text);
}
return false;
}
bool FCEFInterfaceBrowserHandler::OnConsoleMessage(CefRefPtr<CefBrowser> Browser, const CefString& Message, const CefString& Source, int Line)
{
ConsoleMessageDelegate.ExecuteIfBound(Browser, Message, Source, Line);
// Return false to let it output to console.
return false;
}
void FCEFInterfaceBrowserHandler::OnAfterCreated(CefRefPtr<CefBrowser> Browser)
{
if(Browser->IsPopup())
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindowParent = ParentHandler.get() ? ParentHandler->BrowserWindowPtr.Pin() : nullptr;
if(BrowserWindowParent.IsValid() && ParentHandler->OnCreateWindow().IsBound())
{
TSharedPtr<FWebInterfaceBrowserWindowInfo> NewBrowserWindowInfo = MakeShareable(new FWebInterfaceBrowserWindowInfo(Browser, this));
TSharedPtr<IWebInterfaceBrowserWindow> NewBrowserWindow = IWebInterfaceBrowserModule::Get().GetSingleton()->CreateBrowserWindow(
BrowserWindowParent,
NewBrowserWindowInfo
);
{
// @todo: At the moment we need to downcast since the handler does not support using the interface.
TSharedPtr<FCEFWebInterfaceBrowserWindow> HandlerSpecificBrowserWindow = StaticCastSharedPtr<FCEFWebInterfaceBrowserWindow>(NewBrowserWindow);
BrowserWindowPtr = HandlerSpecificBrowserWindow;
}
// Request a UI window for the browser. If it is not created we do some cleanup.
bool bUIWindowCreated = ParentHandler->OnCreateWindow().Execute(TWeakPtr<IWebInterfaceBrowserWindow>(NewBrowserWindow), TWeakPtr<IWebInterfaceBrowserPopupFeatures>(BrowserPopupFeatures));
if(!bUIWindowCreated)
{
NewBrowserWindow->CloseBrowser(true);
}
else
{
checkf(!NewBrowserWindow.IsUnique(), TEXT("Handler indicated that new window UI was created, but failed to save the new WebBrowserWindow instance."));
}
}
else
{
Browser->GetHost()->CloseBrowser(true);
}
}
}
bool FCEFInterfaceBrowserHandler::DoClose(CefRefPtr<CefBrowser> Browser)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if(BrowserWindow.IsValid())
{
BrowserWindow->OnBrowserClosing();
}
#if PLATFORM_WINDOWS
// If we have a window handle, we're rendering directly to the screen and not off-screen
HWND NativeWindowHandle = Browser->GetHost()->GetWindowHandle();
if (NativeWindowHandle != nullptr)
{
HWND ParentWindow = ::GetParent(NativeWindowHandle);
if (ParentWindow)
{
HWND FocusHandle = ::GetFocus();
if (FocusHandle && (FocusHandle == NativeWindowHandle || ::IsChild(NativeWindowHandle, FocusHandle)))
{
// Set focus to the parent window, otherwise keyboard and mouse wheel input will become wonky
::SetFocus(ParentWindow);
}
// CEF will send a WM_CLOSE to the parent window and potentially exit the application if we don't do this
::SetParent(NativeWindowHandle, nullptr);
}
}
#endif
return false;
}
void FCEFInterfaceBrowserHandler::OnBeforeClose(CefRefPtr<CefBrowser> Browser)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnBrowserClosed();
}
}
bool FCEFInterfaceBrowserHandler::OnBeforePopup( CefRefPtr<CefBrowser> Browser,
CefRefPtr<CefFrame> Frame,
const CefString& TargetUrl,
const CefString& TargetFrameName,
const CefPopupFeatures& PopupFeatures,
CefWindowInfo& OutWindowInfo,
CefRefPtr<CefClient>& OutClient,
CefBrowserSettings& OutSettings,
bool* OutNoJavascriptAccess )
{
FString URL = WCHAR_TO_TCHAR(TargetUrl.ToWString().c_str());
FString FrameName = WCHAR_TO_TCHAR(TargetFrameName.ToWString().c_str());
/* If OnBeforePopup() is not bound, we allow creating new windows as long as OnCreateWindow() is bound.
The BeforePopup delegate is always executed even if OnCreateWindow is not bound to anything .
*/
if((OnBeforePopup().IsBound() && OnBeforePopup().Execute(URL, FrameName)) || !OnCreateWindow().IsBound())
{
return true;
}
else
{
TSharedPtr<FCEFInterfaceBrowserPopupFeatures> NewBrowserPopupFeatures = MakeShareable(new FCEFInterfaceBrowserPopupFeatures(PopupFeatures));
bool shouldUseTransparency = URL.Contains(TEXT("chrome-devtools")) ? false : bUseTransparency;
cef_color_t Alpha = shouldUseTransparency ? 0 : CefColorGetA(OutSettings.background_color);
cef_color_t R = CefColorGetR(OutSettings.background_color);
cef_color_t G = CefColorGetG(OutSettings.background_color);
cef_color_t B = CefColorGetB(OutSettings.background_color);
OutSettings.background_color = CefColorSetARGB(Alpha, R, G, B);
CefRefPtr<FCEFInterfaceBrowserHandler> NewHandler(new FCEFInterfaceBrowserHandler(shouldUseTransparency));
NewHandler->ParentHandler = this;
NewHandler->SetPopupFeatures(NewBrowserPopupFeatures);
OutClient = NewHandler;
// Always use off screen rendering so we can integrate with our windows
#if PLATFORM_LINUX
OutWindowInfo.SetAsWindowless(kNullWindowHandle, shouldUseTransparency);
#else
OutWindowInfo.SetAsWindowless(kNullWindowHandle);
#endif
// We need to rely on CEF to create our window so we set the WindowInfo, BrowserSettings, Client, and then return false
return false;
}
}
bool FCEFInterfaceBrowserHandler::OnCertificateError(CefRefPtr<CefBrowser> Browser,
cef_errorcode_t CertError,
const CefString &RequestUrl,
CefRefPtr<CefSSLInfo> SslInfo,
CefRefPtr<CefRequestCallback> Callback)
{
// Forward the cert error to the normal load error handler
CefString ErrorText = "Certificate error";
OnLoadError(Browser, Browser->GetMainFrame(), CertError, ErrorText, RequestUrl);
return false;
}
void FCEFInterfaceBrowserHandler::OnLoadError(CefRefPtr<CefBrowser> Browser,
CefRefPtr<CefFrame> Frame,
CefLoadHandler::ErrorCode InErrorCode,
const CefString& ErrorText,
const CefString& FailedUrl)
{
// notify browser window
if (Frame->IsMain())
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
if (AltRetryDomains.Num() > 0 && AltRetryDomainIdx < (uint32)AltRetryDomains.Num())
{
FString Url = WCHAR_TO_TCHAR(FailedUrl.ToWString().c_str());
FString OriginalUrlDomain = FPlatformHttp::GetUrlDomain(Url);
if (!OriginalUrlDomain.IsEmpty())
{
const FString NewUrl(Url.Replace(*OriginalUrlDomain, *AltRetryDomains[AltRetryDomainIdx++]));
BrowserWindow->LoadURL(NewUrl);
return;
}
}
BrowserWindow->NotifyDocumentError(InErrorCode, ErrorText, FailedUrl);
}
}
}
#if PLATFORM_LINUX
void FCEFInterfaceBrowserHandler::OnLoadStart(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame)
{
}
#elif PLATFORM_WINDOWS
void FCEFInterfaceBrowserHandler::OnLoadStart(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, TransitionType CefTransitionType)
{
if (Browser->GetHost()->GetWindowHandle() != nullptr)
{
RECT rcWnd;
GetWindowRect(Browser->GetHost()->GetWindowHandle(), &rcWnd);
float DPIScaleFactor = FPlatformApplicationMisc::GetDPIScaleFactorAtPoint(rcWnd.left, rcWnd.top);
double ZoomLevel = (double((DPIScaleFactor * 100) - 100)) / 25.0;
Browser->GetHost()->SetZoomLevel(ZoomLevel);
}
}
#else
void FCEFInterfaceBrowserHandler::OnLoadStart(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, TransitionType CefTransitionType)
{
}
#endif
void FCEFInterfaceBrowserHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> Browser, bool bIsLoading, bool bCanGoBack, bool bCanGoForward)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->NotifyDocumentLoadingStateChange(bIsLoading);
}
}
bool FCEFInterfaceBrowserHandler::GetRootScreenRect(CefRefPtr<CefBrowser> Browser, CefRect& Rect)
{
FDisplayMetrics DisplayMetrics;
FSlateApplication::Get().GetDisplayMetrics(DisplayMetrics);
Rect.width = DisplayMetrics.PrimaryDisplayWidth;
Rect.height = DisplayMetrics.PrimaryDisplayHeight;
return true;
}
bool FCEFInterfaceBrowserHandler::GetViewRect(CefRefPtr<CefBrowser> Browser, CefRect& Rect)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
return BrowserWindow->GetViewRect(Rect);
}
else
{
return false;
}
}
void FCEFInterfaceBrowserHandler::OnPaint(CefRefPtr<CefBrowser> Browser,
PaintElementType Type,
const RectList& DirtyRects,
const void* Buffer,
int Width, int Height)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnPaint(Type, DirtyRects, Buffer, Width, Height);
}
}
void FCEFInterfaceBrowserHandler::OnCursorChange(CefRefPtr<CefBrowser> Browser, CefCursorHandle Cursor, CefRenderHandler::CursorType Type, const CefCursorInfo& CustomCursorInfo)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnCursorChange(Cursor, Type, CustomCursorInfo);
}
}
void FCEFInterfaceBrowserHandler::OnPopupShow(CefRefPtr<CefBrowser> Browser, bool bShow)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->ShowPopupMenu(bShow);
}
}
void FCEFInterfaceBrowserHandler::OnPopupSize(CefRefPtr<CefBrowser> Browser, const CefRect& Rect)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->SetPopupMenuPosition(Rect);
}
}
bool FCEFInterfaceBrowserHandler::GetScreenInfo(CefRefPtr<CefBrowser> Browser, CefScreenInfo& ScreenInfo)
{
TSharedPtr<FWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
ScreenInfo.depth = 24;
if (BrowserWindow.IsValid() && BrowserWindow->GetParentWindow().IsValid())
{
ScreenInfo.device_scale_factor = BrowserWindow->GetParentWindow()->GetNativeWindow()->GetDPIScaleFactor();
}
else
{
FDisplayMetrics DisplayMetrics;
FDisplayMetrics::RebuildDisplayMetrics(DisplayMetrics);
ScreenInfo.device_scale_factor = FPlatformApplicationMisc::GetDPIScaleFactorAtPoint(DisplayMetrics.PrimaryDisplayWorkAreaRect.Left, DisplayMetrics.PrimaryDisplayWorkAreaRect.Top);
}
return true;
}
#if !PLATFORM_LINUX
void FCEFInterfaceBrowserHandler::OnImeCompositionRangeChanged(
CefRefPtr<CefBrowser> Browser,
const CefRange& SelectionRange,
const CefRenderHandler::RectList& CharacterBounds)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnImeCompositionRangeChanged(Browser, SelectionRange, CharacterBounds);
}
}
#endif
CefRequestHandler::ReturnValue FCEFInterfaceBrowserHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, CefRefPtr<CefRequest> Request, CefRefPtr<CefRequestCallback> Callback)
{
// Current thread is IO thread. We need to invoke BrowserWindow->GetResourceContent on the UI (aka Game) thread:
CefPostTask(TID_UI, new FCEFInterfaceBrowserClosureTask(this, [=]()
{
const FString LanguageHeaderText(TEXT("Accept-Language"));
const FString LocaleCode = FWebInterfaceBrowserSingleton::GetCurrentLocaleCode();
CefRequest::HeaderMap HeaderMap;
Request->GetHeaderMap(HeaderMap);
auto LanguageHeader = HeaderMap.find(TCHAR_TO_WCHAR(*LanguageHeaderText));
if (LanguageHeader != HeaderMap.end())
{
(*LanguageHeader).second = TCHAR_TO_WCHAR(*LocaleCode);
}
else
{
HeaderMap.insert(std::pair<CefString, CefString>(TCHAR_TO_WCHAR(*LanguageHeaderText), TCHAR_TO_WCHAR(*LocaleCode)));
}
if (BeforeResourceLoadDelegate.IsBound())
{
FRequestHeaders AdditionalHeaders;
BeforeResourceLoadDelegate.Execute(Request->GetURL(), Request->GetResourceType(), AdditionalHeaders);
for (auto Iter = AdditionalHeaders.CreateConstIterator(); Iter; ++Iter)
{
const FString& Header = Iter.Key();
const FString& Value = Iter.Value();
HeaderMap.insert(std::pair<CefString, CefString>(TCHAR_TO_WCHAR(*Header), TCHAR_TO_WCHAR(*Value)));
}
}
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
TOptional<FString> Contents = BrowserWindow->GetResourceContent(Frame, Request);
if(Contents.IsSet())
{
Contents.GetValue().ReplaceInline(TEXT("\n"), TEXT(""), ESearchCase::CaseSensitive);
Contents.GetValue().ReplaceInline(TEXT("\r"), TEXT(""), ESearchCase::CaseSensitive);
// pass the text we'd like to come back as a response to the request post data
CefRefPtr<CefPostData> PostData = CefPostData::Create();
CefRefPtr<CefPostDataElement> Element = CefPostDataElement::Create();
FTCHARToUTF8 UTF8String(*Contents.GetValue());
Element->SetToBytes(UTF8String.Length(), UTF8String.Get());
PostData->AddElement(Element);
Request->SetPostData(PostData);
// Set a custom request header, so we know the mime type if it was specified as a hash on the dummy URL
std::string Url = Request->GetURL().ToString();
std::string::size_type HashPos = Url.find_last_of('#');
if (HashPos != std::string::npos)
{
std::string MimeType = Url.substr(HashPos + 1);
HeaderMap.insert(std::pair<CefString, CefString>(TCHAR_TO_WCHAR(TEXT("Content-Type")), MimeType));
}
// Change http method to tell GetResourceHandler to return the content
Request->SetMethod(TCHAR_TO_WCHAR(*CustomContentMethod));
}
}
Request->SetHeaderMap(HeaderMap);
Callback->Continue(true);
}));
// Tell CEF that we're handling this asynchronously.
return RV_CONTINUE_ASYNC;
}
void FCEFInterfaceBrowserHandler::OnResourceLoadComplete(
CefRefPtr<CefBrowser> Browser,
CefRefPtr<CefFrame> Frame,
CefRefPtr<CefRequest> Request,
CefRefPtr<CefResponse> Response,
URLRequestStatus Status,
int64 Received_content_length)
{
// Current thread is IO thread. We need to invoke our delegates on the UI (aka Game) thread:
CefPostTask(TID_UI, new FCEFInterfaceBrowserClosureTask(this, [=]()
{
ResourceLoadCompleteDelegate.ExecuteIfBound(Request->GetURL(), Request->GetResourceType(), Status, Received_content_length);
}));
}
void FCEFInterfaceBrowserHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> Browser, TerminationStatus Status)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnRenderProcessTerminated(Status);
}
}
bool FCEFInterfaceBrowserHandler::OnBeforeBrowse(CefRefPtr<CefBrowser> Browser,
CefRefPtr<CefFrame> Frame,
CefRefPtr<CefRequest> Request,
bool IsRedirect)
{
// Current thread: UI thread
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
if(BrowserWindow->OnBeforeBrowse(Browser, Frame, Request, IsRedirect))
{
return true;
}
}
return false;
}
CefRefPtr<CefResourceHandler> FCEFInterfaceBrowserHandler::GetResourceHandler( CefRefPtr<CefBrowser> Browser, CefRefPtr< CefFrame > Frame, CefRefPtr< CefRequest > Request )
{
if (Request->GetMethod() == TCHAR_TO_WCHAR(*CustomContentMethod))
{
// Content override header will be set by OnBeforeResourceLoad before passing the request on to this.
if (Request->GetPostData() && Request->GetPostData()->GetElementCount() > 0)
{
// get the mime type from Content-Type header (default to text/html to support old behavior)
FString MimeType = TEXT("text/html"); // default if not specified
CefRequest::HeaderMap HeaderMap;
Request->GetHeaderMap(HeaderMap);
auto ContentOverride = HeaderMap.find(TCHAR_TO_WCHAR(TEXT("Content-Type")));
if (ContentOverride != HeaderMap.end())
{
MimeType = WCHAR_TO_TCHAR(ContentOverride->second.ToWString().c_str());
}
// reply with the post data
CefPostData::ElementVector Elements;
Request->GetPostData()->GetElements(Elements);
return new FCEFInterfaceBrowserByteResource(Elements[0], MimeType);
}
}
return nullptr;
}
void FCEFInterfaceBrowserHandler::SetBrowserWindow(TSharedPtr<FCEFWebInterfaceBrowserWindow> InBrowserWindow)
{
BrowserWindowPtr = InBrowserWindow;
}
bool FCEFInterfaceBrowserHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> Browser,
CefProcessId SourceProcess,
CefRefPtr<CefProcessMessage> Message)
{
bool Retval = false;
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
Retval = BrowserWindow->OnProcessMessageReceived(Browser, SourceProcess, Message);
}
return Retval;
}
bool FCEFInterfaceBrowserHandler::ShowDevTools(const CefRefPtr<CefBrowser>& Browser)
{
CefPoint Point;
CefString TargetUrl = "chrome-devtools://devtools/devtools.html";
CefString TargetFrameName = "devtools";
CefPopupFeatures PopupFeatures;
CefWindowInfo WindowInfo;
CefRefPtr<CefClient> NewClient;
CefBrowserSettings BrowserSettings;
bool NoJavascriptAccess = false;
PopupFeatures.xSet = false;
PopupFeatures.ySet = false;
PopupFeatures.heightSet = false;
PopupFeatures.widthSet = false;
PopupFeatures.locationBarVisible = false;
PopupFeatures.menuBarVisible = false;
PopupFeatures.toolBarVisible = false;
PopupFeatures.statusBarVisible = false;
PopupFeatures.resizable = true;
// Set max framerate to maximum supported.
BrowserSettings.windowless_frame_rate = 60;
// Disable plugins
BrowserSettings.plugins = STATE_DISABLED;
// Dev Tools look best with a white background color
BrowserSettings.background_color = CefColorSetARGB(255, 255, 255, 255);
// OnBeforePopup already takes care of all the details required to ask the host application to create a new browser window.
bool bSuppressWindowCreation = OnBeforePopup(Browser, Browser->GetFocusedFrame(), TargetUrl, TargetFrameName, PopupFeatures, WindowInfo, NewClient, BrowserSettings, &NoJavascriptAccess);
if(! bSuppressWindowCreation)
{
Browser->GetHost()->ShowDevTools(WindowInfo, NewClient, BrowserSettings, Point);
}
return !bSuppressWindowCreation;
}
bool FCEFInterfaceBrowserHandler::OnKeyEvent(CefRefPtr<CefBrowser> Browser,
const CefKeyEvent& Event,
CefEventHandle OsEvent)
{
// Show dev tools on CMD/CTRL+SHIFT+I
if( (Event.type == KEYEVENT_RAWKEYDOWN || Event.type == KEYEVENT_KEYDOWN) &&
#if PLATFORM_MAC
(Event.modifiers == (EVENTFLAG_COMMAND_DOWN | EVENTFLAG_SHIFT_DOWN)) &&
#else
(Event.modifiers == (EVENTFLAG_CONTROL_DOWN | EVENTFLAG_SHIFT_DOWN)) &&
#endif
(Event.unmodified_character == 'i' || Event.unmodified_character == 'I') &&
IWebInterfaceBrowserModule::Get().GetSingleton()->IsDevToolsShortcutEnabled()
)
{
return ShowDevTools(Browser);
}
#if PLATFORM_MAC
// We need to handle standard Copy/Paste/etc... shortcuts on OS X
if( (Event.type == KEYEVENT_RAWKEYDOWN || Event.type == KEYEVENT_KEYDOWN) &&
(Event.modifiers & EVENTFLAG_COMMAND_DOWN) != 0 &&
(Event.modifiers & EVENTFLAG_CONTROL_DOWN) == 0 &&
(Event.modifiers & EVENTFLAG_ALT_DOWN) == 0 &&
( (Event.modifiers & EVENTFLAG_SHIFT_DOWN) == 0 || Event.unmodified_character == 'z' )
)
{
CefRefPtr<CefFrame> Frame = Browser->GetFocusedFrame();
if (Frame)
{
switch (Event.unmodified_character)
{
case 'a':
Frame->SelectAll();
return true;
case 'c':
Frame->Copy();
return true;
case 'v':
Frame->Paste();
return true;
case 'x':
Frame->Cut();
return true;
case 'z':
if( (Event.modifiers & EVENTFLAG_SHIFT_DOWN) == 0 )
{
Frame->Undo();
}
else
{
Frame->Redo();
}
return true;
}
}
}
#endif
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
return BrowserWindow->OnUnhandledKeyEvent(Event);
}
return false;
}
#if PLATFORM_LINUX
bool FCEFInterfaceBrowserHandler::OnJSDialog(CefRefPtr<CefBrowser> Browser, const CefString& OriginUrl, const CefString& AcceptLang, JSDialogType DialogType, const CefString& MessageText, const CefString& DefaultPromptText, CefRefPtr<CefJSDialogCallback> Callback, bool& OutSuppressMessage)
#else
bool FCEFInterfaceBrowserHandler::OnJSDialog(CefRefPtr<CefBrowser> Browser, const CefString& OriginUrl, JSDialogType DialogType, const CefString& MessageText, const CefString& DefaultPromptText, CefRefPtr<CefJSDialogCallback> Callback, bool& OutSuppressMessage)
#endif
{
bool Retval = false;
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
Retval = BrowserWindow->OnJSDialog(DialogType, MessageText, DefaultPromptText, Callback, OutSuppressMessage);
}
return Retval;
}
bool FCEFInterfaceBrowserHandler::OnBeforeUnloadDialog(CefRefPtr<CefBrowser> Browser, const CefString& MessageText, bool IsReload, CefRefPtr<CefJSDialogCallback> Callback)
{
bool Retval = false;
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
Retval = BrowserWindow->OnBeforeUnloadDialog(MessageText, IsReload, Callback);
}
return Retval;
}
void FCEFInterfaceBrowserHandler::OnResetDialogState(CefRefPtr<CefBrowser> Browser)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
BrowserWindow->OnResetDialogState();
}
}
void FCEFInterfaceBrowserHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, CefRefPtr<CefContextMenuParams> Params, CefRefPtr<CefMenuModel> Model)
{
Model->Clear();
}
void FCEFInterfaceBrowserHandler::OnDraggableRegionsChanged(CefRefPtr<CefBrowser> Browser, const std::vector<CefDraggableRegion>& Regions)
{
TSharedPtr<FCEFWebInterfaceBrowserWindow> BrowserWindow = BrowserWindowPtr.Pin();
if (BrowserWindow.IsValid())
{
TArray<FWebInterfaceBrowserDragRegion> DragRegions;
for (uint32 Idx = 0; Idx < Regions.size(); Idx++)
{
DragRegions.Add(FWebInterfaceBrowserDragRegion(
FIntRect(Regions[Idx].bounds.x, Regions[Idx].bounds.y, Regions[Idx].bounds.x + Regions[Idx].bounds.width, Regions[Idx].bounds.y + Regions[Idx].bounds.height),
Regions[Idx].draggable ? true : false));
}
BrowserWindow->UpdateDragRegions(DragRegions);
}
}
#undef LOCTEXT_NAMESPACE
#endif // WITH_CEF