-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_handler.cpp
2423 lines (2254 loc) · 76.1 KB
/
client_handler.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 (c) 2013 The Chromium Embedded Framework 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 "Sazabi.h"
#include "BroView.h"
#include "client_handler.h"
#include <stdio.h>
#include <algorithm>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#pragma warning(push, 0)
#include <codeanalysis/warnings.h>
#pragma warning(disable \
: ALL_CODE_ANALYSIS_WARNINGS)
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_path_util.h"
#include "include/cef_process_util.h"
#include "include/cef_trace.h"
#include "include/wrapper/cef_helpers.h"
#pragma warning(pop)
#include "client_util.h"
#include "DlgAuth.h"
#include "sbcommon.h"
// Required for selecting client certificates
#pragma comment(lib, "Crypt32")
#pragma comment(lib, "cryptui")
// For backward compatibility, use custom dialog for Windows 10
// Because there is a bug that new tab can't be opened after accessing certstore.
// See https://github.com/ThinBridge/Chronos/pull/141
#include "DlgCertification.h"
#pragma warning(push, 0)
#pragma warning(disable : 26812)
// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefMenuModel.html#GetCommandIdAt(int)
#define CH_MENU_INVALID_OR_SEPARATOR (-1)
ClientHandler::ClientHandler()
{
m_bDownLoadStartFlg = FALSE;
m_RendererPID = 0;
}
ClientHandler::~ClientHandler()
{
}
bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
PROC_TIME(DoClose)
// get browser ID
INT nBrowserId = browser->GetIdentifier();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
//ダウンロード中の場合は、警告を表示する。
if (theApp.m_DlMgr.IsDlProgress(nBrowserId))
{
SendMessageTimeout(hWindow, WM_APP_CEF_WINDOW_ACTIVATE, (WPARAM)NULL, (LPARAM)NULL, SMTO_NORMAL, 1000, NULL);
hWindow = GetParent(hWindow);
CString confirmMsg;
confirmMsg.LoadString(IDS_STRING_CONFIRM_CANCEL_DOWNLOAD);
int iRet = theApp.SB_MessageBox(hWindow, confirmMsg, NULL, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2, TRUE);
if (iRet != IDYES)
{
return true;
}
theApp.m_DlMgr.Release_DLDlg(nBrowserId);
}
}
// call parent
return CefLifeSpanHandler::DoClose(browser);
}
void ClientHandler::CreateBrowser(CefWindowInfo const& info, CefBrowserSettings const& settings, CefString const& url)
{
CefBrowserHost::CreateBrowser(info, this, url, settings, nullptr, nullptr);
}
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
PROC_TIME(OnAfterCreated)
#if CHROME_VERSION_MAJOR >= 126
//CEF126.2.7以降、disable-pdf-extensionオプションが非サポートになった。
//そのため、CEF126以降では、ClientHandler::OnAfterCreatedでPreferenceを指定することで同等の処理を行う。
//https://github.com/cefsharp/CefSharp/issues/4880
if (!theApp.m_AppSettings.IsEnablePDFExtension())
{
CefRefPtr<CefRequestContext> requestContext = browser->GetHost()->GetRequestContext();
CefString error;
CefRefPtr<CefValue> value = CefValue::Create();
value->SetBool(true);
requestContext->SetPreference("plugins.always_open_pdf_externally", value, error);
}
#endif
// get browser ID
INT nBrowserId = browser->GetIdentifier();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
//CEF93からポインタを直接SendMessageで渡すことができなくなった。
//関数を直接呼び出す
CChildView* pChild = NULL;
pChild = theApp.GetChildViewPtr(hWindow);
if (pChild)
{
if (SafeWnd(pChild->m_hWnd))
{
if (pChild->m_hWnd == hWindow)
((CChildView*)pChild)->SetBrowserPtr(nBrowserId, browser);
}
}
}
//// assign new browser
////CefBrowser* pBrowser = browser;
//if (SafeWnd(hWindow))
//{
// ::SendMessageTimeout(hWindow, WM_APP_CEF_NEW_BROWSER, (WPARAM)nBrowserId, (LPARAM)pBrowser, SMTO_NORMAL, 1000, NULL);
//}
// call parent
CefLifeSpanHandler::OnAfterCreated(browser);
}
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
PROC_TIME(OnBeforeClose)
// call parent
CefLifeSpanHandler::OnBeforeClose(browser);
}
bool ClientHandler::OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& target_url,
WindowOpenDisposition target_disposition,
bool user_gesture)
{
PROC_TIME(OnOpenURLFromTab)
if (browser->GetHost()->IsWindowRenderingDisabled())
{
// Cancel popups in off-screen rendering mode.
return true;
}
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
LPCWSTR pszURL = NULL;
pszURL = (LPCWSTR)target_url.c_str();
switch (target_disposition)
{
case cef_window_open_disposition_t::CEF_WOD_NEW_FOREGROUND_TAB:
{
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)target_disposition, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
case cef_window_open_disposition_t::CEF_WOD_NEW_BACKGROUND_TAB:
{
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)target_disposition, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
case cef_window_open_disposition_t::CEF_WOD_NEW_WINDOW:
{
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)target_disposition, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
default:
break;
}
}
return false;
}
#if CHROME_VERSION_MAJOR > 130
bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int popup_id,
const CefString& target_url,
const CefString& target_frame_name,
WindowOpenDisposition target_disposition,
bool user_gesture,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
CefRefPtr<CefDictionaryValue>& extra_info,
bool* no_javascript_access)
#else
bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& target_url,
const CefString& target_frame_name,
WindowOpenDisposition target_disposition,
bool user_gesture,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
CefRefPtr<CefDictionaryValue>& extra_info,
bool* no_javascript_access)
#endif
{
PROC_TIME(OnBeforePopup)
if (browser->GetHost()->IsWindowRenderingDisabled())
{
// Cancel popups in off-screen rendering mode.
return true;
}
// set client
client = this;
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
LRESULT lRet = 0;
switch (target_disposition)
{
case cef_window_open_disposition_t::CEF_WOD_NEW_POPUP:
{
lRet = ::SendMessage(hWindow, WM_APP_CEF_NEW_WINDOW, (WPARAM)&popupFeatures, (LPARAM)&windowInfo);
return false;
}
case cef_window_open_disposition_t::CEF_WOD_NEW_FOREGROUND_TAB:
{
#if CHROME_VERSION_MAJOR >= 110
if (popupFeatures.isPopup)
{
// Since CEF110, toolBarVisible and menuBarVisible was removed.
// When isPopup is true, browser interface elements is hidden,
// then create new browser window.
lRet = ::SendMessage(hWindow, WM_APP_CEF_NEW_WINDOW, (WPARAM)&popupFeatures, (LPARAM)&windowInfo);
return false;
}
#else
if (popupFeatures.toolBarVisible)
{
if ( //popupFeatures.locationBarVisible==false
popupFeatures.menuBarVisible == false)
{
lRet = ::SendMessage(hWindow, WM_APP_CEF_NEW_WINDOW, (WPARAM)&popupFeatures, (LPARAM)&windowInfo);
return false;
}
}
#endif
lRet = ::SendMessage(hWindow, WM_APP_CEF_NEW_WINDOW, (WPARAM)NULL, (LPARAM)&windowInfo);
return false;
}
case cef_window_open_disposition_t::CEF_WOD_CURRENT_TAB:
case cef_window_open_disposition_t::CEF_WOD_SINGLETON_TAB:
case cef_window_open_disposition_t::CEF_WOD_NEW_BACKGROUND_TAB:
case cef_window_open_disposition_t::CEF_WOD_NEW_WINDOW:
case cef_window_open_disposition_t::CEF_WOD_SAVE_TO_DISK:
case cef_window_open_disposition_t::CEF_WOD_OFF_THE_RECORD:
case cef_window_open_disposition_t::CEF_WOD_IGNORE_ACTION:
{
lRet = ::SendMessage(hWindow, WM_APP_CEF_NEW_WINDOW, (WPARAM)NULL, (LPARAM)&windowInfo);
return false;
;
}
default:
break;
}
if (lRet == 0)
return false;
}
#if CHROME_VERSION_MAJOR > 130
return CefLifeSpanHandler::OnBeforePopup(browser, frame, popup_id, target_url, target_frame_name, target_disposition, user_gesture, popupFeatures, windowInfo, client, settings, extra_info, no_javascript_access);
#else
return CefLifeSpanHandler::OnBeforePopup(browser, frame, target_url, target_frame_name, target_disposition, user_gesture, popupFeatures, windowInfo, client, settings, extra_info, no_javascript_access);
#endif
}
#if CHROME_VERSION_MAJOR > 125
void ClientHandler::OnBeforeDevToolsPopup(CefRefPtr<CefBrowser> browser,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
CefRefPtr<CefDictionaryValue>& extra_info,
bool* use_default_window)
{
}
#endif
void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model)
{
model->Remove(MENU_ID_VIEW_SOURCE);
model->Remove(MENU_ID_PRINT);
cef_context_menu_type_flags_t Flg = CM_TYPEFLAG_NONE;
Flg = params->GetTypeFlags();
if ((Flg & (CM_TYPEFLAG_PAGE | CM_TYPEFLAG_FRAME)) != 0)
{
if ((Flg & (CM_TYPEFLAG_LINK)) != 0)
{
if (theApp.m_bTabEnable_Init)
{
CString contextMenuOpenLinkTabLabel;
contextMenuOpenLinkTabLabel.LoadString(ID_CONTEXT_MENU_OPEN_LINK_TAB);
CefString cefContextMenuOpenLinkTabLabel(contextMenuOpenLinkTabLabel);
model->InsertItemAt(0, CEF_MENU_ID_OPEN_LINK_NEW, cefContextMenuOpenLinkTabLabel);
CString contextMenuOpenLinkTabInactiveLabel;
contextMenuOpenLinkTabInactiveLabel.LoadString(ID_CONTEXT_MENU_OPEN_LINK_TAB_INACTIVE);
CefString cefContextMenuOpenLinkTabInactiveLabel(contextMenuOpenLinkTabInactiveLabel);
model->InsertItemAt(1, CEF_MENU_ID_OPEN_LINK_NEW_NOACTIVE, cefContextMenuOpenLinkTabInactiveLabel);
}
else
{
CString contextMenuOpenLinkWindowLabel;
contextMenuOpenLinkWindowLabel.LoadString(ID_CONTEXT_MENU_OPEN_LINK_WINDOW);
CefString cefContextMenuOpenLinkWindowLabel(contextMenuOpenLinkWindowLabel);
model->InsertItemAt(0, CEF_MENU_ID_OPEN_LINK_NEW, cefContextMenuOpenLinkWindowLabel);
CString contextMenuOpenLinkWindowInactiveLabel;
contextMenuOpenLinkWindowInactiveLabel.LoadString(ID_CONTEXT_MENU_OPEN_LINK_WINDOW_INACTIVE);
CefString cefContextMenuOpenLinkWindowInactiveLabel(contextMenuOpenLinkWindowInactiveLabel);
model->InsertItemAt(1, CEF_MENU_ID_OPEN_LINK_NEW_NOACTIVE, cefContextMenuOpenLinkWindowInactiveLabel);
}
CString contextMenuCopyLinkLabel;
contextMenuCopyLinkLabel.LoadString(ID_CONTEXT_MENU_COPY_LINK);
CefString cefContextMenuCopyLinkLabel(contextMenuCopyLinkLabel);
model->InsertItemAt(2, CEF_MENU_ID_COPY_LINK, cefContextMenuCopyLinkLabel);
CString contextMenuSaveLinkLabel;
contextMenuSaveLinkLabel.LoadString(ID_CONTEXT_MENU_SAVE_LINK);
CefString cefContextMenuSaveLinkLabel(contextMenuSaveLinkLabel);
model->InsertItemAt(3, CEF_MENU_ID_SAVE_FILE, cefContextMenuSaveLinkLabel);
}
if ((Flg & (CM_TYPEFLAG_MEDIA | CM_MEDIATYPE_IMAGE)) != 0)
{
if (!params->GetSourceUrl().empty())
{
model->Remove(MENU_ID_BACK);
model->Remove(MENU_ID_FORWARD);
if (theApp.m_bTabEnable_Init)
{
CString contextMenuOpenImgTabLabel;
contextMenuOpenImgTabLabel.LoadString(ID_CONTEXT_MENU_OPEN_IMG_TAB);
CefString cefContextMenuOpenImgTabLabel(contextMenuOpenImgTabLabel);
model->AddItem(CEF_MENU_ID_OPEN_IMG, cefContextMenuOpenImgTabLabel);
CString contextMenuOpenImgTabInactiveLabel;
contextMenuOpenImgTabInactiveLabel.LoadString(ID_CONTEXT_MENU_OPEN_IMG_TAB_INACTIVE);
CefString cefContextMenuOpenImgTabInactiveLabel(contextMenuOpenImgTabInactiveLabel);
model->AddItem(CEF_MENU_ID_OPEN_IMG_NOACTIVE, cefContextMenuOpenImgTabInactiveLabel);
}
else
{
CString contextMenuOpenImgWindowLabel;
contextMenuOpenImgWindowLabel.LoadString(ID_CONTEXT_MENU_OPEN_IMG_WINDOW);
CefString cefContextMenuOpenImgWindowLabel(contextMenuOpenImgWindowLabel);
model->AddItem(CEF_MENU_ID_OPEN_IMG, cefContextMenuOpenImgWindowLabel);
CString contextMenuOpenImgWindowInactiveLabel;
contextMenuOpenImgWindowInactiveLabel.LoadString(ID_CONTEXT_MENU_OPEN_IMG_WINDOW_INACTIVE);
CefString cefContextMenuOpenImgWindowInactiveLabel(contextMenuOpenImgWindowInactiveLabel);
model->AddItem(CEF_MENU_ID_OPEN_IMG_NOACTIVE, cefContextMenuOpenImgWindowInactiveLabel);
}
CString contextMenuSaveImgLabel;
contextMenuSaveImgLabel.LoadString(ID_CONTEXT_MENU_SAVE_IMG);
CefString cefContextMenuSaveImgLabel(contextMenuSaveImgLabel);
model->AddItem(CEF_MENU_ID_SAVE_IMG, cefContextMenuSaveImgLabel);
if (!theApp.IsSGMode())
{
CString contextMenuCopyImgLabel;
contextMenuCopyImgLabel.LoadString(ID_CONTEXT_MENU_COPY_IMG);
CefString cefContextMenuCopyImgLabel(contextMenuCopyImgLabel);
model->AddItem(CEF_MENU_ID_IMG_COPY, cefContextMenuCopyImgLabel);
}
CString contextMenuCopyImgLinkLabel;
contextMenuCopyImgLinkLabel.LoadString(ID_CONTEXT_MENU_COPY_IMG_LINK);
CefString cefContextMenuCopyImgLinkLabel(contextMenuCopyImgLinkLabel);
model->AddItem(CEF_MENU_ID_IMG_COPY_LINK, cefContextMenuCopyImgLinkLabel);
}
}
if (Flg & CM_TYPEFLAG_SELECTION)
{
CString strSelText;
CefString strCfSt;
strCfSt = params->GetSelectionText();
strSelText = (LPCWSTR)strCfSt.c_str();
strSelText.TrimLeft();
strSelText.TrimRight();
if (!strSelText.IsEmpty())
{
SBUtil::GetDivChar(strSelText, 48, strSelText, TRUE);
CString contextMenuSearchLabel;
contextMenuSearchLabel.LoadString(ID_CONTEXT_MENU_SEARCH);
CString strFmt;
strFmt.Format(contextMenuSearchLabel, strSelText);
CefString strCFmt(strFmt);
model->InsertItemAt(0, CEF_MENU_ID_OPEN_SEARCH, strCFmt);
}
}
}
CString contextMenuReloadLabel;
contextMenuReloadLabel.LoadString(ID_CONTEXT_MENU_RELOAD);
CefString cefContextMenuReloadLabel(contextMenuReloadLabel);
model->AddItem(MENU_ID_RELOAD, cefContextMenuReloadLabel);
CString contextMenuPrintLabel;
contextMenuPrintLabel.LoadString(ID_CONTEXT_MENU_PRINT);
CefString cefContextMenuPrintLabel(contextMenuPrintLabel);
model->AddItem(MENU_ID_PRINT, cefContextMenuPrintLabel);
// メニュー項目調整後、Separatorが連続することがあるので、連続している場合は削除する。
size_t count = model->GetCount();
int beforeCommandId = CH_MENU_INVALID_OR_SEPARATOR;
int commandId;
for (size_t i = count - 1; i > 0; i--)
{
commandId = model->GetCommandIdAt(i);
if (commandId == CH_MENU_INVALID_OR_SEPARATOR && beforeCommandId == CH_MENU_INVALID_OR_SEPARATOR)
{
model->RemoveAt(i);
}
beforeCommandId = commandId;
}
// 先頭がSeparatorだった場合、まだ残存している。
commandId = model->GetCommandIdAt(0);
if (commandId == CH_MENU_INVALID_OR_SEPARATOR)
{
model->RemoveAt(0);
}
// call parent
CefContextMenuHandler::OnBeforeContextMenu(browser, frame, params, model);
}
bool ClientHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id, EventFlags event_flags)
{
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
if (command_id == CEF_MENU_ID_OPEN_SEARCH)
{
CString strSelText;
CefString strCfSt;
strCfSt = params->GetSelectionText();
strSelText = (LPCWSTR)strCfSt.c_str();
strSelText.TrimLeft();
strSelText.TrimRight();
::SendMessageTimeout(hWindow, WM_APP_CEF_SEARCH_URL, (WPARAM)(LPCTSTR)strSelText, (LPARAM)TRUE, SMTO_NORMAL, 1000, NULL);
return true;
}
else if (command_id == CEF_MENU_ID_OPEN_LINK_NEW)
{
CefString strURLC;
strURLC = params->GetLinkUrl();
LPCWSTR pszURL = {0};
pszURL = (LPCWSTR)strURLC.c_str();
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)cef_window_open_disposition_t::CEF_WOD_NEW_WINDOW, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
else if (command_id == CEF_MENU_ID_OPEN_LINK_NEW_NOACTIVE)
{
CefString strURLC;
strURLC = params->GetLinkUrl();
LPCWSTR pszURL = {0};
pszURL = (LPCWSTR)strURLC.c_str();
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)cef_window_open_disposition_t::CEF_WOD_NEW_BACKGROUND_TAB, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
else if (command_id == CEF_MENU_ID_OPEN_IMG)
{
CefString strURLC;
strURLC = params->GetSourceUrl();
LPCWSTR pszURL = {0};
pszURL = (LPCWSTR)strURLC.c_str();
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)cef_window_open_disposition_t::CEF_WOD_NEW_WINDOW, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
else if (command_id == CEF_MENU_ID_OPEN_IMG_NOACTIVE)
{
CefString strURLC;
strURLC = params->GetSourceUrl();
LPCWSTR pszURL = {0};
pszURL = (LPCWSTR)strURLC.c_str();
::SendMessageTimeout(hWindow, WM_NEW_WINDOW_URL, (WPARAM)cef_window_open_disposition_t::CEF_WOD_NEW_BACKGROUND_TAB, (LPARAM)pszURL, SMTO_NORMAL, 1000, NULL);
return true;
}
else if (command_id == CEF_MENU_ID_SAVE_IMG)
{
browser->GetHost()->StartDownload(params->GetSourceUrl());
return true;
}
else if (command_id == CEF_MENU_ID_SAVE_FILE)
{
browser->GetHost()->StartDownload(params->GetLinkUrl());
return true;
}
else if (command_id == CEF_MENU_ID_IMG_COPY_LINK)
{
CString str;
CefString strURLC;
strURLC = params->GetSourceUrl();
str = (LPCWSTR)strURLC.c_str();
if (!str.IsEmpty())
{
//data:image/pngの場合があるので、IsURL判定を行わない。
//if (SBUtil::IsURL_HTTP(str))
{
if (::OpenClipboard(NULL))
{
int nByte = (str.GetLength() + 1) * sizeof(TCHAR);
HGLOBAL hText = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, nByte);
if (hText != NULL)
{
BYTE* pText = (BYTE*)::GlobalLock(hText);
if (pText != NULL)
{
::memcpy(pText, (LPCTSTR)str, nByte);
::GlobalUnlock(hText);
::EmptyClipboard();
::SetClipboardData(CF_UNICODETEXT, hText);
}
}
::CloseClipboard();
}
}
}
return true;
}
else if (command_id == CEF_MENU_ID_IMG_COPY)
{
CString str;
CefString strURLC;
strURLC = params->GetSourceUrl();
str = (LPCWSTR)strURLC.c_str();
if (!str.IsEmpty())
{
LPCWSTR pszURL = {0};
pszURL = (LPCWSTR)strURLC.c_str();
::SendMessageTimeout(hWindow, WM_COPY_IMAGE, (WPARAM)(LPCTSTR)str, NULL, SMTO_NORMAL, 1000, NULL);
return true;
}
return true;
}
else if (command_id == CEF_MENU_ID_COPY_LINK)
{
CString str;
CefString strURLC;
strURLC = params->GetUnfilteredLinkUrl();
str = (LPCWSTR)strURLC.c_str();
if (!str.IsEmpty())
{
if (::OpenClipboard(NULL))
{
int nByte = (str.GetLength() + 1) * sizeof(TCHAR);
HGLOBAL hText = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, nByte);
if (hText != NULL)
{
BYTE* pText = (BYTE*)::GlobalLock(hText);
if (pText != NULL)
{
::memcpy(pText, (LPCTSTR)str, nByte);
::GlobalUnlock(hText);
::EmptyClipboard();
::SetClipboardData(CF_UNICODETEXT, hText);
}
}
::CloseClipboard();
}
}
return true;
}
}
// call parent
return CefContextMenuHandler::OnContextMenuCommand(browser, frame, params, command_id, event_flags);
}
void ClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward)
{
REQUIRE_UI_THREAD();
INT nState = 0;
// set state
if (isLoading)
nState |= CEF_BIT_IS_LOADING;
if (canGoBack)
nState |= CEF_BIT_CAN_GO_BACK;
if (canGoForward)
nState |= CEF_BIT_CAN_GO_FORWARD;
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
// send message
::SendMessageTimeout(hWindow, WM_APP_CEF_STATE_CHANGE, (WPARAM)nState, NULL, SMTO_NORMAL, 1000, NULL);
}
// call parent
CefLoadHandler::OnLoadingStateChange(browser, isLoading, canGoBack, canGoForward);
}
void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& url)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
if (frame->IsMain())
{
LPCWSTR pszURL = (LPCWSTR)url.c_str();
::SendMessageTimeout(hWindow, WM_APP_CEF_ADDRESS_CHANGE, (WPARAM)pszURL, NULL, SMTO_NORMAL, 1000, NULL);
}
}
// call parent
CefDisplayHandler::OnAddressChange(browser, frame, url);
}
void ClientHandler::OnFullscreenModeChange(CefRefPtr<CefBrowser> browser, bool fullscreen)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
::SendMessageTimeout(hWindow, WM_APP_CEF_FULLSCREEN_MODE_CHANGE, (WPARAM)fullscreen, NULL, SMTO_NORMAL, 1000, NULL);
}
// call parent
CefDisplayHandler::OnFullscreenModeChange(browser, fullscreen);
}
void ClientHandler::OnLoadingProgressChange(CefRefPtr<CefBrowser> browser, double progress)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
progress = progress * 100;
ULONG_PTR ulProgress = 0;
ulProgress = (ULONG_PTR)progress;
DWORD dwProgress = 0;
dwProgress = (DWORD)ulProgress;
::SendMessageTimeout(hWindow, WM_APP_CEF_PROGRESS_CHANGE, (WPARAM)dwProgress, NULL, SMTO_NORMAL, 1000, NULL);
}
// call parent
CefDisplayHandler::OnLoadingProgressChange(browser, progress);
}
void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
LPCWSTR pszTitle = NULL;
pszTitle = (LPCWSTR)title.c_str();
::SendMessageTimeout(hWindow, WM_APP_CEF_TITLE_CHANGE, (WPARAM)pszTitle, NULL, SMTO_NORMAL, 1000, NULL);
}
// call parent
CefDisplayHandler::OnTitleChange(browser, title);
}
void ClientHandler::OnFaviconURLChange(CefRefPtr<CefBrowser> browser, const std::vector<CefString>& icon_urls)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
CefString strIconList;
LPCWSTR pszFavURL = NULL;
for (UINT i = 0; i < icon_urls.size(); i++)
{
strIconList = icon_urls[i];
pszFavURL = (LPCWSTR)strIconList.c_str();
}
if (pszFavURL)
{
::SendMessageTimeout(hWindow, WM_APP_CEF_FAVICON_MESSAGE, (WPARAM)pszFavURL, NULL, SMTO_NORMAL, 1000, NULL);
}
}
// call parent
CefDisplayHandler::OnFaviconURLChange(browser, icon_urls);
}
void ClientHandler::OnStatusMessage(CefRefPtr<CefBrowser> browser, const CefString& value)
{
REQUIRE_UI_THREAD();
// The frame window will be the parent of the browser window
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
LPCWSTR pszStatus = NULL;
pszStatus = (LPCWSTR)value.c_str();
::SendMessageTimeout(hWindow, WM_APP_CEF_STATUS_MESSAGE, (WPARAM)pszStatus, NULL, SMTO_NORMAL, 1000, NULL);
}
// call parent
CefDisplayHandler::OnStatusMessage(browser, value);
}
bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
cef_log_severity_t level,
const CefString& message,
const CefString& source,
int line)
{
REQUIRE_UI_THREAD();
if (level == LOGSEVERITY_DISABLE) return TRUE;
CString strWriteLine;
if (theApp.m_pDebugDlg)
{
CString strLogLevel;
switch (level)
{
case LOGSEVERITY_DEFAULT:
strLogLevel = _T("DEFAULT");
break;
case LOGSEVERITY_VERBOSE:
strLogLevel = _T("VERBOSE");
break;
case LOGSEVERITY_INFO:
strLogLevel = _T("INFO");
break;
case LOGSEVERITY_WARNING:
strLogLevel = _T("WARNING");
break;
case LOGSEVERITY_ERROR:
strLogLevel = _T("ERROR");
break;
case LOGSEVERITY_FATAL:
strLogLevel = _T("FATAL");
break;
default:
strLogLevel = _T("N/A");
break;
}
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
DebugWndLogData dwLogData;
dwLogData.mHWND.Format(_T("CV_WND:0x%08p"), hWindow);
dwLogData.mFUNCTION_NAME = _T("ConsoleMessage");
dwLogData.mMESSAGE1 = (LPCWSTR)message.c_str();
dwLogData.mMESSAGE2 = strLogLevel;
dwLogData.mMESSAGE3.Format(_T("Source:%s"), (LPCWSTR)source.c_str());
dwLogData.mMESSAGE4.Format(_T("Line:%d"), line);
theApp.AppendDebugViewLog(dwLogData);
}
}
if (theApp.m_AppSettings.IsAdvancedLogMode())
{
if (theApp.m_AppSettings.GetAdvancedLogLevel() == DEBUG_LOG_LEVEL_OUTPUT_ALL)
{
CString strLogPath;
strLogPath = theApp.m_strCEFCachePath;
strLogPath += _T("\\console.log");
strWriteLine.Format(_T("Message:%s\nSource:%s\nLine:%d\n"), (LPCWSTR)message.c_str(), (LPCWSTR)source.c_str(), line);
_wsetlocale(LC_ALL, _T("jpn"));
CStdioFile stdFile;
if (stdFile.Open(strLogPath, CFile::modeWrite | CFile::shareDenyNone | CFile::modeCreate | CFile::modeNoTruncate))
{
TRY
{
stdFile.SeekToEnd();
stdFile.WriteString(strWriteLine);
}
CATCH(CFileException, eP) {}
END_CATCH
stdFile.Close();
}
_wsetlocale(LC_ALL, _T(""));
return TRUE;
}
}
return TRUE;
}
#if CHROME_VERSION_MAJOR < 125
void ClientHandler::OnBeforeDownload(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback)
#define RETURN_ON_BEFORE_DOWNLOAD(value) return
#else
bool ClientHandler::OnBeforeDownload(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback)
#define RETURN_ON_BEFORE_DOWNLOAD(value) return (value)
#endif
{
REQUIRE_UI_THREAD();
//Download禁止
if (theApp.m_AppSettings.IsEnableDownloadRestriction())
{
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
HWND hWindowFrm = GetParent(hWindow);
if (SafeWnd(hWindowFrm))
hWindow = hWindowFrm;
CString alertMsg;
alertMsg.LoadString(ID_MSG_FILE_DOWNLOAD_RESTRICTED);
theApp.SB_MessageBox(hWindow, alertMsg, NULL, MB_OK | MB_ICONWARNING, TRUE);
}
EmptyWindowClose(browser);
RETURN_ON_BEFORE_DOWNLOAD(true);
}
m_bDownLoadStartFlg = TRUE;
CString strFileName;
strFileName = (LPCWSTR)suggested_name.c_str();
strFileName.TrimLeft();
strFileName.TrimRight();
//ファイル名に使えない文字を置き換える。
strFileName = SBUtil::GetValidFileName(strFileName);
CString strPath;
if (theApp.IsSGMode())
{
strPath = theApp.m_AppSettings.GetRootPath();
if (strPath.IsEmpty())
strPath = _T("B:\\");
}
else
{
strPath = SBUtil::GetDownloadFolderPath();
}
strPath = strPath.TrimRight('\\');
strPath += _T("\\");
HWND hWindow = GetSafeParentWnd(browser);
if (SafeWnd(hWindow))
{
UINT nBrowserId = browser->GetIdentifier();
CWnd* pCWnd = CWnd::FromHandle(hWindow);
CString strURL;
CefString strURLC;
strURLC = browser->GetMainFrame()->GetURL();
strURL = (LPCWSTR)strURLC.c_str();
if (strURL.IsEmpty())
::SendMessageTimeout(hWindow, WM_APP_CEF_DOWNLOAD_BLANK_PAGE, (WPARAM)TRUE, NULL, SMTO_NORMAL, 1000, NULL);
else
::SendMessageTimeout(hWindow, WM_APP_CEF_DOWNLOAD_BLANK_PAGE, (WPARAM)FALSE, NULL, SMTO_NORMAL, 1000, NULL);
SendMessageTimeout(hWindow, WM_APP_CEF_WINDOW_ACTIVATE, (WPARAM)NULL, (LPARAM)NULL, SMTO_NORMAL, 1000, NULL);
//ダウンロード中の場合は、警告を表示する。
if (theApp.m_DlMgr.IsDlProgress(nBrowserId))
{
HWND hWindowFrm = GetParent(hWindow);
CString inProgressDownloadMessage;
inProgressDownloadMessage.LoadString(ID_MSG_ANOTHER_DOWNLOAD_IN_PROGRESS);
int iRet = theApp.SB_MessageBox(hWindowFrm, inProgressDownloadMessage, NULL, MB_OK | MB_ICONWARNING, TRUE);
RETURN_ON_BEFORE_DOWNLOAD(true);
}
CString szFilter;
szFilter.LoadString(ID_FILE_TYPE_ALL);
CString strTitle;
strTitle.LoadString(ID_DOWNLOAD_FILE_CHOOSER_TITLE);
CStringW strCaption(theApp.m_strThisAppName);
CStringW strRootDrive(theApp.m_AppSettings.GetRootPath());
CStringW strMsg;
INT_PTR bRet = FALSE;
CFileDialog* pFileDlg = new CFileDialog(FALSE, NULL, strFileName, OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, szFilter, pCWnd);
pFileDlg->m_ofn.lpstrTitle = strTitle.GetString();
pFileDlg->m_ofn.lpstrInitialDir = strPath;
bRet = pFileDlg->DoModal();
if (bRet == IDOK)
{
strPath = pFileDlg->GetPathName();
if (!strPath.IsEmpty())
{
CefString strcfPath(strPath);
callback->Continue(strcfPath, false);
if (theApp.m_AppSettings.IsEnableLogging() && theApp.m_AppSettings.IsEnableDownloadLogging())
{
CString strFileName;
TCHAR* ptrFile = NULL;
ptrFile = PathFindFileName(strPath);
if (ptrFile)
{
strFileName = ptrFile;
}
if (strURL.IsEmpty())
{
strURL = (LPCWSTR)download_item->GetURL().c_str();
}
theApp.m_pLogDisp->SendLog(LOG_DOWNLOAD, strFileName, strURL);
}
::SendMessageTimeout(hWindow, WM_APP_CEF_BEFORE_DOWNLOAD, (WPARAM)TRUE, NULL, SMTO_NORMAL, 1000, NULL);
theApp.m_DlMgr.Init_DLDlg(theApp.m_pMainWnd, nBrowserId);
theApp.m_DlMgr.SetDlProgress(nBrowserId, TRUE);
}
}
else
{
theApp.m_DlMgr.SetDlProgress(nBrowserId, FALSE);
theApp.m_DlMgr.Cancel(nBrowserId);
::SendMessageTimeout(hWindow, WM_APP_CEF_BEFORE_DOWNLOAD, (WPARAM)FALSE, NULL, SMTO_NORMAL, 1000, NULL);
EmptyWindowClose(browser);
callback->Continue(_T(""), false);
}
if (pFileDlg)
{
delete pFileDlg;
pFileDlg = NULL;
}
RETURN_ON_BEFORE_DOWNLOAD(true);
}
callback->Continue(_T(""), false);
RETURN_ON_BEFORE_DOWNLOAD(true);
}
#undef RETURN_ON_BEFORE_DOWNLOAD
void ClientHandler::OnDownloadUpdated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item, CefRefPtr<CefDownloadItemCallback> callback)
{
///https://www.catalog.update.microsoft.com/Search.aspx?q=KB4051963
REQUIRE_UI_THREAD();
//Download禁止
if (theApp.m_AppSettings.IsEnableDownloadRestriction())
{
return;
}
CEFDownloadItemValues values = {0};
values.bIsValid = download_item->IsValid();
values.bIsInProgress = download_item->IsInProgress();
values.bIsComplete = download_item->IsComplete();
values.bIsCanceled = download_item->IsCanceled();
values.nProgress = download_item->GetPercentComplete();
values.nSpeed = download_item->GetCurrentSpeed();
values.nReceived = download_item->GetReceivedBytes();
values.nTotal = download_item->GetTotalBytes();
if (download_item->IsValid())
{
CefString cefFulPath = download_item->GetFullPath();
LPCWSTR fullPath = (LPCWSTR)cefFulPath.c_str();
if (fullPath)
StringCchCopy(values.szFullPath, 512, fullPath);
}
HWND hWindow = GetSafeParentWnd(browser);
UINT nBrowserId = browser->GetIdentifier();
//theApp.m_DlMgr.SetDlProgress(nBrowserId, FALSE);
if (SafeWnd(hWindow))
{
if (values.bIsComplete)
{
theApp.m_DlMgr.SetDlProgress(nBrowserId, FALSE);
::SendMessageTimeout(hWindow, WM_APP_CEF_DOWNLOAD_UPDATE, (WPARAM)FALSE, NULL, SMTO_NORMAL, 1000, NULL);
theApp.m_DlMgr.DLComp_DLDlg(nBrowserId, values.szFullPath);
EmptyWindowClose(browser);
m_bDownLoadStartFlg = FALSE;
return;
}
else if (values.bIsInProgress)
{