Index: src/plugin/AdblockPlusClient.cpp |
=================================================================== |
--- a/src/plugin/AdblockPlusClient.cpp |
+++ b/src/plugin/AdblockPlusClient.cpp |
@@ -9,7 +9,6 @@ |
#include "AdblockPlusClient.h" |
-#include "../shared/Communication.h" |
#include "../shared/Utils.h" |
namespace |
@@ -355,11 +354,9 @@ |
} |
} |
-void CAdblockPlusClient::AddFilter(const std::wstring& text) |
+// A helper method to reuse the code |
+void CAdblockPlusClient::PostRequest(Communication::OutputBuffer request) |
Wladimir Palant
2013/07/22 06:43:11
I can't say I like the PostRequest() and FetchResp
Oleksandr
2013/07/22 09:53:31
I do disagree, at least partially. The code looks
Felix Dahlke
2013/07/23 10:26:12
I can see how you'd like to avoid the duplication
Wladimir Palant
2013/07/23 12:18:27
If you look closely, we aren't really swallowing e
Felix Dahlke
2013/07/23 12:40:15
What I meant was that we ignore the actual excepti
|
{ |
- Communication::OutputBuffer request; |
- request << Communication::PROC_ADD_FILTER << ToUtf8String(text); |
- |
try |
{ |
CallAdblockPlusEngineProcedure(request); |
@@ -370,37 +367,50 @@ |
} |
} |
+void CAdblockPlusClient::AddFilter(const std::wstring& text) |
+{ |
+ Communication::OutputBuffer request; |
+ request << Communication::PROC_ADD_FILTER << ToUtf8String(text); |
+ PostRequest(request); |
+} |
+ |
void CAdblockPlusClient::RemoveFilter(const std::wstring& text) |
{ |
Communication::OutputBuffer request; |
request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); |
- |
- try |
- { |
- CallAdblockPlusEngineProcedure(request); |
- } |
- catch (const std::exception& e) |
- { |
- DEBUG_GENERAL(e.what()); |
- } |
+ PostRequest(request); |
} |
void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& value) |
{ |
Communication::OutputBuffer request; |
request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String(value); |
- |
- try |
- { |
- CallAdblockPlusEngineProcedure(request); |
- } |
- catch (const std::exception& e) |
- { |
- DEBUG_GENERAL(e.what()); |
- } |
+ PostRequest(request); |
} |
-std::wstring CAdblockPlusClient::GetPref(const std::wstring& name) |
+void CAdblockPlusClient::SetPref(const std::string& name, const std::string& value) |
Wladimir Palant
2013/07/22 06:43:11
I don't think we need that function - the plugin a
|
+{ |
+ Communication::OutputBuffer request; |
+ request << Communication::PROC_SET_PREF << name << value; |
+ PostRequest(request); |
+} |
+ |
+ |
+void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value) |
+{ |
+ Communication::OutputBuffer request; |
+ request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; |
+ PostRequest(request); |
+} |
+ |
+void CAdblockPlusClient::SetPref(const std::wstring& name, bool value) |
+{ |
+ Communication::OutputBuffer request; |
+ request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; |
+ PostRequest(request); |
+} |
+ |
+Communication::InputBuffer CAdblockPlusClient::FetchResponse(const std::wstring& name) |
{ |
Communication::OutputBuffer request; |
request << Communication::PROC_GET_PREF << ToUtf8String(name); |
@@ -408,12 +418,64 @@ |
try |
{ |
Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request); |
- std::vector<std::wstring> retValue = ReadStrings(response); |
- return retValue.size() > 0 ? retValue.at(0) : L""; |
+ return response; |
} |
catch (const std::exception& e) |
{ |
DEBUG_GENERAL(e.what()); |
- return L""; |
} |
+ return Communication::InputBuffer(""); |
} |
+std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, LPCWSTR defaultValue) |
Wladimir Palant
2013/07/22 06:43:11
I guess you have that here because const char* is
|
+{ |
+ return GetPref(name, std::wstring(defaultValue)); |
+} |
+std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::wstring& defaultValue) |
+{ |
+ Communication::InputBuffer response = FetchResponse(name); |
+ if (response.IsEmpty()) |
+ return defaultValue; |
+ bool success; |
+ response >> success; |
+ if (success) |
+ { |
+ std::string value; |
+ response >> value; |
+ return ToUtf16String(value); |
+ } |
+ else |
+ return defaultValue; |
+} |
+ |
+bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue) |
+{ |
+ Communication::InputBuffer response = FetchResponse(name); |
+ if (response.IsEmpty()) |
+ return defaultValue; |
+ bool success; |
+ response >> success; |
+ if (success) |
+ { |
+ bool value; |
+ response >> value; |
+ return value; |
+ } |
+ else |
+ return defaultValue; |
+} |
+int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultValue) |
+{ |
+ Communication::InputBuffer response = FetchResponse(name); |
+ if (response.IsEmpty()) |
+ return defaultValue; |
+ bool success; |
+ response >> success; |
+ if (success) |
+ { |
+ int64_t value; |
+ response >> value; |
+ return value; |
+ } |
+ else |
+ return defaultValue; |
+} |