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: Refactoring CallAdblockPlusEngine Created July 24, 2013, 9:09 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
« no previous file with comments | « no previous file | src/plugin/PluginSettings.cpp » ('j') | src/plugin/PluginSettings.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 description.title = ToUtf16String(title); 96 description.title = ToUtf16String(title);
97 std::string specialization; 97 std::string specialization;
98 message >> specialization; 98 message >> specialization;
99 description.specialization = ToUtf16String(specialization); 99 description.specialization = ToUtf16String(specialization);
100 message >> description.listed; 100 message >> description.listed;
101 result.push_back(description); 101 result.push_back(description);
102 } 102 }
103 return result; 103 return result;
104 } 104 }
105 105
106 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::Outpu tBuffer& message) 106 bool CallEngine(Communication::OutputBuffer& message, Communication::InputBuff er* inputBuffer = NULL)
Felix Dahlke 2013/07/25 13:52:33 Since we always read the data anyway, I think we s
107 { 107 {
108 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe(); 108 try
109 pipe->WriteMessage(message); 109 {
110 return pipe->ReadMessage(); 110 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe();
111 pipe->WriteMessage(message);
112 if (inputBuffer != NULL)
Felix Dahlke 2013/07/25 13:52:33 if (inputBuffer) does the same.
113 {
114 *inputBuffer = pipe->ReadMessage();
115 }
116 else
117 {
118 pipe->ReadMessage();
119 }
120 }
121 catch (const std::exception& e)
122 {
123 DEBUG_GENERAL(e.what());
124 return false;
125 }
126 return true;
111 } 127 }
112 128
113 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::ProcT ype proc) 129 bool CallEngine(Communication::ProcType proc, Communication::InputBuffer* inpu tBuffer = NULL)
114 { 130 {
115 Communication::OutputBuffer message; 131 Communication::OutputBuffer message;
116 message << proc; 132 message << proc;
117 return CallAdblockPlusEngineProcedure(message); 133 return CallEngine(message, inputBuffer);
118 } 134 }
119 } 135 }
120 136
121 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 137 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
122 138
123 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 139 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
124 { 140 {
125 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 141 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
126 } 142 }
127 143
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } 221 }
206 m_criticalSectionFilter.Unlock(); 222 m_criticalSectionFilter.Unlock();
207 return isHidden; 223 return isHidden;
208 } 224 }
209 225
210 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 226 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
211 { 227 {
212 Communication::OutputBuffer request; 228 Communication::OutputBuffer request;
213 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url); 229 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url);
214 230
215 try 231 Communication::InputBuffer response;
216 { 232 if (!CallEngine(request, &response)) return false;;
Felix Dahlke 2013/07/25 13:52:33 One semicolon will do :)
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
217 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
218 233
219 bool isWhitelisted; 234 bool isWhitelisted;
220 response >> isWhitelisted; 235 response >> isWhitelisted;
221 return isWhitelisted; 236 return isWhitelisted;
222 }
223 catch (const std::exception& e)
224 {
225 DEBUG_GENERAL(e.what());
226 return false;
227 }
228 } 237 }
229 238
230 int CAdblockPlusClient::GetIEVersion() 239 int CAdblockPlusClient::GetIEVersion()
231 { 240 {
232 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 241 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
233 HKEY hKey; 242 HKEY hKey;
234 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey); 243 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey);
235 if (status != 0) 244 if (status != 0)
236 { 245 {
237 return 0; 246 return 0;
238 } 247 }
239 DWORD type, cbData; 248 DWORD type, cbData;
240 BYTE version[50]; 249 BYTE version[50];
241 cbData = 50; 250 cbData = 50;
242 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 251 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
243 if (status != 0) 252 if (status != 0)
244 { 253 {
245 return 0; 254 return 0;
246 } 255 }
247 RegCloseKey(hKey); 256 RegCloseKey(hKey);
248 return (int)(version[0] - 48); 257 return (int)(version[0] - 48);
249 } 258 }
250 259
251 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain) 260 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain)
252 { 261 {
253 Communication::OutputBuffer request; 262 Communication::OutputBuffer request;
254 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 263 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
255 264
256 try 265 Communication::InputBuffer response;
257 { 266 if (!CallEngine(request, &response)) return false;;
Felix Dahlke 2013/07/25 13:52:33 As above, one semicolon will do.
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
258 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
259 267
260 bool match; 268 bool match;
261 response >> match; 269 response >> match;
262 return match; 270 return match;
263 }
264 catch (const std::exception& e)
265 {
266 DEBUG_GENERAL(e.what());
267 return false;
268 }
269 } 271 }
270 272
271 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 273 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
272 { 274 {
273 Communication::OutputBuffer request; 275 Communication::OutputBuffer request;
274 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 276 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
275 277
276 try 278 Communication::InputBuffer response;
277 { 279 if (!CallEngine(request, &response)) return std::vector<std::wstring>();
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
278 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 280 return ReadStrings(response);
279 return ReadStrings(response);
280 }
281 catch (const std::exception& e)
282 {
283 DEBUG_GENERAL(e.what());
284 return std::vector<std::wstring>();
285 }
286 } 281 }
287 282
288 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 283 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
289 { 284 {
290 try 285 Communication::InputBuffer response;
291 { 286 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, &response)) retur n std::vector<SubscriptionDescription>();
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
292 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS); 287 return ReadSubscriptions(response);
293 return ReadSubscriptions(response);
294 }
295 catch (const std::exception& e)
296 {
297 DEBUG_GENERAL(e.what());
298 return std::vector<SubscriptionDescription>();
299 }
300 } 288 }
301 289
302 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 290 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
303 { 291 {
304 try 292 Communication::InputBuffer response;
305 { 293 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, &response)) return s td::vector<SubscriptionDescription>();
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
306 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS); 294 return ReadSubscriptions(response);
307 return ReadSubscriptions(response);
308 }
309 catch (const std::exception& e)
310 {
311 DEBUG_GENERAL(e.what());
312 return std::vector<SubscriptionDescription>();
313 }
314 } 295 }
315 296
316 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 297 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
317 { 298 {
318 Communication::OutputBuffer request; 299 Communication::OutputBuffer request;
319 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 300 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
320 301 CallEngine(request);
321 try
322 {
323 CallAdblockPlusEngineProcedure(request);
324 }
325 catch (const std::exception& e)
326 {
327 DEBUG_GENERAL(e.what());
328 }
329 } 302 }
330 303
331 void CAdblockPlusClient::UpdateAllSubscriptions() 304 void CAdblockPlusClient::UpdateAllSubscriptions()
332 { 305 {
333 try 306 CallEngine(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS);
334 {
335 CallAdblockPlusEngineProcedure(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS) ;
336 }
337 catch (const std::exception& e)
338 {
339 DEBUG_GENERAL(e.what());
340 }
341 } 307 }
342 308
343 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 309 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
344 { 310 {
345 try 311 Communication::InputBuffer response;
346 { 312 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS)) return std::vector <std::wstring>();
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
347 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS); 313 return ReadStrings(response);
348 return ReadStrings(response);
349 }
350 catch (const std::exception& e)
351 {
352 DEBUG_GENERAL(e.what());
353 return std::vector<std::wstring>();
354 }
355 }
356
357 // A helper method to reuse the code
358 void CAdblockPlusClient::PostRequest(Communication::OutputBuffer request)
359 {
360 try
361 {
362 CallAdblockPlusEngineProcedure(request);
363 }
364 catch (const std::exception& e)
365 {
366 DEBUG_GENERAL(e.what());
367 }
368 } 314 }
369 315
370 void CAdblockPlusClient::AddFilter(const std::wstring& text) 316 void CAdblockPlusClient::AddFilter(const std::wstring& text)
371 { 317 {
372 Communication::OutputBuffer request; 318 Communication::OutputBuffer request;
373 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 319 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
374 PostRequest(request); 320 CallEngine(request);
375 } 321 }
376 322
377 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 323 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
378 { 324 {
379 Communication::OutputBuffer request; 325 Communication::OutputBuffer request;
380 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 326 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
381 PostRequest(request); 327 CallEngine(request);
382 } 328 }
383 329
384 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue) 330 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
385 { 331 {
386 Communication::OutputBuffer request; 332 Communication::OutputBuffer request;
387 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value); 333 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
388 PostRequest(request); 334 CallEngine(request);
389 } 335 }
390 336
391 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value ) 337 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
392 { 338 {
393 Communication::OutputBuffer request; 339 Communication::OutputBuffer request;
394 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; 340 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
395 PostRequest(request); 341 CallEngine(request);
396 } 342 }
397 343
398 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value) 344 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
399 { 345 {
400 Communication::OutputBuffer request; 346 Communication::OutputBuffer request;
401 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; 347 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
402 PostRequest(request); 348 CallEngine(request);
403 } 349 }
404 350
405 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const wchar_t * defaultValue) 351 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const wchar_t * defaultValue)
406 { 352 {
407 return GetPref(name, std::wstring(defaultValue)); 353 return GetPref(name, std::wstring(defaultValue));
408 } 354 }
409 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue) 355 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue)
410 { 356 {
411 Communication::OutputBuffer request; 357 Communication::OutputBuffer request;
412 request << Communication::PROC_GET_PREF << ToUtf8String(name); 358 request << Communication::PROC_GET_PREF << ToUtf8String(name);
413 359
414 try 360 Communication::InputBuffer response;
361 if (!CallEngine(request, &response)) return defaultValue;
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
362 bool success;
363 response >> success;
364 if (success)
415 { 365 {
416 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 366 std::string value;
417 bool success; 367 response >> value;
418 response >> success; 368 return ToUtf16String(value);
419 if (success)
420 {
421 std::string value;
422 response >> value;
423 return ToUtf16String(value);
424 }
425 else
426 return defaultValue;
427 } 369 }
428 catch (const std::exception& e) 370 else
429 {
430 DEBUG_GENERAL(e.what());
431 return defaultValue; 371 return defaultValue;
432 }
433 } 372 }
434 373
435 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue) 374 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
436 { 375 {
437 Communication::OutputBuffer request; 376 Communication::OutputBuffer request;
438 request << Communication::PROC_GET_PREF << ToUtf8String(name); 377 request << Communication::PROC_GET_PREF << ToUtf8String(name);
439 378
440 try 379 Communication::InputBuffer response;
380 if (!CallEngine(request, &response)) return defaultValue;
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
381 bool success;
382 response >> success;
383 if (success)
441 { 384 {
442 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 385 bool value;
443 bool success; 386 response >> value;
444 response >> success; 387 return value;
445 if (success)
446 {
447 bool value;
448 response >> value;
449 return value;
450 }
451 else
452 return defaultValue;
453 } 388 }
454 catch (const std::exception& e) 389 else
455 {
456 DEBUG_GENERAL(e.what());
457 return defaultValue; 390 return defaultValue;
458 }
459 } 391 }
460 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue) 392 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
461 { 393 {
462 Communication::OutputBuffer request; 394 Communication::OutputBuffer request;
463 request << Communication::PROC_GET_PREF << ToUtf8String(name); 395 request << Communication::PROC_GET_PREF << ToUtf8String(name);
464 396
465 try 397 Communication::InputBuffer response;
398 if (!CallEngine(request, &response)) return defaultValue;
Wladimir Palant 2013/07/26 16:45:37 Style nit: please put the return statement on the
399 bool success;
Wladimir Palant 2013/07/26 16:45:37 Wrong indentation?
400 response >> success;
401 if (success)
466 { 402 {
467 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 403 int64_t value;
468 bool success; 404 response >> value;
469 response >> success; 405 return value;
470 if (success)
471 {
472 int64_t value;
473 response >> value;
474 return value;
475 }
476 else
477 return defaultValue;
478 } 406 }
479 catch (const std::exception& e) 407 else
480 {
481 DEBUG_GENERAL(e.what());
482 return defaultValue; 408 return defaultValue;
483 }
484 } 409 }
OLDNEW
« no previous file with comments | « no previous file | src/plugin/PluginSettings.cpp » ('j') | src/plugin/PluginSettings.cpp » ('J')

Powered by Google App Engine
This is Rietveld