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: 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:
View unified diff | Download patch
« no previous file with comments | « src/plugin/AdblockPlusClient.h ('k') | src/plugin/AdblockPlusTab.h » ('j') | no next file with comments »
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 "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))
219 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 226 return false;
220 227
221 bool isWhitelisted; 228 bool isWhitelisted;
222 response >> isWhitelisted; 229 response >> isWhitelisted;
223 return isWhitelisted; 230 return isWhitelisted;
224 }
225 catch (const std::exception& e)
226 {
227 DEBUG_GENERAL(e.what());
228 return false;
229 }
230 } 231 }
231 232
232 int CAdblockPlusClient::GetIEVersion() 233 int CAdblockPlusClient::GetIEVersion()
233 { 234 {
234 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 235 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
235 HKEY hKey; 236 HKEY hKey;
236 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);
237 if (status != 0) 238 if (status != 0)
238 { 239 {
239 return 0; 240 return 0;
240 } 241 }
241 DWORD type, cbData; 242 DWORD type, cbData;
242 BYTE version[50]; 243 BYTE version[50];
243 cbData = 50; 244 cbData = 50;
244 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 245 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
245 if (status != 0) 246 if (status != 0)
246 { 247 {
247 return 0; 248 return 0;
248 } 249 }
249 RegCloseKey(hKey); 250 RegCloseKey(hKey);
250 return (int)(version[0] - 48); 251 return (int)(version[0] - 48);
251 } 252 }
252 253
253 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)
254 { 255 {
255 Communication::OutputBuffer request; 256 Communication::OutputBuffer request;
256 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 257 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
257 258
258 try 259 Communication::InputBuffer response;
259 { 260 if (!CallEngine(request, response))
260 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 261 return false;
261 262
262 bool match; 263 bool match;
263 response >> match; 264 response >> match;
264 return match; 265 return match;
265 }
266 catch (const std::exception& e)
267 {
268 DEBUG_GENERAL(e.what());
269 return false;
270 }
271 } 266 }
272 267
273 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 268 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
274 { 269 {
275 Communication::OutputBuffer request; 270 Communication::OutputBuffer request;
276 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 271 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
277 272
278 try 273 Communication::InputBuffer response;
279 { 274 if (!CallEngine(request, response))
280 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
281 return ReadStrings(response);
282 }
283 catch (const std::exception& e)
284 {
285 DEBUG_GENERAL(e.what());
286 return std::vector<std::wstring>(); 275 return std::vector<std::wstring>();
287 } 276 return ReadStrings(response);
288 } 277 }
289 278
290 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 279 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
291 { 280 {
292 try 281 Communication::InputBuffer response;
293 { 282 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, response))
294 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS);
295 return ReadSubscriptions(response);
296 }
297 catch (const std::exception& e)
298 {
299 DEBUG_GENERAL(e.what());
300 return std::vector<SubscriptionDescription>(); 283 return std::vector<SubscriptionDescription>();
301 } 284 return ReadSubscriptions(response);
302 } 285 }
303 286
304 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 287 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
305 { 288 {
306 try 289 Communication::InputBuffer response;
307 { 290 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, response))
308 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS);
309 return ReadSubscriptions(response);
310 }
311 catch (const std::exception& e)
312 {
313 DEBUG_GENERAL(e.what());
314 return std::vector<SubscriptionDescription>(); 291 return std::vector<SubscriptionDescription>();
315 } 292 return ReadSubscriptions(response);
316 } 293 }
317 294
318 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 295 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
319 { 296 {
320 Communication::OutputBuffer request; 297 Communication::OutputBuffer request;
321 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 298 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
322 299 CallEngine(request);
323 try
324 {
325 CallAdblockPlusEngineProcedure(request);
326 }
327 catch (const std::exception& e)
328 {
329 DEBUG_GENERAL(e.what());
330 }
331 } 300 }
332 301
333 void CAdblockPlusClient::UpdateAllSubscriptions() 302 void CAdblockPlusClient::UpdateAllSubscriptions()
334 { 303 {
335 try 304 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 } 305 }
344 306
345 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 307 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
346 { 308 {
347 try 309 Communication::InputBuffer response;
348 { 310 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS))
349 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS);
350 return ReadStrings(response);
351 }
352 catch (const std::exception& e)
353 {
354 DEBUG_GENERAL(e.what());
355 return std::vector<std::wstring>(); 311 return std::vector<std::wstring>();
356 } 312 return ReadStrings(response);
357 } 313 }
358 314
359 void CAdblockPlusClient::AddFilter(const std::wstring& text) 315 void CAdblockPlusClient::AddFilter(const std::wstring& text)
360 { 316 {
361 Communication::OutputBuffer request; 317 Communication::OutputBuffer request;
362 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 318 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
363 319 CallEngine(request);
364 try
365 {
366 CallAdblockPlusEngineProcedure(request);
367 }
368 catch (const std::exception& e)
369 {
370 DEBUG_GENERAL(e.what());
371 }
372 } 320 }
373 321
374 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 322 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
375 { 323 {
376 Communication::OutputBuffer request; 324 Communication::OutputBuffer request;
377 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 325 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
326 CallEngine(request);
327 }
378 328
379 try 329 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
330 {
331 Communication::OutputBuffer request;
332 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
333 CallEngine(request);
334 }
335
336 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
337 {
338 Communication::OutputBuffer request;
339 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
340 CallEngine(request);
341 }
342
343 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
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)
355 {
356 Communication::OutputBuffer request;
357 request << Communication::PROC_GET_PREF << ToUtf8String(name);
358
359 Communication::InputBuffer response;
360 if (!CallEngine(request, response))
361 return defaultValue;
362 bool success;
363 response >> success;
364 if (success)
380 { 365 {
381 CallAdblockPlusEngineProcedure(request); 366 std::string value;
367 response >> value;
368 return ToUtf16String(value);
382 } 369 }
383 catch (const std::exception& e) 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)
384 { 385 {
385 DEBUG_GENERAL(e.what()); 386 bool value;
387 response >> value;
388 return value;
386 } 389 }
390 else
391 return defaultValue;
387 } 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 }
OLDNEW
« no previous file with comments | « src/plugin/AdblockPlusClient.h ('k') | src/plugin/AdblockPlusTab.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld