-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSimpleBrowser.cpp
More file actions
4239 lines (3701 loc) · 114 KB
/
SimpleBrowser.cpp
File metadata and controls
4239 lines (3701 loc) · 114 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
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
// SimpleBrowser.cpp --- simple Win32 browser
// Copyright (C) 2019 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
// This file is public domain software.
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#include <mmsystem.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <mshtml.h>
#include <intshcut.h>
#include <urlhist.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cctype>
#include <cassert>
#include <strsafe.h>
#include <comdef.h>
#include <mshtmcid.h>
#include <process.h>
#include "MWebBrowserEx.hpp"
#include "MEventSink.hpp"
#include "MBindStatusCallback.hpp"
#include "AddLinkDlg.hpp"
#include "AboutBox.hpp"
#include "Settings.hpp"
#include "mime_info.h"
#include "mstr.hpp"
#include "color_value.h"
#include "AmsiScanner.hpp"
#include "resource.h"
// button size
#define BTN_WIDTH 120
#define BTN_HEIGHT 60
#define DROPDOWN_HEIGHT 500
// timer IDs
#define SOURCE_DONE_TIMER 999
#define REFRESH_TIMER 888
#define DOWNLOAD_TIMER_INTERVAL 500
#define MIN_COMMAND_ID 20000
static const TCHAR s_szName[] = TEXT("SB Simple Browser");
static HINSTANCE s_hInst = NULL;
static HACCEL s_hAccel = NULL;
static HWND s_hMainWnd = NULL;
static INT s_nCmdShow = SW_SHOWNORMAL;
static HWND s_hStatusBar = NULL;
static HWND s_hAddrBarComboBox = NULL;
static HWND s_hAddrBarEdit = NULL;
typedef std::unordered_map<HWND, BOOL> download_map_type;
static download_map_type s_downloadings;
static MWebBrowserEx *s_pWebBrowser = NULL;
static HFONT s_hButtonFont = NULL;
static HFONT s_hAddressFont = NULL;
static MEventSink *s_pEventSink = MEventSink::Create();
static BOOL s_bLoadingPage = FALSE;
static HBITMAP s_hbmSecure = NULL;
static HBITMAP s_hbmInsecure = NULL;
static std::wstring s_strURL;
static std::wstring s_strTitle;
static BOOL s_bKiosk = FALSE;
static const TCHAR s_szButton[] = TEXT("BUTTON");
static std::wstring s_strStop = L"Stop";
static std::wstring s_strRefresh = L"Refresh";
static std::unordered_map<HWND, std::wstring> s_hwnd2url;
static std::unordered_map<HWND, COLORREF> s_hwnd2color;
static std::unordered_map<HWND, COLORREF> s_hwnd2bgcolor;
static DWORD s_bgcolor = RGB(255, 255, 255);
static DWORD s_color = RGB(0, 0, 0);
static std::wstring s_upside_data;
static std::vector<HWND> s_upside_hwnds;
static std::wstring s_downside_data;
static std::vector<HWND> s_downside_hwnds;
static std::wstring s_leftside_data;
static std::vector<HWND> s_leftside_hwnds;
static std::wstring s_rightside_data;
static std::vector<HWND> s_rightside_hwnds;
static std::wstring s_popup_default_data;
static std::wstring s_popup_image_data;
static std::wstring s_popup_text_data;
static std::wstring s_popup_anchor_data;
static BOOL s_bEnableForward = FALSE;
static BOOL s_bEnableBack = FALSE;
static std::vector<std::wstring> s_menu_links;
static INT s_nSecurity = 0;
static std::unordered_set<std::wstring> s_insecure_url;
void RememberInsecureURL(const WCHAR *url)
{
s_insecure_url.insert(url);
}
void MarkSecurity(INT nSecurity, BOOL bOverwrite = FALSE)
{
if (bOverwrite)
{
s_nSecurity = nSecurity;
}
else
{
if (nSecurity < 0)
s_nSecurity = -1;
}
InvalidateRect(s_hAddrBarComboBox, NULL, TRUE);
SendMessage(s_hStatusBar, SB_SETTEXT, 1 | SBT_OWNERDRAW, 0);
InvalidateRect(s_hStatusBar, NULL, TRUE);
}
void DoUpdateURL(const WCHAR *url)
{
::SetWindowTextW(s_hAddrBarComboBox, url);
}
// load a resource string using rotated buffers
LPTSTR LoadStringDx(INT nID)
{
static UINT s_index = 0;
const UINT cchBuffMax = 1024;
static TCHAR s_sz[4][cchBuffMax];
TCHAR *pszBuff = s_sz[s_index];
s_index = (s_index + 1) % _countof(s_sz);
pszBuff[0] = 0;
if (!::LoadString(NULL, nID, pszBuff, cchBuffMax))
assert(0);
return pszBuff;
}
UINT GetCheck(HWND hwnd)
{
DWORD style = GetWindowLong(hwnd, GWL_STYLE);
if (!(style & BS_PUSHLIKE))
return FALSE;
return (BOOL)(LONG_PTR)GetWindowLongPtr(hwnd, GWLP_USERDATA);
}
void SetCheck(HWND hwnd, UINT uCheck)
{
DWORD style = GetWindowLong(hwnd, GWL_STYLE);
if (!(style & BS_PUSHLIKE))
return;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)(LONG)uCheck);
InvalidateRect(hwnd, NULL, TRUE);
}
std::wstring text2html(const WCHAR *text)
{
std::wstring contents;
contents.reserve(wcslen(text));
for (; *text; ++text)
{
if (*text == L'<')
contents += L"<";
else if (*text == L'>')
contents += L">";
else if (*text == L'&')
contents += L"&";
else
contents += *text;
}
std::wstring ret = L"<html><body><pre>";
ret += contents;
ret += L"</pre></body></html>";
return ret;
}
void SetDocumentContents(IHTMLDocument2 *pDocument, const WCHAR *text,
bool is_html = true)
{
std::wstring str;
if (!is_html)
{
str = text2html(text);
}
else
{
str = text;
}
if (BSTR bstr = SysAllocString(str.c_str()))
{
if (SAFEARRAY *sa = SafeArrayCreateVector(VT_VARIANT, 0, 1))
{
VARIANT *pvar;
HRESULT hr = SafeArrayAccessData(sa, (void **)&pvar);
if (SUCCEEDED(hr))
{
pvar->vt = VT_BSTR;
pvar->bstrVal = bstr;
SafeArrayDestroy(sa);
pDocument->write(sa);
}
}
SysFreeString(bstr);
}
}
void SetInternalPageContents(const WCHAR *html, bool is_html = true)
{
if (IHTMLDocument2 *pDocument = s_pWebBrowser->GetIHTMLDocument2())
{
pDocument->close();
SetDocumentContents(pDocument, html, is_html);
pDocument->Release();
}
}
BOOL UrlInBlackList(const WCHAR *url)
{
std::wstring strURL = url;
SETTINGS::list_type::const_iterator it, end = g_settings.m_black_list.end();
for (it = g_settings.m_black_list.begin(); it != end; ++it)
{
if (strURL.find(*it) != std::wstring::npos)
{
return TRUE;
}
}
return FALSE;
}
BOOL IsAccessibleProtocol(const std::wstring& protocol)
{
if (protocol == L"http" ||
protocol == L"https" ||
protocol == L"view-source" ||
protocol == L"about" ||
protocol == L"javascript" ||
protocol == L"res")
{
return TRUE;
}
if (g_settings.m_local_file_access && !g_settings.m_kiosk_mode)
{
if (protocol == L"file")
return TRUE;
}
return FALSE;
}
BOOL IsURL(const WCHAR *url)
{
if (PathIsURL(url) || UrlIs(url, URLIS_APPLIABLE))
return TRUE;
if (wcsstr(url, L"www.") == url || wcsstr(url, L"ftp.") == url)
return TRUE;
int cch = lstrlenW(url);
if (cch >= 4 && wcsstr(&url[cch - 4], L".com") != NULL)
return TRUE;
if (cch >= 5 && wcsstr(&url[cch - 5], L".com/") != NULL)
return TRUE;
if (cch >= 6 && wcsstr(&url[cch - 6], L".co.jp") != NULL)
return TRUE;
if (cch >= 7 && wcsstr(&url[cch - 7], L".co.jp/") != NULL)
return TRUE;
return FALSE;
}
BOOL IsAccessible(const WCHAR *url)
{
if (PathFileExists(url) || UrlIsFileUrl(url) ||
PathIsUNC(url) || PathIsNetworkPath(url))
{
return g_settings.m_local_file_access && !g_settings.m_kiosk_mode;
}
if (LPCWSTR pch = wcschr(url, L':'))
{
std::wstring protocol(url, pch - url);
if (!IsAccessibleProtocol(protocol))
return FALSE;
if (g_settings.m_local_file_access && !g_settings.m_kiosk_mode)
{
if (protocol == L"file")
return TRUE;
}
}
if (IsURL(url))
return TRUE;
return FALSE;
}
inline LPTSTR MakeFilterDx(LPTSTR psz)
{
for (LPTSTR pch = psz; *pch; ++pch)
{
if (*pch == TEXT('|'))
*pch = 0;
}
return psz;
}
std::wstring URL_encode(const std::wstring& url)
{
std::string str;
size_t len = url.size() * 4;
str.resize(len);
if (len > 0)
WideCharToMultiByte(CP_UTF8, 0, url.c_str(), -1, &str[0], (INT)len, NULL, NULL);
len = strlen(str.c_str());
str.resize(len);
std::wstring ret;
WCHAR buf[4];
static const WCHAR s_hex[] = L"0123456789ABCDEF";
for (size_t i = 0; i < str.size(); ++i)
{
if (str[i] == ' ')
{
ret += L'+';
}
else if (std::isalnum(str[i]))
{
ret += (char)str[i];
}
else
{
switch (str[i])
{
case L'.':
case L'-':
case L'_':
case L'*':
ret += (char)str[i];
break;
default:
buf[0] = L'%';
buf[1] = s_hex[(str[i] >> 4) & 0xF];
buf[2] = s_hex[str[i] & 0xF];
buf[3] = 0;
ret += buf;
break;
}
}
}
return ret;
}
std::string URL_decode(const std::string& str)
{
std::string ret;
char buf[3];
buf[2] = 0;
for (size_t i = 0; i < str.size(); ++i)
{
if (str[i] == '+')
{
ret += ' ';
}
else if (str[i] == '%' && i + 2 < str.size())
{
buf[0] = str[i + 1];
buf[1] = str[i + 2];
if (std::isxdigit(buf[0]) && std::isxdigit(buf[1]))
{
i += 2;
ret += (char)std::strtoul(buf, NULL, 16);
}
else
{
ret += '%';
}
}
else
{
ret += str[i];
}
}
return ret;
}
void DoNavigate(HWND hwnd, const WCHAR *url, DWORD dwFlags = 0);
void OnNew(HWND hwnd, LPCWSTR url);
BOOL DoSaveURL(HWND hwnd, LPCWSTR pszURL);
void DoSearch(HWND hwnd, LPCWSTR str)
{
std::wstring query = LoadStringDx(IDS_QUERY_URL);
std::wstring encoded = URL_encode(str);
query += encoded;
DoNavigate(hwnd, query.c_str(), navNoHistory);
}
struct MEventHandler : MEventSinkListener
{
virtual void BeforeNavigate2(
IDispatch *pDispatch,
VARIANT *url,
VARIANT *flags,
VARIANT *target,
VARIANT *PostData,
VARIANT *headers,
VARIANT_BOOL *Cancel)
{
assert(url->vt == VT_BSTR);
assert(flags->vt == VT_I4);
assert(target->vt == VT_BSTR);
assert(PostData->vt == VARTYPE(VT_BYREF | VT_VARIANT));
assert(headers->vt == VT_BSTR);
BSTR bstrURL = url->bstrVal;
DWORD dwFlags = flags->lVal;
BSTR bstrTarget = target->bstrVal;
BSTR bstrHeaders = headers->bstrVal;
IDispatch *pApp = NULL;
HRESULT hr = s_pWebBrowser->get_Application(&pApp);
printf("BeforeNavigate2: (%p, %p): '%ls', '%ls', '%ls': %08lX\n",
pDispatch, pApp, bstrURL, bstrTarget, bstrHeaders, dwFlags);
if (bstrHeaders)
printf("Has additional headers: '%ls'\n", bstrHeaders);
if (SUCCEEDED(hr))
{
if (pApp == pDispatch)
{
if (UrlInBlackList(bstrURL))
{
printf("in black list: %ls\n", bstrURL);
s_pWebBrowser->Stop();
s_strURL = bstrURL;
SetInternalPageContents(LoadStringDx(IDS_HITBLACKLIST));
*Cancel = VARIANT_TRUE;
PostMessage(s_hMainWnd, WM_COMMAND, ID_DOCUMENT_COMPLETE, 0);
return;
}
if (!IsAccessible(bstrURL))
{
printf("inaccessible: %ls\n", bstrURL);
s_pWebBrowser->Stop();
s_strURL = bstrURL;
SetInternalPageContents(LoadStringDx(IDS_ACCESS_FAIL));
*Cancel = VARIANT_TRUE;
PostMessage(s_hMainWnd, WM_COMMAND, ID_DOCUMENT_COMPLETE, 0);
return;
}
s_bLoadingPage = TRUE;
MarkSecurity(0, TRUE);
DoUpdateURL(bstrURL);
::SetDlgItemText(s_hMainWnd, ID_STOP_REFRESH, s_strStop.c_str());
}
pApp->Release();
}
}
virtual void NavigateComplete2(
IDispatch *pDispatch,
BSTR url)
{
IDispatch *pApp = NULL;
HRESULT hr = s_pWebBrowser->get_Application(&pApp);
printf("NavigateComplete2: (%p, %p): '%ls'\n",
pDispatch, pApp, url);
if (SUCCEEDED(hr))
{
if (pApp == pDispatch)
{
s_strURL = url;
::SetDlgItemText(s_hMainWnd, ID_STOP_REFRESH, s_strRefresh.c_str());
s_bLoadingPage = FALSE;
PostMessage(s_hMainWnd, WM_COMMAND, ID_DOCUMENT_COMPLETE, 0);
}
pApp->Release();
}
}
virtual void NewWindow3(
IDispatch **ppDisp,
VARIANT_BOOL *Cancel,
DWORD dwFlags,
BSTR bstrUrlContext,
BSTR bstrUrl)
{
printf("NewWindow3: '%ls', '%ls', 0x%08lX\n", bstrUrl, bstrUrlContext, dwFlags);
//*Cancel = VARIANT_TRUE;
IDispatch *pApp = NULL;
HRESULT hr = s_pWebBrowser->get_Application(&pApp);
*ppDisp = pApp;
std::wstring url = bstrUrl;
if (g_settings.m_dont_popup || g_settings.m_kiosk_mode)
{
DoNavigate(s_hMainWnd, url.c_str());
}
else
{
OnNew(s_hMainWnd, url.c_str());
}
}
virtual void CommandStateChange(
long Command,
VARIANT_BOOL Enable)
{
printf("CommandStateChange: 0x%08lX, %d\n", Command, Enable);
//*Cancel = VARIANT_TRUE;
if (Command == CSC_NAVIGATEFORWARD)
{
s_bEnableForward = (Enable == VARIANT_TRUE);
}
else if (Command == CSC_NAVIGATEBACK)
{
s_bEnableBack = (Enable == VARIANT_TRUE);
}
::EnableWindow(::GetDlgItem(s_hMainWnd, ID_BACK), s_bEnableBack);
::EnableWindow(::GetDlgItem(s_hMainWnd, ID_NEXT), s_bEnableForward);
}
virtual void StatusTextChange(BSTR Text)
{
printf("StatusTextChange: '%ls'\n", Text);
SendMessage(s_hStatusBar, SB_SETTEXT, 0 | 0, (LPARAM)Text);
}
virtual void TitleTextChange(BSTR Text)
{
WCHAR szText[256];
printf("TitleTextChange: '%ls'\n", Text);
StringCbPrintfW(szText, sizeof(szText), LoadStringDx(IDS_TITLE_TEXT), Text);
SetWindowTextW(s_hMainWnd, szText);
s_strTitle = Text;
}
virtual void FileDownload(
VARIANT_BOOL ActiveDocument,
VARIANT_BOOL *Cancel)
{
printf("FileDownload: %d\n", ActiveDocument);
if (g_settings.m_kiosk_mode)
{
*Cancel = VARIANT_TRUE;
}
}
virtual void DocumentComplete(
IDispatch *pDisp,
BSTR bstrURL)
{
printf("DocumentComplete: %p, '%ls'\n", pDisp, bstrURL);
}
virtual void NavigateError(
IDispatch *pDisp,
VARIANT *url,
VARIANT *target,
LONG StatusCode,
VARIANT_BOOL *Cancel)
{
assert(url->vt == VT_BSTR);
assert(target->vt == VT_BSTR);
BSTR bstrURL = url->bstrVal;
BSTR bstrTarget = target->bstrVal;
printf("NavigateError: %p, '%ls', '%ls', %08lX\n", pDisp, bstrURL, bstrTarget, StatusCode);
if (!IsURL(bstrURL))
{
DoSearch(s_hMainWnd, bstrURL);
}
#ifndef INET_E_BLOCKED_REDIRECT_XSECURITYID
#define INET_E_BLOCKED_REDIRECT_XSECURITYID 0x800C001B
#endif
else if (StatusCode == INET_E_BLOCKED_REDIRECT_XSECURITYID)
{
s_pWebBrowser->Stop();
SetInternalPageContents(L"");
}
}
virtual void DownloadBegin()
{
printf("DownloadBegin\n");
}
virtual void DownloadComplete()
{
printf("DownloadComplete\n");
}
virtual void SetSecureLockIcon(DWORD SecureLockIcon)
{
if (s_nSecurity == 0)
{
if (SecureLockIcon == 0)
{
MarkSecurity(-1);
}
else
{
BSTR url = NULL;
s_pWebBrowser->get_LocationURL(&url);
if (url)
{
if (s_insecure_url.count(url) != 0)
{
MarkSecurity(-1);
}
else
{
MarkSecurity(1, TRUE);
}
SysFreeString(url);
}
}
}
// SecureLockIconConstants
printf("SetSecureLockIcon: 0x%08X\n", SecureLockIcon);
}
virtual void ProgressChange(LONG Progress, LONG ProgressMax)
{
printf("ProgressChange: %ld, %ld\n", Progress, ProgressMax);
}
virtual void BeforeScriptExecute(IDispatch *pDisp)
{
printf("BeforeScriptExecute: %p\n", pDisp);
}
virtual void OnQuit(void)
{
printf("OnQuit\n");
}
};
MEventHandler s_listener;
LPTSTR DoGetTemporaryFile(void)
{
static TCHAR s_szFile[MAX_PATH];
TCHAR szPath[MAX_PATH];
if (GetTempPath(ARRAYSIZE(szPath), szPath))
{
if (GetTempFileName(szPath, TEXT("sbt"), 0, s_szFile))
{
return s_szFile;
}
}
return NULL;
}
void DoNavigate(HWND hwnd, const WCHAR *url, DWORD dwFlags)
{
std::wstring strURL;
WCHAR *pszURL = _wcsdup(url);
if (pszURL)
{
StrTrimW(pszURL, L" \t\n\r\f\v");
strURL = pszURL;
free(pszURL);
}
else
{
assert(0);
return;
}
if (strURL.find(L"view-source:") == 0)
{
if (WCHAR *file = DoGetTemporaryFile())
{
MBindStatusCallback *pCallback = MBindStatusCallback::Create();
std::wstring new_url, substr = strURL.substr(wcslen(L"view-source:"));
HRESULT hr = E_FAIL;
if (FAILED(hr))
{
new_url = substr;
hr = URLDownloadToFile(NULL, new_url.c_str(), file, 0, pCallback);
}
if (FAILED(hr))
{
new_url = L"https:" + substr;
hr = URLDownloadToFile(NULL, new_url.c_str(), file, 0, pCallback);
}
if (FAILED(hr))
{
new_url = L"https://" + substr;
hr = URLDownloadToFile(NULL, new_url.c_str(), file, 0, pCallback);
}
if (FAILED(hr))
{
new_url = L"http:" + substr;
hr = URLDownloadToFile(NULL, new_url.c_str(), file, 0, pCallback);
}
if (FAILED(hr))
{
new_url = L"http://" + substr;
hr = URLDownloadToFile(NULL, new_url.c_str(), file, 0, pCallback);
}
if (SUCCEEDED(hr))
{
while (!pCallback->IsCompleted() && !pCallback->IsCancelled() &&
GetAsyncKeyState(VK_ESCAPE) >= 0)
{
Sleep(100);
}
if (pCallback->IsCompleted())
{
std::string contents;
char buf[512];
if (FILE *fp = _wfopen(file, L"rb"))
{
while (size_t count = fread(buf, 1, 512, fp))
{
contents.append(buf, count);
}
fclose(fp);
// contents to wide
UINT nCodePage = CP_UTF8;
if (contents.find("Shift_JIS") != std::string::npos ||
contents.find("shift_jis") != std::string::npos ||
contents.find("x-sjis") != std::string::npos)
{
nCodePage = 932;
}
else if (contents.find("ISO-8859-1") != std::string::npos ||
contents.find("iso-8859-1") != std::string::npos)
{
nCodePage = 28591;
}
int ret;
ret = MultiByteToWideChar(nCodePage, 0, contents.c_str(), -1, NULL, 0);
std::wstring wide(ret + 1, 0);
ret = MultiByteToWideChar(nCodePage, 0, contents.c_str(), -1, &wide[0], ret + 1);
DWORD error = GetLastError();
wide.resize(ret);
SetInternalPageContents(wide.c_str(), false);
}
else
{
assert(0);
}
}
else
{
assert(0);
}
}
else
{
assert(0);
}
pCallback->Release();
DeleteFile(file);
}
else
{
assert(0);
}
DoUpdateURL(strURL.c_str());
SetTimer(s_hMainWnd, SOURCE_DONE_TIMER, 500, NULL);
}
else
{
HRESULT hr = s_pWebBrowser->Navigate2(url, dwFlags);
}
}
BOOL DoSetBrowserEmulation(DWORD dwValue)
{
static const TCHAR s_szFeatureControl[] =
TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl");
TCHAR szPath[MAX_PATH], *pchFileName;
GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath));
pchFileName = PathFindFileName(szPath);
BOOL bOK = FALSE;
HKEY hkeyControl = NULL;
RegOpenKeyEx(HKEY_CURRENT_USER, s_szFeatureControl, 0, KEY_ALL_ACCESS, &hkeyControl);
if (hkeyControl)
{
HKEY hkeyEmulation = NULL;
RegCreateKeyEx(hkeyControl, TEXT("FEATURE_BROWSER_EMULATION"), 0, NULL, 0,
KEY_ALL_ACCESS, NULL, &hkeyEmulation, NULL);
if (hkeyEmulation)
{
if (dwValue)
{
DWORD value = dwValue, size = sizeof(value);
LONG result = RegSetValueEx(hkeyEmulation, pchFileName, 0,
REG_DWORD, (LPBYTE)&value, size);
bOK = (result == ERROR_SUCCESS);
}
else
{
RegDeleteValue(hkeyEmulation, pchFileName);
bOK = TRUE;
}
RegCloseKey(hkeyEmulation);
}
RegCloseKey(hkeyControl);
}
return bOK;
}
LRESULT CALLBACK
AddressBarEditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WNDPROC fn = (WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (uMsg)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
if (ComboBox_GetDroppedState(s_hAddrBarComboBox))
{
ComboBox_ShowDropdown(s_hAddrBarComboBox, FALSE);
return 0;
}
}
else if (wParam == VK_DELETE)
{
if (ComboBox_GetDroppedState(s_hAddrBarComboBox))
{
INT iItem = ComboBox_GetCurSel(s_hAddrBarComboBox);
if (iItem != CB_ERR)
{
ComboBox_DeleteString(s_hAddrBarComboBox, iItem);
g_settings.m_url_list.erase(g_settings.m_url_list.begin() + iItem);
return 0;
}
}
}
break;
}
LRESULT result = CallWindowProc(fn, hwnd, uMsg, wParam, lParam);
return result;
}
void InitAddrBarComboBox(void)
{
INT cch = GetWindowTextLengthW(s_hAddrBarComboBox);
std::wstring str;
str.resize(cch);
if (cch > 0)
GetWindowText(s_hAddrBarComboBox, &str[0], cch + 1);
ComboBox_ResetContent(s_hAddrBarComboBox);
SETTINGS::list_type::const_iterator it, end = g_settings.m_url_list.end();
for (it = g_settings.m_url_list.begin(); it != end; ++it)
{
ComboBox_AddString(s_hAddrBarComboBox, it->c_str());
}
SetWindowText(s_hAddrBarComboBox, str.c_str());
}
void OnRefresh(HWND hwnd);
void DoMakeItKiosk(HWND hwnd, BOOL bKiosk)
{
if (s_bKiosk == bKiosk)
return;
s_bKiosk = bKiosk;
static DWORD s_old_style;
static DWORD s_old_exstyle;
static BOOL s_old_maximized;
static RECT s_old_rect;
if (bKiosk)
{
s_old_style = GetWindowLong(hwnd, GWL_STYLE);
s_old_exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
s_old_maximized = g_settings.m_bMaximized;
GetWindowRect(hwnd, &s_old_rect);
DWORD style = s_old_exstyle & ~(WS_CAPTION | WS_THICKFRAME);
DWORD exstyle = s_old_exstyle & ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE |
WS_EX_DLGMODALFRAME | WS_EX_STATICEDGE);
exstyle |= WS_EX_TOPMOST;
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowLong(hwnd, GWL_EXSTYLE, exstyle);
HMONITOR hMonitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO mi;
mi.cbSize = sizeof(mi);
::GetMonitorInfo(hMonitor, &mi);
RECT& rect = mi.rcMonitor;
::MoveWindow(hwnd, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
TRUE);
ShowWindow(hwnd, SW_SHOWNORMAL);
}
else
{
SetWindowLong(hwnd, GWL_STYLE, s_old_style);
SetWindowLong(hwnd, GWL_EXSTYLE, s_old_exstyle);
MoveWindow(hwnd, s_old_rect.left, s_old_rect.top,
s_old_rect.right - s_old_rect.left,
s_old_rect.bottom - s_old_rect.top,
TRUE);
if (s_old_maximized)
ShowWindow(hwnd, SW_MAXIMIZE);
else
ShowWindow(hwnd, SW_SHOWNORMAL);
}
InvalidateRect(hwnd, NULL, TRUE);
PostMessage(hwnd, WM_MOVE, 0, 0);
PostMessage(hwnd, WM_SIZE, 0, 0);
OnRefresh(hwnd);
}
static
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
std::vector<HWND> *pbuttons = (std::vector<HWND> *)lParam;
TCHAR szClass[64];
GetClassName(hwnd, szClass, ARRAYSIZE(szClass));
if (lstrcmpi(szClass, s_szButton) == 0)
{
pbuttons->push_back(hwnd);
}
return TRUE;
}
void DoDeleteButtons(HWND hwnd)
{
std::vector<HWND> buttons;
EnumChildWindows(hwnd, EnumChildProc, (LPARAM)&buttons);
for (size_t i = 0; i < buttons.size(); ++i)
{
DestroyWindow(buttons[i]);
}
HWND hAddressBar = GetDlgItem(hwnd, ID_ADDRESS_BAR);
DestroyWindow(hAddressBar);
}
BOOL LoadDataFile(HWND hwnd, const WCHAR *path, std::wstring& data)
{
FILE *fp = _wfopen(path, L"r");
if (!fp)
return FALSE;
std::vector<std::wstring> lines;
std::vector<std::wstring> fields;
char buf[256];
WCHAR szText[256];
while (fgets(buf, ARRAYSIZE(buf), fp))
{
if (char *pch = strchr(buf, ';'))