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

Delta Between Two Patch Sets: src/plugin/AdblockPlusClient.cpp

Issue 11013110: Cleanup (Closed)
Left Patch Set: SetPref/GetPref type safety. Comments addressed. Created July 22, 2013, 12:42 a.m.
Right Patch Set: More beautification and addressing comments Created July 29, 2013, 12:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/plugin/AdblockPlusClient.h ('k') | src/plugin/AdblockPlusTab.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 = Communication::InputBuffer())
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 } 111 pipe->WriteMessage(message);
112 112 inputBuffer = pipe->ReadMessage();
113 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::ProcT ype proc) 113 }
114 catch (const std::exception& e)
115 {
116 DEBUG_GENERAL(e.what());
117 return false;
118 }
119 return true;
120 }
121
122 bool CallEngine(Communication::ProcType proc, Communication::InputBuffer& inpu tBuffer = Communication::InputBuffer())
114 { 123 {
115 Communication::OutputBuffer message; 124 Communication::OutputBuffer message;
116 message << proc; 125 message << proc;
117 return CallAdblockPlusEngineProcedure(message); 126 return CallEngine(message, inputBuffer);
118 } 127 }
119 } 128 }
120 129
121 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 130 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
122 131
123 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 132 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
124 { 133 {
125 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 134 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
126 } 135 }
127 136
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } 214 }
206 m_criticalSectionFilter.Unlock(); 215 m_criticalSectionFilter.Unlock();
207 return isHidden; 216 return isHidden;
208 } 217 }
209 218
210 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 219 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
211 { 220 {
212 Communication::OutputBuffer request; 221 Communication::OutputBuffer request;
213 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url); 222 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url);
214 223
215 try 224 Communication::InputBuffer response;
216 { 225 if (!CallEngine(request, response))
217 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
218
219 bool isWhitelisted;
220 response >> isWhitelisted;
221 return isWhitelisted;
222 }
223 catch (const std::exception& e)
224 {
225 DEBUG_GENERAL(e.what());
226 return false; 226 return false;
227 } 227
228 bool isWhitelisted;
229 response >> isWhitelisted;
230 return isWhitelisted;
228 } 231 }
229 232
230 int CAdblockPlusClient::GetIEVersion() 233 int CAdblockPlusClient::GetIEVersion()
231 { 234 {
232 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 235 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
233 HKEY hKey; 236 HKEY hKey;
234 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey); 237 LSTATUS status = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Interne t Explorer", &hKey);
235 if (status != 0) 238 if (status != 0)
236 { 239 {
237 return 0; 240 return 0;
238 } 241 }
239 DWORD type, cbData; 242 DWORD type, cbData;
240 BYTE version[50]; 243 BYTE version[50];
241 cbData = 50; 244 cbData = 50;
242 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 245 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
243 if (status != 0) 246 if (status != 0)
244 { 247 {
245 return 0; 248 return 0;
246 } 249 }
247 RegCloseKey(hKey); 250 RegCloseKey(hKey);
248 return (int)(version[0] - 48); 251 return (int)(version[0] - 48);
249 } 252 }
250 253
251 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain) 254 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain)
252 { 255 {
253 Communication::OutputBuffer request; 256 Communication::OutputBuffer request;
254 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 257 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
255 258
256 try 259 Communication::InputBuffer response;
257 { 260 if (!CallEngine(request, response))
258 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
259
260 bool match;
261 response >> match;
262 return match;
263 }
264 catch (const std::exception& e)
265 {
266 DEBUG_GENERAL(e.what());
267 return false; 261 return false;
268 } 262
263 bool match;
264 response >> match;
265 return match;
269 } 266 }
270 267
271 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 268 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
272 { 269 {
273 Communication::OutputBuffer request; 270 Communication::OutputBuffer request;
274 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 271 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
275 272
276 try 273 Communication::InputBuffer response;
277 { 274 if (!CallEngine(request, response))
278 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
279 return ReadStrings(response);
280 }
281 catch (const std::exception& e)
282 {
283 DEBUG_GENERAL(e.what());
284 return std::vector<std::wstring>(); 275 return std::vector<std::wstring>();
285 } 276 return ReadStrings(response);
286 } 277 }
287 278
288 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 279 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
289 { 280 {
290 try 281 Communication::InputBuffer response;
291 { 282 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, response))
292 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS);
293 return ReadSubscriptions(response);
294 }
295 catch (const std::exception& e)
296 {
297 DEBUG_GENERAL(e.what());
298 return std::vector<SubscriptionDescription>(); 283 return std::vector<SubscriptionDescription>();
299 } 284 return ReadSubscriptions(response);
300 } 285 }
301 286
302 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 287 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
303 { 288 {
304 try 289 Communication::InputBuffer response;
305 { 290 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, response))
306 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS);
307 return ReadSubscriptions(response);
308 }
309 catch (const std::exception& e)
310 {
311 DEBUG_GENERAL(e.what());
312 return std::vector<SubscriptionDescription>(); 291 return std::vector<SubscriptionDescription>();
313 } 292 return ReadSubscriptions(response);
314 } 293 }
315 294
316 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 295 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
317 { 296 {
318 Communication::OutputBuffer request; 297 Communication::OutputBuffer request;
319 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 298 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
320 299 CallEngine(request);
321 try
322 {
323 CallAdblockPlusEngineProcedure(request);
324 }
325 catch (const std::exception& e)
326 {
327 DEBUG_GENERAL(e.what());
328 }
329 } 300 }
330 301
331 void CAdblockPlusClient::UpdateAllSubscriptions() 302 void CAdblockPlusClient::UpdateAllSubscriptions()
332 { 303 {
333 try 304 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 } 305 }
342 306
343 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 307 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
344 { 308 {
345 try 309 Communication::InputBuffer response;
346 { 310 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS))
347 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS);
348 return ReadStrings(response);
349 }
350 catch (const std::exception& e)
351 {
352 DEBUG_GENERAL(e.what());
353 return std::vector<std::wstring>(); 311 return std::vector<std::wstring>();
354 } 312 return ReadStrings(response);
355 }
356
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 {
360 try
361 {
362 CallAdblockPlusEngineProcedure(request);
363 }
364 catch (const std::exception& e)
365 {
366 DEBUG_GENERAL(e.what());
367 }
368 } 313 }
369 314
370 void CAdblockPlusClient::AddFilter(const std::wstring& text) 315 void CAdblockPlusClient::AddFilter(const std::wstring& text)
371 { 316 {
372 Communication::OutputBuffer request; 317 Communication::OutputBuffer request;
373 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 318 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
374 PostRequest(request); 319 CallEngine(request);
375 } 320 }
376 321
377 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 322 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
378 { 323 {
379 Communication::OutputBuffer request; 324 Communication::OutputBuffer request;
380 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 325 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
381 PostRequest(request); 326 CallEngine(request);
382 } 327 }
383 328
384 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue) 329 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
385 { 330 {
386 Communication::OutputBuffer request; 331 Communication::OutputBuffer request;
387 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value); 332 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
388 PostRequest(request); 333 CallEngine(request);
389 } 334 }
390
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 335
399 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value ) 336 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
400 { 337 {
401 Communication::OutputBuffer request; 338 Communication::OutputBuffer request;
402 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; 339 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
403 PostRequest(request); 340 CallEngine(request);
404 } 341 }
405 342
406 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value) 343 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
407 { 344 {
408 Communication::OutputBuffer request; 345 Communication::OutputBuffer request;
409 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value; 346 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
410 PostRequest(request); 347 CallEngine(request);
411 } 348 }
412 349
413 Communication::InputBuffer CAdblockPlusClient::FetchResponse(const std::wstring& name) 350 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const wchar_t * defaultValue)
351 {
352 return GetPref(name, std::wstring(defaultValue));
353 }
354 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue)
414 { 355 {
415 Communication::OutputBuffer request; 356 Communication::OutputBuffer request;
416 request << Communication::PROC_GET_PREF << ToUtf8String(name); 357 request << Communication::PROC_GET_PREF << ToUtf8String(name);
417 358
418 try 359 Communication::InputBuffer response;
419 { 360 if (!CallEngine(request, response))
420 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
421 return response;
422 }
423 catch (const std::exception& e)
424 {
425 DEBUG_GENERAL(e.what());
426 }
427 return Communication::InputBuffer("");
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; 361 return defaultValue;
438 bool success; 362 bool success;
439 response >> success; 363 response >> success;
440 if (success) 364 if (success)
441 { 365 {
442 std::string value; 366 std::string value;
443 response >> value; 367 response >> value;
444 return ToUtf16String(value); 368 return ToUtf16String(value);
445 } 369 }
446 else 370 else
447 return defaultValue; 371 return defaultValue;
448 } 372 }
449 373
450 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue) 374 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
451 { 375 {
452 Communication::InputBuffer response = FetchResponse(name); 376 Communication::OutputBuffer request;
453 if (response.IsEmpty()) 377 request << Communication::PROC_GET_PREF << ToUtf8String(name);
378
379 Communication::InputBuffer response;
380 if (!CallEngine(request, response))
454 return defaultValue; 381 return defaultValue;
455 bool success; 382 bool success;
456 response >> success; 383 response >> success;
457 if (success) 384 if (success)
458 { 385 {
459 bool value; 386 bool value;
460 response >> value; 387 response >> value;
461 return value; 388 return value;
462 } 389 }
463 else 390 else
464 return defaultValue; 391 return defaultValue;
465 } 392 }
466 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue) 393 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
467 { 394 {
468 Communication::InputBuffer response = FetchResponse(name); 395 Communication::OutputBuffer request;
469 if (response.IsEmpty()) 396 request << Communication::PROC_GET_PREF << ToUtf8String(name);
397
398 Communication::InputBuffer response;
399 if (!CallEngine(request, response))
470 return defaultValue; 400 return defaultValue;
471 bool success; 401 bool success;
472 response >> success; 402 response >> success;
473 if (success) 403 if (success)
474 { 404 {
475 int64_t value; 405 int64_t value;
476 response >> value; 406 response >> value;
477 return value; 407 return value;
478 } 408 }
479 else 409 else
480 return defaultValue; 410 return defaultValue;
481 } 411 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld