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

Side by Side Diff: src/plugin/PluginSettings.cpp

Issue 11013110: Cleanup (Closed)
Patch Set: More refactoring. Removing main thread, tab counting. Implementing SetPref and GetPref. Addressing … Created July 9, 2013, 12:59 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
OLDNEW
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include <Wbemidl.h> 3 #include <Wbemidl.h>
4 #include <time.h> 4 #include <time.h>
5 #include "PluginSettings.h" 5 #include "PluginSettings.h"
6 #include "PluginClient.h" 6 #include "PluginClient.h"
7 #include "PluginSystem.h" 7 #include "PluginSystem.h"
8 #ifdef SUPPORT_FILTER 8 #ifdef SUPPORT_FILTER
9 #include "PluginFilter.h" 9 #include "PluginFilter.h"
10 #endif 10 #endif
11 #include "PluginMutex.h" 11 #include "PluginMutex.h"
12 #include "../shared/Utils.h" 12 #include "../shared/Utils.h"
13 #include <memory> 13 #include <memory>
14 14
15 15
16 // IE functions 16 // IE functions
17 #pragma comment(lib, "iepmapi.lib") 17 #pragma comment(lib, "iepmapi.lib")
18 18
19 #include <knownfolders.h> 19 #include <knownfolders.h>
20 20
21 namespace 21 namespace
22 { 22 {
23 std::wstring CreateDomainWhitelistingFilter(CString domain) 23 std::wstring CreateDomainWhitelistingFilter(CString domain)
24 { 24 {
25 return L"@@||" + domain + L"^$document"; 25 return std::wstring(L"@@||" + domain + L"^$document");
Wladimir Palant 2013/07/11 12:53:10 What's wrong with the implicit conversion? Instead
26 } 26 }
27 } 27 }
28 28
29 class TSettings 29 class TSettings
30 { 30 {
31 DWORD processorId; 31 DWORD processorId;
32 32
33 char sPluginId[44]; 33 char sPluginId[44];
34 }; 34 };
35 35
(...skipping 21 matching lines...) Expand all
57 public: 57 public:
58 CPluginSettingsWhitelistLock() : CPluginMutex("SettingsFileWhitelist", PLUGIN_ ERROR_MUTEX_SETTINGS_FILE_WHITELIST) {} 58 CPluginSettingsWhitelistLock() : CPluginMutex("SettingsFileWhitelist", PLUGIN_ ERROR_MUTEX_SETTINGS_FILE_WHITELIST) {}
59 ~CPluginSettingsWhitelistLock() {} 59 ~CPluginSettingsWhitelistLock() {}
60 }; 60 };
61 61
62 #endif 62 #endif
63 63
64 CPluginSettings* CPluginSettings::s_instance = NULL; 64 CPluginSettings* CPluginSettings::s_instance = NULL;
65 65
66 CComAutoCriticalSection CPluginSettings::s_criticalSectionLocal; 66 CComAutoCriticalSection CPluginSettings::s_criticalSectionLocal;
67 #ifdef SUPPORT_WHITELIST
68 CComAutoCriticalSection CPluginSettings::s_criticalSectionDomainHistory;
69 #endif
70 67
71 68
72 CPluginSettings::CPluginSettings() : 69 CPluginSettings::CPluginSettings() :
73 m_settingsVersion("1"), m_isDirty(false), m_isFirstRun(false), m_isFirstRunUpd ate(false), m_dwMainProcessId(0), m_dwMainThreadId(0), m_dwWorkingThreadId(0), 70 m_settingsVersion("1"), m_isFirstRun(false), m_dwWorkingThreadId(0), m_isPlugi nEnabledTab(true)
Wladimir Palant 2013/07/11 12:53:10 m_settingsVersion property can be removed as well.
74 m_isPluginEnabledTab(true), m_tabNumber("1")
75 { 71 {
76
77 CPluginSettings *lightInstance = s_instance;
78 s_instance = NULL; 72 s_instance = NULL;
79 73
80 m_WindowsBuildNumber = 0; 74 m_WindowsBuildNumber = 0;
81 75
82 #ifdef SUPPORT_WHITELIST 76 #ifdef SUPPORT_WHITELIST
83 ClearWhitelist(); 77 ClearWhitelist();
84 #endif 78 #endif
85 } 79 }
86 80
87 81
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 144 }
151 CString lang; 145 CString lang;
152 lang.Append(language); 146 lang.Append(language);
153 lang.Append(L"-"); 147 lang.Append(L"-");
154 lang.Append(country); 148 lang.Append(country);
155 149
156 return lang; 150 return lang;
157 151
158 } 152 }
159 153
160 CString CPluginSettings::GetTempPath(const CString& filename)
161 {
162 CString tempPath;
163
164 LPWSTR pwszCacheDir = NULL;
165
166 HRESULT hr = ::IEGetWriteableFolderPath(FOLDERID_InternetCache, &pwszCacheDir) ;
167 if (SUCCEEDED(hr))
168 {
169 tempPath = pwszCacheDir;
170 }
171 // Not implemented in IE6
172 else if (hr == E_NOTIMPL)
173 {
174 TCHAR path[MAX_PATH] = _T("");
175
176 if (::SHGetSpecialFolderPath(NULL, path, CSIDL_INTERNET_CACHE, TRUE))
177 {
178 tempPath = path;
179 }
180 else
181 {
182 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSIN FO_GET_SPECIAL_FOLDER_TEMP, "Settings::GetTempPath failed");
183 }
184 }
185 // Other error
186 else
187 {
188 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_TEMP_PATH, "S ettings::GetTempPath failed");
189 }
190
191 ::CoTaskMemFree(pwszCacheDir);
192
193 return tempPath + "\\" + filename;
194 }
195
196 CString CPluginSettings::GetTempFile(const CString& prefix, const CString& exten sion)
197 {
198 TCHAR nameBuffer[MAX_PATH] = _T("");
199
200 CString tempPath;
201
202 DWORD dwRetVal = ::GetTempFileName(GetTempPath(), prefix, 0, nameBuffer);
203 if (dwRetVal == 0)
204 {
205 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO _TEMP_FILE, "Settings::GetTempFileName failed");
206
207 tempPath = GetDataPath();
208 }
209 else
210 {
211 tempPath = nameBuffer;
212 if (!extension.IsEmpty())
213 {
214 int pos = tempPath.ReverseFind(_T('.'));
215 if (pos >= 0)
216 {
217 tempPath = tempPath.Left(pos+1) + extension;
218 }
219 }
220 }
221
222 return tempPath;
223 }
224
225 154
226 bool CPluginSettings::IsPluginEnabled() const 155 bool CPluginSettings::IsPluginEnabled() const
227 { 156 {
228 return m_isPluginEnabledTab; 157 return m_isPluginEnabledTab;
229 } 158 }
230 159
231 160
232 std::map<CString, CString> CPluginSettings::GetFilterLanguageTitleList() const 161 std::map<CString, CString> CPluginSettings::GetFilterLanguageTitleList() const
233 { 162 {
234 std::map<CString, CString> filterList; 163 std::map<CString, CString> filterList;
235 for (size_t i = 0; i < m_subscriptions.size(); i ++) 164 for (size_t i = 0; i < m_subscriptions.size(); i ++)
236 { 165 {
237 SubscriptionDescription it = m_subscriptions[i]; 166 SubscriptionDescription it = m_subscriptions[i];
238 filterList.insert(std::make_pair(CString(it.url.c_str()), CString(it.title.c _str()))); 167 filterList.insert(std::make_pair(CString(it.url.c_str()), CString(it.title.c _str())));
239 } 168 }
240 return filterList; 169 return filterList;
241 } 170 }
242 171
243 bool CPluginSettings::IsMainProcess(DWORD dwProcessId) const
244 {
245 if (dwProcessId == 0)
246 {
247 dwProcessId = ::GetCurrentProcessId();
248 }
249 return m_dwMainProcessId == dwProcessId;
250 }
251
252 void CPluginSettings::SetMainProcessId()
253 {
254 m_dwMainProcessId = ::GetCurrentProcessId();
255 }
256
257 void CPluginSettings::SetMainProcessId(DWORD id)
258 {
259 m_dwMainProcessId = id;
260 }
261
262
263 bool CPluginSettings::IsMainUiThread(DWORD dwThreadId) const
264 {
265 if (dwThreadId == 0)
266 {
267 dwThreadId = ::GetCurrentThreadId();
268 }
269 return m_dwMainUiThreadId == dwThreadId;
270 }
271
272 void CPluginSettings::SetMainUiThreadId()
273 {
274 m_dwMainUiThreadId = ::GetCurrentThreadId();
275 }
276
277 void CPluginSettings::SetMainUiThreadId(DWORD id)
278 {
279 m_dwMainUiThreadId = id;
280 }
281 bool CPluginSettings::IsMainThread(DWORD dwThreadId) const
282 {
283 if (dwThreadId == 0)
284 {
285 dwThreadId = ::GetCurrentThreadId();
286 }
287 return m_dwMainThreadId == dwThreadId;
288 }
289
290 void CPluginSettings::SetMainThreadId()
291 {
292 m_dwMainThreadId = ::GetCurrentThreadId();
293 }
294
295 void CPluginSettings::SetMainThreadId(DWORD id)
296 {
297 m_dwMainThreadId = id;
298 }
299
300 bool CPluginSettings::IsWorkingThread(DWORD dwThreadId) const 172 bool CPluginSettings::IsWorkingThread(DWORD dwThreadId) const
301 { 173 {
302 if (dwThreadId == 0) 174 if (dwThreadId == 0)
303 { 175 {
304 dwThreadId = ::GetCurrentThreadId(); 176 dwThreadId = ::GetCurrentThreadId();
305 } 177 }
306 return m_dwWorkingThreadId == dwThreadId; 178 return m_dwWorkingThreadId == dwThreadId;
307 } 179 }
308 180
309 void CPluginSettings::SetWorkingThreadId() 181 void CPluginSettings::SetWorkingThreadId()
310 { 182 {
311 m_dwWorkingThreadId = ::GetCurrentThreadId(); 183 m_dwWorkingThreadId = ::GetCurrentThreadId();
312 } 184 }
313 185
314 void CPluginSettings::SetWorkingThreadId(DWORD id) 186 void CPluginSettings::SetWorkingThreadId(DWORD id)
315 { 187 {
316 m_dwWorkingThreadId = id; 188 m_dwWorkingThreadId = id;
317 } 189 }
318 190
319 void CPluginSettings::SetFirstRun() 191 void CPluginSettings::SetFirstRun()
320 { 192 {
321 m_isFirstRun = true; 193 m_isFirstRun = true;
322 } 194 }
323 195
324 bool CPluginSettings::IsFirstRun() const 196 bool CPluginSettings::IsFirstRun() const
325 { 197 {
326 return m_isFirstRun; 198 return m_isFirstRun;
327 } 199 }
328 200
329 void CPluginSettings::SetFirstRunUpdate()
330 {
331 m_isFirstRunUpdate = true;
332 }
333
334 bool CPluginSettings::IsFirstRunUpdate() const
335 {
336 return m_isFirstRunUpdate;
337 }
338
339 bool CPluginSettings::IsFirstRunAny() const
340 {
341 return m_isFirstRun || m_isFirstRunUpdate;
342 }
343
344
345 bool CPluginSettings::IncrementTabCount()
346 {
347 int tabCount = 1;
348
349 CPluginSettingsTabLock lock;
350 if (lock.IsLocked())
351 {
352 SYSTEMTIME systemTime;
353 ::GetSystemTime(&systemTime);
354
355 CString today;
356 today.Format(L"%d-%d-%d", systemTime.wYear, systemTime.wMonth, systemTime.wD ay);
357
358 s_criticalSectionLocal.Lock();
359 {
360 //TODO: Increment tab count in the AdblockPlusEngine
361
362 m_tabNumber.Format(L"%d", tabCount);
363 }
364 s_criticalSectionLocal.Unlock();
365 }
366
367 return tabCount == 1;
368 }
369
370
371 CString CPluginSettings::GetTabNumber() const
372 {
373 CString tabNumber;
374
375 s_criticalSectionLocal.Lock();
376 {
377 tabNumber = m_tabNumber;
378 }
379 s_criticalSectionLocal.Unlock();
380
381 return tabNumber;
382 }
383
384
385 bool CPluginSettings::DecrementTabCount()
386 {
387 int tabCount = 0;
388
389 CPluginSettingsTabLock lock;
390 if (lock.IsLocked())
391 {
392
393 s_criticalSectionLocal.Lock();
394 {
395 //TODO: Retrieve tab count from the AdblockPlusEngine
396 m_tabNumber.Format(L"%d", tabCount);
397 }
398 s_criticalSectionLocal.Unlock();
399 }
400
401 return tabCount == 0;
402 }
403
404
405 void CPluginSettings::TogglePluginEnabled() 201 void CPluginSettings::TogglePluginEnabled()
406 { 202 {
407 CPluginSettingsTabLock lock; 203 CPluginSettingsTabLock lock;
408 if (lock.IsLocked()) 204 if (lock.IsLocked())
409 { 205 {
410 s_criticalSectionLocal.Lock(); 206 s_criticalSectionLocal.Lock();
411 { 207 {
412 //TODO: Query if plugin is enabled from the AdblockPlusEngine 208 //TODO: Query if plugin is enabled from the AdblockPlusEngine
413 m_isPluginEnabledTab = m_isPluginEnabledTab ? false : true; 209 m_isPluginEnabledTab = m_isPluginEnabledTab ? false : true;
414 //TODO: Set plugin enabled/disabled in AdblockPlusEngine 210 //TODO: Set plugin enabled/disabled in AdblockPlusEngine
(...skipping 28 matching lines...) Expand all
443 } 239 }
444 } 240 }
445 bool CPluginSettings::GetPluginEnabled() const 241 bool CPluginSettings::GetPluginEnabled() const
446 { 242 {
447 //TODO: Query AdblockPlusEngine 243 //TODO: Query AdblockPlusEngine
448 return m_isPluginEnabledTab; 244 return m_isPluginEnabledTab;
449 } 245 }
450 246
451 bool CPluginSettings::GetStatusBarAsked() 247 bool CPluginSettings::GetStatusBarAsked()
452 { 248 {
453 //TOTO: Get value from the registry; 249 std::wstring res = CPluginClient::GetInstance()->GetPref(L"statusbarasked");
454 return false; 250 return res == L"true";
Wladimir Palant 2013/07/11 12:53:10 As mentioned before, the lack of type safety here
455 } 251 }
456 252
457 void CPluginSettings::SetStatusBarAsked(bool asked) 253 void CPluginSettings::SetStatusBarAsked()
458 { 254 {
459 //TODO: Set value in the registry 255 CPluginClient::GetInstance()->SetPref(L"statusbarasked", L"true");
460 } 256 }
461 257
462 258
463 void CPluginSettings::AddError(const CString& error, const CString& errorCode) 259 void CPluginSettings::AddError(const CString& error, const CString& errorCode)
464 { 260 {
465 DEBUG_SETTINGS(L"SettingsTab::AddError error:" + error + " code:" + errorCode) 261 DEBUG_SETTINGS(L"SettingsTab::AddError error:" + error + " code:" + errorCode)
466 } 262 }
467 263
468 264
469 // ============================================================================ 265 // ============================================================================
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 367
572 m_WindowsBuildNumber = osvi.dwBuildNumber; 368 m_WindowsBuildNumber = osvi.dwBuildNumber;
573 } 369 }
574 370
575 return m_WindowsBuildNumber; 371 return m_WindowsBuildNumber;
576 } 372 }
577 373
578 void CPluginSettings::SetSubscription(const std::wstring& url) 374 void CPluginSettings::SetSubscription(const std::wstring& url)
579 { 375 {
580 CPluginClient::GetInstance()->SetSubscription(url); 376 CPluginClient::GetInstance()->SetSubscription(url);
581 RefreshFilterlist();
582 RefreshWhitelist(); 377 RefreshWhitelist();
583 } 378 }
584 379
585 CString CPluginSettings::GetSubscription() 380 CString CPluginSettings::GetSubscription()
586 { 381 {
587 std::vector<SubscriptionDescription> subscriptions = CPluginClient::GetInstanc e()->GetListedSubscriptions(); 382 std::vector<SubscriptionDescription> subscriptions = CPluginClient::GetInstanc e()->GetListedSubscriptions();
588 if (subscriptions.size() > 0) 383 if (subscriptions.size() > 0)
589 return CString(subscriptions.front().url.c_str()); 384 return CString(subscriptions.front().url.c_str());
590 else 385 else
591 return CString(L""); 386 return CString(L"");
592 } 387 }
593 388
594 389
595 void CPluginSettings::RefreshFilterlist()
596 {
597 CPluginClient::GetInstance()->UpdateAllSubscriptions();
598 }
599 390
600 #endif // SUPPORT_WHITELIST 391 #endif // SUPPORT_WHITELIST
OLDNEW

Powered by Google App Engine
This is Rietveld