-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ClientAdapter.cpp
1292 lines (1029 loc) · 53 KB
/
ClientAdapter.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
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "Stdafx.h"
#include "include\cef_client.h"
#include "include\wrapper\cef_stream_resource_handler.h"
#include "ClientAdapter.h"
#include "ManagedCefBrowserAdapter.h"
#include "CefRequestWrapper.h"
#include "CefResponseWrapper.h"
#include "CefContextMenuParamsWrapper.h"
#include "CefMenuModelWrapper.h"
#include "CefDragDataWrapper.h"
#include "TypeConversion.h"
#include "CefSharpBrowserWrapper.h"
#include "CefDownloadItemCallbackWrapper.h"
#include "CefBeforeDownloadCallbackWrapper.h"
#include "CefGeolocationCallbackWrapper.h"
#include "CefFileDialogCallbackWrapper.h"
#include "CefAuthCallbackWrapper.h"
#include "CefJSDialogCallbackWrapper.h"
#include "CefRequestCallbackWrapper.h"
#include "CefRunContextMenuCallbackWrapper.h"
#include "WindowInfo.h"
#include "Serialization\Primitives.h"
#include "Serialization\V8Serialization.h"
#include "Serialization\JsObjectsSerialization.h"
#include "Serialization\ObjectsSerialization.h"
#include "Messaging\Messages.h"
#include "CefResponseFilterAdapter.h"
#include "PopupFeatures.h"
#include "CefCertificateCallbackWrapper.h"
#include "CefSslInfoWrapper.h"
using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;
using namespace System::Security::Cryptography::X509Certificates;
namespace CefSharp
{
namespace Internals
{
IBrowser^ ClientAdapter::GetBrowserWrapper(int browserId)
{
if (_cefBrowser.get())
{
if (_cefBrowser->GetIdentifier() == browserId)
{
return _browser;
}
IBrowser^ popupBrowser;
if (_popupBrowsers->TryGetValue(browserId, popupBrowser))
{
return popupBrowser;
}
}
return nullptr;
}
IBrowser^ ClientAdapter::GetBrowserWrapper(int browserId, bool isPopup)
{
if (_browserControl->HasParent)
{
return _browser;
}
if(isPopup)
{
IBrowser^ popupBrowser;
if (_popupBrowsers->TryGetValue(browserId, popupBrowser))
{
return popupBrowser;
}
return nullptr;
}
return _browser;
}
void ClientAdapter::CloseAllPopups(bool forceClose)
{
if (_popupBrowsers->Count > 0)
{
for each (IBrowser^ browser in _popupBrowsers->Values)
{
browser->GetHost()->CloseBrowser(forceClose);
// NOTE: We don't dispose the IBrowsers here
// because ->CloseBrowser() will invoke
// ->OnBeforeClose() for the browser.
// OnBeforeClose() disposes the IBrowser there.
}
}
}
bool ClientAdapter::OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, 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, bool* no_javascript_access)
{
auto handler = _browserControl->LifeSpanHandler;
if (handler == nullptr)
{
return false;
}
IWebBrowser^ newBrowser = nullptr;
bool createdWrapper = false;
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
PopupFeatures popupFeaturesWrapper(&popupFeatures);
BrowserSettings browserSettingsWrapper(&settings);
WindowInfo windowInfoWrapper(&windowInfo);
auto result = handler->OnBeforePopup(
_browserControl, browserWrapper,
%frameWrapper, StringUtils::ToClr(target_url),
StringUtils::ToClr(target_frame_name),
(CefSharp::WindowOpenDisposition)target_disposition,
user_gesture, %popupFeaturesWrapper,
%windowInfoWrapper, %browserSettingsWrapper, *no_javascript_access, newBrowser);
if (newBrowser != nullptr)
{
//newBrowser should never be set to _browserControl (I've seen user code where someone attepted to do this).
//So throw exception to make that perfectly clear.
if (Object::ReferenceEquals(_browserControl, newBrowser))
{
throw gcnew Exception("newBrowser should be a new instance of ChromiumWebBrowser or null.");
}
auto newBrowserInternal = dynamic_cast<IWebBrowserInternal^>(newBrowser);
if (newBrowserInternal != nullptr)
{
//This should already be set using the SetAsPopup extension, just making doubly sure
newBrowserInternal->HasParent = true;
auto browserAdapter = dynamic_cast<ManagedCefBrowserAdapter^>(newBrowserInternal->BrowserAdapter);
if (browserAdapter != nullptr)
{
client = browserAdapter->GetClientAdapter().get();
auto renderClientAdapter = dynamic_cast<RenderClientAdapter*>(client.get());
if (renderClientAdapter != nullptr)
{
renderClientAdapter->CreateBitmapInfo();
}
}
}
}
return result;
}
void ClientAdapter::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
auto browserWrapper = gcnew CefSharpBrowserWrapper(browser);
auto isPopup = browser->IsPopup() && !_browserControl->HasParent;
if (isPopup)
{
// Add to the list of popup browsers.
_popupBrowsers->Add(browser->GetIdentifier(), browserWrapper);
}
else
{
_browserHwnd = browser->GetHost()->GetWindowHandle();
_cefBrowser = browser;
_browser = browserWrapper;
if (!Object::ReferenceEquals(_browserAdapter, nullptr))
{
_browserAdapter->OnAfterBrowserCreated(browserWrapper);
}
}
auto handler = _browserControl->LifeSpanHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), isPopup);
handler->OnAfterCreated(_browserControl, browserWrapper);
}
}
bool ClientAdapter::DoClose(CefRefPtr<CefBrowser> browser)
{
auto handler = _browserControl->LifeSpanHandler;
if (handler != nullptr)
{
//By this point it's possible IBrowser references have been disposed
//Rather than attempting to rework the rather complex closing logic
//It's easier to pass in a new wrapper and dispose it straight away
CefSharpBrowserWrapper browserWrapper(browser);
return handler->DoClose(_browserControl, %browserWrapper);
}
return false;
}
void ClientAdapter::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
auto isPopup = browser->IsPopup() && !_browserControl->HasParent;
auto handler = _browserControl->LifeSpanHandler;
if (handler != nullptr)
{
//By this point it's possible IBrowser references have been disposed
//Rather than attempting to rework the rather complex closing logic
//It's easier to pass in a new wrapper and dispose it straight away
CefSharpBrowserWrapper browserWrapper(browser);
handler->OnBeforeClose(_browserControl, %browserWrapper);
}
if (isPopup)
{
// Remove from the browser popup list.
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true);
_popupBrowsers->Remove(browser->GetIdentifier());
// Dispose the CefSharpBrowserWrapper
delete browserWrapper;
}
//TODO: When creating a new ChromiumWebBrowser and passing in a newBrowser to OnBeforePopup
//the handles don't match up (at least in WPF), need to investigate further.
else if (_browserHwnd == browser->GetHost()->GetWindowHandle() || _browserControl->HasParent)
{
_cefBrowser = NULL;
}
}
void ClientAdapter::OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto args = gcnew LoadingStateChangedEventArgs(browserWrapper, canGoBack, canGoForward, isLoading);
if (!browser->IsPopup())
{
_browserControl->SetLoadingStateChange(args);
}
auto handler = _browserControl->LoadHandler;
if (handler != nullptr)
{
handler->OnLoadingStateChange(_browserControl, args);
}
}
void ClientAdapter::OnAddressChange(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& address)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto args = gcnew AddressChangedEventArgs(browserWrapper, StringUtils::ToClr(address));
if (!browser->IsPopup())
{
_browserControl->SetAddress(args);
}
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
handler->OnAddressChanged(_browserControl, args);
}
}
void ClientAdapter::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
auto args = gcnew TitleChangedEventArgs(StringUtils::ToClr(title));
if (browser->IsPopup() && !_browserControl->HasParent)
{
// Set the popup window title
auto hwnd = browser->GetHost()->GetWindowHandle();
SetWindowText(hwnd, std::wstring(title).c_str());
}
else
{
_browserControl->SetTitle(args);
}
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
handler->OnTitleChanged(_browserControl, args);
}
}
void ClientAdapter::OnFaviconURLChange(CefRefPtr<CefBrowser> browser, const std::vector<CefString>& iconUrls)
{
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnFaviconUrlChange(_browserControl, browserWrapper, StringUtils::ToClr(iconUrls));
}
}
void ClientAdapter::OnFullscreenModeChange(CefRefPtr<CefBrowser> browser, bool fullscreen)
{
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnFullscreenModeChange(_browserControl, browserWrapper, fullscreen);
}
}
bool ClientAdapter::OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text)
{
auto tooltip = StringUtils::ToClr(text);
bool hasChanged = tooltip != _tooltip;
bool returnFlag = true;
//NOTE: Only called if tooltip changed otherwise called many times
// also only called when using OSR, https://bitbucket.org/chromiumembedded/cef/issues/783
if (hasChanged)
{
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
returnFlag = handler->OnTooltipChanged(_browserControl, tooltip);
}
if (!browser->IsPopup())
{
_tooltip = tooltip;
_browserControl->SetTooltipText(_tooltip);
}
}
return returnFlag;
}
bool ClientAdapter::OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefString& message, const CefString& source, int line)
{
auto args = gcnew ConsoleMessageEventArgs(StringUtils::ToClr(message), StringUtils::ToClr(source), line);
if (!browser->IsPopup())
{
_browserControl->OnConsoleMessage(args);
}
auto handler = _browserControl->DisplayHandler;
if (handler == nullptr)
{
return false;
}
return handler->OnConsoleMessage(_browserControl, args);
}
void ClientAdapter::OnStatusMessage(CefRefPtr<CefBrowser> browser, const CefString& value)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto args = gcnew StatusMessageEventArgs(browserWrapper, StringUtils::ToClr(value));
if (!browser->IsPopup())
{
_browserControl->OnStatusMessage(args);
}
auto handler = _browserControl->DisplayHandler;
if (handler != nullptr)
{
handler->OnStatusMessage(_browserControl, args);
}
}
bool ClientAdapter::OnKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event)
{
auto handler = _browserControl->KeyboardHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
return handler->OnKeyEvent(
_browserControl, browserWrapper, (KeyType)event.type, event.windows_key_code,
event.native_key_code,
(CefEventFlags)event.modifiers, event.is_system_key == 1);
}
bool ClientAdapter::OnPreKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut)
{
auto handler = _browserControl->KeyboardHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
return handler->OnPreKeyEvent(
_browserControl, browserWrapper, (KeyType)event.type, event.windows_key_code,
event.native_key_code, (CefEventFlags)event.modifiers, event.is_system_key == 1,
*is_keyboard_shortcut);
}
void ClientAdapter::OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, TransitionType transitionType)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
if (!browser->IsPopup())
{
_browserControl->OnFrameLoadStart(gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper, (CefSharp::TransitionType)transitionType));
}
auto handler = _browserControl->LoadHandler;
if (handler != nullptr)
{
handler->OnFrameLoadStart(_browserControl, gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper, (CefSharp::TransitionType)transitionType));
}
}
void ClientAdapter::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
if (!browser->IsPopup())
{
_browserControl->OnFrameLoadEnd(gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode));
}
auto handler = _browserControl->LoadHandler;
if (handler != nullptr)
{
handler->OnFrameLoadEnd(_browserControl, gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode));
}
}
void ClientAdapter::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
if (!browser->IsPopup())
{
_browserControl->OnLoadError(gcnew LoadErrorEventArgs(browserWrapper, %frameWrapper,
(CefErrorCode)errorCode, StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl)));
}
auto handler = _browserControl->LoadHandler;
if (handler != nullptr)
{
handler->OnLoadError(_browserControl,
gcnew LoadErrorEventArgs(browserWrapper, %frameWrapper, (CefErrorCode)errorCode, StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl)));
}
}
bool ClientAdapter::OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool isRedirect)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
return handler->OnBeforeBrowse(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, isRedirect);
}
bool ClientAdapter::OnOpenURLFromTab(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& targetUrl,
CefRequestHandler::WindowOpenDisposition targetDisposition, bool userGesture)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
return handler->OnOpenUrlFromTab(_browserControl, browserWrapper, %frameWrapper, StringUtils::ToClr(targetUrl), (CefSharp::WindowOpenDisposition)targetDisposition, userGesture);
}
bool ClientAdapter::OnCertificateError(CefRefPtr<CefBrowser> browser, cef_errorcode_t cert_error, const CefString& request_url, CefRefPtr<CefSSLInfo> ssl_info, CefRefPtr<CefRequestCallback> callback)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
// If callback is empty the error cannot be recovered from and the request will be canceled automatically.
// Still notify the user of the certificate error just don't provide a callback.
auto requestCallback = callback == NULL ? nullptr : gcnew CefRequestCallbackWrapper(callback);
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto sslInfoWrapper = gcnew CefSslInfoWrapper(ssl_info);
return handler->OnCertificateError(_browserControl, browserWrapper, (CefErrorCode)cert_error, StringUtils::ToClr(request_url), sslInfoWrapper, requestCallback);
}
bool ClientAdapter::OnQuotaRequest(CefRefPtr<CefBrowser> browser, const CefString& originUrl, int64 newSize, CefRefPtr<CefRequestCallback> callback)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto requestCallback = gcnew CefRequestCallbackWrapper(callback);
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
return handler->OnQuotaRequest(_browserControl, browserWrapper, StringUtils::ToClr(originUrl), newSize, requestCallback);
}
void ClientAdapter::OnPluginCrashed(CefRefPtr<CefBrowser> browser, const CefString& plugin_path)
{
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnPluginCrashed(_browserControl, browserWrapper, StringUtils::ToClr(plugin_path));
}
}
void ClientAdapter::OnRenderViewReady(CefRefPtr<CefBrowser> browser)
{
if (!Object::ReferenceEquals(_browserAdapter, nullptr) && !_browserAdapter->IsDisposed && !browser->IsPopup())
{
auto objectRepository = _browserAdapter->JavascriptObjectRepository;
if (objectRepository->HasBoundObjects)
{
//transmit async bound objects
auto jsRootObjectMessage = CefProcessMessage::Create(kJavascriptRootObjectRequest);
auto argList = jsRootObjectMessage->GetArgumentList();
SerializeJsObject(objectRepository->AsyncRootObject, argList, 0);
SerializeJsObject(objectRepository->RootObject, argList, 1);
browser->SendProcessMessage(CefProcessId::PID_RENDERER, jsRootObjectMessage);
}
}
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnRenderViewReady(_browserControl, browserWrapper);
}
}
void ClientAdapter::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser, TerminationStatus status)
{
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnRenderProcessTerminated(_browserControl, browserWrapper, (CefTerminationStatus)status);
}
}
void ClientAdapter::OnResourceRedirect(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, CefString& newUrl)
{
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto managedNewUrl = StringUtils::ToClr(newUrl);
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
CefResponseWrapper responseWrapper(response);
handler->OnResourceRedirect(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper, managedNewUrl);
newUrl = StringUtils::ToNative(managedNewUrl);
}
}
bool ClientAdapter::OnResourceResponse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
CefResponseWrapper responseWrapper(response);
return handler->OnResourceResponse(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper);
}
CefRefPtr<CefResponseFilter> ClientAdapter::GetResourceResponseFilter(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return NULL;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
CefResponseWrapper responseWrapper(response);
auto filter = handler->GetResourceResponseFilter(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper);
if (filter == nullptr)
{
return NULL;
}
return new CefResponseFilterAdapter(filter);
}
void ClientAdapter::OnResourceLoadComplete(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, URLRequestStatus status, int64 receivedContentLength)
{
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
CefResponseWrapper responseWrapper(response);
handler->OnResourceLoadComplete(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, %responseWrapper, (UrlRequestStatus)status, receivedContentLength);
}
}
void ClientAdapter::OnProtocolExecution(CefRefPtr<CefBrowser> browser, const CefString& url, bool& allowOSExecution)
{
auto handler = _browserControl->RequestHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
allowOSExecution = handler->OnProtocolExecution(_browserControl, browserWrapper, StringUtils::ToClr(url));
}
}
// Called on the IO thread before a resource is loaded. To allow the resource
// to load normally return NULL. To specify a handler for the resource return
// a CefResourceHandler object. The |request| object should not be modified in
// this callback.
CefRefPtr<CefResourceHandler> ClientAdapter::GetResourceHandler(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request)
{
auto factory = _browserControl->ResourceHandlerFactory;
if (factory == nullptr || !factory->HasHandlers)
{
return NULL;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefRequestWrapper requestWrapper(request);
auto handler = factory->GetResourceHandler(_browserControl, browserWrapper, %frameWrapper, %requestWrapper);
if (handler == nullptr)
{
return NULL;
}
if (handler->GetType() == FileResourceHandler::typeid)
{
auto resourceHandler = static_cast<FileResourceHandler^>(handler);
auto streamReader = CefStreamReader::CreateForFile(StringUtils::ToNative(resourceHandler->FilePath));
if (streamReader.get())
{
return new CefStreamResourceHandler(StringUtils::ToNative(resourceHandler->MimeType), streamReader);
}
else
{
auto msg = "Unable to load resource CefStreamReader::CreateForFile returned NULL for file:" + resourceHandler->FilePath;
LOG(ERROR) << StringUtils::ToNative(msg).ToString();
return NULL;
}
}
else if (handler->GetType() == ByteArrayResourceHandler::typeid)
{
auto resourceHandler = static_cast<ByteArrayResourceHandler^>(handler);
//NOTE: Prefix with cli:: namespace as VS2015 gets confused with std::array
cli::array<Byte>^ buffer = resourceHandler->Data;
pin_ptr<Byte> src = &buffer[0];
auto streamReader = CefStreamReader::CreateForData(static_cast<void*>(src), buffer->Length);
return new CefStreamResourceHandler(StringUtils::ToNative(resourceHandler->MimeType), streamReader);
}
return new ResourceHandlerWrapper(handler);
}
cef_return_value_t ClientAdapter::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefRequestCallback> callback)
{
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return cef_return_value_t::RV_CONTINUE;
}
auto frameWrapper = gcnew CefFrameWrapper(frame);
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto requestWrapper = gcnew CefRequestWrapper(request);
auto requestCallback = gcnew CefRequestCallbackWrapper(callback, frameWrapper, requestWrapper);
return (cef_return_value_t)handler->OnBeforeResourceLoad(_browserControl, browserWrapper, frameWrapper, requestWrapper, requestCallback);
}
bool ClientAdapter::GetAuthCredentials(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, bool isProxy,
const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr<CefAuthCallback> callback)
{
if (isProxy && CefSharpSettings::Proxy != nullptr && CefSharpSettings::Proxy->IP == StringUtils::ToClr(host) && CefSharpSettings::Proxy->HasUsernameAndPassword())
{
callback->Continue(StringUtils::ToNative(CefSharpSettings::Proxy->Username), StringUtils::ToNative(CefSharpSettings::Proxy->Password));
return true;
}
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto frameWrapper = gcnew CefFrameWrapper(frame);
auto callbackWrapper = gcnew CefAuthCallbackWrapper(callback, frameWrapper);
return handler->GetAuthCredentials(
_browserControl, browserWrapper, frameWrapper, isProxy,
StringUtils::ToClr(host), port, StringUtils::ToClr(realm),
StringUtils::ToClr(scheme), callbackWrapper);
}
bool ClientAdapter::OnSelectClientCertificate(CefRefPtr<CefBrowser> browser, bool isProxy, const CefString& host,
int port, const CefRequestHandler::X509CertificateList& certificates, CefRefPtr<CefSelectClientCertificateCallback> callback) {
auto handler = _browserControl->RequestHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto callbackWrapper = gcnew CefCertificateCallbackWrapper(callback, certificates);
auto list = gcnew X509Certificate2Collection();
std::vector<CefRefPtr<CefX509Certificate> >::const_iterator it =
certificates.begin();
for (; it != certificates.end(); ++it)
{
auto bytes((*it)->GetDEREncoded());
auto byteSize = bytes->GetSize();
auto bufferByte = gcnew cli::array<Byte>(byteSize);
pin_ptr<Byte> src = &bufferByte[0]; // pin pointer to first element in arr
bytes->GetData(static_cast<void*>(src), byteSize, 0);
auto cert = gcnew X509Certificate2(bufferByte);
list->Add(cert);
}
return handler->OnSelectClientCertificate(
_browserControl, browserWrapper, isProxy,
StringUtils::ToClr(host), port, list, callbackWrapper);
}
void ClientAdapter::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model)
{
auto handler = _browserControl->MenuHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefContextMenuParamsWrapper contextMenuParamsWrapper(params);
CefMenuModelWrapper menuModelWrapper(model);
handler->OnBeforeContextMenu(_browserControl, browserWrapper, %frameWrapper, %contextMenuParamsWrapper, %menuModelWrapper);
}
}
bool ClientAdapter::OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params, int commandId, EventFlags eventFlags)
{
auto handler = _browserControl->MenuHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefContextMenuParamsWrapper contextMenuParamsWrapper(params);
return handler->OnContextMenuCommand(_browserControl, browserWrapper, %frameWrapper, %contextMenuParamsWrapper,
(CefMenuCommand)commandId, (CefEventFlags)eventFlags);
}
return false;
}
void ClientAdapter::OnContextMenuDismissed(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
{
auto handler = _browserControl->MenuHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
handler->OnContextMenuDismissed(_browserControl, browserWrapper, %frameWrapper);
}
}
bool ClientAdapter::RunContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model, CefRefPtr<CefRunContextMenuCallback> callback)
{
auto handler = _browserControl->MenuHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
CefFrameWrapper frameWrapper(frame);
CefContextMenuParamsWrapper contextMenuParamsWrapper(params);
CefMenuModelWrapper menuModelWrapper(model);
auto callbackWrapper = gcnew CefRunContextMenuCallbackWrapper(callback);
return handler->RunContextMenu(_browserControl, browserWrapper, %frameWrapper, %contextMenuParamsWrapper, %menuModelWrapper, callbackWrapper);
}
void ClientAdapter::OnGotFocus(CefRefPtr<CefBrowser> browser)
{
auto handler = _browserControl->FocusHandler;
if (handler == nullptr)
{
return;
}
// NOTE: a popup handler for OnGotFocus doesn't make sense yet because
// non-offscreen windows don't wrap popup browser's yet.
if (!browser->IsPopup())
{
handler->OnGotFocus();
}
}
bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
{
auto handler = _browserControl->FocusHandler;
if (handler == nullptr)
{
// Allow the focus to be set by default.
return false;
}
// NOTE: a popup handler for OnGotFocus doesn't make sense yet because
// non-offscreen windows don't wrap popup browser's yet.
if (!browser->IsPopup())
{
return handler->OnSetFocus((CefFocusSource)source);
}
// Allow the focus to be set by default.
return false;
}
void ClientAdapter::OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next)
{
auto handler = _browserControl->FocusHandler;
if (handler == nullptr)
{
return;
}
// NOTE: a popup handler for OnGotFocus doesn't make sense yet because
// non-offscreen windows don't wrap popup browser's yet.
if (!browser->IsPopup())
{
handler->OnTakeFocus(next);
}
}
bool ClientAdapter::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)
{
auto handler = _browserControl->JsDialogHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto callbackWrapper = gcnew CefJSDialogCallbackWrapper(callback);
return handler->OnJSDialog(_browserControl, browserWrapper,
StringUtils::ToClr(origin_url), (CefJsDialogType)dialog_type,
StringUtils::ToClr(message_text), StringUtils::ToClr(default_prompt_text), callbackWrapper, suppress_message);
}
bool ClientAdapter::OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser, const CefString& message_text, bool is_reload, CefRefPtr<CefJSDialogCallback> callback)
{
auto handler = _browserControl->JsDialogHandler;
if (handler == nullptr)
{
return false;
}
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto callbackWrapper = gcnew CefJSDialogCallbackWrapper(callback);
return handler->OnJSBeforeUnload(_browserControl, browserWrapper, StringUtils::ToClr(message_text), is_reload, callbackWrapper);
}
void ClientAdapter::OnResetDialogState(CefRefPtr<CefBrowser> browser)
{
auto handler = _browserControl->JsDialogHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnResetDialogState(_browserControl, browserWrapper);
}
}
void ClientAdapter::OnDialogClosed(CefRefPtr<CefBrowser> browser)
{
auto handler = _browserControl->JsDialogHandler;
if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
handler->OnDialogClosed(_browserControl, browserWrapper);
}
}
bool ClientAdapter::OnFileDialog(CefRefPtr<CefBrowser> browser, FileDialogMode mode, const CefString& title,
const CefString& default_file_path, const std::vector<CefString>& accept_filters, int selected_accept_filter,
CefRefPtr<CefFileDialogCallback> callback)
{
auto handler = _browserControl->DialogHandler;
if (handler == nullptr)