Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/PluginSettings.cpp

Issue 10845030: Marshal all libadblockplus calls to the engine process (Closed)
Patch Set: Created May 31, 2013, 2:20 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/plugin/PluginSettings.cpp
===================================================================
--- a/src/plugin/PluginSettings.cpp
+++ b/src/plugin/PluginSettings.cpp
@@ -24,18 +24,16 @@
class TSettings
{
DWORD processorId;
char sPluginId[44];
};
-static void SubsCallback(std::vector<AdblockPlus::SubscriptionPtr>& subscriptions);
-
class CPluginSettingsLock : public CPluginMutex
{
public:
CPluginSettingsLock() : CPluginMutex("SettingsFile", PLUGIN_ERROR_MUTEX_SETTINGS_FILE) {}
~CPluginSettingsLock() {}
};
@@ -66,18 +64,18 @@ CPluginSettings* CPluginSettings::s_inst
bool CPluginSettings::s_isLightOnly = false;
CComAutoCriticalSection CPluginSettings::s_criticalSectionLocal;
#ifdef SUPPORT_WHITELIST
CComAutoCriticalSection CPluginSettings::s_criticalSectionDomainHistory;
#endif
-CPluginSettings::CPluginSettings() :
- m_settingsVersion("1"), m_isDirty(false), m_isFirstRun(false), m_isFirstRunUpdate(false), m_dwMainProcessId(0), m_dwMainThreadId(0), m_dwWorkingThreadId(0),
+CPluginSettings::CPluginSettings() :
+ m_settingsVersion("1"), m_isDirty(false), m_isFirstRun(false), m_isFirstRunUpdate(false), m_dwMainProcessId(0), m_dwMainThreadId(0), m_dwWorkingThreadId(0),
m_isDirtyTab(false), m_isPluginEnabledTab(true), m_tabNumber("1")
{
CPluginSettings *lightInstance = s_instance;
s_instance = NULL;
m_settingsFile = std::auto_ptr<CPluginIniFileW>(new CPluginIniFileW(GetDataPath(SETTINGS_INI_FILE), false));
m_settingsFileTab = std::auto_ptr<CPluginIniFileW>(new CPluginIniFileW(GetDataPath(SETTINGS_INI_FILE_TAB), true));
@@ -98,51 +96,50 @@ CPluginSettings::CPluginSettings() :
{
std::ifstream is;
is.open(GetDataPath(SETTINGS_INI_FILE), std::ios_base::in);
if (!is.is_open())
{
TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
0,
- pf,
- CSIDL_PROGRAM_FILESX86,
- FALSE );
+ pf,
+ CSIDL_PROGRAM_FILESX86,
+ FALSE );
//No files found, copy from the dll location
CString pathToDll;
DWORD pathResult = GetModuleFileNameW((HINSTANCE)&__ImageBase, pathToDll.GetBufferSetLength(MAX_PATH), MAX_PATH);
if (pathResult > 0)
{
CString cpyPath;
cpyPath = pathToDll.Left(pathToDll.ReverseFind('\\') + 1);
BOOL res = CopyFile(cpyPath + SETTINGS_INI_FILE, GetDataPath(SETTINGS_INI_FILE), TRUE);
res = CopyFile(cpyPath + DICTIONARY_INI_FILE, GetDataPath(DICTIONARY_INI_FILE), TRUE);
res = CopyFile(cpyPath + SETTING_PAGE_INI_FILE, GetDataPath(SETTING_PAGE_INI_FILE), TRUE);
SHFILEOPSTRUCT pFileStruct;
- ZeroMemory(&pFileStruct, sizeof(SHFILEOPSTRUCT));
+ ZeroMemory(&pFileStruct, sizeof(SHFILEOPSTRUCT));
pFileStruct.hwnd = NULL;
pFileStruct.wFunc = FO_COPY;
- WCHAR fromPath[MAX_PATH + 2];
+ WCHAR fromPath[MAX_PATH + 2];
WCHAR toPath[MAX_PATH + 2];
CString source = cpyPath + "html\\*";
wcscpy(fromPath, source);
fromPath[source.GetLength()] = '\0';
fromPath[source.GetLength() + 1] = '\0';
wcscpy(toPath, GetDataPath(L"html"));
toPath[GetDataPath(L"html").GetLength()] = '\0';
toPath[GetDataPath(L"html").GetLength() + 1] = '\0';
pFileStruct.pFrom = fromPath;
- pFileStruct.pTo = toPath;
- pFileStruct.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NO_UI | FOF_RENAMEONCOLLISION;
- bool i = pFileStruct.fAnyOperationsAborted ;
+ pFileStruct.pTo = toPath;
+ pFileStruct.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NO_UI | FOF_RENAMEONCOLLISION;
SHFileOperation(&pFileStruct);
}
is.open(GetDataPath(SETTINGS_INI_FILE), std::ios_base::in);
if (!is.is_open())
{
m_isDirty = true;
}
else
@@ -189,49 +186,41 @@ CPluginSettings::~CPluginSettings()
if (s_dataPathParent != NULL)
{
delete s_dataPathParent;
}
s_instance = NULL;
}
-CPluginSettings* CPluginSettings::GetInstance()
+CPluginSettings* CPluginSettings::GetInstance()
{
CPluginSettings* instance = NULL;
s_criticalSectionLocal.Lock();
{
if ((!s_instance) || (s_isLightOnly))
{
s_instance = new CPluginSettings();
#ifdef USE_CONSOLE
CONSOLE("Fetching Available Subscription\n");
#endif
- try
- {
- CPluginSettings::GetInstance()->m_subscriptions = CPluginClient::GetInstance()->FetchAvailableSubscriptions();
- }
- catch(std::exception ex)
- {
- DEBUG_GENERAL(ex.what());
- throw ex;
- }
+ CPluginSettings::GetInstance()->m_subscriptions = CPluginClient::GetInstance()->FetchAvailableSubscriptions();
s_isLightOnly = false;
}
instance = s_instance;
}
s_criticalSectionLocal.Unlock();
return instance;
}
-bool CPluginSettings::HasInstance()
+bool CPluginSettings::HasInstance()
{
bool hasInstance = true;
s_criticalSectionLocal.Lock();
{
hasInstance = s_instance != NULL;
}
s_criticalSectionLocal.Unlock();
@@ -249,17 +238,17 @@ bool CPluginSettings::Read(bool bDebug)
if (bDebug)
{
DEBUG_GENERAL(L"*** Loading settings:" + m_settingsFile->GetFilePath());
}
CPluginSettingsLock lock;
if (lock.IsLocked())
{
- isRead = m_settingsFile->Read();
+ isRead = m_settingsFile->Read();
if (isRead)
{
if (m_settingsFile->IsValidChecksum())
{
m_properties = m_settingsFile->GetSectionData("Settings");
}
else
{
@@ -357,31 +346,31 @@ bool CPluginSettings::MakeRequestForUpda
}
}
return true;
}
CString CPluginSettings::GetDataPathParent()
{
- if (s_dataPathParent == NULL)
+ if (s_dataPathParent == NULL)
{
WCHAR* lpData = new WCHAR[MAX_PATH];
OSVERSIONINFO osVersionInfo;
::ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFO));
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&osVersionInfo);
- //Windows Vista - 6.0
- //Windows Server 2003 R2 - 5.2
- //Windows Server 2003 - 5.2
- //Windows XP - 5.1
+ //Windows Vista - 6.0
+ //Windows Server 2003 R2 - 5.2
+ //Windows Server 2003 - 5.2
+ //Windows XP - 5.1
if (osVersionInfo.dwMajorVersion >= 6)
{
if (::SHGetSpecialFolderPath(NULL, lpData, CSIDL_LOCAL_APPDATA, TRUE))
{
wcscat(lpData, L"Low");
}
else
{
@@ -410,31 +399,31 @@ CString CPluginSettings::GetDataPathPare
}
}
return s_dataPathParent;
}
CString CPluginSettings::GetDataPath(const CString& filename)
{
- if (s_dataPath == NULL)
+ if (s_dataPath == NULL)
{
WCHAR* lpData = new WCHAR[MAX_PATH];
OSVERSIONINFO osVersionInfo;
::ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFO));
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&osVersionInfo);
- //Windows Vista - 6.0
- //Windows Server 2003 R2 - 5.2
- //Windows Server 2003 - 5.2
- //Windows XP - 5.1
+ //Windows Vista - 6.0
+ //Windows Server 2003 R2 - 5.2
+ //Windows Server 2003 - 5.2
+ //Windows XP - 5.1
if (osVersionInfo.dwMajorVersion >= 6)
{
if (::SHGetSpecialFolderPath(NULL, lpData, CSIDL_LOCAL_APPDATA, TRUE))
{
wcscat(lpData, L"Low");
}
else
{
@@ -489,17 +478,17 @@ CString CPluginSettings::GetSystemLangua
}
CString CPluginSettings::GetTempPath(const CString& filename)
{
CString tempPath;
LPWSTR pwszCacheDir = NULL;
- HRESULT hr = ::IEGetWriteableFolderPath(FOLDERID_InternetCache, &pwszCacheDir);
+ HRESULT hr = ::IEGetWriteableFolderPath(FOLDERID_InternetCache, &pwszCacheDir);
if (SUCCEEDED(hr))
{
tempPath = pwszCacheDir;
}
// Not implemented in IE6
else if (hr == E_NOTIMPL)
{
TCHAR path[MAX_PATH] = _T("");
@@ -566,17 +555,17 @@ bool CPluginSettings::Has(const CString&
return hasKey;
}
void CPluginSettings::Remove(const CString& key)
{
s_criticalSectionLocal.Lock();
- {
+ {
TProperties::iterator it = m_properties.find(key);
if (it != m_properties.end())
{
m_properties.erase(it);
m_isDirty = true;
}
}
s_criticalSectionLocal.Unlock();
@@ -614,17 +603,17 @@ void CPluginSettings::SetString(const CS
TProperties::iterator it = m_properties.find(key);
if (it != m_properties.end() && it->second != value)
{
it->second = value;
m_isDirty = true;
}
else if (it == m_properties.end())
{
- m_properties[key] = value;
+ m_properties[key] = value;
m_isDirty = true;
}
}
s_criticalSectionLocal.Unlock();
}
int CPluginSettings::GetValue(const CString& key, int defaultValue) const
@@ -663,17 +652,17 @@ void CPluginSettings::SetValue(const CSt
TProperties::iterator it = m_properties.find(key);
if (it != m_properties.end() && it->second != sValue)
{
it->second = sValue;
m_isDirty = true;
}
else if (it == m_properties.end())
{
- m_properties[key] = sValue;
+ m_properties[key] = sValue;
m_isDirty = true;
}
}
s_criticalSectionLocal.Unlock();
}
bool CPluginSettings::GetBool(const CString& key, bool defaultValue) const
@@ -703,36 +692,24 @@ void CPluginSettings::SetBool(const CStr
}
bool CPluginSettings::IsPluginEnabled() const
{
return m_isPluginEnabledTab;
}
-static void SubsCallback(std::vector<AdblockPlus::SubscriptionPtr>& subscriptions)
-{
- CPluginSettings::GetInstance()->m_subscriptions = subscriptions;
- return;
-}
-
std::map<CString, CString> CPluginSettings::GetFilterLanguageTitleList() const
{
std::map<CString, CString> filterList;
- for (int i = 0; i < m_subscriptions.size(); i ++)
- {
- AdblockPlus::SubscriptionPtr it = m_subscriptions[i];
- std::string title = "";
- std::string url = "";
-
- title = it.get()->GetProperty("specialization")->AsString();
- url = it.get()->GetProperty("url")->AsString();
-
- filterList.insert(std::make_pair(CString(CA2T(url.c_str(), CP_UTF8)), CString(CA2T(title.c_str(), CP_UTF8))));
+ for (size_t i = 0; i < m_subscriptions.size(); i ++)
+ {
+ SubscriptionDescription it = m_subscriptions[i];
+ filterList.insert(std::make_pair(CString(CA2T(it.url.c_str(), CP_UTF8)), CString(CA2T(it.title.c_str(), CP_UTF8))));
}
return filterList;
}
bool CPluginSettings::Write(bool isDebug)
{
bool isWritten = true;
@@ -748,17 +725,17 @@ bool CPluginSettings::Write(bool isDebug
}
CPluginSettingsLock lock;
if (lock.IsLocked())
{
m_settingsFile->Clear();
// Properties
- CPluginIniFileW::TSectionData settings;
+ CPluginIniFileW::TSectionData settings;
s_criticalSectionLocal.Lock();
{
for (TProperties::iterator it = m_properties.begin(); it != m_properties.end(); ++it)
{
settings[it->first] = it->second;
}
}
@@ -941,17 +918,17 @@ bool CPluginSettings::ReadTab(bool bDebu
DEBUG_SETTINGS(L"SettingsTab::Read tab")
if (bDebug)
{
DEBUG_GENERAL(L"*** Loading tab settings:" + m_settingsFileTab->GetFilePath());
}
- isRead = m_settingsFileTab->Read();
+ isRead = m_settingsFileTab->Read();
if (isRead)
{
ClearTab();
if (m_settingsFileTab->IsValidChecksum())
{
s_criticalSectionLocal.Lock();
{
@@ -1004,18 +981,18 @@ bool CPluginSettings::WriteTab(bool isDe
if (isDebug)
{
DEBUG_GENERAL(L"*** Writing changed tab settings")
}
m_settingsFileTab->Clear();
// Properties & errors
- CPluginIniFileW::TSectionData settings;
- CPluginIniFileW::TSectionData errors;
+ CPluginIniFileW::TSectionData settings;
+ CPluginIniFileW::TSectionData errors;
s_criticalSectionLocal.Lock();
{
for (TProperties::iterator it = m_propertiesTab.begin(); it != m_propertiesTab.end(); ++it)
{
settings[it->first] = it->second;
}
@@ -1072,32 +1049,32 @@ bool CPluginSettings::IncrementTabCount(
today.Format(L"%d-%d-%d", systemTime.wYear, systemTime.wMonth, systemTime.wDay);
ReadTab(false);
s_criticalSectionLocal.Lock();
{
TProperties::iterator it = m_propertiesTab.find(SETTING_TAB_COUNT);
if (it != m_propertiesTab.end())
- {
+ {
tabCount = _wtoi(it->second) + 1;
}
it = m_propertiesTab.find(SETTING_TAB_START_TIME);
//Is this a first IE instance?
HWND ieWnd = FindWindow(L"IEFrame", NULL);
if (ieWnd != NULL)
{
ieWnd = FindWindowEx(NULL, ieWnd, L"IEFrame", NULL);
}
if ((it != m_propertiesTab.end() && it->second != today))
{
- tabCount = 1;
+ tabCount = 1;
}
m_tabNumber.Format(L"%d", tabCount);
m_propertiesTab[SETTING_TAB_COUNT] = m_tabNumber;
m_propertiesTab[SETTING_TAB_START_TIME] = today;
// Main tab?
if (tabCount == 1)
@@ -1114,17 +1091,17 @@ bool CPluginSettings::IncrementTabCount(
m_propertiesTab[SETTING_TAB_CONFIG_VERSION] = "1";
#endif
}
}
s_criticalSectionLocal.Unlock();
m_isDirtyTab = true;
- WriteTab(false);
+ WriteTab(false);
}
return tabCount == 1;
}
CString CPluginSettings::GetTabNumber() const
{
@@ -1172,17 +1149,17 @@ bool CPluginSettings::DecrementTabCount(
it = m_propertiesTab.find(SETTING_TAB_COUNT);
if (it != m_propertiesTab.end())
{
m_propertiesTab.erase(it);
}
}
- m_isDirtyTab = true;
+ m_isDirtyTab = true;
}
}
s_criticalSectionLocal.Unlock();
WriteTab(false);
}
return tabCount == 0;
@@ -1257,17 +1234,17 @@ void CPluginSettings::AddError(const CSt
if (lock.IsLocked())
{
ReadTab(false);
s_criticalSectionLocal.Lock();
{
if (m_errorsTab.find(error) == m_errorsTab.end())
{
- m_errorsTab[error] = errorCode;
+ m_errorsTab[error] = errorCode;
m_isDirtyTab = true;
}
}
s_criticalSectionLocal.Unlock();
WriteTab(false);
}
}
@@ -1452,17 +1429,17 @@ void CPluginSettings::IncrementTabVersio
versionString.Format(L"%d", version);
m_propertiesTab[key] = versionString;
}
s_criticalSectionLocal.Unlock();
m_isDirtyTab = true;
- WriteTab(false);
+ WriteTab(false);
}
}
// ============================================================================
// Whitelist settings
// ============================================================================
@@ -1490,45 +1467,17 @@ bool CPluginSettings::ReadWhitelist(bool
}
CPluginSettingsWhitelistLock lock;
if (lock.IsLocked())
{
ClearWhitelist();
s_criticalSectionLocal.Lock();
- try
- {
- std::vector<AdblockPlus::FilterPtr> filters = CPluginClient::GetInstance()->GetListedFilters();
- for (int i = 0; i < filters.size(); i ++)
- {
- if (filters[i]->GetType() == AdblockPlus::Filter::Type::TYPE_EXCEPTION)
- {
- std::string text = filters[i]->GetProperty("text")->AsString();
- //@@||example.com^$document
- size_t endPos = text.rfind("^$document");
- if (endPos != std::string::npos)
- {
- size_t startPos = text.find("@@||") + 4;
- if (startPos != std::string::npos)
- {
- m_whitelistedDomains.push_back(text.substr(startPos, endPos - startPos));
- }
- }
- }
- }
- }
- catch(std::runtime_error ex)
- {
- DEBUG_GENERAL(ex.what());
- }
- catch(std::exception ex)
- {
- DEBUG_GENERAL(ex.what());
- }
+ m_whitelistedDomains = CPluginClient::GetInstance()->GetExceptionDomains();
s_criticalSectionLocal.Unlock();
}
else
{
isRead = false;
}
return isRead;
@@ -1550,20 +1499,17 @@ void CPluginSettings::AddWhiteListedDoma
std::string newDomain = CW2A(domain, CP_UTF8);
//Domain already present?
if (std::find(m_whitelistedDomains.begin(), m_whitelistedDomains.end(), newDomain) != m_whitelistedDomains.end())
{
return;
}
s_criticalSectionLocal.Lock();
- {
- AdblockPlus::FilterPtr whitelistFilter = CPluginClient::GetInstance()->GetFilter(std::string("@@||").append(CW2A(domain)).append("^$document"));
- whitelistFilter->AddToList();
- }
+ CPluginClient::GetInstance()->AddFilter(std::string("@@||").append(CW2A(domain)).append("^$document"));
s_criticalSectionLocal.Unlock();
}
if (isForcingUpdateOnStart)
{
ForceConfigurationUpdateOnStart();
}
@@ -1638,128 +1584,40 @@ DWORD CPluginSettings::GetWindowsBuildNu
void CPluginSettings::SetSubscription(BSTR url)
{
std::string urlConverted = CT2A(url, CP_UTF8);
SetSubscription(urlConverted);
}
void CPluginSettings::SetSubscription(std::string url)
{
- try
- {
- std::vector<AdblockPlus::SubscriptionPtr> subscriptions = CPluginClient::GetInstance()->GetListedSubscriptions();
- if (subscriptions.size() > 0)
- {
- for (int i = 0; i < subscriptions.size(); i ++)
- {
- subscriptions[i]->RemoveFromList();
- }
- }
- AdblockPlus::SubscriptionPtr subscription = CPluginClient::GetInstance()->GetSubscription(url);
- subscription->AddToList();
- RefreshFilterlist();
- RefreshWhitelist();
- }
- catch(std::exception ex)
- {
- DEBUG_GENERAL(ex.what());
- }
- catch(std::runtime_error ex)
- {
- DEBUG_GENERAL(ex.what());
- }
+ CPluginClient::GetInstance()->SetSubscription(url);
+ RefreshFilterlist();
+ RefreshWhitelist();
}
std::vector<std::string> split(const std::string& s, char delim) {
std::vector<std::string> retTokens;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
retTokens.push_back(item);
}
return retTokens;
}
-void CPluginSettings::SetDefaultSubscription()
-{
- CPluginSystem* system = CPluginSystem::GetInstance();
- CString lng = system->GetBrowserLanguage().Left(2);
- std::string browserLanguage = CW2A(lng, CP_UTF8);
- std::vector<SubscriptionPtr> subscriptions = CPluginClient::GetInstance()->FetchAvailableSubscriptions();
- bool subscriptionSet = false;
- while (!subscriptionSet)
- {
- for (int i = 0; i < subscriptions.size(); i++)
- {
- std::string prefixes = subscriptions[i]->GetProperty("prefixes")->AsString();
- std::vector<std::string> tokens = split(prefixes, ',');
- for (int j = 0; j < tokens.size(); j ++)
- {
- if (tokens[j] == browserLanguage)
- {
- SetSubscription(subscriptions[i]->GetProperty("url")->AsString());
- subscriptionSet = true;
- }
- }
- }
-
- if (browserLanguage == "en")
- break;
- // failed to set the subscription for browser language. Try "en"
- browserLanguage = "en";
-
- }
-}
-
CString CPluginSettings::GetSubscription()
{
- try
- {
- std::vector<AdblockPlus::SubscriptionPtr> subscriptions = CPluginClient::GetInstance()->GetListedSubscriptions();
-
- if (subscriptions.size() == 0)
- {
- SetDefaultSubscription();
- subscriptions = CPluginClient::GetInstance()->GetListedSubscriptions();
- }
- for (int i = 0; i < subscriptions.size(); i ++)
- {
- return CString(CA2T(subscriptions[i]->GetProperty("url")->AsString().c_str(), CP_UTF8));
- }
- }
- catch(std::exception ex)
- {
- DEBUG_GENERAL(ex.what());
- }
- catch(std::runtime_error ex)
- {
- DEBUG_GENERAL(ex.what());
- }
- return CString(L"");
+ std::vector<SubscriptionDescription> subscriptions = CPluginClient::GetInstance()->GetListedSubscriptions();
+ if (subscriptions.size() > 0)
+ return CString(CA2T(subscriptions.front().url.c_str(), CP_UTF8));
+ else
+ return CString(L"");
}
void CPluginSettings::RefreshFilterlist()
{
- try
- {
- // Make sure at least the default subscription is set
- CPluginSettings* settings = CPluginSettings::GetInstance();
- settings->GetSubscription();
-
- std::vector<AdblockPlus::SubscriptionPtr> subscriptions = CPluginClient::GetInstance()->GetListedSubscriptions();
- for (int i = 0; i < subscriptions.size(); i ++)
- {
- subscriptions[i]->UpdateFilters();
- }
- }
- catch(std::exception ex)
- {
- DEBUG_GENERAL(ex.what());
- }
- catch(std::runtime_error ex)
- {
- DEBUG_GENERAL(ex.what());
- }
+ CPluginClient::GetInstance()->UpdateAllSubscriptions();
}
#endif // SUPPORT_WHITELIST

Powered by Google App Engine
This is Rietveld