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

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

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Left Patch Set: address some comments Created Feb. 18, 2015, 2:05 p.m.
Right Patch Set: rebase Created April 2, 2015, 11:09 a.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 | « no previous file | src/shared/Communication.h » ('j') | test/CommunicationTest.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
1 #include "PluginStdAfx.h" 18 #include "PluginStdAfx.h"
2 #include "PluginSettings.h" 19 #include "PluginSettings.h"
3 #include "PluginSystem.h" 20 #include "PluginSystem.h"
4 #include "PluginFilter.h" 21 #include "PluginFilter.h"
5 #include "PluginClientFactory.h" 22 #include "PluginClientFactory.h"
6 #include "PluginMutex.h" 23 #include "PluginMutex.h"
7 #include "PluginClass.h" 24 #include "PluginClass.h"
8 25
9 #include "AdblockPlusClient.h" 26 #include "AdblockPlusClient.h"
10 27
11 #include "../shared/Utils.h" 28 #include "../shared/Utils.h"
12 29
13 namespace 30 namespace
14 { 31 {
15 void SpawnAdblockPlusEngine() 32 void SpawnAdblockPlusEngine()
16 { 33 {
17 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe"; 34 std::wstring engineExecutablePath = GetDllDir() + L"AdblockPlusEngine.exe";
18 CString params = to_CString(L"AdblockPlusEngine.exe " + GetBrowserLanguage() ); 35 CString params = ToCString(L"AdblockPlusEngine.exe " + GetBrowserLanguage()) ;
19 36
20 STARTUPINFO startupInfo = {}; 37 STARTUPINFO startupInfo = {};
21 PROCESS_INFORMATION processInformation = {}; 38 PROCESS_INFORMATION processInformation = {};
22 39
23 HANDLE token; 40 HANDLE token;
24 OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &token); 41 OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &token);
25 42
26 TOKEN_APPCONTAINER_INFORMATION *acs = NULL; 43 TOKEN_APPCONTAINER_INFORMATION *acs = NULL;
27 DWORD length = 0; 44 DWORD length = 0;
28 45
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 message >> description.title; 138 message >> description.title;
122 message >> description.specialization; 139 message >> description.specialization;
123 message >> description.listed; 140 message >> description.listed;
124 result.push_back(description); 141 result.push_back(description);
125 } 142 }
126 return result; 143 return result;
127 } 144 }
128 } 145 }
129 146
130 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 147 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
131 148 CComAutoCriticalSection CAdblockPlusClient::s_criticalSectionLocal;
132 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 149
150 CAdblockPlusClient::CAdblockPlusClient()
133 { 151 {
134 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 152 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
135 } 153 }
136 154
137 bool CAdblockPlusClient::CallEngine(Communication::OutputBuffer& message, Commun ication::InputBuffer& inputBuffer) 155 bool CAdblockPlusClient::CallEngine(Communication::OutputBuffer& message, Commun ication::InputBuffer& inputBuffer)
138 { 156 {
139 DEBUG_GENERAL("CallEngine start"); 157 DEBUG_GENERAL("CallEngine start");
140 CriticalSection::Lock lock(enginePipeLock); 158 CriticalSection::Lock lock(enginePipeLock);
141 try 159 try
142 { 160 {
143 if (!enginePipe) 161 if (!enginePipe)
144 enginePipe.reset(OpenEnginePipe()); 162 enginePipe.reset(OpenEnginePipe());
145 enginePipe->WriteMessage(message); 163 enginePipe->WriteMessage(message);
146 inputBuffer = enginePipe->ReadMessage(); 164 inputBuffer = enginePipe->ReadMessage();
147 } 165 }
148 catch (const std::exception& e) 166 catch (const std::exception& ex)
149 { 167 {
150 DEBUG_GENERAL(e.what()); 168 DEBUG_EXCEPTION(ex);
151 return false; 169 return false;
152 } 170 }
153 DEBUG_GENERAL("CallEngine end"); 171 DEBUG_GENERAL("CallEngine end");
154 return true; 172 return true;
155 } 173 }
156 174
157 bool CAdblockPlusClient::CallEngine(Communication::ProcType proc, Communication: :InputBuffer& inputBuffer) 175 bool CAdblockPlusClient::CallEngine(Communication::ProcType proc, Communication: :InputBuffer& inputBuffer)
158 { 176 {
159 Communication::OutputBuffer message; 177 Communication::OutputBuffer message;
160 message << proc; 178 message << proc;
(...skipping 19 matching lines...) Expand all
180 s_instance = client; 198 s_instance = client;
181 } 199 }
182 200
183 instance = s_instance; 201 instance = s_instance;
184 } 202 }
185 s_criticalSectionLocal.Unlock(); 203 s_criticalSectionLocal.Unlock();
186 204
187 return instance; 205 return instance;
188 } 206 }
189 207
190 bool CAdblockPlusClient::ShouldBlock(const std::wstring& src, int contentType, c onst std::wstring& domain, bool addDebug) 208 bool CAdblockPlusClient::ShouldBlock(const std::wstring& src, AdblockPlus::Filte rEngine::ContentType contentType, const std::wstring& domain, bool addDebug)
191 { 209 {
192 bool isBlocked = false; 210 bool isBlocked = false;
193 bool isCached = false; 211 bool isCached = false;
194 m_criticalSectionCache.Lock(); 212 m_criticalSectionCache.Lock();
195 { 213 {
196 auto it = m_cacheBlockedSources.find(src); 214 auto it = m_cacheBlockedSources.find(src);
197 215
198 isCached = it != m_cacheBlockedSources.end(); 216 isCached = it != m_cacheBlockedSources.end();
199 if (isCached) 217 if (isCached)
200 { 218 {
201 isBlocked = it->second; 219 isBlocked = it->second;
202 } 220 }
203 } 221 }
204 m_criticalSectionCache.Unlock(); 222 m_criticalSectionCache.Unlock();
205 223
206 if (!isCached) 224 if (!isCached)
207 { 225 {
208 m_criticalSectionFilter.Lock(); 226 m_criticalSectionFilter.Lock();
209 { 227 {
210 isBlocked = m_filter->ShouldBlock(src, contentType, domain, addDebug); 228 isBlocked = m_filter->ShouldBlock(src, contentType, domain, addDebug);
211 } 229 }
212 m_criticalSectionFilter.Unlock(); 230 m_criticalSectionFilter.Unlock();
213 231
214 // Cache result, if content type is defined 232 // Cache result, if content type is defined
215 if (contentType != CFilter::contentTypeAny) 233 if (contentType != AdblockPlus::FilterEngine::ContentType::CONTENT_TYPE_OTHE R)
216 { 234 {
217 m_criticalSectionCache.Lock(); 235 m_criticalSectionCache.Lock();
218 { 236 {
219 m_cacheBlockedSources[src] = isBlocked; 237 m_cacheBlockedSources[src] = isBlocked;
220 } 238 }
221 m_criticalSectionCache.Unlock(); 239 m_criticalSectionCache.Unlock();
222 } 240 }
223 } 241 }
224 return isBlocked; 242 return isBlocked;
225 } 243 }
226 244
227 bool CAdblockPlusClient::IsElementHidden(const std::wstring& tag, IHTMLElement* pEl, const std::wstring& domain, const std::wstring& indent, CPluginFilter* filt er) 245 bool CAdblockPlusClient::IsElementHidden(const std::wstring& tag, IHTMLElement* pEl, const std::wstring& domain, const std::wstring& indent, CPluginFilter* filt er)
228 { 246 {
229 bool isHidden; 247 bool isHidden;
230 m_criticalSectionFilter.Lock(); 248 m_criticalSectionFilter.Lock();
231 { 249 {
232 isHidden = filter && filter->IsElementHidden(tag, pEl, domain, indent); 250 isHidden = filter && filter->IsElementHidden(tag, pEl, domain, indent);
233 } 251 }
234 m_criticalSectionFilter.Unlock(); 252 m_criticalSectionFilter.Unlock();
235 return isHidden; 253 return isHidden;
236 } 254 }
237 255
238 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url) 256 bool CAdblockPlusClient::IsWhitelistedUrl(const std::wstring& url)
239 { 257 {
240 DEBUG_GENERAL((L"IsWhitelistedUrl: " + url + L" start").c_str()); 258 return !GetWhitelistingFilter(url).empty();
241 Communication::OutputBuffer request; 259 }
242 request << Communication::PROC_IS_WHITELISTED_URL << url; 260
243 261 std::string CAdblockPlusClient::GetWhitelistingFilter(const std::wstring& url)
244 Communication::InputBuffer response; 262 {
245 if (!CallEngine(request, response)) 263 DEBUG_GENERAL((L"GetWhitelistingFilter: " + url + L" start").c_str());
246 return false; 264 Communication::OutputBuffer request;
247 265 request << Communication::PROC_GET_WHITELISTING_FITER << url;
248 bool isWhitelisted; 266
249 response >> isWhitelisted; 267 Communication::InputBuffer response;
250 268 if (!CallEngine(request, response))
251 DEBUG_GENERAL((L"IsWhitelistedUrl: " + url + L" end").c_str()); 269 return "";
252 return isWhitelisted; 270
271 std::string filterText;
272 response >> filterText;
273
274 DEBUG_GENERAL((L"GetWhitelistingFilter: " + url + L" end").c_str());
275 return filterText;
253 } 276 }
254 277
255 bool CAdblockPlusClient::IsElemhideWhitelistedOnDomain(const std::wstring& url) 278 bool CAdblockPlusClient::IsElemhideWhitelistedOnDomain(const std::wstring& url)
256 { 279 {
257 Communication::OutputBuffer request; 280 Communication::OutputBuffer request;
258 request << Communication::PROC_IS_ELEMHIDE_WHITELISTED_ON_URL << url; 281 request << Communication::PROC_IS_ELEMHIDE_WHITELISTED_ON_URL << url;
259 282
260 Communication::InputBuffer response; 283 Communication::InputBuffer response;
261 if (!CallEngine(request, response)) 284 if (!CallEngine(request, response))
262 return false; 285 return false;
263 286
264 bool isWhitelisted; 287 bool isWhitelisted;
265 response >> isWhitelisted; 288 response >> isWhitelisted;
266 return isWhitelisted; 289 return isWhitelisted;
267 } 290 }
268 291
269 bool CAdblockPlusClient::Matches(const std::wstring& url, const std::wstring& co ntentType, const std::wstring& domain) 292 bool CAdblockPlusClient::Matches(const std::wstring& url, AdblockPlus::FilterEng ine::ContentType contentType, const std::wstring& domain)
270 { 293 {
271 Communication::OutputBuffer request; 294 Communication::OutputBuffer request;
272 request << Communication::PROC_MATCHES << url << contentType << domain; 295 request << Communication::PROC_MATCHES << url << contentType << domain;
273 296
274 Communication::InputBuffer response; 297 Communication::InputBuffer response;
275 if (!CallEngine(request, response)) 298 if (!CallEngine(request, response))
276 return false; 299 return false;
277 300
278 bool match; 301 bool match;
279 response >> match; 302 response >> match;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 } 397 }
375 398
376 void CAdblockPlusClient::AddFilter(const std::wstring& text) 399 void CAdblockPlusClient::AddFilter(const std::wstring& text)
377 { 400 {
378 Communication::OutputBuffer request; 401 Communication::OutputBuffer request;
379 request << Communication::PROC_ADD_FILTER << text; 402 request << Communication::PROC_ADD_FILTER << text;
380 CallEngine(request); 403 CallEngine(request);
381 } 404 }
382 405
383 void CAdblockPlusClient::RemoveFilter(const std::wstring& text) 406 void CAdblockPlusClient::RemoveFilter(const std::wstring& text)
407 {
408 RemoveFilter(ToUtf8String(text));
409 }
410
411 void CAdblockPlusClient::RemoveFilter(const std::string& text)
384 { 412 {
385 Communication::OutputBuffer request; 413 Communication::OutputBuffer request;
386 request << Communication::PROC_REMOVE_FILTER << text; 414 request << Communication::PROC_REMOVE_FILTER << text;
387 CallEngine(request); 415 CallEngine(request);
388 } 416 }
389 417
390 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue) 418 void CAdblockPlusClient::SetPref(const std::wstring& name, const std::wstring& v alue)
391 { 419 {
392 Communication::OutputBuffer request; 420 Communication::OutputBuffer request;
393 request << Communication::PROC_SET_PREF << name << value; 421 request << Communication::PROC_SET_PREF << name << value;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 DEBUG_GENERAL("CompareVersions"); 562 DEBUG_GENERAL("CompareVersions");
535 Communication::OutputBuffer request; 563 Communication::OutputBuffer request;
536 request << Communication::PROC_COMPARE_VERSIONS << v1 << v2; 564 request << Communication::PROC_COMPARE_VERSIONS << v1 << v2;
537 Communication::InputBuffer response; 565 Communication::InputBuffer response;
538 if (!CallEngine(request, response)) 566 if (!CallEngine(request, response))
539 return 0; 567 return 0;
540 int result; 568 int result;
541 response >> result; 569 response >> result;
542 return result; 570 return result;
543 } 571 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld