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

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

Issue 5979857238360064: Issues #1163, #1173 - refactor CPluginUserSettings (Closed)
Left Patch Set: Created Jan. 4, 2015, 9 p.m.
Right Patch Set: rebase + add one final space character Created Feb. 23, 2015, 1:37 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/PluginUserSettings.h ('k') | test/plugin/UserSettingsTest.cpp » ('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 /*
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 "PluginUserSettings.h" 19 #include "PluginUserSettings.h"
3 #include <algorithm> 20 #include <algorithm>
4 #include "PluginSettings.h" 21 #include "PluginSettings.h"
5 #include "PluginClient.h" 22 #include "PluginClient.h"
6 #include "../shared/Dictionary.h" 23 #include "../shared/Dictionary.h"
7 #include <unordered_map> 24 #include <unordered_map>
8 25
9 namespace 26 namespace
10 { 27 {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 81
65 // ENTRY POINT 82 // ENTRY POINT
66 STDMETHODIMP CPluginUserSettings::QueryInterface(REFIID riid, void **ppvObj) 83 STDMETHODIMP CPluginUserSettings::QueryInterface(REFIID riid, void **ppvObj)
67 { 84 {
68 if (!ppvObj) 85 if (!ppvObj)
69 { 86 {
70 return E_POINTER; 87 return E_POINTER;
71 } 88 }
72 if (riid == IID_IUnknown || riid == IID_IDispatch) // GUID comparison does not throw 89 if (riid == IID_IUnknown || riid == IID_IDispatch) // GUID comparison does not throw
73 { 90 {
74 *ppvObj = static_cast<void *>(this); 91 *ppvObj = static_cast<void*>(this);
75 return S_OK; 92 return S_OK;
76 } 93 }
77 return E_NOINTERFACE; 94 return E_NOINTERFACE;
78 } 95 }
79 96
80 /** 97 /**
81 * \par Limitation 98 * \par Limitation
82 * CPluginUserSettings is not allocated on the heap. 99 * CPluginUserSettings is not allocated on the heap.
83 * It appears only as a member variable in CPluginTabBase. 100 * It appears only as a member variable in CPluginTabBase.
84 * 'AddRef' and 'Release' don't need reference counting because they don't pre sent COM factories. 101 * 'AddRef' and 'Release' don't need reference counting because they don't pre sent COM factories.
(...skipping 29 matching lines...) Expand all
114 { 131 {
115 if (!name || !id) 132 if (!name || !id)
116 { 133 {
117 return E_POINTER; 134 return E_POINTER;
118 } 135 }
119 if (count != 1) 136 if (count != 1)
120 { 137 {
121 return E_FAIL; 138 return E_FAIL;
122 } 139 }
123 auto item = methodIndex.find(*name); // unordered_map::find is not declared noexcept 140 auto item = methodIndex.find(*name); // unordered_map::find is not declared noexcept
124 if (item==methodIndex.end()) 141 if (item == methodIndex.end())
125 { 142 {
126 return DISP_E_UNKNOWNNAME; 143 return DISP_E_UNKNOWNNAME;
127 } 144 }
128 *id = item->second; 145 *id = item->second;
129 } 146 }
130 catch (...) 147 catch (...)
131 { 148 {
132 return E_FAIL; 149 return E_FAIL;
133 } 150 }
134 return S_OK; 151 return S_OK;
135 } 152 }
136 153
137 CStringW sGetMessage(const CString& section, const CString& key) 154 CStringW sGetMessage(const CString& section, const CString& key)
138 { 155 {
139 Dictionary* dictionary = Dictionary::GetInstance(); 156 Dictionary* dictionary = Dictionary::GetInstance();
140 return CStringW(dictionary->Lookup(std::string(CW2A(section)), std::string(CW2 A(key))).c_str()); 157 return CStringW(dictionary->Lookup(std::string(CW2A(section)), std::string(CW2 A(key))).c_str());
141 } 158 }
142 159
143 STDMETHODIMP CPluginUserSettings::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispparams, VARIANT* pVarResult, 160 STDMETHODIMP CPluginUserSettings::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispparams, VARIANT* pVarResult,
144 EXCEPINFO* pExcepinfo, UINT* pArgErr) 161 EXCEPINFO* pExcepinfo, UINT* pArgErr)
145 { 162 {
146 try 163 try
147 { 164 {
148 if (!pDispparams || !pExcepinfo) 165 if (!pDispparams)
sergei 2015/01/09 15:42:54 pExcepinfo can be nullptr, it's not an error.
Eric 2015/01/09 16:25:24 I'm mostly trying to add an exception handler in t
149 { 166 {
150 return E_POINTER; 167 return E_POINTER;
151 } 168 }
152 if (pDispparams->cNamedArgs != 0) 169 if (pDispparams->cNamedArgs != 0)
153 { 170 {
154 return DISP_E_NONAMEDARGS; 171 return DISP_E_NONAMEDARGS;
155 } 172 }
156 CPluginSettings* settings = CPluginSettings::GetInstance(); 173 CPluginSettings* settings = CPluginSettings::GetInstance();
157 switch (dispidMember) 174 switch (dispidMember)
158 { 175 {
159 case dispatchID_GetMessage: 176 case dispatchID_GetMessage:
160 { 177 {
161 if (pDispparams->cArgs != 2) 178 if (pDispparams->cArgs != 2)
162 { 179 {
163 return DISP_E_BADPARAMCOUNT; 180 return DISP_E_BADPARAMCOUNT;
164 } 181 }
165 if (pDispparams->rgvarg[0].vt != VT_BSTR || pDispparams->rgvarg[1].vt != VT_BSTR) 182 if (pDispparams->rgvarg[0].vt != VT_BSTR || pDispparams->rgvarg[1].vt != VT_BSTR)
166 { 183 {
167 return DISP_E_TYPEMISMATCH; 184 return DISP_E_TYPEMISMATCH;
168 } 185 }
169 if (pVarResult) 186 if (pVarResult)
sergei 2015/01/09 15:42:54 Wouldn't it better here and below to have if (!pVa
Eric 2015/01/09 16:25:24 No. It's not an error to ignore the return value.
sergei 2015/01/30 16:01:40 What is about GetAppLocale and below? We should te
Eric 2015/02/02 14:46:39 Indeed, there were four defect where 'pVarResult'
170 { 187 {
171 CComBSTR key = pDispparams->rgvarg[0].bstrVal; 188 CComBSTR key = pDispparams->rgvarg[0].bstrVal;
172 CComBSTR section = pDispparams->rgvarg[1].bstrVal; 189 CComBSTR section = pDispparams->rgvarg[1].bstrVal;
173 CStringW message = sGetMessage((BSTR)section, (BSTR)key); 190 CStringW message = sGetMessage((BSTR)section, (BSTR)key);
174 191
175 pVarResult->vt = VT_BSTR; 192 pVarResult->vt = VT_BSTR;
176 pVarResult->bstrVal = SysAllocString(message); 193 pVarResult->bstrVal = SysAllocString(message);
177 } 194 }
178 } 195 }
179 break; 196 break;
(...skipping 11 matching lines...) Expand all
191 pVarResult->lVal = static_cast<LONG>(languageList.size()); 208 pVarResult->lVal = static_cast<LONG>(languageList.size());
192 } 209 }
193 } 210 }
194 break; 211 break;
195 case dispatchID_GetLanguageByIndex: 212 case dispatchID_GetLanguageByIndex:
196 { 213 {
197 if (pDispparams->cArgs != 1) 214 if (pDispparams->cArgs != 1)
198 { 215 {
199 return DISP_E_BADPARAMCOUNT; 216 return DISP_E_BADPARAMCOUNT;
200 } 217 }
201 if (VT_I4 != pDispparams->rgvarg[0].vt) 218 if (pDispparams->rgvarg[0].vt != VT_I4)
202 { 219 {
203 return DISP_E_TYPEMISMATCH; 220 return DISP_E_TYPEMISMATCH;
204 } 221 }
205 if (pVarResult) 222 if (pVarResult)
206 { 223 {
207 int indx = pDispparams->rgvarg[0].lVal; 224 int index = pDispparams->rgvarg[0].lVal;
208 225
209 std::map<CString, CString> languageTitleList = settings->GetFilterLang uageTitleList(); 226 std::map<CString, CString> languageTitleList = settings->GetFilterLang uageTitleList();
210 227
211 if (indx < 0 || indx >= (int)languageTitleList.size()) 228 if (index < 0 || index >= static_cast<int>(languageTitleList.size()) )
212 return DISP_E_EXCEPTION; 229 return DISP_E_EXCEPTION;
213 230
214 CString language; 231 CString language;
215 232
216 int curIndx = 0; 233 int loopIndex = 0;
217 for(std::map<CString, CString>::const_iterator it = languageTitleList. begin(); it != languageTitleList.end(); ++it) 234 for (std::map<CString, CString>::const_iterator it = languageTitleList .begin(); it != languageTitleList.end(); ++it)
218 { 235 {
219 if (curIndx == indx) 236 if (loopIndex == index)
220 { 237 {
221 language = it->first; 238 language = it->first;
222 break; 239 break;
223 } 240 }
224 curIndx++; 241 ++loopIndex;
225 } 242 }
226 243
227 pVarResult->vt = VT_BSTR; 244 pVarResult->vt = VT_BSTR;
228 pVarResult->bstrVal = SysAllocString(language); 245 pVarResult->bstrVal = SysAllocString(language);
229 } 246 }
230 } 247 }
231 break; 248 break;
232 case dispatchID_GetLanguageTitleByIndex: 249 case dispatchID_GetLanguageTitleByIndex:
233 { 250 {
234 if (pDispparams->cArgs != 1) 251 if (pDispparams->cArgs != 1)
235 { 252 {
236 return DISP_E_BADPARAMCOUNT; 253 return DISP_E_BADPARAMCOUNT;
237 } 254 }
238 if (VT_I4 != pDispparams->rgvarg[0].vt) 255 if (pDispparams->rgvarg[0].vt != VT_I4)
239 { 256 {
240 return DISP_E_TYPEMISMATCH; 257 return DISP_E_TYPEMISMATCH;
241 } 258 }
242 if (pVarResult) 259 if (pVarResult)
243 { 260 {
244 int indx = pDispparams->rgvarg[0].lVal; 261 int index = pDispparams->rgvarg[0].lVal;
245 262
246 std::map<CString, CString> languageTitleList = settings->GetFilterLang uageTitleList(); 263 std::map<CString, CString> languageTitleList = settings->GetFilterLang uageTitleList();
247 264
248 if (indx < 0 || indx >= (int)languageTitleList.size()) 265 if (index < 0 || index >= static_cast<int>(languageTitleList.size()) )
249 return DISP_E_EXCEPTION; 266 return DISP_E_EXCEPTION;
250 267
251 CString languageTitle; 268 CString languageTitle;
252 269
253 int curIndx = 0; 270 int loopIndex = 0;
254 for(std::map<CString, CString>::const_iterator it = languageTitleList. begin(); it != languageTitleList.end(); ++it) 271 for (std::map<CString, CString>::const_iterator it = languageTitleList .begin(); it != languageTitleList.end(); ++it)
255 { 272 {
256 if (curIndx == indx) 273 if (loopIndex == index)
257 { 274 {
258 languageTitle = it->second; 275 languageTitle = it->second;
259 break; 276 break;
260 } 277 }
261 curIndx++; 278 loopIndex++;
262 } 279 }
263 280
264 pVarResult->vt = VT_BSTR; 281 pVarResult->vt = VT_BSTR;
265 pVarResult->bstrVal = SysAllocString(languageTitle); 282 pVarResult->bstrVal = SysAllocString(languageTitle);
266 } 283 }
267 } 284 }
268 break; 285 break;
269 case dispatchID_SetLanguage: 286 case dispatchID_SetLanguage:
270 { 287 {
271 if (pDispparams->cArgs != 1) 288 if (pDispparams->cArgs != 1)
272 { 289 {
273 return DISP_E_BADPARAMCOUNT; 290 return DISP_E_BADPARAMCOUNT;
274 } 291 }
275 if (VT_BSTR != pDispparams->rgvarg[0].vt) 292 if (pDispparams->rgvarg[0].vt != VT_BSTR)
276 { 293 {
277 return DISP_E_TYPEMISMATCH; 294 return DISP_E_TYPEMISMATCH;
278 } 295 }
279 CComBSTR url = pDispparams->rgvarg[0].bstrVal; 296 CComBSTR url = pDispparams->rgvarg[0].bstrVal;
280 settings->SetSubscription((BSTR)url); 297 settings->SetSubscription((BSTR)url);
281 } 298 }
282 break; 299 break;
283 case dispatchID_GetLanguage: 300 case dispatchID_GetLanguage:
284 { 301 {
285 if (pDispparams->cArgs != 0) 302 if (pDispparams->cArgs != 0)
(...skipping 30 matching lines...) Expand all
316 pVarResult->bstrVal = SysAllocString(sWhiteList); 333 pVarResult->bstrVal = SysAllocString(sWhiteList);
317 } 334 }
318 } 335 }
319 break; 336 break;
320 case dispatchID_AddWhitelistDomain: 337 case dispatchID_AddWhitelistDomain:
321 { 338 {
322 if (pDispparams->cArgs != 1) 339 if (pDispparams->cArgs != 1)
323 { 340 {
324 return DISP_E_BADPARAMCOUNT; 341 return DISP_E_BADPARAMCOUNT;
325 } 342 }
326 if (VT_BSTR != pDispparams->rgvarg[0].vt) 343 if (pDispparams->rgvarg[0].vt != VT_BSTR)
327 { 344 {
328 return DISP_E_TYPEMISMATCH; 345 return DISP_E_TYPEMISMATCH;
329 } 346 }
330 CComBSTR domain = pDispparams->rgvarg[0].bstrVal; 347 CComBSTR domain = pDispparams->rgvarg[0].bstrVal;
331 if (domain.Length()) 348 if (domain.Length())
332 { 349 {
333 settings->AddWhiteListedDomain((BSTR)domain); 350 settings->AddWhiteListedDomain((BSTR)domain);
334 } 351 }
335 } 352 }
336 break; 353 break;
337 case dispatchID_RemoveWhitelistDomain: 354 case dispatchID_RemoveWhitelistDomain:
338 { 355 {
339 if (pDispparams->cArgs != 1) 356 if (pDispparams->cArgs != 1)
340 { 357 {
341 return DISP_E_BADPARAMCOUNT; 358 return DISP_E_BADPARAMCOUNT;
342 } 359 }
343 if (VT_BSTR != pDispparams->rgvarg[0].vt) 360 if (pDispparams->rgvarg[0].vt != VT_BSTR)
344 { 361 {
345 return DISP_E_TYPEMISMATCH; 362 return DISP_E_TYPEMISMATCH;
346 } 363 }
347 CComBSTR domain = pDispparams->rgvarg[0].bstrVal; 364 CComBSTR domain = pDispparams->rgvarg[0].bstrVal;
348 if (domain.Length()) 365 if (domain.Length())
349 { 366 {
350 settings->RemoveWhiteListedDomain((BSTR)domain); 367 settings->RemoveWhiteListedDomain((BSTR)domain);
351 } 368 }
352 } 369 }
353 break; 370 break;
354 case dispatchID_GetAppLocale: 371 case dispatchID_GetAppLocale:
355 { 372 {
356 if (pDispparams->cArgs != 0) 373 if (pDispparams->cArgs != 0)
357 { 374 {
358 return DISP_E_BADPARAMCOUNT; 375 return DISP_E_BADPARAMCOUNT;
359 } 376 }
360 pVarResult->vt = VT_BSTR; 377 if (pVarResult)
361 pVarResult->bstrVal = SysAllocString(settings->GetAppLocale()); 378 {
379 pVarResult->vt = VT_BSTR;
380 pVarResult->bstrVal = SysAllocString(settings->GetAppLocale());
381 }
362 } 382 }
363 break; 383 break;
364 case dispatchID_GetDocumentationLink: 384 case dispatchID_GetDocumentationLink:
365 { 385 {
366 if (pDispparams->cArgs != 0) 386 if (pDispparams->cArgs != 0)
367 { 387 {
368 return DISP_E_BADPARAMCOUNT; 388 return DISP_E_BADPARAMCOUNT;
369 } 389 }
370 pVarResult->vt = VT_BSTR; 390 if (pVarResult)
371 pVarResult->bstrVal = SysAllocString(settings->GetDocumentationLink()); 391 {
392 pVarResult->vt = VT_BSTR;
393 pVarResult->bstrVal = SysAllocString(settings->GetDocumentationLink()) ;
394 }
372 } 395 }
373 break; 396 break;
374 case dispatchID_IsAcceptableAdsEnabled: 397 case dispatchID_IsAcceptableAdsEnabled:
375 { 398 {
376 if (pDispparams->cArgs != 0) 399 if (pDispparams->cArgs != 0)
377 { 400 {
378 return DISP_E_BADPARAMCOUNT; 401 return DISP_E_BADPARAMCOUNT;
379 } 402 }
380 pVarResult->vt = VT_BOOL; 403 if (pVarResult)
381 pVarResult->boolVal = CPluginClient::GetInstance()->IsAcceptableAdsEnabl ed() ? VARIANT_TRUE : VARIANT_FALSE; 404 {
405 pVarResult->vt = VT_BOOL;
406 pVarResult->boolVal = CPluginClient::GetInstance()->IsAcceptableAdsEna bled() ? VARIANT_TRUE : VARIANT_FALSE;
407 }
382 } 408 }
383 break; 409 break;
384 case dispatchID_SetAcceptableAdsEnabled: 410 case dispatchID_SetAcceptableAdsEnabled:
385 { 411 {
386 if (pDispparams->cArgs != 1) 412 if (pDispparams->cArgs != 1)
387 { 413 {
388 return DISP_E_BADPARAMCOUNT; 414 return DISP_E_BADPARAMCOUNT;
389 } 415 }
390 if (pDispparams->rgvarg[0].vt != VT_BOOL) 416 if (pDispparams->rgvarg[0].vt != VT_BOOL)
391 { 417 {
(...skipping 10 matching lines...) Expand all
402 client->RemoveSubscription(client->GetPref(L"subscriptions_exceptionsu rl", L"")); 428 client->RemoveSubscription(client->GetPref(L"subscriptions_exceptionsu rl", L""));
403 } 429 }
404 } 430 }
405 break; 431 break;
406 case dispatchID_IsUpdate: 432 case dispatchID_IsUpdate:
407 { 433 {
408 if (pDispparams->cArgs != 0) 434 if (pDispparams->cArgs != 0)
409 { 435 {
410 return DISP_E_BADPARAMCOUNT; 436 return DISP_E_BADPARAMCOUNT;
411 } 437 }
412 pVarResult->vt = VT_BOOL; 438 if (pVarResult)
413 pVarResult->boolVal = CPluginClient::GetInstance()->GetPref(L"displayUpd atePage", false) ? VARIANT_TRUE : VARIANT_FALSE; 439 {
440 pVarResult->vt = VT_BOOL;
441 pVarResult->boolVal = CPluginClient::GetInstance()->GetPref(L"displayU pdatePage", false) ? VARIANT_TRUE : VARIANT_FALSE;
442 }
414 } 443 }
415 break; 444 break;
416 default: 445 default:
417 return DISP_E_MEMBERNOTFOUND; 446 return DISP_E_MEMBERNOTFOUND;
418 break; 447 break;
419 } 448 }
420 } 449 }
421 catch(...) 450 catch (...)
422 { 451 {
423 return E_FAIL; 452 return E_FAIL;
424 } 453 }
425 return S_OK; 454 return S_OK;
426 } 455 }
427
LEFTRIGHT

Powered by Google App Engine
This is Rietveld