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: More refactoring. Removing main thread, tab counting. Implementing SetPref and GetPref. Addressing … Created July 9, 2013, 12:59 p.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"
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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 description.title = ToUtf16String(title); 96 description.title = ToUtf16String(title);
98 std::string specialization; 97 std::string specialization;
99 message >> specialization; 98 message >> specialization;
100 description.specialization = ToUtf16String(specialization); 99 description.specialization = ToUtf16String(specialization);
101 message >> description.listed; 100 message >> description.listed;
102 result.push_back(description); 101 result.push_back(description);
103 } 102 }
104 return result; 103 return result;
105 } 104 }
106 105
107 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::Outpu tBuffer& message) 106 bool CallEngine(Communication::OutputBuffer& message, Communication::InputBuff er& inputBuffer = Communication::InputBuffer())
108 { 107 {
109 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe(); 108 try
110 pipe->WriteMessage(message); 109 {
111 return pipe->ReadMessage(); 110 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe();
112 } 111 pipe->WriteMessage(message);
113 112 inputBuffer = pipe->ReadMessage();
114 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())
115 { 123 {
116 Communication::OutputBuffer message; 124 Communication::OutputBuffer message;
117 message << proc; 125 message << proc;
118 return CallAdblockPlusEngineProcedure(message); 126 return CallEngine(message, inputBuffer);
119 } 127 }
120 } 128 }
121 129
122 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 130 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
123 131
124 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 132 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
125 { 133 {
126 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 134 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
127 } 135 }
128 136
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 214 }
207 m_criticalSectionFilter.Unlock(); 215 m_criticalSectionFilter.Unlock();
208 return isHidden; 216 return isHidden;
209 } 217 }
210 218
211 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 219 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
212 { 220 {
213 Communication::OutputBuffer request; 221 Communication::OutputBuffer request;
214 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url); 222 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url);
215 223
216 try 224 Communication::InputBuffer response;
217 { 225 if (!CallEngine(request, response))
218 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
219
220 bool isWhitelisted;
221 response >> isWhitelisted;
222 return isWhitelisted;
223 }
224 catch (const std::exception& e)
225 {
226 DEBUG_GENERAL(e.what());
227 return false; 226 return false;
228 } 227
228 bool isWhitelisted;
229 response >> isWhitelisted;
230 return isWhitelisted;
229 } 231 }
230 232
231 int CAdblockPlusClient::GetIEVersion() 233 int CAdblockPlusClient::GetIEVersion()
232 { 234 {
233 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 235 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
234 HKEY hKey; 236 HKEY hKey;
235 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);
236 if (status != 0) 238 if (status != 0)
237 { 239 {
238 return 0; 240 return 0;
239 } 241 }
240 DWORD type, cbData; 242 DWORD type, cbData;
241 BYTE version[50]; 243 BYTE version[50];
242 cbData = 50; 244 cbData = 50;
243 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 245 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
244 if (status != 0) 246 if (status != 0)
245 { 247 {
246 return 0; 248 return 0;
247 } 249 }
248 RegCloseKey(hKey); 250 RegCloseKey(hKey);
249 return (int)(version[0] - 48); 251 return (int)(version[0] - 48);
250 } 252 }
251 253
252 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)
253 { 255 {
254 Communication::OutputBuffer request; 256 Communication::OutputBuffer request;
255 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 257 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
256 258
257 try 259 Communication::InputBuffer response;
258 { 260 if (!CallEngine(request, response))
259 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
260
261 bool match;
262 response >> match;
263 return match;
264 }
265 catch (const std::exception& e)
266 {
267 DEBUG_GENERAL(e.what());
268 return false; 261 return false;
269 } 262
263 bool match;
264 response >> match;
265 return match;
270 } 266 }
271 267
272 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 268 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
273 { 269 {
274 Communication::OutputBuffer request; 270 Communication::OutputBuffer request;
275 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 271 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
276 272
277 try 273 Communication::InputBuffer response;
278 { 274 if (!CallEngine(request, response))
279 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
280 return ReadStrings(response);
281 }
282 catch (const std::exception& e)
283 {
284 DEBUG_GENERAL(e.what());
285 return std::vector<std::wstring>(); 275 return std::vector<std::wstring>();
286 } 276 return ReadStrings(response);
287 } 277 }
288 278
289 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 279 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
290 { 280 {
291 try 281 Communication::InputBuffer response;
292 { 282 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, response))
293 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS);
294 return ReadSubscriptions(response);
295 }
296 catch (const std::exception& e)
297 {
298 DEBUG_GENERAL(e.what());
299 return std::vector<SubscriptionDescription>(); 283 return std::vector<SubscriptionDescription>();
300 } 284 return ReadSubscriptions(response);
301 } 285 }
302 286
303 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 287 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
304 { 288 {
305 try 289 Communication::InputBuffer response;
306 { 290 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, response))
307 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS);
308 return ReadSubscriptions(response);
309 }
310 catch (const std::exception& e)
311 {
312 DEBUG_GENERAL(e.what());
313 return std::vector<SubscriptionDescription>(); 291 return std::vector<SubscriptionDescription>();
314 } 292 return ReadSubscriptions(response);
315 } 293 }
316 294
317 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 295 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
318 { 296 {
319 Communication::OutputBuffer request; 297 Communication::OutputBuffer request;
320 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 298 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
321 299 CallEngine(request);
322 try
323 {
324 CallAdblockPlusEngineProcedure(request);
325 }
326 catch (const std::exception& e)
327 {
328 DEBUG_GENERAL(e.what());
329 }
330 } 300 }
331 301
332 void CAdblockPlusClient::UpdateAllSubscriptions() 302 void CAdblockPlusClient::UpdateAllSubscriptions()
333 { 303 {
334 try 304 CallEngine(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS);
335 {
336 CallAdblockPlusEngineProcedure(Communication::PROC_UPDATE_ALL_SUBSCRIPTIONS) ;
337 }
338 catch (const std::exception& e)
339 {
340 DEBUG_GENERAL(e.what());
341 }
342 } 305 }
343 306
344 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 307 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
345 { 308 {
346 try 309 Communication::InputBuffer response;
347 { 310 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS))
348 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS);
349 return ReadStrings(response);
350 }
351 catch (const std::exception& e)
352 {
353 DEBUG_GENERAL(e.what());
354 return std::vector<std::wstring>(); 311 return std::vector<std::wstring>();
355 } 312 return ReadStrings(response);
356 } 313 }
357 314
358 void CAdblockPlusClient::AddFilter(const std::wstring& text) 315 void CAdblockPlusClient::AddFilter(const std::wstring& text)
359 { 316 {
360 Communication::OutputBuffer request; 317 Communication::OutputBuffer request;
361 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 318 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
362 319 CallEngine(request);
363 try
364 {
365 CallAdblockPlusEngineProcedure(request);
366 }
367 catch (const std::exception& e)
368 {
369 DEBUG_GENERAL(e.what());
370 }
371 } 320 }
372 321
373 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 322 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
374 { 323 {
375 Communication::OutputBuffer request; 324 Communication::OutputBuffer request;
376 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 325 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
377 326 CallEngine(request);
378 try
379 {
380 CallAdblockPlusEngineProcedure(request);
381 }
382 catch (const std::exception& e)
383 {
384 DEBUG_GENERAL(e.what());
385 }
386 } 327 }
387 328
388 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)
389 { 330 {
390 Communication::OutputBuffer request; 331 Communication::OutputBuffer request;
391 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value); 332 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
392 333 CallEngine(request);
393 try 334 }
394 { 335
395 CallAdblockPlusEngineProcedure(request); 336 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
396 } 337 {
397 catch (const std::exception& e) 338 Communication::OutputBuffer request;
398 { 339 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
399 DEBUG_GENERAL(e.what()); 340 CallEngine(request);
400 } 341 }
401 } 342
402 343 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
403 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name) 344 {
345 Communication::OutputBuffer request;
346 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
347 CallEngine(request);
348 }
349
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)
404 { 355 {
405 Communication::OutputBuffer request; 356 Communication::OutputBuffer request;
406 request << Communication::PROC_GET_PREF << ToUtf8String(name); 357 request << Communication::PROC_GET_PREF << ToUtf8String(name);
407 358
408 try 359 Communication::InputBuffer response;
409 { 360 if (!CallEngine(request, response))
410 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 361 return defaultValue;
411 std::vector<std::wstring> retValue = ReadStrings(response); 362 bool success;
412 return retValue.size() > 0 ? retValue.at(0) : L""; 363 response >> success;
Wladimir Palant 2013/07/11 12:53:10 This really shouldn't try to read a list of string
413 } 364 if (success)
414 catch (const std::exception& e) 365 {
415 { 366 std::string value;
416 DEBUG_GENERAL(e.what()); 367 response >> value;
417 return L""; 368 return ToUtf16String(value);
418 } 369 }
419 } 370 else
371 return defaultValue;
372 }
373
374 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
375 {
376 Communication::OutputBuffer request;
377 request << Communication::PROC_GET_PREF << ToUtf8String(name);
378
379 Communication::InputBuffer response;
380 if (!CallEngine(request, response))
381 return defaultValue;
382 bool success;
383 response >> success;
384 if (success)
385 {
386 bool value;
387 response >> value;
388 return value;
389 }
390 else
391 return defaultValue;
392 }
393 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
394 {
395 Communication::OutputBuffer request;
396 request << Communication::PROC_GET_PREF << ToUtf8String(name);
397
398 Communication::InputBuffer response;
399 if (!CallEngine(request, response))
400 return defaultValue;
401 bool success;
402 response >> success;
403 if (success)
404 {
405 int64_t value;
406 response >> value;
407 return value;
408 }
409 else
410 return defaultValue;
411 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld