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: SetPref/GetPref type safety. Comments addressed. Created July 22, 2013, 12:42 a.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 "PluginMutex.h" 7 #include "PluginMutex.h"
8 #include "PluginClass.h" 8 #include "PluginClass.h"
9 9
10 #include "AdblockPlusClient.h" 10 #include "AdblockPlusClient.h"
11 11
12 #include "../shared/Communication.h"
13 #include "../shared/Utils.h" 12 #include "../shared/Utils.h"
14 13
15 namespace 14 namespace
16 { 15 {
17 void SpawnAdblockPlusEngine() 16 void SpawnAdblockPlusEngine()
18 { 17 {
19 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe"; 18 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe";
20 CString params = L"AdblockPlusEngine.exe " + CPluginSystem::GetInstance()->G etBrowserLanguage(); 19 CString params = L"AdblockPlusEngine.exe " + CPluginSystem::GetInstance()->G etBrowserLanguage();
21 20
22 STARTUPINFO startupInfo = {}; 21 STARTUPINFO startupInfo = {};
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS); 347 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS);
349 return ReadStrings(response); 348 return ReadStrings(response);
350 } 349 }
351 catch (const std::exception& e) 350 catch (const std::exception& e)
352 { 351 {
353 DEBUG_GENERAL(e.what()); 352 DEBUG_GENERAL(e.what());
354 return std::vector<std::wstring>(); 353 return std::vector<std::wstring>();
355 } 354 }
356 } 355 }
357 356
358 void CAdblockPlusClient::AddFilter(const std::wstring& text) 357 // A helper method to reuse the code
358 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
359 { 359 {
360 Communication::OutputBuffer request;
361 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
362
363 try 360 try
364 { 361 {
365 CallAdblockPlusEngineProcedure(request); 362 CallAdblockPlusEngineProcedure(request);
366 } 363 }
367 catch (const std::exception& e) 364 catch (const std::exception& e)
368 { 365 {
369 DEBUG_GENERAL(e.what()); 366 DEBUG_GENERAL(e.what());
370 } 367 }
371 } 368 }
372 369
370 void CAdblockPlusClient::AddFilter(const std::wstring& text)
371 {
372 Communication::OutputBuffer request;
373 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
374 PostRequest(request);
375 }
376
373 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 377 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
374 { 378 {
375 Communication::OutputBuffer request; 379 Communication::OutputBuffer request;
376 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 380 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
377 381 PostRequest(request);
378 try
379 {
380 CallAdblockPlusEngineProcedure(request);
381 }
382 catch (const std::exception& e)
383 {
384 DEBUG_GENERAL(e.what());
385 }
386 } 382 }
387 383
388 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue) 384 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
389 { 385 {
390 Communication::OutputBuffer request; 386 Communication::OutputBuffer request;
391 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value); 387 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
392 388 PostRequest(request);
393 try
394 {
395 CallAdblockPlusEngineProcedure(request);
396 }
397 catch (const std::exception& e)
398 {
399 DEBUG_GENERAL(e.what());
400 }
401 } 389 }
402 390
403 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name) 391 void CAdblockPlusClient::SetPref(const std::string& name, const std::string& val ue)
Wladimir Palant 2013/07/22 06:43:11 I don't think we need that function - the plugin a
392 {
393 Communication::OutputBuffer request;
394 request << Communication::PROC_SET_PREF << name << value;
395 PostRequest(request);
396 }
397
398
399 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
400 {
401 Communication::OutputBuffer request;
402 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
403 PostRequest(request);
404 }
405
406 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
407 {
408 Communication::OutputBuffer request;
409 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
410 PostRequest(request);
411 }
412
413 Communication::InputBuffer CAdblockPlusClient::FetchResponse(const std::wstring& name)
404 { 414 {
405 Communication::OutputBuffer request; 415 Communication::OutputBuffer request;
406 request << Communication::PROC_GET_PREF << ToUtf8String(name); 416 request << Communication::PROC_GET_PREF << ToUtf8String(name);
407 417
408 try 418 try
409 { 419 {
410 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 420 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
411 std::vector<std::wstring> retValue = ReadStrings(response); 421 return response;
412 return retValue.size() > 0 ? retValue.at(0) : L"";
413 } 422 }
414 catch (const std::exception& e) 423 catch (const std::exception& e)
415 { 424 {
416 DEBUG_GENERAL(e.what()); 425 DEBUG_GENERAL(e.what());
417 return L"";
418 } 426 }
427 return Communication::InputBuffer("");
419 } 428 }
429 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, LPCWSTR defau ltValue)
Wladimir Palant 2013/07/22 06:43:11 I guess you have that here because const char* is
430 {
431 return GetPref(name, std::wstring(defaultValue));
432 }
433 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue)
434 {
435 Communication::InputBuffer response = FetchResponse(name);
436 if (response.IsEmpty())
437 return defaultValue;
438 bool success;
439 response >> success;
440 if (success)
441 {
442 std::string value;
443 response >> value;
444 return ToUtf16String(value);
445 }
446 else
447 return defaultValue;
448 }
449
450 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
451 {
452 Communication::InputBuffer response = FetchResponse(name);
453 if (response.IsEmpty())
454 return defaultValue;
455 bool success;
456 response >> success;
457 if (success)
458 {
459 bool value;
460 response >> value;
461 return value;
462 }
463 else
464 return defaultValue;
465 }
466 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
467 {
468 Communication::InputBuffer response = FetchResponse(name);
469 if (response.IsEmpty())
470 return defaultValue;
471 bool success;
472 response >> success;
473 if (success)
474 {
475 int64_t value;
476 response >> value;
477 return value;
478 }
479 else
480 return defaultValue;
481 }
OLDNEW

Powered by Google App Engine
This is Rietveld