Index: src/plugin/PluginClass.cpp |
diff --git a/src/plugin/PluginClass.cpp b/src/plugin/PluginClass.cpp |
index a93c0f74851ed6fa244eea4edf5955861a711574..3ebb03d0c6bf2de0b19454fb057c87a499379311 100644 |
--- a/src/plugin/PluginClass.cpp |
+++ b/src/plugin/PluginClass.cpp |
@@ -32,6 +32,7 @@ |
#include "../shared/Version.h" |
#include <thread> |
#include <array> |
+#include "WebBrowserEventsListener.h" |
#ifdef DEBUG_HIDE_EL |
DWORD profileTime = 0; |
@@ -91,6 +92,7 @@ namespace AdblockPlus |
} |
CPluginClass::CPluginClass() |
+ : m_data(std::make_shared<Data>()) |
{ |
//Use this line to debug memory leaks |
// _CrtDumpMemoryLeaks(); |
@@ -105,17 +107,14 @@ CPluginClass::CPluginClass() |
m_isInitializedOk = false; |
- m_tab = new CPluginTab(this); |
- |
+ m_data->tab.reset(new CPluginTab(this)); |
Dictionary::Create(GetBrowserLanguage()); |
} |
CPluginClass::~CPluginClass() |
{ |
- delete m_tab; |
} |
- |
///////////////////////////////////////////////////////////////////////////// |
// Initialization |
@@ -128,7 +127,7 @@ void CPluginClass::FinalRelease() |
{ |
s_criticalSectionBrowser.Lock(); |
{ |
- m_webBrowser2.Release(); |
+ m_data.reset(); |
Eric
2015/12/02 17:00:13
'FinalRelease' is slated to disappear as part of t
|
} |
s_criticalSectionBrowser.Unlock(); |
} |
@@ -157,7 +156,7 @@ CComQIPtr<IWebBrowser2> CPluginClass::GetBrowser() const |
s_criticalSectionBrowser.Lock(); |
{ |
- browser = m_webBrowser2; |
+ browser = m_data->webBrowser2; |
} |
s_criticalSectionBrowser.Unlock(); |
@@ -192,7 +191,7 @@ std::wstring CPluginClass::GetBrowserUrl() const |
} |
else |
{ |
- url = m_tab->GetDocumentUrl(); |
+ url = m_data->tab->GetDocumentUrl(); |
} |
return url; |
} |
@@ -236,7 +235,7 @@ STDMETHODIMP CPluginClass::SetSite(IUnknown* unknownSite) |
s_criticalSectionBrowser.Lock(); |
{ |
- m_webBrowser2 = unknownSite; |
+ m_data->webBrowser2 = ATL::CComQIPtr<IWebBrowser2>(unknownSite); |
} |
s_criticalSectionBrowser.Unlock(); |
@@ -331,7 +330,7 @@ STDMETHODIMP CPluginClass::SetSite(IUnknown* unknownSite) |
// Release browser interface |
s_criticalSectionBrowser.Lock(); |
{ |
- m_webBrowser2.Release(); |
+ m_data->webBrowser2.Release(); |
} |
s_criticalSectionBrowser.Unlock(); |
@@ -483,6 +482,7 @@ void STDMETHODCALLTYPE CPluginClass::OnBeforeNavigate2( |
return; |
} |
std::wstring url(urlVariant->bstrVal, SysStringLen(urlVariant->bstrVal)); |
+ EnsureWebBrowserConnected(webBrowser); |
// If webbrowser2 is equal to top level browser (as set in SetSite), we are |
// navigating new page |
@@ -492,7 +492,7 @@ void STDMETHODCALLTYPE CPluginClass::OnBeforeNavigate2( |
} |
else if (GetBrowser().IsEqualObject(webBrowser)) |
{ |
- m_tab->OnNavigate(url); |
+ m_data->tab->OnNavigate(url); |
DEBUG_GENERAL( |
L"================================================================================\n" |
L"Begin main navigation url:" + url + L"\n" |
@@ -506,7 +506,7 @@ void STDMETHODCALLTYPE CPluginClass::OnBeforeNavigate2( |
else |
{ |
DEBUG_NAVI(L"Navi::Begin navigation url:" + url) |
- m_tab->CacheFrame(url); |
+ m_data->tab->CacheFrame(url); |
} |
} |
catch (...) |
@@ -523,7 +523,7 @@ void STDMETHODCALLTYPE CPluginClass::OnDownloadComplete() |
ATL::CComPtr<IWebBrowser2> browser = GetBrowser(); |
if (browser) |
{ |
- m_tab->OnDownloadComplete(browser); |
+ m_data->tab->OnDownloadComplete(browser); |
} |
} |
catch (...) |
@@ -532,26 +532,6 @@ void STDMETHODCALLTYPE CPluginClass::OnDownloadComplete() |
} |
// Entry point |
-void STDMETHODCALLTYPE CPluginClass::OnDocumentComplete(IDispatch* frameBrowserDisp, VARIANT* /*urlOrPidl*/) |
-{ |
- try |
- { |
- DEBUG_NAVI(L"Navi::Document Complete"); |
- ATL::CComQIPtr<IWebBrowser2> webBrowser2 = frameBrowserDisp; |
- if (!webBrowser2) |
- { |
- return; |
- } |
- std::wstring frameSrc = GetLocationUrl(*webBrowser2); |
- bool isRootPageBrowser = GetBrowser().IsEqualObject(webBrowser2); |
- m_tab->OnDocumentComplete(webBrowser2, frameSrc, isRootPageBrowser); |
- } |
- catch (...) |
- { |
- } |
-} |
- |
-// Entry point |
void STDMETHODCALLTYPE CPluginClass::OnWindowStateChanged(unsigned long flags, unsigned long validFlagsMask) |
{ |
try |
@@ -984,7 +964,7 @@ CPluginClass* CPluginClass::FindInstance(HWND hStatusBarWnd) |
CPluginTab* CPluginClass::GetTab() |
{ |
- return m_tab; |
+ return m_data->tab.get(); |
} |
CPluginTab* CPluginClass::GetTab(DWORD dwThreadId) |
@@ -996,7 +976,7 @@ CPluginTab* CPluginClass::GetTab(DWORD dwThreadId) |
std::map<DWORD,CPluginClass*>::const_iterator it = s_threadInstances.find(dwThreadId); |
if (it != s_threadInstances.end()) |
{ |
- tab = it->second->m_tab; |
+ tab = it->second->m_data->tab.get(); |
} |
} |
s_criticalSectionLocal.Unlock(); |
@@ -1122,6 +1102,7 @@ void CPluginClass::DisplayPluginMenu(HMENU hMenu, int nToolbarCmdID, POINT pt, U |
} |
case ID_MENU_DISABLE_ON_SITE: |
{ |
+ CPluginSettings* settings = CPluginSettings::GetInstance(); |
std::wstring urlString = GetTab()->GetDocumentUrl(); |
std::string filterText = client->GetWhitelistingFilter(urlString); |
if (!filterText.empty()) |
@@ -1130,7 +1111,7 @@ void CPluginClass::DisplayPluginMenu(HMENU hMenu, int nToolbarCmdID, POINT pt, U |
} |
else |
{ |
- CPluginSettings::GetInstance()->AddWhiteListedDomain(client->GetHostFromUrl(urlString)); |
+ settings->AddWhiteListedDomain(client->GetHostFromUrl(urlString)); |
Eric
2015/12/02 17:00:14
This change and the one immediately above are a re
sergei
2016/02/25 17:48:26
Done.
|
} |
} |
default: |
@@ -1223,7 +1204,7 @@ STDMETHODIMP CPluginClass::Exec(const GUID*, DWORD nCmdID, DWORD, VARIANTARG*, V |
} |
// Create menu |
- HMENU hMenu = CreatePluginMenu(m_tab->GetDocumentUrl()); |
+ HMENU hMenu = CreatePluginMenu(m_data->tab->GetDocumentUrl()); |
if (!hMenu) |
{ |
return E_FAIL; |
@@ -1621,7 +1602,6 @@ void CPluginClass::UpdateStatusBar() |
} |
} |
- |
void CPluginClass::Unadvise() |
{ |
s_criticalSectionLocal.Lock(); |
@@ -1639,6 +1619,42 @@ void CPluginClass::Unadvise() |
s_criticalSectionLocal.Unlock(); |
} |
+void CPluginClass::EnsureWebBrowserConnected(const ATL::CComPtr<IWebBrowser2>& webBrowser) |
+{ |
+ auto it = m_data->connectedWebBrowsersCache.find(webBrowser); |
+ if (it != m_data->connectedWebBrowsersCache.end()) |
+ { |
+ return; |
+ } |
+ ATL::CComObject<WebBrowserEventsListener>* listenerImpl = nullptr; |
+ if (FAILED(ATL::CComObject<WebBrowserEventsListener>::CreateInstance(&listenerImpl))) |
+ { |
+ return; |
+ } |
+ ATL::CComPtr<IUnknown> listenerRefCounterGuard(listenerImpl->GetUnknown()); |
+ std::weak_ptr<Data> dataForCapturing = m_data; |
+ auto onListenerDestroy = [webBrowser, dataForCapturing] |
+ { |
+ if (auto data = dataForCapturing.lock()) |
+ { |
+ data->connectedWebBrowsersCache.erase(webBrowser); |
+ } |
+ }; |
+ auto onReloaded = [webBrowser, dataForCapturing] |
+ { |
+ if (auto data = dataForCapturing.lock()) |
+ { |
+ auto frameSrc = GetLocationUrl(*webBrowser); |
+ data->tab->OnDocumentComplete(webBrowser, frameSrc, data->webBrowser2.IsEqualObject(webBrowser)); |
+ } |
+ }; |
+ if (FAILED(listenerImpl->Init(webBrowser, onListenerDestroy, onReloaded))) |
+ { |
+ return; |
+ } |
+ m_data->connectedWebBrowsersCache.emplace(webBrowser, listenerImpl); |
+} |
+ |
HICON CPluginClass::GetIcon(int type) |
{ |
HICON icon = NULL; |