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

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

Issue 6567422169448448: Issue 119 - Switch to injecting CSS for element hiding (Closed)
Patch Set: Created Dec. 10, 2014, 5:12 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 "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 <dispex.h> 10 #include <dispex.h>
11 #include <Mshtmhst.h> 11 #include <Mshtmhst.h>
12 #include "../shared/Utils.h"
12 13
13 int CPluginTabBase::s_dictionaryVersion = 0; 14 int CPluginTabBase::s_dictionaryVersion = 0;
14 int CPluginTabBase::s_settingsVersion = 1; 15 int CPluginTabBase::s_settingsVersion = 1;
15 int CPluginTabBase::s_filterVersion = 0; 16 int CPluginTabBase::s_filterVersion = 0;
16 int CPluginTabBase::s_whitelistVersion = 0; 17 int CPluginTabBase::s_whitelistVersion = 0;
17 18
18 CPluginTabBase::CPluginTabBase(CPluginClass* plugin) 19 CPluginTabBase::CPluginTabBase(CPluginClass* plugin)
19 : m_plugin(plugin) 20 : m_plugin(plugin)
20 , m_isActivated(false) 21 , m_isActivated(false)
21 , m_continueThreadRunning(true) 22 , m_continueThreadRunning(true)
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 } 109 }
109 DEBUG_GENERAL(L"Going to inject"); 110 DEBUG_GENERAL(L"Going to inject");
110 CComPtr<IDispatch> pDocDispatch; 111 CComPtr<IDispatch> pDocDispatch;
111 browser->get_Document(&pDocDispatch); 112 browser->get_Document(&pDocDispatch);
112 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; 113 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch;
113 if (!pDoc2) 114 if (!pDoc2)
114 { 115 {
115 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt"); 116 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt");
116 return; 117 return;
117 } 118 }
119
118 CComPtr<IHTMLWindow2> pWnd2; 120 CComPtr<IHTMLWindow2> pWnd2;
119 pDoc2->get_parentWindow(&pWnd2); 121 pDoc2->get_parentWindow(&pWnd2);
120 if (!pWnd2) 122 if (!pWnd2)
121 { 123 {
122 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 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to get paren t window");
123 return; 125 return;
124 } 126 }
125 CComQIPtr<IDispatchEx> pWndEx = pWnd2; 127 CComQIPtr<IDispatchEx> pWndEx = pWnd2;
126 if (!pWndEx) 128 if (!pWndEx)
127 { 129 {
(...skipping 19 matching lines...) Expand all
147 params.rgvarg = &var; 149 params.rgvarg = &var;
148 params.rgdispidNamedArgs = 0; 150 params.rgdispidNamedArgs = 0;
149 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0); 151 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0);
150 DEBUG_GENERAL("Invoke"); 152 DEBUG_GENERAL("Invoke");
151 if (FAILED(hr)) 153 if (FAILED(hr))
152 { 154 {
153 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 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");
154 } 156 }
155 } 157 }
156 158
159 bool CPluginTabBase::IsTraverserEnabled()
160 {
161 return !IsCSSInjectionEnabled();
162 }
163
164 bool CPluginTabBase::IsCSSInjectionEnabled()
165 {
166 return IsWindowsVistaOrLater() && CPluginClient::GetInstance()->GetIEVersion() >= 10;
167 }
168
169 namespace
170 {
171 void InjectABPCSS(IHTMLDocument2& htmlDocument2, const std::vector<std::wstrin g>& hideFilters)
172 {
173 ATL::CComPtr<IHTMLElement> stylePureHtmlElement;
174 ATL::CComQIPtr<IHTMLStyleElement> styleHtmlElement;
175 if (FAILED(htmlDocument2.createElement(ATL::CComBSTR(L"style"), &stylePureHt mlElement)))
176 {
177 DEBUG_GENERAL(L"Cannot create style element");
178 return;
179 }
180 if (!(styleHtmlElement = stylePureHtmlElement))
181 {
182 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleElement from IHTMLElement");
183 return;
184 }
185 if (FAILED(styleHtmlElement->put_type(ATL::CComBSTR("text/css"))))
186 {
187 DEBUG_GENERAL(L"Cannot set type text/css");
188 return;
189 }
190 ATL::CComQIPtr<IHTMLStyleSheet> styleSheet;
191 // IHTMLStyleElement2 is availabe starting from IE9, Vista
192 ATL::CComQIPtr<IHTMLStyleElement2> styleHtmlElement2 = styleHtmlElement;
193 if (!styleHtmlElement2)
194 {
195 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleElement2 from IHTMLStyleElement");
196 return;
197 }
198 if (FAILED(styleHtmlElement2->get_sheet(&styleSheet)) || !styleSheet)
199 {
200 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleSheet");
201 return;
202 }
203 // IHTMLStyleSheet4 is availabe starting from IE9, Vista
204 ATL::CComQIPtr<IHTMLStyleSheet4> styleSheet4 = styleSheet;
205 if (!styleSheet4)
206 {
207 DEBUG_GENERAL(L"Cannot obtain IHTMLStyleSheet4");
208 return;
209 }
210 long newIndex = 0;
211 std::wstring cssValue = L"{ display: none !important; }";
212 for (const auto& selector : hideFilters)
213 {
214 auto cssRule = selector + cssValue;
215 ATL::CComBSTR selector(cssRule.size(), cssRule.c_str());
216 if (SUCCEEDED(styleSheet4->insertRule(selector, newIndex, &newIndex)))
217 {
218 ++newIndex;
219 }
220 else
221 {
222 DEBUG_GENERAL(ToCString(L"Cannot add rule for selector " + cssRule));
223 }
224 }
225
226 ATL::CComQIPtr<IHTMLDOMNode> styleNode = stylePureHtmlElement;
227 if (!styleNode)
228 {
229 DEBUG_GENERAL(L"Cannot obtain IHTMLDOMNode from stylePureHtmlElement");
230 return;
231 }
232 ATL::CComPtr<IHTMLElement> headHtmlElement;
233 // IHTMLDocument7 is availabe starting from IE9, Vista
234 ATL::CComQIPtr<IHTMLDocument7> htmlDocument7 = &htmlDocument2;
235 if (!htmlDocument7)
236 {
237 DEBUG_GENERAL(L"Cannot obtain IHTMLDocument7 from htmlDocument2");
238 return;
239 }
240 if (FAILED(htmlDocument7->get_head(&headHtmlElement)))
241 {
242 DEBUG_GENERAL(L"Cannot obtain head from pDoc7");
243 return;
244 }
245 ATL::CComQIPtr<IHTMLDOMNode> headNode = headHtmlElement;
246 if (!headNode)
247 {
248 DEBUG_GENERAL(L"Cannot obtain headNode from headHtmlElement");
249 return;
250 }
251 if (FAILED(headNode->appendChild(styleNode, nullptr)))
252 {
253 DEBUG_GENERAL(L"Cannot append blocking style");
254 }
255 }
256 }
257
157 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser) 258 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser)
158 { 259 {
159 CPluginClient* client = CPluginClient::GetInstance(); 260 if (IsTraverserEnabled())
160 std::wstring url = to_wstring(GetDocumentUrl());
161 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain(u rl))
162 { 261 {
163 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl() ); 262 CPluginClient* client = CPluginClient::GetInstance();
263 std::wstring url = to_wstring(GetDocumentUrl());
264 if (!client->IsWhitelistedUrl(url) && !client->IsElemhideWhitelistedOnDomain (url))
265 {
266 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl ());
267 }
164 } 268 }
165 InjectABP(browser); 269 InjectABP(browser);
166 } 270 }
167 271
168 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur l, bool isDocumentBrowser) 272 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur l, bool isDocumentBrowser)
169 { 273 {
170 CString documentUrl = GetDocumentUrl(); 274 CString documentUrl = GetDocumentUrl();
171 275
172 if (isDocumentBrowser) 276 if (isDocumentBrowser)
173 { 277 {
174 if (url != documentUrl) 278 if (url != documentUrl)
175 { 279 {
176 SetDocumentUrl(url); 280 SetDocumentUrl(url);
177 } 281 }
178 InjectABP(browser); 282 InjectABP(browser);
179 } 283 }
180 if (url.Left(6) != "res://") 284 if (url.Left(6) == "res://")
181 { 285 {
182 // Get document 286 return;
183 CComPtr<IDispatch> pDocDispatch; 287 }
184 HRESULT hr = browser->get_Document(&pDocDispatch); 288 // Get document
185 if (FAILED(hr) || !pDocDispatch) 289 ATL::CComPtr<IDispatch> pDocDispatch;
290 HRESULT hr = browser->get_Document(&pDocDispatch);
291 if (FAILED(hr) || !pDocDispatch)
292 {
293 return;
294 }
295
296 ATL::CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch;
297 if (!pDoc)
298 {
299 return;
300 }
301
302 if (IsCSSInjectionEnabled())
303 {
304 DEBUG_GENERAL(ToCString(L"Inject CSS into ") + url);
305 InjectABPCSS(*pDoc, m_filter->getHideFilters());
306 }
307
308 ATL::CComPtr<IOleObject> pOleObj;
309 pDocDispatch->QueryInterface(IID_IOleObject, (void**)&pOleObj);
310
311 CComPtr<IOleClientSite> pClientSite;
312 pOleObj->GetClientSite(&pClientSite);
313 if (pClientSite != NULL)
314 {
315 CComPtr<IDocHostUIHandler> docHostUIHandler;
316 pClientSite->QueryInterface(IID_IDocHostUIHandler, (void**)&docHostUIHandler );
317 if (docHostUIHandler != NULL)
186 { 318 {
187 return; 319 docHostUIHandler->UpdateUI();
188 }
189
190 CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch;
191 if (!pDoc)
192 {
193 return;
194 }
195 CComPtr<IOleObject> pOleObj;
196
197 pDocDispatch->QueryInterface(IID_IOleObject, (void**)&pOleObj);
198
199
200 CComPtr<IOleClientSite> pClientSite;
201 pOleObj->GetClientSite(&pClientSite);
202 if (pClientSite != NULL)
203 {
204 CComPtr<IDocHostUIHandler> docHostUIHandler;
205 pClientSite->QueryInterface(IID_IDocHostUIHandler, (void**)&docHostUIHandl er);
206 if (docHostUIHandler != NULL)
207 {
208 docHostUIHandler->UpdateUI();
209 }
210 } 320 }
211 } 321 }
212 } 322 }
213 323
214 std::wstring CPluginTabBase::GetDocumentDomain() 324 std::wstring CPluginTabBase::GetDocumentDomain()
215 { 325 {
216 std::wstring domain; 326 std::wstring domain;
217 327
218 m_criticalSection.Lock(); 328 m_criticalSection.Lock();
219 { 329 {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId()); 457 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId());
348 } 458 }
349 459
350 // Non-hanging sleep 460 // Non-hanging sleep
351 Sleep(50); 461 Sleep(50);
352 } 462 }
353 463
354 tabLoopIteration++; 464 tabLoopIteration++;
355 } 465 }
356 } 466 }
OLDNEW

Powered by Google App Engine
This is Rietveld