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

Side by Side Diff: src/plugin/AdblockPlusClient.cpp

Issue 11013110: Cleanup (Closed)
Patch Set: Minor CallEngine refactoring Created July 26, 2013, 2:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include "PluginSettings.h" 3 #include "PluginSettings.h"
4 #include "PluginSystem.h" 4 #include "PluginSystem.h"
5 #include "PluginFilter.h" 5 #include "PluginFilter.h"
6 #include "PluginClientFactory.h" 6 #include "PluginClientFactory.h"
7 #include "PluginHttpRequest.h"
8 #include "PluginMutex.h" 7 #include "PluginMutex.h"
9 #include "PluginClass.h" 8 #include "PluginClass.h"
10 9
11 #include "AdblockPlusClient.h" 10 #include "AdblockPlusClient.h"
12 11
13 #include "../shared/Communication.h"
14 #include "../shared/Utils.h" 12 #include "../shared/Utils.h"
15 13
16 namespace 14 namespace
17 { 15 {
18 void SpawnAdblockPlusEngine() 16 void SpawnAdblockPlusEngine()
19 { 17 {
20 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe"; 18 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe";
21 CString params = L"AdblockPlusEngine.exe " + CPluginSystem::GetInstance()->G etBrowserLanguage(); 19 CString params = L"AdblockPlusEngine.exe " + CPluginSystem::GetInstance()->G etBrowserLanguage();
22 20
23 STARTUPINFO startupInfo = {}; 21 STARTUPINFO startupInfo = {};
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 description.title = ToUtf16String(title); 96 description.title = ToUtf16String(title);
99 std::string specialization; 97 std::string specialization;
100 message >> specialization; 98 message >> specialization;
101 description.specialization = ToUtf16String(specialization); 99 description.specialization = ToUtf16String(specialization);
102 message >> description.listed; 100 message >> description.listed;
103 result.push_back(description); 101 result.push_back(description);
104 } 102 }
105 return result; 103 return result;
106 } 104 }
107 105
108 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::Outpu tBuffer& message) 106 bool CallEngine(Communication::OutputBuffer& message, Communication::InputBuff er& inputBuffer = Communication::InputBuffer())
109 { 107 {
110 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe(); 108 try
111 pipe->WriteMessage(message); 109 {
112 return pipe->ReadMessage(); 110 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe();
111 pipe->WriteMessage(message);
112 inputBuffer = pipe->ReadMessage();
113 }
114 catch (const std::exception& e)
115 {
116 DEBUG_GENERAL(e.what());
117 return false;
118 }
119 return true;
113 } 120 }
114 121
115 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::ProcT ype proc) 122 bool CallEngine(Communication::ProcType proc, Communication::InputBuffer& inpu tBuffer = Communication::InputBuffer())
116 { 123 {
117 Communication::OutputBuffer message; 124 Communication::OutputBuffer message;
118 message << proc; 125 message << proc;
119 return CallAdblockPlusEngineProcedure(message); 126 return CallEngine(message, inputBuffer);
120 } 127 }
121 } 128 }
122 129
123 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 130 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
124 131
125 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 132 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
126 { 133 {
127 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 134 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
128 } 135 }
129 136
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 214 }
208 m_criticalSectionFilter.Unlock(); 215 m_criticalSectionFilter.Unlock();
209 return isHidden; 216 return isHidden;
210 } 217 }
211 218
212 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 219 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
213 { 220 {
214 Communication::OutputBuffer request; 221 Communication::OutputBuffer request;
215 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url); 222 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url);
216 223
217 try 224 Communication::InputBuffer response;
218 { 225 if (!CallEngine(request, response)) return false;
219 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
220 226
221 bool isWhitelisted; 227 bool isWhitelisted;
222 response >> isWhitelisted; 228 response >> isWhitelisted;
223 return isWhitelisted; 229 return isWhitelisted;
224 }
225 catch (const std::exception& e)
226 {
227 DEBUG_GENERAL(e.what());
228 return false;
229 }
230 } 230 }
231 231
232 int CAdblockPlusClient::GetIEVersion() 232 int CAdblockPlusClient::GetIEVersion()
233 { 233 {
234 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 234 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
235 HKEY hKey; 235 HKEY hKey;
236 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey); 236 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey);
237 if (status != 0) 237 if (status != 0)
238 { 238 {
239 return 0; 239 return 0;
240 } 240 }
241 DWORD type, cbData; 241 DWORD type, cbData;
242 BYTE version[50]; 242 BYTE version[50];
243 cbData = 50; 243 cbData = 50;
244 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 244 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
245 if (status != 0) 245 if (status != 0)
246 { 246 {
247 return 0; 247 return 0;
248 } 248 }
249 RegCloseKey(hKey); 249 RegCloseKey(hKey);
250 return (int)(version[0] - 48); 250 return (int)(version[0] - 48);
251 } 251 }
252 252
253 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain) 253 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain)
254 { 254 {
255 Communication::OutputBuffer request; 255 Communication::OutputBuffer request;
256 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 256 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
257 257
258 try 258 Communication::InputBuffer response;
259 { 259 if (!CallEngine(request, response)) return false;
260 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
261 260
262 bool match; 261 bool match;
263 response >> match; 262 response >> match;
264 return match; 263 return match;
265 }
266 catch (const std::exception& e)
267 {
268 DEBUG_GENERAL(e.what());
269 return false;
270 }
271 } 264 }
272 265
273 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 266 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
274 { 267 {
275 Communication::OutputBuffer request; 268 Communication::OutputBuffer request;
276 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 269 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
277 270
278 try 271 Communication::InputBuffer response;
279 { 272 if (!CallEngine(request, response)) return std::vector<std::wstring>();
280 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 273 return ReadStrings(response);
281 return ReadStrings(response);
282 }
283 catch (const std::exception& e)
284 {
285 DEBUG_GENERAL(e.what());
286 return std::vector<std::wstring>();
287 }
288 } 274 }
289 275
290 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 276 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
291 { 277 {
292 try 278 Communication::InputBuffer response;
293 { 279 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, response)) return std::vector<SubscriptionDescription>();
294 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS); 280 return ReadSubscriptions(response);
295 return ReadSubscriptions(response);
296 }
297 catch (const std::exception& e)
298 {
299 DEBUG_GENERAL(e.what());
300 return std::vector<SubscriptionDescription>();
301 }
302 } 281 }
303 282
304 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 283 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
305 { 284 {
306 try 285 Communication::InputBuffer response;
307 { 286 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, response)) return st d::vector<SubscriptionDescription>();
308 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS); 287 return ReadSubscriptions(response);
309 return ReadSubscriptions(response);
310 }
311 catch (const std::exception& e)
312 {
313 DEBUG_GENERAL(e.what());
314 return std::vector<SubscriptionDescription>();
315 }
316 } 288 }
317 289
318 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 290 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
319 { 291 {
320 Communication::OutputBuffer request; 292 Communication::OutputBuffer request;
321 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 293 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
322 294 CallEngine(request);
323 try
324 {
325 CallAdblockPlusEngineProcedure(request);
326 }
327 catch (const std::exception& e)
328 {
329 DEBUG_GENERAL(e.what());
330 }
331 } 295 }
332 296
333 void CAdblockPlusClient::UpdateAllSubscriptions() 297 void CAdblockPlusClient::UpdateAllSubscriptions()
334 { 298 {
335 try 299 CallEngine(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS);
336 {
337 CallAdblockPlusEngineProcedure(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS) ;
338 }
339 catch (const std::exception& e)
340 {
341 DEBUG_GENERAL(e.what());
342 }
343 } 300 }
344 301
345 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 302 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
346 { 303 {
347 try 304 Communication::InputBuffer response;
348 { 305 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS)) return std::vector <std::wstring>();
349 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS); 306 return ReadStrings(response);
350 return ReadStrings(response);
351 }
352 catch (const std::exception& e)
353 {
354 DEBUG_GENERAL(e.what());
355 return std::vector<std::wstring>();
356 }
357 } 307 }
358 308
359 void CAdblockPlusClient::AddFilter(const std::wstring& text) 309 void CAdblockPlusClient::AddFilter(const std::wstring& text)
360 { 310 {
361 Communication::OutputBuffer request; 311 Communication::OutputBuffer request;
362 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 312 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
363 313 CallEngine(request);
364 try
365 {
366 CallAdblockPlusEngineProcedure(request);
367 }
368 catch (const std::exception& e)
369 {
370 DEBUG_GENERAL(e.what());
371 }
372 } 314 }
373 315
374 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 316 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
375 { 317 {
376 Communication::OutputBuffer request; 318 Communication::OutputBuffer request;
377 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 319 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
320 CallEngine(request);
321 }
378 322
379 try 323 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
324 {
325 Communication::OutputBuffer request;
326 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
327 CallEngine(request);
328 }
329
330 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
331 {
332 Communication::OutputBuffer request;
333 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
334 CallEngine(request);
335 }
336
337 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
338 {
339 Communication::OutputBuffer request;
340 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
341 CallEngine(request);
342 }
343
344 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const wchar_t * defaultValue)
345 {
346 return GetPref(name, std::wstring(defaultValue));
347 }
348 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue)
349 {
350 Communication::OutputBuffer request;
351 request << Communication::PROC_GET_PREF << ToUtf8String(name);
352
353 Communication::InputBuffer response;
354 if (!CallEngine(request, response)) return defaultValue;
355 bool success;
356 response >> success;
357 if (success)
380 { 358 {
381 CallAdblockPlusEngineProcedure(request); 359 std::string value;
360 response >> value;
361 return ToUtf16String(value);
382 } 362 }
383 catch (const std::exception& e) 363 else
364 return defaultValue;
365 }
366
367 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
368 {
369 Communication::OutputBuffer request;
370 request << Communication::PROC_GET_PREF << ToUtf8String(name);
371
372 Communication::InputBuffer response;
373 if (!CallEngine(request, response)) return defaultValue;
374 bool success;
375 response >> success;
376 if (success)
384 { 377 {
385 DEBUG_GENERAL(e.what()); 378 bool value;
379 response >> value;
380 return value;
386 } 381 }
382 else
383 return defaultValue;
387 } 384 }
385 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
386 {
387 Communication::OutputBuffer request;
388 request << Communication::PROC_GET_PREF << ToUtf8String(name);
389
390 Communication::InputBuffer response;
391 if (!CallEngine(request, response)) return defaultValue;
392 bool success;
393 response >> success;
394 if (success)
395 {
396 int64_t value;
397 response >> value;
398 return value;
399 }
400 else
401 return defaultValue;
402 }
OLDNEW

Powered by Google App Engine
This is Rietveld