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

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

Issue 5447868882092032: Issue 1793 - check whether the frame is whitelisted before injecting CSS (Closed)
Patch Set: update Created Jan. 13, 2015, 3:20 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
« src/engine/Main.cpp ('K') | « src/plugin/AdblockPlusClient.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include "PluginClient.h" 3 #include "PluginClient.h"
4 #include "PluginSettings.h" 4 #include "PluginSettings.h"
5 #include "PluginTab.h" 5 #include "PluginTab.h"
6 #include "AdblockPlusDomTraverser.h" 6 #include "AdblockPlusDomTraverser.h"
7 #include "PluginClass.h" 7 #include "PluginClass.h"
8 #include "PluginTabBase.h" 8 #include "PluginTabBase.h"
9 #include "PluginUtil.h" 9 #include "PluginUtil.h"
10 #include "../shared/IE_version.h" 10 #include "../shared/IE_version.h"
11 #include "../shared/Utils.h"
11 #include <dispex.h> 12 #include <dispex.h>
12 #include <Mshtmhst.h> 13 #include <Mshtmhst.h>
13 14
14 int CPluginTabBase::s_dictionaryVersion = 0; 15 int CPluginTabBase::s_dictionaryVersion = 0;
15 int CPluginTabBase::s_settingsVersion = 1; 16 int CPluginTabBase::s_settingsVersion = 1;
16 int CPluginTabBase::s_filterVersion = 0; 17 int CPluginTabBase::s_filterVersion = 0;
17 int CPluginTabBase::s_whitelistVersion = 0; 18 int CPluginTabBase::s_whitelistVersion = 0;
18 19
19 CPluginTabBase::CPluginTabBase(CPluginClass* plugin) 20 CPluginTabBase::CPluginTabBase(CPluginClass* plugin)
20 : m_plugin(plugin) 21 : m_plugin(plugin)
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 110 }
110 DEBUG_GENERAL(L"Going to inject"); 111 DEBUG_GENERAL(L"Going to inject");
111 CComPtr<IDispatch> pDocDispatch; 112 CComPtr<IDispatch> pDocDispatch;
112 browser->get_Document(&pDocDispatch); 113 browser->get_Document(&pDocDispatch);
113 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; 114 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch;
114 if (!pDoc2) 115 if (!pDoc2)
115 { 116 {
116 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt"); 117 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt");
117 return; 118 return;
118 } 119 }
120
119 CComPtr<IHTMLWindow2> pWnd2; 121 CComPtr<IHTMLWindow2> pWnd2;
120 pDoc2->get_parentWindow(&pWnd2); 122 pDoc2->get_parentWindow(&pWnd2);
121 if (!pWnd2) 123 if (!pWnd2)
122 { 124 {
123 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to get paren t window"); 125 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to get paren t window");
124 return; 126 return;
125 } 127 }
126 CComQIPtr<IDispatchEx> pWndEx = pWnd2; 128 CComQIPtr<IDispatchEx> pWndEx = pWnd2;
127 if (!pWndEx) 129 if (!pWndEx)
128 { 130 {
(...skipping 19 matching lines...) Expand all
148 params.rgvarg = &var; 150 params.rgvarg = &var;
149 params.rgdispidNamedArgs = 0; 151 params.rgdispidNamedArgs = 0;
150 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0); 152 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0);
151 DEBUG_GENERAL("Invoke"); 153 DEBUG_GENERAL("Invoke");
152 if (FAILED(hr)) 154 if (FAILED(hr))
153 { 155 {
154 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR EATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to create S ettings in JavaScript"); 156 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR EATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to create S ettings in JavaScript");
155 } 157 }
156 } 158 }
157 159
160 namespace
161 {
162 void InjectABPCSS(IHTMLDocument2& htmlDocument2, const std::vector<std::wstrin g>& hideFilters)
Eric 2015/01/13 19:52:52 We're not distinguishing between fatal errors and
Oleksandr 2015/03/13 10:00:27 On 2015/01/13 19:52:52, Eric wrote: What's not cor
sergei 2015/04/13 08:06:41 I agree this function should not be in this code r
163 {
164 ATL::CComPtr<IHTMLElement> stylePureHtmlElement;
165 ATL::CComQIPtr<IHTMLStyleElement> styleHtmlElement;
166 if (FAILED(htmlDocument2.createElement(ATL::CComBSTR(L"style"), &stylePureHt mlElement)))
167 {
168 DEBUG_GENERAL(L"Cannot create style element");
169 return;
170 }
171 if (!(styleHtmlElement = stylePureHtmlElement))
172 {
173 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleElement from IHTMLElement");
174 return;
175 }
176 if (FAILED(styleHtmlElement->put_type(ATL::CComBSTR("text/css"))))
177 {
178 DEBUG_GENERAL(L"Cannot set type text/css");
179 return;
180 }
181 ATL::CComQIPtr<IHTMLStyleSheet> styleSheet;
182 // IHTMLStyleElement2 is availabe starting from IE9, Vista
183 ATL::CComQIPtr<IHTMLStyleElement2> styleHtmlElement2 = styleHtmlElement;
184 if (!styleHtmlElement2)
185 {
186 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleElement2 from IHTMLStyleElement");
187 return;
188 }
189 if (FAILED(styleHtmlElement2->get_sheet(&styleSheet)) || !styleSheet)
190 {
191 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleSheet");
192 return;
193 }
194 // IHTMLStyleSheet4 is availabe starting from IE9, Vista
195 ATL::CComQIPtr<IHTMLStyleSheet4> styleSheet4 = styleSheet;
196 if (!styleSheet4)
197 {
198 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleSheet4");
199 return;
200 }
201 long newIndex = 0;
202 std::wstring cssValue = L"{ display: none !important; }";
203 for (const auto& selector : hideFilters)
204 {
205 auto cssRule = selector + cssValue;
206 ATL::CComBSTR selector(cssRule.size(), cssRule.c_str());
207 if (SUCCEEDED(styleSheet4->insertRule(selector, newIndex, &newIndex)))
208 {
209 ++newIndex;
210 }
211 else
212 {
213 DEBUG_GENERAL(ToCString(L"Cannot add rule for selector " + cssRule));
214 }
215 }
216
217 ATL::CComQIPtr<IHTMLDOMNode> styleNode = stylePureHtmlElement;
218 if (!styleNode)
219 {
220 DEBUG_GENERAL(L"Cannot obtain IHTMLDOMNode from stylePureHtmlElement");
221 return;
222 }
223 ATL::CComPtr<IHTMLElement> headHtmlElement;
224 // IHTMLDocument7 is availabe starting from IE9, Vista
225 ATL::CComQIPtr<IHTMLDocument7> htmlDocument7 = &htmlDocument2;
226 if (!htmlDocument7)
227 {
228 DEBUG_GENERAL(L"Cannot obtain IHTMLDocument7 from htmlDocument2");
229 return;
230 }
231 if (FAILED(htmlDocument7->get_head(&headHtmlElement)))
232 {
233 DEBUG_GENERAL(L"Cannot obtain head from pDoc7");
234 return;
235 }
236 ATL::CComQIPtr<IHTMLDOMNode> headNode = headHtmlElement;
237 if (!headNode)
238 {
239 DEBUG_GENERAL(L"Cannot obtain headNode from headHtmlElement");
240 return;
241 }
242 if (FAILED(headNode->appendChild(styleNode, nullptr)))
243 {
244 DEBUG_GENERAL(L"Cannot append blocking style");
245 }
246 }
247 }
248
158 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser) 249 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser)
159 { 250 {
160 CPluginClient* client = CPluginClient::GetInstance(); 251 CPluginClient* client = CPluginClient::GetInstance();
161 std::wstring url = to_wstring(GetDocumentUrl()); 252 std::wstring url = to_wstring(GetDocumentUrl());
162 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain(u rl)) 253 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain(u rl))
163 { 254 {
164 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl() ); 255 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl() );
165 } 256 }
166 InjectABP(browser); 257 InjectABP(browser);
167 } 258 }
168 259
260 ATL::CComPtr<IWebBrowser2> GetParent(const ATL::CComPtr<IWebBrowser2>& browser)
Eric 2015/01/13 19:52:52 Since you're not copying the CComPtr and incurring
sergei 2015/04/13 08:06:41 fixed to use `IWebBrowser2&`.
261 {
262 ATL::CComPtr<IWebBrowser2> retValue;
263 ATL::CComPtr<IDispatch> browserParentDispatch;
264 if (FAILED(browser->get_Parent(&browserParentDispatch)) || !browserParentDispa tch)
265 {
266 return retValue;
Eric 2015/01/13 19:52:52 "return nullptr" is much clearer here.
sergei 2015/04/13 08:06:41 fixed
267 }
268 // The InternetExplorer application always returns a pointer to itself.
269 if (browserParentDispatch.IsEqualObject(browser))
270 {
271 return retValue;
Eric 2015/01/13 19:52:52 As before "return nullptr"
sergei 2015/04/13 08:06:41 fixed
272 }
273 ATL::CComQIPtr<IServiceProvider> parentDocumentServiceProvider = browserParent Dispatch;
274 if (!parentDocumentServiceProvider)
275 {
276 return retValue;
Eric 2015/01/13 19:52:52 return nullptr
sergei 2015/04/13 08:06:41 fixed
277 }
Eric 2015/01/13 19:52:52 This is the right place to declare 'retValue'. As
sergei 2015/04/13 08:06:41 fixed
278 if (FAILED(parentDocumentServiceProvider->QueryService(IID_IWebBrowserApp, &re tValue)) || !retValue)
Oleksandr 2015/03/13 10:00:27 Why is this querying for IWebBrowserApp and not IW
sergei 2015/04/13 08:06:41 fixed
279 {
280 // error
Eric 2015/01/13 19:52:52 You've got to do something here, since a failed CO
Oleksandr 2015/03/13 10:00:27 return nullptr would be better IMO
sergei 2015/04/13 08:06:41 I don't think we should expect undefined state of
281 }
282 return retValue;
283 }
284
285 bool IsFrameWhiteListed(ATL::CComPtr<IWebBrowser2> frame)
286 {
287 if (!frame)
288 {
289 return false;
290 }
291 CPluginClient* client = CPluginClient::GetInstance();
292 if (!client)
293 {
294 return false;
Eric 2015/01/13 19:52:52 This is really a pretty serious error. Personally
295 }
296 auto url = GetLocationUrl(*frame);
297 std::vector<std::string> frameHierarchy;
298 for(frame = GetParent(frame); frame; frame = GetParent(frame))
299 {
300 frameHierarchy.push_back(ToUtf8String(GetLocationUrl(*frame)));
301 }
302 return client->IsWhitelistedUrl(url, frameHierarchy)
303 || client->IsElemhideWhitelistedOnDomain(url, frameHierarchy);
304 }
305
169 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur l, bool isDocumentBrowser) 306 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur l, bool isDocumentBrowser)
170 { 307 {
171 CString documentUrl = GetDocumentUrl(); 308 CString documentUrl = GetDocumentUrl();
172 309
173 if (isDocumentBrowser) 310 if (isDocumentBrowser)
174 { 311 {
175 if (url != documentUrl) 312 if (url != documentUrl)
176 { 313 {
177 SetDocumentUrl(url); 314 SetDocumentUrl(url);
178 } 315 }
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId()); 485 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId());
349 } 486 }
350 487
351 // Non-hanging sleep 488 // Non-hanging sleep
352 Sleep(50); 489 Sleep(50);
353 } 490 }
354 491
355 tabLoopIteration++; 492 tabLoopIteration++;
356 } 493 }
357 } 494 }
OLDNEW
« src/engine/Main.cpp ('K') | « src/plugin/AdblockPlusClient.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld