/
CWebCore.cpp
811 lines (675 loc) · 24.8 KB
/
CWebCore.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* (Shared logic for modifications)
* LICENSE: See LICENSE in the top level directory
* FILE: core/CWebCore.cpp
* PURPOSE: Webbrowser class
*
*****************************************************************************/
#include "StdInc.h"
#include "CWebCore.h"
#include "CWebView.h"
#include "CWebsiteRequests.h"
#include <cef3/include/cef_app.h>
#include <cef3/include/cef_browser.h>
#include <cef3/include/cef_sandbox_win.h>
#include <cef3/include/cef_parser.h>
#include "WebBrowserHelpers.h"
#include "CWebApp.h"
//#define CEF_ENABLE_SANDBOX
#ifdef CEF_ENABLE_SANDBOX
#pragma comment(lib, "cef_sandbox.lib")
#endif
CWebCore::CWebCore()
{
m_pRequestsGUI = nullptr;
m_bTestmodeEnabled = false;
m_pXmlConfig = nullptr;
m_pFocusedWebView = nullptr;
MakeSureXMLNodesExist();
InitialiseWhiteAndBlacklist();
// Update dynamic lists from QA server
UpdateListsFromMaster();
}
CWebCore::~CWebCore()
{
// Unregister schema factories
CefClearSchemeHandlerFactories();
// Shutdown CEF
CefShutdown();
delete m_pRequestsGUI;
delete m_pXmlConfig;
}
bool CWebCore::Initialise()
{
CefMainArgs mainArgs;
void* sandboxInfo = nullptr;
CefRefPtr<CWebApp> app(new CWebApp);
#ifdef CEF_ENABLE_SANDBOX
CefScopedSandboxInfo scopedSandbox;
sandboxInfo = scopedSandbox.sandbox_info();
#endif
CefSettings settings;
#ifndef CEF_ENABLE_SANDBOX
settings.no_sandbox = true;
#endif
// Specifiy sub process executable path
#ifndef MTA_DEBUG
CefString(&settings.browser_subprocess_path).FromWString(FromUTF8(CalcMTASAPath("MTA\\CEF\\CEFLauncher.exe")));
#else
CefString(&settings.browser_subprocess_path).FromWString(FromUTF8(CalcMTASAPath("MTA\\CEF\\CEFLauncher_d.exe")));
#endif
CefString(&settings.resources_dir_path).FromWString(FromUTF8(CalcMTASAPath("MTA\\CEF")));
CefString(&settings.locales_dir_path).FromWString(FromUTF8(CalcMTASAPath("MTA\\CEF\\locales")));
CefString(&settings.log_file).FromWString(FromUTF8(CalcMTASAPath("MTA\\CEF\\cefdebug.txt")));
#ifdef MTA_DEBUG
settings.log_severity = cef_log_severity_t::LOGSEVERITY_INFO;
#else
settings.log_severity = cef_log_severity_t::LOGSEVERITY_WARNING;
#endif
settings.multi_threaded_message_loop = true;
settings.windowless_rendering_enabled = true;
bool state = CefInitialize(mainArgs, settings, app, sandboxInfo);
// Register custom scheme handler factory
CefRegisterSchemeHandlerFactory("mtalocal", "", app);
CefRegisterSchemeHandlerFactory("http", "mta", app);
return state;
}
CWebViewInterface* CWebCore::CreateWebView(unsigned int uiWidth, unsigned int uiHeight, bool bIsLocal, CWebBrowserItem* pWebBrowserRenderItem,
bool bTransparent)
{
// Create our webview implementation
CefRefPtr<CWebView> pWebView = new CWebView(bIsLocal, pWebBrowserRenderItem, bTransparent);
m_WebViews.push_back(pWebView);
return static_cast<CWebViewInterface*>(pWebView.get());
}
void CWebCore::DestroyWebView(CWebViewInterface* pWebViewInterface)
{
CefRefPtr<CWebView> pWebView = dynamic_cast<CWebView*>(pWebViewInterface);
if (pWebView)
{
// Ensure that no attached events are in the queue
RemoveWebViewEvents(pWebView);
m_WebViews.remove(pWebView);
// pWebView->Release(); // Do not release since other references get corrupted then
pWebView->CloseBrowser();
}
}
void CWebCore::DoPulse()
{
// Check for queued whitelist/blacklist downloads
g_pCore->GetNetwork()->GetHTTPDownloadManager(EDownloadModeType::WEBBROWSER_LISTS)->ProcessQueuedFiles();
// Execute queued events on the main thread (Lua calls must be executed on the main thread)
DoEventQueuePulse();
}
CWebView* CWebCore::FindWebView(CefRefPtr<CefBrowser> browser)
{
if (!browser)
return nullptr;
for (auto pWebView : m_WebViews)
{
if (!pWebView)
continue;
CefRefPtr<CefBrowser> pBrowser = pWebView->GetCefBrowser();
if (!pBrowser)
continue;
// CefBrowser objects are not unique
if (pBrowser->GetIdentifier() == browser->GetIdentifier())
return pWebView.get();
}
return nullptr;
}
void CWebCore::AddEventToEventQueue(std::function<void()> event, CWebView* pWebView, const SString& name)
{
#ifndef MTA_DEBUG
UNREFERENCED_PARAMETER(name);
#endif
if (pWebView && pWebView->IsBeingDestroyed())
return;
std::lock_guard<std::mutex> lock(m_EventQueueMutex);
#ifndef MTA_DEBUG
m_EventQueue.push_back(EventEntry(event, pWebView));
#else
m_EventQueue.push_back(EventEntry(event, pWebView, name));
#endif
}
void CWebCore::RemoveWebViewEvents(CWebView* pWebView)
{
std::lock_guard<std::mutex> lock(m_EventQueueMutex);
for (auto iter = m_EventQueue.begin(); iter != m_EventQueue.end();)
{
// Increment iterator before we remove the element from the list (to guarantee iterator validity)
auto tempIterator = iter++;
if (tempIterator->pWebView == pWebView)
m_EventQueue.erase(tempIterator);
}
}
void CWebCore::DoEventQueuePulse()
{
std::list<EventEntry> eventQueue;
{
std::lock_guard<std::mutex> lock(m_EventQueueMutex);
std::swap(eventQueue, m_EventQueue);
}
for (auto& event : eventQueue)
{
event.callback();
}
// Invoke paint method if necessary on the main thread
for (auto& view : m_WebViews)
{
view->UpdateTexture();
}
}
eURLState CWebCore::GetDomainState(const SString& strURL, bool bOutputDebug)
{
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
// Initialize wildcard whitelist (be careful with modifying) | Todo: Think about the following
static SString wildcardWhitelist[] = {"*.googlevideo.com", "*.google.com", "*.youtube.com", "*.ytimg.com", "*.vimeocdn.com"};
for (int i = 0; i < sizeof(wildcardWhitelist) / sizeof(SString); ++i)
{
if (WildcardMatch(wildcardWhitelist[i], strURL))
return eURLState::WEBPAGE_ALLOWED;
}
google::dense_hash_map<SString, WebFilterPair>::iterator iter = m_Whitelist.find(strURL);
if (iter != m_Whitelist.end())
{
if (iter->second.first == true)
return eURLState::WEBPAGE_ALLOWED;
else
{
if (m_bTestmodeEnabled && bOutputDebug)
DebugOutputThreadsafe(SString("[BROWSER] Blocked page: %s", strURL.c_str()), 255, 0, 0);
return eURLState::WEBPAGE_DISALLOWED;
}
}
if (m_bTestmodeEnabled && bOutputDebug)
DebugOutputThreadsafe(SString("[BROWSER] Blocked page: %s", strURL.c_str()), 255, 0, 0);
return eURLState::WEBPAGE_NOT_LISTED;
}
SString CWebCore::GetDomainFromURL(const SString& strURL)
{
CefURLParts urlParts;
if (strURL.empty() || !CefParseURL(strURL, urlParts) || !urlParts.host.str)
return "";
return UTF16ToMbUTF8(urlParts.host.str);
}
void CWebCore::ResetFilter(bool bResetRequestsOnly)
{
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
// Clear old data
m_PendingRequests.clear();
if (!bResetRequestsOnly)
{
m_Whitelist.clear();
// Re-add "default" entries
InitialiseWhiteAndBlacklist();
}
else
{
// Close requests GUI
if (m_pRequestsGUI)
{
delete m_pRequestsGUI;
m_pRequestsGUI = nullptr;
}
// Erase all WEBFILTER_REQUEST entries
google::dense_hash_map<SString, WebFilterPair>::iterator iter = m_Whitelist.begin();
for (; iter != m_Whitelist.end();)
{
if (iter->second.second == eWebFilterType::WEBFILTER_REQUEST)
m_Whitelist.erase(iter++);
else
++iter;
}
}
}
void CWebCore::InitialiseWhiteAndBlacklist(bool bAddHardcoded, bool bAddDynamic)
{
if (bAddDynamic)
{
// Hardcoded whitelist
static SString whitelist[] = {
"google.com", "youtube.com", "www.youtube-nocookie.com", "vimeo.com", "player.vimeo.com", "code.jquery.com", "mtasa.com",
"multitheftauto.com", "mtavc.com", "www.googleapis.com", "ajax.googleapis.com", "localhost", "127.0.0.1"};
// Hardcoded blacklist
static SString blacklist[] = {"nobrain.dk"};
// Blacklist or whitelist URLs now
for (unsigned int i = 0; i < sizeof(whitelist) / sizeof(SString); ++i)
{
AddAllowedPage(whitelist[i], eWebFilterType::WEBFILTER_HARDCODED);
}
for (unsigned int i = 0; i < sizeof(blacklist) / sizeof(SString); ++i)
{
AddBlockedPage(blacklist[i], eWebFilterType::WEBFILTER_HARDCODED);
}
}
// Load dynamic and custom blacklist from XML config
if (bAddDynamic)
LoadListsFromXML(true, true, true);
}
void CWebCore::AddAllowedPage(const SString& strURL, eWebFilterType filterType)
{
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
m_Whitelist[strURL] = std::pair<bool, eWebFilterType>(true, filterType);
}
void CWebCore::AddBlockedPage(const SString& strURL, eWebFilterType filterType)
{
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
m_Whitelist[strURL] = std::pair<bool, eWebFilterType>(false, filterType);
}
void CWebCore::RequestPages(const std::vector<SString>& pages, WebRequestCallback* pCallback)
{
// Add to pending pages queue
bool bNewItem = false;
for (const auto& page : pages)
{
eURLState status = GetDomainState(page);
if (status == eURLState::WEBPAGE_ALLOWED || status == eURLState::WEBPAGE_DISALLOWED)
continue;
m_PendingRequests.insert(page);
bNewItem = true;
}
if (bNewItem)
{
// Open CEGUI dialog
if (!m_pRequestsGUI)
m_pRequestsGUI = new CWebsiteRequests;
// Set pending requests memo content and show the window
m_pRequestsGUI->SetPendingRequests(m_PendingRequests, pCallback);
m_pRequestsGUI->Show();
}
else
{
// Call callback immediately if nothing has changed (all entries are most likely already on the whitelist)
// There is still the possibility that all websites are blacklisted; this is not the usual case tho, so ignore for now (TODO)
if (pCallback)
(*pCallback)(true, std::unordered_set<SString>(pages.begin(), pages.end()));
}
}
std::unordered_set<SString> CWebCore::AllowPendingPages(bool bRemember)
{
for (const auto& request : m_PendingRequests)
{
AddAllowedPage(request, !bRemember ? eWebFilterType::WEBFILTER_REQUEST : eWebFilterType::WEBFILTER_USER);
}
// Trigger an event now
auto pCurrentMod = g_pCore->GetModManager()->GetCurrentMod();
if (!pCurrentMod)
return std::unordered_set<SString>();
pCurrentMod->WebsiteRequestResultHandler(m_PendingRequests);
if (bRemember)
{
std::vector<std::pair<SString, bool>> result; // Contains only allowed entries
g_pCore->GetWebCore()->GetFilterEntriesByType(result, eWebFilterType::WEBFILTER_USER, eWebFilterState::WEBFILTER_ALLOWED);
std::vector<SString> customWhitelist;
for (std::vector<std::pair<SString, bool>>::iterator iter = result.begin(); iter != result.end(); ++iter)
customWhitelist.push_back(iter->first);
WriteCustomList("customwhitelist", customWhitelist, false);
}
auto allowedRequests(std::move(m_PendingRequests));
m_PendingRequests.clear(); // MSVC's move constructor already clears the list which isn't specified by the C++ standard though
return allowedRequests;
}
std::unordered_set<SString> CWebCore::DenyPendingPages()
{
auto deniedRequests(std::move(m_PendingRequests));
m_PendingRequests.clear();
return deniedRequests;
}
bool CWebCore::IsRequestsGUIVisible()
{
return m_pRequestsGUI && m_pRequestsGUI->IsVisible();
}
void CWebCore::DebugOutputThreadsafe(const SString& message, unsigned char R, unsigned char G, unsigned char B)
{
AddEventToEventQueue([message, R, G, B]() { g_pCore->DebugEchoColor(message, R, G, B); }, nullptr, "DebugOutputThreadsafe");
}
bool CWebCore::GetRemotePagesEnabled()
{
bool bCanLoadRemotePages;
g_pCore->GetCVars()->Get("browser_remote_websites", bCanLoadRemotePages);
return bCanLoadRemotePages;
}
bool CWebCore::GetRemoteJavascriptEnabled()
{
bool bIsRemoteJavascriptEnabled;
g_pCore->GetCVars()->Get("browser_remote_javascript", bIsRemoteJavascriptEnabled);
return bIsRemoteJavascriptEnabled;
}
void CWebCore::OnPreScreenshot()
{
// Clear all textures
g_pCore->GetWebCore()->ClearTextures();
}
void CWebCore::OnPostScreenshot()
{
// Re-draw textures
for (auto& pWebView : m_WebViews)
{
pWebView->GetCefBrowser()->GetHost()->Invalidate(CefBrowserHost::PaintElementType::PET_VIEW);
}
}
void CWebCore::ProcessInputMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (!m_pFocusedWebView ||
!(uMsg == WM_KEYDOWN || uMsg == WM_KEYUP || uMsg == WM_CHAR || uMsg == WM_SYSCHAR || uMsg == WM_SYSKEYDOWN || uMsg == WM_SYSKEYUP))
return;
CefKeyEvent keyEvent;
keyEvent.windows_key_code = wParam;
keyEvent.native_key_code = lParam;
keyEvent.modifiers = GetCefKeyboardModifiers(wParam, lParam);
keyEvent.is_system_key = uMsg == WM_SYSCHAR || uMsg == WM_SYSKEYDOWN || uMsg == WM_SYSKEYUP;
if (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN)
keyEvent.type = cef_key_event_type_t::KEYEVENT_RAWKEYDOWN;
else if (uMsg == WM_KEYUP || uMsg == WM_SYSKEYUP)
keyEvent.type = cef_key_event_type_t::KEYEVENT_KEYUP;
else if (uMsg == WM_CHAR || uMsg == WM_SYSCHAR)
keyEvent.type = cef_key_event_type_t::KEYEVENT_CHAR;
m_pFocusedWebView->InjectKeyboardEvent(keyEvent);
}
void CWebCore::ClearTextures()
{
for (auto& pWebBrowser : m_WebViews)
{
pWebBrowser->ClearTexture();
}
}
bool CWebCore::SetGlobalAudioVolume(float fVolume)
{
if (fVolume < 0.0f || fVolume > 1.0f)
return false;
for (auto& pWebView : m_WebViews)
{
pWebView->SetAudioVolume(fVolume);
}
return true;
}
bool CWebCore::UpdateListsFromMaster()
{
if (!m_pXmlConfig)
return false;
CXMLNode* pRootNode = m_pXmlConfig->GetRootNode();
if (!pRootNode || !MakeSureXMLNodesExist())
return false;
// Fetch white- and blacklist revision from config
CXMLNode* pWhiteRevNode = pRootNode->FindSubNode("whitelistrev");
if (!pWhiteRevNode || !pWhiteRevNode->GetTagContent(m_iWhitelistRevision))
{
m_iWhitelistRevision = 0;
}
CXMLNode* pBlackRevNode = pRootNode->FindSubNode("blacklistrev");
if (!pBlackRevNode || !pBlackRevNode->GetTagContent(m_iBlacklistRevision))
{
m_iBlacklistRevision = 0;
}
// Get last update timestamp and compare with current time
CXMLNode* pLastUpdateNode = pRootNode->FindSubNode("lastupdate");
if (pLastUpdateNode)
{
SString lastUpdateTime = pLastUpdateNode->GetTagContent();
time_t currentTime;
time(¤tTime);
if (lastUpdateTime < SString("%d", (long long)currentTime - BROWSER_LIST_UPDATE_INTERVAL))
{
#ifdef MTA_DEBUG
OutputDebugLine("Updating white- and blacklist...");
#endif
SHttpRequestOptions options;
options.uiConnectionAttempts = 3;
g_pCore->GetNetwork()
->GetHTTPDownloadManager(EDownloadModeType::WEBBROWSER_LISTS)
->QueueFile(SString("%s?type=getrev", BROWSER_UPDATE_URL), NULL, this, &CWebCore::StaticFetchRevisionFinished, options);
pLastUpdateNode->SetTagContent(SString("%d", (long long)currentTime));
m_pXmlConfig->Write();
}
}
return true;
}
bool CWebCore::MakeSureXMLNodesExist()
{
// Check xml file
if (!m_pXmlConfig)
{
SString browserDataPath = CalcMTASAPath(MTA_BROWSERDATA_PATH);
bool exists = FileExists(browserDataPath);
m_pXmlConfig = g_pCore->GetXML()->CreateXML(browserDataPath);
if (!m_pXmlConfig || (exists && !m_pXmlConfig->Parse()))
return false;
}
CXMLNode* pRootNode = m_pXmlConfig->GetRootNode();
if (!pRootNode)
{
pRootNode = m_pXmlConfig->CreateRootNode("browserdata");
if (!pRootNode)
return false;
}
if (!pRootNode->FindSubNode("lastupdate"))
{
CXMLNode* pNode = pRootNode->CreateSubNode("lastupdate");
if (!pNode)
return false;
pNode->SetTagContent(0);
}
if (!pRootNode->FindSubNode("whitelistrev"))
{
if (!pRootNode->CreateSubNode("whitelistrev"))
return false;
}
if (!pRootNode->FindSubNode("blacklistrev"))
{
if (!pRootNode->CreateSubNode("blacklistrev"))
return false;
}
if (!pRootNode->FindSubNode("globalwhitelist"))
{
if (!pRootNode->CreateSubNode("globalwhitelist"))
return false;
}
if (!pRootNode->FindSubNode("globalblacklist"))
{
if (!pRootNode->CreateSubNode("globalblacklist"))
return false;
}
if (!pRootNode->FindSubNode("customblacklist"))
{
if (!pRootNode->CreateSubNode("customblacklist"))
return false;
}
if (!pRootNode->FindSubNode("customwhitelist"))
{
if (!pRootNode->CreateSubNode("customwhitelist"))
return false;
}
return true;
}
void CWebCore::LoadListsFromXML(bool bWhitelist, bool bBlacklist, bool bCustomLists)
{
if (!m_pXmlConfig)
return;
CXMLNode* pRootNode = m_pXmlConfig->GetRootNode();
if (!pRootNode)
return;
if (bWhitelist)
{
CXMLNode* pWhiteSubNode = pRootNode->FindSubNode("globalwhitelist");
if (pWhiteSubNode)
{
for (std::list<CXMLNode*>::iterator iter = pWhiteSubNode->ChildrenBegin(); iter != pWhiteSubNode->ChildrenEnd(); ++iter)
{
AddAllowedPage((*iter)->GetTagContent(), eWebFilterType::WEBFILTER_DYNAMIC);
}
}
}
if (bBlacklist)
{
CXMLNode* pBlackSubNode = pRootNode->FindSubNode("globalblacklist");
if (pBlackSubNode)
{
for (std::list<CXMLNode*>::iterator iter = pBlackSubNode->ChildrenBegin(); iter != pBlackSubNode->ChildrenEnd(); ++iter)
{
AddBlockedPage((*iter)->GetTagContent(), eWebFilterType::WEBFILTER_DYNAMIC);
}
}
}
if (bCustomLists)
{
CXMLNode* pBlackSubNode = pRootNode->FindSubNode("customblacklist");
if (pBlackSubNode)
{
for (std::list<CXMLNode*>::iterator iter = pBlackSubNode->ChildrenBegin(); iter != pBlackSubNode->ChildrenEnd(); ++iter)
{
AddBlockedPage((*iter)->GetTagContent(), eWebFilterType::WEBFILTER_USER);
}
}
CXMLNode* pWhiteSubNode = pRootNode->FindSubNode("customwhitelist");
if (pWhiteSubNode)
{
for (std::list<CXMLNode*>::iterator iter = pWhiteSubNode->ChildrenBegin(); iter != pWhiteSubNode->ChildrenEnd(); ++iter)
{
AddAllowedPage((*iter)->GetTagContent(), eWebFilterType::WEBFILTER_USER);
}
}
}
}
void CWebCore::WriteCustomList(const SString& strListName, const std::vector<SString>& customList, bool bReset)
{
if (!m_pXmlConfig || !MakeSureXMLNodesExist())
return;
CXMLNode* pRootNode = m_pXmlConfig->GetRootNode();
if (!pRootNode)
return;
CXMLNode* pCustomListNode = pRootNode->FindSubNode(strListName);
if (!pCustomListNode)
return;
pCustomListNode->DeleteAllSubNodes();
for (std::vector<SString>::const_iterator iter = customList.begin(); iter != customList.end(); ++iter)
{
CXMLNode* pNode = pCustomListNode->CreateSubNode("url");
if (pNode)
pNode->SetTagContent(*iter);
}
// Write custom blacklist and reload from XML
m_pXmlConfig->Write();
if (bReset)
ResetFilter(false);
}
void CWebCore::GetFilterEntriesByType(std::vector<std::pair<SString, bool>>& outEntries, eWebFilterType filterType, eWebFilterState state)
{
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
google::dense_hash_map<SString, WebFilterPair>::iterator iter = m_Whitelist.begin();
for (; iter != m_Whitelist.end(); ++iter)
{
if (iter->second.second == filterType)
{
if (state == eWebFilterState::WEBFILTER_ALL)
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
else if (state == eWebFilterState::WEBFILTER_ALLOWED && iter->second.first == true)
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
else
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
}
}
}
void CWebCore::StaticFetchRevisionFinished(const SHttpDownloadResult& result)
{
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
if (result.bSuccess)
{
SString strData = result.pData;
SString strWhiteRevision, strBlackRevision;
strData.Split(";", &strWhiteRevision, &strBlackRevision);
if (!strWhiteRevision.empty() && !strBlackRevision.empty())
{
int iWhiteListRevision = atoi(strWhiteRevision);
if (iWhiteListRevision > pWebCore->m_iWhitelistRevision)
{
SHttpRequestOptions options;
options.uiConnectionAttempts = 3;
g_pCore->GetNetwork()
->GetHTTPDownloadManager(EDownloadModeType::WEBBROWSER_LISTS)
->QueueFile(SString("%s?type=fetchwhite", BROWSER_UPDATE_URL), NULL, pWebCore, &CWebCore::StaticFetchWhitelistFinished, options);
pWebCore->m_iWhitelistRevision = iWhiteListRevision;
}
int iBlackListRevision = atoi(strBlackRevision);
if (iBlackListRevision > pWebCore->m_iBlacklistRevision)
{
SHttpRequestOptions options;
options.uiConnectionAttempts = 3;
g_pCore->GetNetwork()
->GetHTTPDownloadManager(EDownloadModeType::WEBBROWSER_LISTS)
->QueueFile(SString("%s?type=fetchblack", BROWSER_UPDATE_URL), NULL, pWebCore, &CWebCore::StaticFetchBlacklistFinished, options);
pWebCore->m_iBlacklistRevision = iBlackListRevision;
}
}
}
}
void CWebCore::StaticFetchWhitelistFinished(const SHttpDownloadResult& result)
{
if (!result.bSuccess)
return;
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
if (!pWebCore->m_pXmlConfig)
return;
if (!pWebCore->MakeSureXMLNodesExist())
return;
CXMLNode* pRootNode = pWebCore->m_pXmlConfig->GetRootNode();
std::vector<SString> whitelist;
SString strData = result.pData;
strData.Split(";", whitelist);
CXMLNode* pListNode = pRootNode->FindSubNode("globalwhitelist");
if (!pListNode)
return;
pListNode->DeleteAllSubNodes();
for (std::vector<SString>::const_iterator iter = whitelist.begin(); iter != whitelist.end(); ++iter)
{
CXMLNode* pNode = pListNode->CreateSubNode("url");
pNode->SetTagContent(*iter);
}
// Set whitelist revision
CXMLNode* pNode = pRootNode->FindSubNode("whitelistrev");
if (!pNode)
return;
pNode->SetTagContent(pWebCore->m_iWhitelistRevision);
// Write changes to the XML file
pWebCore->m_pXmlConfig->Write();
pWebCore->LoadListsFromXML(true, false, false);
#ifdef MTA_DEBUG
OutputDebugLine("Updated whitelist!");
#endif
}
void CWebCore::StaticFetchBlacklistFinished(const SHttpDownloadResult& result)
{
if (!result.bSuccess)
return;
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
if (!pWebCore->m_pXmlConfig)
return;
if (!pWebCore->MakeSureXMLNodesExist())
return;
CXMLNode* pRootNode = pWebCore->m_pXmlConfig->GetRootNode();
std::vector<SString> blacklist;
SString strData = result.pData;
strData.Split(";", blacklist);
CXMLNode* pListNode = pRootNode->FindSubNode("globalblacklist");
if (!pListNode)
return;
pListNode->DeleteAllSubNodes();
for (std::vector<SString>::const_iterator iter = blacklist.begin(); iter != blacklist.end(); ++iter)
{
CXMLNode* pNode = pListNode->CreateSubNode("url");
pNode->SetTagContent(*iter);
}
// Set blacklist revision
CXMLNode* pNode = pRootNode->FindSubNode("blacklistrev");
if (!pNode)
return;
pNode->SetTagContent(pWebCore->m_iBlacklistRevision);
// Write changes to the XML file
pWebCore->m_pXmlConfig->Write();
pWebCore->LoadListsFromXML(false, true, false);
#ifdef MTA_DEBUG
OutputDebugLine("Updated browser blacklist!");
#endif
}