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

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

Issue 29332959: Issue #1173 - Protect more entry points with try-catch blocks (Closed)
Left Patch Set: address comments Created Jan. 4, 2016, 4:37 p.m.
Right Patch Set: Add log messages in new exception handlers Created Jan. 5, 2016, 3:57 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 | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 int Width() const 85 int Width() const
86 { 86 {
87 return right - left; 87 return right - left;
88 } 88 }
89 }; 89 };
90 } 90 }
91 91
92 CPluginClass::CPluginClass() 92 CPluginClass::CPluginClass()
93 : m_webBrowser2(nullptr) 93 : m_webBrowser2(nullptr)
94 { 94 {
95 DEBUG_GENERAL([this]() -> std::wstring
96 {
97 std::wstring s = L"CPluginClass::<constructor>, this = ";
98 s += ToHexLiteral(this);
99 return s;
100 }());
101
95 //Use this line to debug memory leaks 102 //Use this line to debug memory leaks
96 // _CrtDumpMemoryLeaks(); 103 // _CrtDumpMemoryLeaks();
97 104
98 m_isAdvised = false; 105 m_isAdvised = false;
99 m_hTabWnd = NULL; 106 m_hTabWnd = NULL;
100 m_hStatusBarWnd = NULL; 107 m_hStatusBarWnd = NULL;
101 m_hPaneWnd = NULL; 108 m_hPaneWnd = NULL;
102 m_nPaneWidth = 0; 109 m_nPaneWidth = 0;
103 m_pWndProcStatus = NULL; 110 m_pWndProcStatus = NULL;
104 m_hTheme = NULL; 111 m_hTheme = NULL;
105 m_isInitializedOk = false; 112 m_isInitializedOk = false;
106 113
107 114
108 m_tab = new CPluginTab(this); 115 m_tab = new CPluginTab();
109 116
110 Dictionary::Create(GetBrowserLanguage()); 117 Dictionary::Create(GetBrowserLanguage());
111 } 118 }
112 119
113 CPluginClass::~CPluginClass() 120 CPluginClass::~CPluginClass()
114 { 121 {
122 DEBUG_GENERAL([this]() -> std::wstring
123 {
124 std::wstring s = L"CPluginClass::<destructor>, this = ";
125 s += ToHexLiteral(this);
126 return s;
127 }());
128
115 delete m_tab; 129 delete m_tab;
116 } 130 }
117 131
118 HWND CPluginClass::GetBrowserHWND() const 132 HWND CPluginClass::GetBrowserHWND() const
119 { 133 {
120 if (!m_webBrowser2) 134 if (!m_webBrowser2)
121 { 135 {
122 DEBUG_ERROR_LOG(0, 0, 0, "CPluginClass::GetBrowserHWND - Reached with m_webB rowser2 == nullptr"); 136 DEBUG_ERROR_LOG(0, 0, 0, "CPluginClass::GetBrowserHWND - Reached with m_webB rowser2 == nullptr");
123 return nullptr; 137 return nullptr;
124 } 138 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 * 'unknownSite' will be null. Extraordinarily, this is sometimes _not_ called w hen IE 204 * 'unknownSite' will be null. Extraordinarily, this is sometimes _not_ called w hen IE
191 * is shutting down. Thus 'SetSite(nullptr)' has some similarities with a destru ctor, 205 * is shutting down. Thus 'SetSite(nullptr)' has some similarities with a destru ctor,
192 * but it is not a proper substitute for one. 206 * but it is not a proper substitute for one.
193 */ 207 */
194 STDMETHODIMP CPluginClass::SetSite(IUnknown* unknownSite) 208 STDMETHODIMP CPluginClass::SetSite(IUnknown* unknownSite)
195 { 209 {
196 try 210 try
197 { 211 {
198 if (unknownSite) 212 if (unknownSite)
199 { 213 {
200 214 DEBUG_GENERAL(L"========================================================== ======================\nNEW TAB UI\n============================================ ====================================");
201 DEBUG_GENERAL(L"========================================================== ======================\nNEW TAB UI\n============================================ ====================================")
202 215
203 HRESULT hr = ::CoInitialize(NULL); 216 HRESULT hr = ::CoInitialize(NULL);
204 if (FAILED(hr)) 217 if (FAILED(hr))
205 { 218 {
206 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_COINIT, "Class::SetSite - CoInitialize"); 219 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_COINIT, "Class::SetSite - CoInitialize");
207 } 220 }
208 221
209 /* 222 /*
210 * We were instantiated as a BHO, so our site is always of type IWebBrowse r2. 223 * We were instantiated as a BHO, so our site is always of type IWebBrowse r2.
211 */ 224 */
212 m_webBrowser2 = ATL::CComQIPtr<IWebBrowser2>(unknownSite); 225 m_webBrowser2 = ATL::CComQIPtr<IWebBrowser2>(unknownSite);
213 if (!m_webBrowser2) 226 if (!m_webBrowser2)
214 { 227 {
215 throw std::logic_error("CPluginClass::SetSite - Unable to convert site p ointer to IWebBrowser2*"); 228 throw std::logic_error("CPluginClass::SetSite - Unable to convert site p ointer to IWebBrowser2*");
216 } 229 }
230 DEBUG_GENERAL([this]() -> std::wstring
231 {
232 std::wstringstream ss;
233 ss << L"CPluginClass::SetSite, this = " << ToHexLiteral(this);
234 ss << L", browser = " << ToHexLiteral(m_webBrowser2);
235 return ss.str();
236 }());
217 237
218 //register the mimefilter 238 //register the mimefilter
219 //and only mimefilter 239 //and only mimefilter
220 //on some few computers the mimefilter does not get properly registered wh en it is done on another thread 240 //on some few computers the mimefilter does not get properly registered wh en it is done on another thread
221 s_criticalSectionLocal.Lock(); 241 s_criticalSectionLocal.Lock();
222 { 242 {
223 // Always register on startup, then check if we need to unregister in a separate thread 243 // Always register on startup, then check if we need to unregister in a separate thread
224 s_mimeFilter = CPluginClientFactory::GetMimeFilterClientInstance(); 244 s_mimeFilter = CPluginClientFactory::GetMimeFilterClientInstance();
225 s_asyncWebBrowser2 = unknownSite; 245 s_asyncWebBrowser2 = unknownSite;
226 s_instances.insert(this); 246 s_instances.insert(this);
227 } 247 }
228 s_criticalSectionLocal.Unlock(); 248 s_criticalSectionLocal.Unlock();
229 249
230 try 250 try
231 { 251 {
232 DEBUG_GENERAL("Loaded as BHO");
233 HRESULT hr = DispEventAdvise(m_webBrowser2); 252 HRESULT hr = DispEventAdvise(m_webBrowser2);
234 if (SUCCEEDED(hr)) 253 if (SUCCEEDED(hr))
235 { 254 {
236 m_isAdvised = true; 255 m_isAdvised = true;
237 try 256 try
238 { 257 {
239 std::thread startInitObjectThread(StartInitObject, this); 258 std::thread startInitObjectThread(StartInitObject, this);
240 startInitObjectThread.detach(); // TODO: but actually we should wait for the thread in the dtr. 259 startInitObjectThread.detach(); // TODO: but actually we should wait for the thread in the dtr.
241 } 260 }
242 catch (const std::system_error& ex) 261 catch (const std::system_error& ex)
243 { 262 {
244 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_TH READ_CREATE_PROCESS, 263 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_TH READ_CREATE_PROCESS,
245 "Class::Thread - Failed to create StartInitObject thread"); 264 "Class::Thread - Failed to create StartInitObject thread");
246 } 265 }
247 } 266 }
248 else 267 else
249 { 268 {
250 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_ADVIC E, "Class::SetSite - Advise"); 269 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_ADVIC E, "Class::SetSite - Advise");
251 } 270 }
252 } 271 }
253 catch (const std::runtime_error& ex) 272 catch (const std::runtime_error& ex)
254 { 273 {
255 DEBUG_EXCEPTION(ex); 274 DEBUG_EXCEPTION(ex);
256 Unadvise(); 275 Unadvise();
257 } 276 }
258 } 277 }
259 else 278 else
260 { 279 {
280 DEBUG_GENERAL([this]() -> std::wstring
281 {
282 std::wstringstream ss;
283 ss << L"CPluginClass::SetSite, this = " << ToHexLiteral(this);
284 ss << L", browser = nullptr";
285 return ss.str();
286 }());
287
261 Unadvise(); 288 Unadvise();
262 289
263 // Destroy window 290 // Destroy window
264 if (m_pWndProcStatus) 291 if (m_pWndProcStatus)
265 { 292 {
266 ::SetWindowLongPtr(m_hStatusBarWnd, GWLP_WNDPROC, (LPARAM)(WNDPROC)m_pWn dProcStatus); 293 ::SetWindowLongPtr(m_hStatusBarWnd, GWLP_WNDPROC, (LPARAM)(WNDPROC)m_pWn dProcStatus);
267 294
268 m_pWndProcStatus = NULL; 295 m_pWndProcStatus = NULL;
269 } 296 }
270 297
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 { 1003 {
977 try 1004 try
978 { 1005 {
979 if (cCmds == 0) return E_INVALIDARG; 1006 if (cCmds == 0) return E_INVALIDARG;
980 if (prgCmds == 0) return E_POINTER; 1007 if (prgCmds == 0) return E_POINTER;
981 1008
982 prgCmds[0].cmdf = OLECMDF_ENABLED; 1009 prgCmds[0].cmdf = OLECMDF_ENABLED;
983 } 1010 }
984 catch (...) 1011 catch (...)
985 { 1012 {
1013 DEBUG_GENERAL(L"CPluginClass::QueryStatus - exception");
986 return E_FAIL; 1014 return E_FAIL;
987 } 1015 }
988 return S_OK; 1016 return S_OK;
989 } 1017 }
990 1018
991 HMENU CPluginClass::CreatePluginMenu(const std::wstring& url) 1019 HMENU CPluginClass::CreatePluginMenu(const std::wstring& url)
992 { 1020 {
993 DEBUG_GENERAL("CreatePluginMenu"); 1021 DEBUG_GENERAL("CreatePluginMenu");
994 HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance(); 1022 HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();
995 1023
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1262 } 1290 }
1263 else 1291 else
1264 { 1292 {
1265 nFlags |= TPM_LEFTALIGN; 1293 nFlags |= TPM_LEFTALIGN;
1266 } 1294 }
1267 1295
1268 DisplayPluginMenu(hMenu, nIDCommand, pt, nFlags); 1296 DisplayPluginMenu(hMenu, nIDCommand, pt, nFlags);
1269 } 1297 }
1270 catch (...) 1298 catch (...)
1271 { 1299 {
1272 return E_FAIL; // Suppress exceptions, should log 1300 // Suppress exception, log only
Oleksandr 2016/01/05 02:06:38 Not really following the comment. Does this mean t
Eric 2016/01/05 15:59:43 The only logging we have is in Debug builds. I wou
1301 DEBUG_GENERAL(L"CPluginClass::Exec - exception");
1302 return E_FAIL;
1273 } 1303 }
1274 1304
1275 return S_OK; 1305 return S_OK;
1276 } 1306 }
1277 1307
1278 // Entry point 1308 // Entry point
1279 LRESULT CALLBACK CPluginClass::NewStatusProc(HWND hWnd, UINT message, WPARAM wPa ram, LPARAM lParam) 1309 LRESULT CALLBACK CPluginClass::NewStatusProc(HWND hWnd, UINT message, WPARAM wPa ram, LPARAM lParam)
1280 { 1310 {
1281 CPluginClass *pClass; 1311 CPluginClass *pClass;
1282 try 1312 try
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1348 ::LocalFree(hLocal); 1378 ::LocalFree(hLocal);
1349 return hRet; 1379 return hRet;
1350 } 1380 }
1351 1381
1352 default: 1382 default:
1353 break; 1383 break;
1354 } 1384 }
1355 } 1385 }
1356 catch (...) 1386 catch (...)
1357 { 1387 {
1358 // Suppress exception. Fall through to default handler. 1388 // Suppress exception. Fall through to default handler.
Oleksandr 2016/01/05 02:06:38 Empty catch blocks always look awry to me. Maybe w
1389 DEBUG_GENERAL(L"CPluginClass::NewStatusProc - exception");
1359 } 1390 }
1360 return ::CallWindowProc(pClass->m_pWndProcStatus, hWnd, message, wParam, lPara m); 1391 return ::CallWindowProc(pClass->m_pWndProcStatus, hWnd, message, wParam, lPara m);
1361 } 1392 }
1362 1393
1363 1394
1364 HICON CPluginClass::GetStatusBarIcon(const std::wstring& url) 1395 HICON CPluginClass::GetStatusBarIcon(const std::wstring& url)
1365 { 1396 {
1366 // use the disable icon as defualt, if the client doesn't exists 1397 // use the disable icon as defualt, if the client doesn't exists
1367 HICON hIcon = GetIcon(ICON_PLUGIN_DEACTIVATED); 1398 HICON hIcon = GetIcon(ICON_PLUGIN_DEACTIVATED);
1368 1399
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 std::wstring downloadingText = dictionary->Lookup("updater", "downloadin g-update-text"); 1616 std::wstring downloadingText = dictionary->Lookup("updater", "downloadin g-update-text");
1586 std::wstring downloadingTitle = dictionary->Lookup("updater", "downloadi ng-update-title"); 1617 std::wstring downloadingTitle = dictionary->Lookup("updater", "downloadi ng-update-title");
1587 pClass->notificationMessage.SetTextAndIcon(downloadingText, downloadingT itle, TTI_INFO); 1618 pClass->notificationMessage.SetTextAndIcon(downloadingText, downloadingT itle, TTI_INFO);
1588 break; 1619 break;
1589 } 1620 }
1590 } 1621 }
1591 } 1622 }
1592 catch (...) 1623 catch (...)
1593 { 1624 {
1594 // Suppress exception. Fall through to default handler. 1625 // Suppress exception. Fall through to default handler.
1626 DEBUG_GENERAL(L"CPluginClass::PaneWindowProc - exception");
1595 } 1627 }
1596 return ::DefWindowProc(hWnd, message, wParam, lParam); 1628 return ::DefWindowProc(hWnd, message, wParam, lParam);
1597 } 1629 }
1598 1630
1599 1631
1600 void CPluginClass::UpdateStatusBar() 1632 void CPluginClass::UpdateStatusBar()
1601 { 1633 {
1602 DEBUG_GENERAL("*** Updating statusbar") 1634 DEBUG_GENERAL("*** Updating statusbar")
1603 if (m_hPaneWnd == NULL) 1635 if (m_hPaneWnd == NULL)
1604 { 1636 {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1655 s_criticalSectionLocal.Unlock(); 1687 s_criticalSectionLocal.Unlock();
1656 1688
1657 return icon; 1689 return icon;
1658 } 1690 }
1659 1691
1660 ATOM CPluginClass::GetAtomPaneClass() 1692 ATOM CPluginClass::GetAtomPaneClass()
1661 { 1693 {
1662 return s_atomPaneClass; 1694 return s_atomPaneClass;
1663 } 1695 }
1664 1696
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld