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

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

Issue 11043057: First run page triggering (Closed)
Left Patch Set: Comments addressed Created July 20, 2013, 8:11 p.m.
Right Patch Set: Minor refactoring. Renaming and a small cleanup. Created July 26, 2013, 12:05 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/PluginClass.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 = NULL)
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 if (inputBuffer != NULL)
114 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::ProcT ype proc) 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;
127 }
128
129 bool CallEngine(Communication::ProcType proc, Communication::InputBuffer* inpu tBuffer = NULL)
115 { 130 {
116 Communication::OutputBuffer message; 131 Communication::OutputBuffer message;
117 message << proc; 132 message << proc;
118 return CallAdblockPlusEngineProcedure(message); 133 return CallEngine(message, inputBuffer);
119 } 134 }
120 } 135 }
121 136
122 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 137 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
123 138
124 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 139 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
125 { 140 {
126 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 141 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
127 } 142 }
128 143
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 221 }
207 m_criticalSectionFilter.Unlock(); 222 m_criticalSectionFilter.Unlock();
208 return isHidden; 223 return isHidden;
209 } 224 }
210 225
211 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 226 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
212 { 227 {
213 Communication::OutputBuffer request; 228 Communication::OutputBuffer request;
214 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url); 229 request << Communication::PROC_IS_WHITELISTED_URL << ToUtf8String(url);
215 230
216 try 231 Communication::InputBuffer response;
217 { 232 if (!CallEngine(request, &response)) return false;;
218 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 233
219 234 bool isWhitelisted;
220 bool isWhitelisted; 235 response >> isWhitelisted;
221 response >> isWhitelisted; 236 return isWhitelisted;
222 return isWhitelisted;
223 }
224 catch (const std::exception& e)
225 {
226 DEBUG_GENERAL(e.what());
227 return false;
228 }
229 } 237 }
230 238
231 int CAdblockPlusClient::GetIEVersion() 239 int CAdblockPlusClient::GetIEVersion()
232 { 240 {
233 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer 241 //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer
234 HKEY hKey; 242 HKEY hKey;
235 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);
236 if (status != 0) 244 if (status != 0)
237 { 245 {
238 return 0; 246 return 0;
239 } 247 }
240 DWORD type, cbData; 248 DWORD type, cbData;
241 BYTE version[50]; 249 BYTE version[50];
242 cbData = 50; 250 cbData = 50;
243 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 251 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
244 if (status != 0) 252 if (status != 0)
245 { 253 {
246 return 0; 254 return 0;
247 } 255 }
248 RegCloseKey(hKey); 256 RegCloseKey(hKey);
249 return (int)(version[0] - 48); 257 return (int)(version[0] - 48);
250 } 258 }
251 259
252 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)
253 { 261 {
254 Communication::OutputBuffer request; 262 Communication::OutputBuffer request;
255 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain); 263 request << Communication::PROC_MATCHES << ToUtf8String(url) << ToUtf8String(co ntentType) << ToUtf8String(domain);
256 264
257 try 265 Communication::InputBuffer response;
258 { 266 if (!CallEngine(request, &response)) return false;;
259 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 267
260 268 bool match;
261 bool match; 269 response >> match;
262 response >> match; 270 return match;
263 return match;
264 }
265 catch (const std::exception& e)
266 {
267 DEBUG_GENERAL(e.what());
268 return false;
269 }
270 } 271 }
271 272
272 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain) 273 std::vector<std::wstring> CAdblockPlusClient::GetElementHidingSelectors(const st d::wstring& domain)
273 { 274 {
274 Communication::OutputBuffer request; 275 Communication::OutputBuffer request;
275 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain); 276 request << Communication::PROC_GET_ELEMHIDE_SELECTORS << ToUtf8String(domain);
276 277
277 try 278 Communication::InputBuffer response;
278 { 279 if (!CallEngine(request, &response)) return std::vector<std::wstring>();
279 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 280 return ReadStrings(response);
280 return ReadStrings(response);
281 }
282 catch (const std::exception& e)
283 {
284 DEBUG_GENERAL(e.what());
285 return std::vector<std::wstring>();
286 }
287 } 281 }
288 282
289 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions() 283 std::vector<SubscriptionDescription> CAdblockPlusClient::FetchAvailableSubscript ions()
290 { 284 {
291 try 285 Communication::InputBuffer response;
292 { 286 if (!CallEngine(Communication::PROC_AVAILABLE_SUBSCRIPTIONS, &response)) retur n std::vector<SubscriptionDescription>();
293 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_AVAILABLE_SUBSCRIPTIONS); 287 return ReadSubscriptions(response);
294 return ReadSubscriptions(response);
295 }
296 catch (const std::exception& e)
297 {
298 DEBUG_GENERAL(e.what());
299 return std::vector<SubscriptionDescription>();
300 }
301 } 288 }
302 289
303 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( ) 290 std::vector<SubscriptionDescription> CAdblockPlusClient::GetListedSubscriptions( )
304 { 291 {
305 try 292 Communication::InputBuffer response;
306 { 293 if (!CallEngine(Communication::PROC_LISTED_SUBSCRIPTIONS, &response)) return s td::vector<SubscriptionDescription>();
307 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_LISTED_SUBSCRIPTIONS); 294 return ReadSubscriptions(response);
308 return ReadSubscriptions(response);
309 }
310 catch (const std::exception& e)
311 {
312 DEBUG_GENERAL(e.what());
313 return std::vector<SubscriptionDescription>();
314 }
315 } 295 }
316 296
317 void CAdblockPlusClient::SetSubscription(const std::wstring& url) 297 void CAdblockPlusClient::SetSubscription(const std::wstring& url)
318 { 298 {
319 Communication::OutputBuffer request; 299 Communication::OutputBuffer request;
320 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url); 300 request << Communication::PROC_SET_SUBSCRIPTION << ToUtf8String(url);
321 301 CallEngine(request);
322 try
323 {
324 CallAdblockPlusEngineProcedure(request);
325 }
326 catch (const std::exception& e)
327 {
328 DEBUG_GENERAL(e.what());
329 }
330 } 302 }
331 303
332 void CAdblockPlusClient::UpdateAllSubscriptions() 304 void CAdblockPlusClient::UpdateAllSubscriptions()
333 { 305 {
334 try 306 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 } 307 }
343 308
344 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains() 309 std::vector<std::wstring> CAdblockPlusClient::GetExceptionDomains()
345 { 310 {
346 try 311 Communication::InputBuffer response;
347 { 312 if (!CallEngine(Communication::PROC_GET_EXCEPTION_DOMAINS)) return std::vector <std::wstring>();
348 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_GET_EXCEPTION_DOMAINS); 313 return ReadStrings(response);
349 return ReadStrings(response); 314 }
350 } 315
351 catch (const std::exception& e) 316 bool CAdblockPlusClient::IsFirstRun()
352 { 317 {
353 DEBUG_GENERAL(e.what()); 318 Communication::InputBuffer response;
354 return std::vector<std::wstring>(); 319 if (!CallEngine(Communication::PROC_IS_FIRST_RUN_ACTION_NEEDED, &response)) re turn false;
355 } 320 bool res;
356 } 321 response >> res;
357 322 return res;
323 }
358 void CAdblockPlusClient::AddFilter(const std::wstring& text) 324 void CAdblockPlusClient::AddFilter(const std::wstring& text)
359 { 325 {
360 Communication::OutputBuffer request; 326 Communication::OutputBuffer request;
361 request << Communication::PROC_ADD_FILTER << ToUtf8String(text); 327 request << Communication::PROC_ADD_FILTER << ToUtf8String(text);
362 328 CallEngine(request);
363 try
364 {
365 CallAdblockPlusEngineProcedure(request);
366 }
367 catch (const std::exception& e)
368 {
369 DEBUG_GENERAL(e.what());
370 }
371 } 329 }
372 330
373 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 331 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
374 { 332 {
375 Communication::OutputBuffer request; 333 Communication::OutputBuffer request;
376 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text); 334 request << Communication::PROC_REMOVE_FILTER << ToUtf8String(text);
377 335 CallEngine(request);
378 try
379 {
380 CallAdblockPlusEngineProcedure(request);
381 }
382 catch (const std::exception& e)
383 {
384 DEBUG_GENERAL(e.what());
385 }
386 } 336 }
387 337
388 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue) 338 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
389 { 339 {
390 Communication::OutputBuffer request; 340 Communication::OutputBuffer request;
391 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value); 341 request << Communication::PROC_SET_PREF << ToUtf8String(name) << ToUtf8String( value);
392 342 CallEngine(request);
393 try 343 }
394 { 344
395 CallAdblockPlusEngineProcedure(request); 345 void CAdblockPlusClient::SetPref(const std::wstring& name, const int64_t & value )
396 } 346 {
397 catch (const std::exception& e) 347 Communication::OutputBuffer request;
398 { 348 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
399 DEBUG_GENERAL(e.what()); 349 CallEngine(request);
400 } 350 }
401 } 351
402 352 void CAdblockPlusClient::SetPref(const std::wstring& name, bool value)
403 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name) 353 {
354 Communication::OutputBuffer request;
355 request << Communication::PROC_SET_PREF << ToUtf8String(name) << value;
356 CallEngine(request);
357 }
358
359 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const wchar_t * defaultValue)
360 {
361 return GetPref(name, std::wstring(defaultValue));
362 }
363 std::wstring CAdblockPlusClient::GetPref(const std::wstring& name, const std::ws tring& defaultValue)
404 { 364 {
405 Communication::OutputBuffer request; 365 Communication::OutputBuffer request;
406 request << Communication::PROC_GET_PREF << ToUtf8String(name); 366 request << Communication::PROC_GET_PREF << ToUtf8String(name);
407 367
408 try 368 Communication::InputBuffer response;
409 { 369 if (!CallEngine(request, &response)) return defaultValue;
410 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 370 bool success;
411 std::vector<std::wstring> retValue = ReadStrings(response); 371 response >> success;
412 return retValue.size() > 0 ? retValue.at(0) : L""; 372 if (success)
413 } 373 {
414 catch (const std::exception& e) 374 std::string value;
415 { 375 response >> value;
416 DEBUG_GENERAL(e.what()); 376 return ToUtf16String(value);
417 return L""; 377 }
418 } 378 else
419 } 379 return defaultValue;
420 380 }
421 bool CAdblockPlusClient::IsFirstRun() 381
422 { 382 bool CAdblockPlusClient::GetPref(const std::wstring& name, bool defaultValue)
423 try 383 {
424 { 384 Communication::OutputBuffer request;
425 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(Communi cation::PROC_IS_FIRST_RUN_ACTION_NEEDED); 385 request << Communication::PROC_GET_PREF << ToUtf8String(name);
426 bool res; 386
427 response >> res; 387 Communication::InputBuffer response;
428 return res; 388 if (!CallEngine(request, &response)) return defaultValue;
429 } 389 bool success;
430 catch (const std::exception& e) 390 response >> success;
431 { 391 if (success)
432 DEBUG_GENERAL(e.what()); 392 {
433 } 393 bool value;
434 return false; 394 response >> value;
435 } 395 return value;
396 }
397 else
398 return defaultValue;
399 }
400 int64_t CAdblockPlusClient::GetPref(const std::wstring& name, int64_t defaultVal ue)
401 {
402 Communication::OutputBuffer request;
403 request << Communication::PROC_GET_PREF << ToUtf8String(name);
404
405 Communication::InputBuffer response;
406 if (!CallEngine(request, &response)) return defaultValue;
407 bool success;
408 response >> success;
409 if (success)
410 {
411 int64_t value;
412 response >> value;
413 return value;
414 }
415 else
416 return defaultValue;
417 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld