| LEFT | RIGHT |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 1 #include "PluginStdAfx.h" | 18 #include "PluginStdAfx.h" |
| 2 | 19 |
| 3 #include "PluginClient.h" | 20 #include "PluginClient.h" |
| 4 #include "PluginSettings.h" | 21 #include "PluginSettings.h" |
| 5 #include "PluginTab.h" | |
| 6 #include "AdblockPlusDomTraverser.h" | 22 #include "AdblockPlusDomTraverser.h" |
| 7 #include "PluginClass.h" | 23 #include "PluginClass.h" |
| 8 #include "PluginTabBase.h" | 24 #include "PluginTabBase.h" |
| 9 #include "PluginUtil.h" | 25 #include "PluginUtil.h" |
| 10 #include "IE_Version.h" | 26 #include "IeVersion.h" |
| 11 #include <dispex.h> | 27 #include <dispex.h> |
| 12 #include <Mshtmhst.h> | 28 #include <Mshtmhst.h> |
| 13 | |
| 14 int CPluginTabBase::s_dictionaryVersion = 0; | |
| 15 int CPluginTabBase::s_settingsVersion = 1; | |
| 16 int CPluginTabBase::s_filterVersion = 0; | |
| 17 int CPluginTabBase::s_whitelistVersion = 0; | |
| 18 | 29 |
| 19 CPluginTabBase::CPluginTabBase(CPluginClass* plugin) | 30 CPluginTabBase::CPluginTabBase(CPluginClass* plugin) |
| 20 : m_plugin(plugin) | 31 : m_plugin(plugin) |
| 21 , m_isActivated(false) | 32 , m_isActivated(false) |
| 22 , m_continueThreadRunning(true) | 33 , m_continueThreadRunning(true) |
| 23 { | 34 { |
| 24 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); | 35 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); |
| 25 m_filter->hideFiltersLoadedEvent = CreateEvent(NULL, true, false, NULL); | 36 m_filter->hideFiltersLoadedEvent = CreateEvent(NULL, true, false, NULL); |
| 26 | 37 |
| 27 CPluginClient* client = CPluginClient::GetInstance(); | 38 CPluginClient* client = CPluginClient::GetInstance(); |
| 28 if (AdblockPlus::IE::InstalledMajorVersion() < 10) | 39 if (AdblockPlus::IE::InstalledMajorVersion() < 10) |
| 29 { | 40 { |
| 30 m_isActivated = true; | 41 m_isActivated = true; |
| 31 } | 42 } |
| 32 | 43 |
| 33 try | 44 try |
| 34 { | 45 { |
| 35 m_thread = std::thread(&CPluginTabBase::ThreadProc, this); | 46 m_thread = std::thread(&CPluginTabBase::ThreadProc, this); |
| 36 } | 47 } |
| 37 catch (const std::system_error& ex) | 48 catch (const std::system_error& ex) |
| 38 { | 49 { |
| 39 auto errDescription = std::string("Tab::Thread - Failed to create tab thread
") + | 50 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_TAB_THREAD_CREA
TE_PROCESS, |
| 40 ex.code().message() + ex.what(); | 51 "Tab::Thread - Failed to create tab thread"); |
| 41 DEBUG_ERROR_LOG(ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_TAB_THR
EAD_CREATE_PROCESS, errDescription.c_str()); | |
| 42 } | 52 } |
| 43 m_traverser = new CPluginDomTraverser(static_cast<CPluginTab*>(this)); | 53 m_traverser = new CPluginDomTraverser(static_cast<CPluginTab*>(this)); |
| 44 } | 54 } |
| 45 | 55 |
| 46 | 56 |
| 47 CPluginTabBase::~CPluginTabBase() | 57 CPluginTabBase::~CPluginTabBase() |
| 48 { | 58 { |
| 49 delete m_traverser; | 59 delete m_traverser; |
| 50 m_traverser = NULL; | 60 m_traverser = NULL; |
| 51 m_continueThreadRunning = false; | 61 m_continueThreadRunning = false; |
| 52 if (m_thread.joinable()) { | 62 if (m_thread.joinable()) { |
| 53 m_thread.join(); | 63 m_thread.join(); |
| 54 } | 64 } |
| 55 } | 65 } |
| 56 | 66 |
| 57 void CPluginTabBase::OnActivate() | 67 void CPluginTabBase::OnActivate() |
| 58 { | 68 { |
| 59 m_isActivated = true; | 69 m_isActivated = true; |
| 60 } | 70 } |
| 61 | 71 |
| 62 | 72 |
| 63 void CPluginTabBase::OnUpdate() | 73 void CPluginTabBase::OnUpdate() |
| 64 { | 74 { |
| 65 m_isActivated = true; | 75 m_isActivated = true; |
| 66 } | 76 } |
| 67 | 77 |
| 68 namespace | 78 namespace |
| 69 { | 79 { |
| 80 // Entry Point |
| 70 void FilterLoader(CPluginTabBase* tabBase) | 81 void FilterLoader(CPluginTabBase* tabBase) |
| 71 { | 82 { |
| 72 tabBase->m_filter->LoadHideFilters(CPluginClient::GetInstance()->GetElementH
idingSelectors(tabBase->GetDocumentDomain())); | 83 try |
| 73 SetEvent(tabBase->m_filter->hideFiltersLoadedEvent); | 84 { |
| 74 } | 85 tabBase->m_filter->LoadHideFilters(CPluginClient::GetInstance()->GetElemen
tHidingSelectors(tabBase->GetDocumentDomain())); |
| 75 } | 86 SetEvent(tabBase->m_filter->hideFiltersLoadedEvent); |
| 76 | 87 } |
| 77 void CPluginTabBase::OnNavigate(const CString& url) | 88 catch (...) |
| 89 { |
| 90 // As a thread-main function, we truncate any C++ exception. |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 void CPluginTabBase::OnNavigate(const std::wstring& url) |
| 78 { | 96 { |
| 79 SetDocumentUrl(url); | 97 SetDocumentUrl(url); |
| 80 ClearFrameCache(GetDocumentDomain()); | 98 ClearFrameCache(GetDocumentDomain()); |
| 81 std::wstring domainString = GetDocumentDomain(); | 99 std::wstring domainString = GetDocumentDomain(); |
| 82 ResetEvent(m_filter->hideFiltersLoadedEvent); | 100 ResetEvent(m_filter->hideFiltersLoadedEvent); |
| 83 try | 101 try |
| 84 { | 102 { |
| 85 std::thread filterLoaderThread(&FilterLoader, this); | 103 std::thread filterLoaderThread(&FilterLoader, this); |
| 86 filterLoaderThread.detach(); // TODO: but actually we should wait for the th
read in the dtr. | 104 filterLoaderThread.detach(); // TODO: but actually we should wait for the th
read in the dtr. |
| 87 } | 105 } |
| 88 catch (const std::system_error& ex) | 106 catch (const std::system_error& ex) |
| 89 { | 107 { |
| 90 auto errDescription = std::string("Class::Thread - Failed to start filter lo
ader thread, ") + | 108 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_THREAD_CRE
ATE_PROCESS, |
| 91 ex.code().message() + ex.what(); | 109 "Class::Thread - Failed to start filter loader thread"); |
| 92 DEBUG_ERROR_LOG(ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_TH
READ_CREATE_PROCESS, errDescription.c_str()); | |
| 93 } | 110 } |
| 94 m_traverser->ClearCache(); | 111 m_traverser->ClearCache(); |
| 95 } | 112 } |
| 96 | 113 |
| 97 void CPluginTabBase::InjectABP(IWebBrowser2* browser) | 114 void CPluginTabBase::InjectABP(IWebBrowser2* browser) |
| 98 { | 115 { |
| 99 CriticalSection::Lock lock(m_csInject); | 116 CriticalSection::Lock lock(m_csInject); |
| 100 CString url = GetDocumentUrl(); | 117 auto url = GetDocumentUrl(); |
| 101 CString log; | 118 |
| 102 log.Format(L"InjectABP. Current URL: %s, settings URL: %s", url, UserSettingsF
ileUrl().c_str()); | 119 std::wstring log = L"InjectABP. Current URL: "; |
| 120 log += url; |
| 121 log += L", settings URL: "; |
| 122 log += UserSettingsFileUrl(); |
| 103 DEBUG_GENERAL(log); | 123 DEBUG_GENERAL(log); |
| 104 if (!(0 == url.CompareNoCase(CString(UserSettingsFileUrl().c_str())) || | 124 |
| 105 0 == url.CompareNoCase(CString(FirstRunPageFileUrl().c_str())))) | 125 CString urlLegacy = ToCString(url); |
| 126 if (!(0 == urlLegacy.CompareNoCase(CString(UserSettingsFileUrl().c_str())) || |
| 127 0 == urlLegacy.CompareNoCase(CString(FirstRunPageFileUrl().c_str())))) |
| 106 { | 128 { |
| 107 DEBUG_GENERAL(L"Not injecting"); | 129 DEBUG_GENERAL(L"Not injecting"); |
| 108 return; | 130 return; |
| 109 } | 131 } |
| 110 DEBUG_GENERAL(L"Going to inject"); | 132 DEBUG_GENERAL(L"Going to inject"); |
| 111 CComPtr<IDispatch> pDocDispatch; | 133 CComPtr<IDispatch> pDocDispatch; |
| 112 browser->get_Document(&pDocDispatch); | 134 browser->get_Document(&pDocDispatch); |
| 113 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; | 135 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; |
| 114 if (!pDoc2) | 136 if (!pDoc2) |
| 115 { | 137 { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 DEBUG_GENERAL("Invoke"); | 173 DEBUG_GENERAL("Invoke"); |
| 152 if (FAILED(hr)) | 174 if (FAILED(hr)) |
| 153 { | 175 { |
| 154 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR
EATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to create S
ettings in JavaScript"); | 176 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR
EATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to create S
ettings in JavaScript"); |
| 155 } | 177 } |
| 156 } | 178 } |
| 157 | 179 |
| 158 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser) | 180 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser) |
| 159 { | 181 { |
| 160 CPluginClient* client = CPluginClient::GetInstance(); | 182 CPluginClient* client = CPluginClient::GetInstance(); |
| 161 std::wstring url = to_wstring(GetDocumentUrl()); | 183 std::wstring url = GetDocumentUrl(); |
| 162 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain(u
rl)) | 184 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain(u
rl)) |
| 163 { | 185 { |
| 164 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl()
); | 186 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl()
); |
| 165 } | 187 } |
| 166 InjectABP(browser); | 188 InjectABP(browser); |
| 167 } | 189 } |
| 168 | 190 |
| 169 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur
l, bool isDocumentBrowser) | 191 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const std::wstrin
g& url, bool isDocumentBrowser) |
| 170 { | 192 { |
| 171 CString documentUrl = GetDocumentUrl(); | 193 std::wstring documentUrl = GetDocumentUrl(); |
| 172 | 194 |
| 173 if (isDocumentBrowser) | 195 if (isDocumentBrowser) |
| 174 { | 196 { |
| 175 if (url != documentUrl) | 197 if (url != documentUrl) |
| 176 { | 198 { |
| 177 SetDocumentUrl(url); | 199 SetDocumentUrl(url); |
| 178 } | 200 } |
| 179 InjectABP(browser); | 201 InjectABP(browser); |
| 180 } | 202 } |
| 181 if (url.Left(6) != "res://") | 203 CString urlLegacy = ToCString(url); |
| 204 if (urlLegacy.Left(6) != "res://") |
| 182 { | 205 { |
| 183 // Get document | 206 // Get document |
| 184 CComPtr<IDispatch> pDocDispatch; | 207 CComPtr<IDispatch> pDocDispatch; |
| 185 HRESULT hr = browser->get_Document(&pDocDispatch); | 208 HRESULT hr = browser->get_Document(&pDocDispatch); |
| 186 if (FAILED(hr) || !pDocDispatch) | 209 if (FAILED(hr) || !pDocDispatch) |
| 187 { | 210 { |
| 188 return; | 211 return; |
| 189 } | 212 } |
| 190 | 213 |
| 191 CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch; | 214 CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 218 | 241 |
| 219 m_criticalSection.Lock(); | 242 m_criticalSection.Lock(); |
| 220 { | 243 { |
| 221 domain = m_documentDomain; | 244 domain = m_documentDomain; |
| 222 } | 245 } |
| 223 m_criticalSection.Unlock(); | 246 m_criticalSection.Unlock(); |
| 224 | 247 |
| 225 return domain; | 248 return domain; |
| 226 } | 249 } |
| 227 | 250 |
| 228 void CPluginTabBase::SetDocumentUrl(const CString& url) | 251 void CPluginTabBase::SetDocumentUrl(const std::wstring& url) |
| 229 { | 252 { |
| 230 m_criticalSection.Lock(); | 253 m_criticalSection.Lock(); |
| 231 { | 254 { |
| 232 m_documentUrl = url; | 255 m_documentUrl = url; |
| 233 m_documentDomain = CAdblockPlusClient::GetInstance()->GetHostFromUrl(to_wstr
ing(url)); | 256 m_documentDomain = CAdblockPlusClient::GetInstance()->GetHostFromUrl(url); |
| 234 } | 257 } |
| 235 m_criticalSection.Unlock(); | 258 m_criticalSection.Unlock(); |
| 236 } | 259 } |
| 237 | 260 |
| 238 CString CPluginTabBase::GetDocumentUrl() | 261 std::wstring CPluginTabBase::GetDocumentUrl() |
| 239 { | 262 { |
| 240 CString url; | 263 std::wstring url; |
| 241 | 264 |
| 242 m_criticalSection.Lock(); | 265 m_criticalSection.Lock(); |
| 243 { | 266 { |
| 244 url = m_documentUrl; | 267 url = m_documentUrl; |
| 245 } | 268 } |
| 246 m_criticalSection.Unlock(); | 269 m_criticalSection.Unlock(); |
| 247 | 270 |
| 248 return url; | 271 return url; |
| 249 } | 272 } |
| 250 | 273 |
| 251 | 274 |
| 252 // ============================================================================ | 275 // ============================================================================ |
| 253 // Frame caching | 276 // Frame caching |
| 254 // ============================================================================ | 277 // ============================================================================ |
| 255 bool CPluginTabBase::IsFrameCached(const CString& url) | 278 bool CPluginTabBase::IsFrameCached(const std::wstring& url) |
| 256 { | 279 { |
| 257 bool isFrame; | 280 bool isFrame; |
| 258 | 281 |
| 259 m_criticalSectionCache.Lock(); | 282 m_criticalSectionCache.Lock(); |
| 260 { | 283 { |
| 261 isFrame = m_cacheFrames.find(url) != m_cacheFrames.end(); | 284 isFrame = m_cacheFrames.find(url) != m_cacheFrames.end(); |
| 262 } | 285 } |
| 263 m_criticalSectionCache.Unlock(); | 286 m_criticalSectionCache.Unlock(); |
| 264 | 287 |
| 265 return isFrame; | 288 return isFrame; |
| 266 } | 289 } |
| 267 | 290 |
| 268 void CPluginTabBase::CacheFrame(const CString& url) | 291 void CPluginTabBase::CacheFrame(const std::wstring& url) |
| 269 { | 292 { |
| 270 m_criticalSectionCache.Lock(); | 293 m_criticalSectionCache.Lock(); |
| 271 { | 294 { |
| 272 m_cacheFrames.insert(url); | 295 m_cacheFrames.insert(url); |
| 273 } | 296 } |
| 274 m_criticalSectionCache.Unlock(); | 297 m_criticalSectionCache.Unlock(); |
| 275 } | 298 } |
| 276 | 299 |
| 277 void CPluginTabBase::ClearFrameCache(const std::wstring& domain) | 300 void CPluginTabBase::ClearFrameCache(const std::wstring& domain) |
| 278 { | 301 { |
| 279 m_criticalSectionCache.Lock(); | 302 m_criticalSectionCache.Lock(); |
| 280 { | 303 { |
| 281 if (domain.empty() || domain != m_cacheDomain) | 304 if (domain.empty() || domain != m_cacheDomain) |
| 282 { | 305 { |
| 283 m_cacheFrames.clear(); | 306 m_cacheFrames.clear(); |
| 284 m_cacheDomain = domain; | 307 m_cacheDomain = domain; |
| 285 } | 308 } |
| 286 } | 309 } |
| 287 m_criticalSectionCache.Unlock(); | 310 m_criticalSectionCache.Unlock(); |
| 288 } | 311 } |
| 289 | 312 |
| 290 void CPluginTabBase::ThreadProc() | 313 void CPluginTabBase::ThreadProc() |
| 291 { | 314 { |
| 292 // Force loading/creation of settings | 315 // Force loading/creation of settings |
| 293 CPluginSettings* settings = CPluginSettings::GetInstance(); | 316 CPluginSettings::GetInstance()->SetWorkingThreadId(); |
| 294 | 317 |
| 295 settings->SetWorkingThreadId(); | 318 std::string message = |
| 296 | 319 "===========================================================================
=====\n" |
| 297 CString threadInfo; | 320 "TAB THREAD process="; |
| 298 threadInfo.Format(L"%d.%d", ::GetCurrentProcessId(), ::GetCurrentThreadId()); | 321 message += std::to_string(::GetCurrentProcessId()); |
| 299 | 322 message + " thread="; |
| 300 CString debugText; | 323 message += std::to_string(::GetCurrentThreadId()); |
| 301 | 324 message += |
| 302 debugText += L"===============================================================
================="; | 325 "\n" |
| 303 debugText += L"\nTAB THREAD " + threadInfo; | 326 "===========================================================================
====="; |
| 304 debugText += L"\n=============================================================
==================="; | 327 DEBUG_GENERAL(message); |
| 305 | |
| 306 DEBUG_GENERAL(debugText) | |
| 307 | 328 |
| 308 // -------------------------------------------------------------------- | 329 // -------------------------------------------------------------------- |
| 309 // Tab loop | 330 // Tab loop |
| 310 // -------------------------------------------------------------------- | 331 // -------------------------------------------------------------------- |
| 311 | 332 |
| 312 DWORD loopCount = 0; | 333 DWORD loopCount = 0; |
| 313 DWORD tabLoopIteration = 1; | 334 DWORD tabLoopIteration = 1; |
| 314 | 335 |
| 315 while (this->m_continueThreadRunning) | 336 while (this->m_continueThreadRunning) |
| 316 { | 337 { |
| 317 #ifdef ENABLE_DEBUG_THREAD | 338 #ifdef ENABLE_DEBUG_THREAD |
| 318 CStringA sTabLoopIteration; | 339 CStringA sTabLoopIteration; |
| 319 sTabLoopIteration.Format("%u", tabLoopIteration); | 340 sTabLoopIteration.Format("%u", tabLoopIteration); |
| 320 | 341 |
| 321 DEBUG_THREAD("--------------------------------------------------------------
------------------") | 342 DEBUG_THREAD("--------------------------------------------------------------
------------------") |
| 322 DEBUG_THREAD("Loop iteration " + sTabLoopIteration); | 343 DEBUG_THREAD("Loop iteration " + sTabLoopIteration); |
| 323 DEBUG_THREAD("--------------------------------------------------------------
------------------") | 344 DEBUG_THREAD("--------------------------------------------------------------
------------------") |
| 324 #endif | 345 #endif |
| 325 if (this->m_isActivated) | 346 this->m_isActivated = false; |
| 326 { | |
| 327 bool isChanged = false; | |
| 328 | |
| 329 if (isChanged) | |
| 330 { | |
| 331 this->m_plugin->UpdateStatusBar(); | |
| 332 } | |
| 333 | |
| 334 this->m_isActivated = false; | |
| 335 } | |
| 336 | 347 |
| 337 // -------------------------------------------------------------------- | 348 // -------------------------------------------------------------------- |
| 338 // End loop | 349 // End loop |
| 339 // -------------------------------------------------------------------- | 350 // -------------------------------------------------------------------- |
| 340 | 351 |
| 341 // Sleep loop | 352 // Sleep loop |
| 342 while (this->m_continueThreadRunning && !this->m_isActivated && (++loopCou
nt % (TIMER_THREAD_SLEEP_TAB_LOOP / 50)) != 0) | 353 while (this->m_continueThreadRunning && !this->m_isActivated && (++loopCou
nt % (TIMER_THREAD_SLEEP_TAB_LOOP / 50)) != 0) |
| 343 { | 354 { |
| 344 // Post async plugin error | 355 // Post async plugin error |
| 345 CPluginError pluginError; | 356 CPluginError pluginError; |
| 346 if (CPluginClient::PopFirstPluginError(pluginError)) | 357 if (LogQueue::PopFirstPluginError(pluginError)) |
| 347 { | 358 { |
| 348 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError.
GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr
ue, pluginError.GetProcessId(), pluginError.GetThreadId()); | 359 LogQueue::LogPluginError(pluginError.GetErrorCode(), pluginError.GetEr
rorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), true, p
luginError.GetProcessId(), pluginError.GetThreadId()); |
| 349 } | 360 } |
| 350 | 361 |
| 351 // Non-hanging sleep | 362 // Non-hanging sleep |
| 352 Sleep(50); | 363 Sleep(50); |
| 353 } | 364 } |
| 354 | 365 |
| 355 tabLoopIteration++; | 366 tabLoopIteration++; |
| 356 } | 367 } |
| 357 } | 368 } |
| LEFT | RIGHT |