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

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

Issue 5447868882092032: Issue 1793 - check whether the frame is whitelisted before injecting CSS (Closed)
Left Patch Set: fix according to comments Created May 15, 2015, 11:55 a.m.
Right Patch Set: rebase and rename webBrowser to parentBrowser Created Nov. 30, 2015, 3:27 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/AdblockPlusClient.cpp ('k') | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "PluginStdAfx.h" 18 #include "PluginStdAfx.h"
19 19 #include "AdblockPlusClient.h"
20 #include "PluginClient.h" 20 #include "PluginClientBase.h"
21 #include "PluginSettings.h" 21 #include "PluginSettings.h"
22 #include "AdblockPlusDomTraverser.h" 22 #include "AdblockPlusDomTraverser.h"
23 #include "PluginClass.h"
24 #include "PluginTabBase.h" 23 #include "PluginTabBase.h"
25 #include "PluginUtil.h" 24 #include "IeVersion.h"
26 #include "../shared/IE_version.h" 25 #include "../shared/Utils.h"
27 #include <dispex.h>
28 #include <Mshtmhst.h> 26 #include <Mshtmhst.h>
29 27
30 CPluginTabBase::CPluginTabBase(CPluginClass* plugin) 28 CPluginTabBase::CPluginTabBase(CPluginClass* plugin)
31 : m_plugin(plugin) 29 : m_plugin(plugin)
32 , m_isActivated(false) 30 , m_isActivated(false)
33 , m_continueThreadRunning(true) 31 , m_continueThreadRunning(true)
34 { 32 {
35 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 33 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
36 m_filter->hideFiltersLoadedEvent = CreateEvent(NULL, true, false, NULL); 34 m_filter->hideFiltersLoadedEvent = CreateEvent(NULL, true, false, NULL);
37 35
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 filterLoaderThread.detach(); // TODO: but actually we should wait for the th read in the dtr. 102 filterLoaderThread.detach(); // TODO: but actually we should wait for the th read in the dtr.
105 } 103 }
106 catch (const std::system_error& ex) 104 catch (const std::system_error& ex)
107 { 105 {
108 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_THREAD_CRE ATE_PROCESS, 106 DEBUG_SYSTEM_EXCEPTION(ex, PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_THREAD_CRE ATE_PROCESS,
109 "Class::Thread - Failed to start filter loader thread"); 107 "Class::Thread - Failed to start filter loader thread");
110 } 108 }
111 m_traverser->ClearCache(); 109 m_traverser->ClearCache();
112 } 110 }
113 111
112 namespace
113 {
114 /**
115 * Determine if the HTML file is one of ours.
116 * The criterion is that it appear in the "html/templates" folder within our i nstallation.
117 *
118 * Warning: This function may fail if the argument is not a "file://" URL.
119 * This is occasionally the case in circumstances yet to be characterized.
120 */
121 bool IsOurHtmlFile(const std::wstring& url)
122 {
123 // Declared static because the value is derived from an installation directo ry, which won't change during run-time.
124 static auto dir = FileUrl(HtmlFolderPath());
125
126 DEBUG_GENERAL([&]() -> std::wstring {
127 std::wstring log = L"InjectABP. Current URL: ";
128 log += url;
129 log += L", template directory URL: ";
130 log += dir;
131 return log;
132 }());
133
134 /*
135 * The length check here is defensive, in case the document URL is truncated for some reason.
136 */
137 if (url.length() < 5)
138 {
139 // We can't match ".html" at the end of the URL if it's too short.
140 return false;
141 }
142 auto urlCstr = url.c_str();
143 // Check the prefix to match our directory
144 // Check the suffix to be an HTML file
145 return (_wcsnicmp(urlCstr, dir.c_str(), dir.length()) == 0) &&
146 (_wcsnicmp(urlCstr + url.length() - 5, L".html", 5) == 0);
147 }
148 }
149
114 void CPluginTabBase::InjectABP(IWebBrowser2* browser) 150 void CPluginTabBase::InjectABP(IWebBrowser2* browser)
115 { 151 {
116 CriticalSection::Lock lock(m_csInject); 152 CriticalSection::Lock lock(m_csInject);
117 auto url = GetDocumentUrl(); 153 auto url = GetDocumentUrl();
118 154 if (!IsOurHtmlFile(url))
119 std::wstring log = L"InjectABP. Current URL: "; 155 {
120 log += url; 156 DEBUG_GENERAL(L"InjectABP. Not injecting");
121 log += L", settings URL: ";
122 log += UserSettingsFileUrl();
123 DEBUG_GENERAL(log);
124
125 CString urlLegacy = ToCString(url);
126 if (!(0 == urlLegacy.CompareNoCase(CString(UserSettingsFileUrl().c_str())) ||
127 0 == urlLegacy.CompareNoCase(CString(FirstRunPageFileUrl().c_str()))))
128 {
129 DEBUG_GENERAL(L"Not injecting");
130 return; 157 return;
131 } 158 }
132 DEBUG_GENERAL(L"Going to inject"); 159 DEBUG_GENERAL(L"InjectABP. Injecting");
133 CComPtr<IDispatch> pDocDispatch; 160 CComPtr<IDispatch> pDocDispatch;
134 browser->get_Document(&pDocDispatch); 161 browser->get_Document(&pDocDispatch);
135 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; 162 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch;
136 if (!pDoc2) 163 if (!pDoc2)
137 { 164 {
138 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt"); 165 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt");
139 return; 166 return;
140 } 167 }
141 CComPtr<IHTMLWindow2> pWnd2; 168 CComPtr<IHTMLWindow2> pWnd2;
142 pDoc2->get_parentWindow(&pWnd2); 169 pDoc2->get_parentWindow(&pWnd2);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 namespace 207 namespace
181 { 208 {
182 ATL::CComPtr<IWebBrowser2> GetParent(IWebBrowser2& browser) 209 ATL::CComPtr<IWebBrowser2> GetParent(IWebBrowser2& browser)
183 { 210 {
184 ATL::CComPtr<IDispatch> parentDispatch; 211 ATL::CComPtr<IDispatch> parentDispatch;
185 if (FAILED(browser.get_Parent(&parentDispatch)) || !parentDispatch) 212 if (FAILED(browser.get_Parent(&parentDispatch)) || !parentDispatch)
186 { 213 {
187 return nullptr; 214 return nullptr;
188 } 215 }
189 // The InternetExplorer application always returns a pointer to itself. 216 // The InternetExplorer application always returns a pointer to itself.
217 // https://msdn.microsoft.com/en-us/library/aa752136(v=vs.85).aspx
190 if (parentDispatch.IsEqualObject(&browser)) 218 if (parentDispatch.IsEqualObject(&browser))
191 { 219 {
192 return nullptr; 220 return nullptr;
193 } 221 }
194 ATL::CComQIPtr<IServiceProvider> parentDocumentServiceProvider = parentDispa tch; 222 ATL::CComQIPtr<IServiceProvider> parentDocumentServiceProvider = parentDispa tch;
195 if (!parentDocumentServiceProvider) 223 if (!parentDocumentServiceProvider)
196 { 224 {
197 return nullptr; 225 return nullptr;
198 } 226 }
199 ATL::CComPtr<IWebBrowserApp> webBrowserApp; 227 ATL::CComPtr<IWebBrowser2> parentBrowser;
200 if (FAILED(parentDocumentServiceProvider->QueryService(IID_IWebBrowserApp, & webBrowserApp)) || !webBrowserApp) 228 if (FAILED(parentDocumentServiceProvider->QueryService(SID_SWebBrowserApp, & parentBrowser)))
201 { 229 {
202 return nullptr; 230 return nullptr;
203 } 231 }
204 return ATL::CComQIPtr<IWebBrowser2>(webBrowserApp); 232 return parentBrowser;
205 } 233 }
206 234
207 bool IsFrameWhiteListed(ATL::CComPtr<IWebBrowser2> frame) 235 bool IsFrameWhiteListed(ATL::CComPtr<IWebBrowser2> frame)
208 { 236 {
209 if (!frame) 237 if (!frame)
210 { 238 {
211 return false; 239 return false;
212 } 240 }
213 auto url = GetLocationUrl(*frame); 241 auto url = GetLocationUrl(*frame);
214 std::vector<std::string> frameHierarchy; 242 std::vector<std::string> frameHierarchy;
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 LogQueue::LogPluginError(pluginError.GetErrorCode(), pluginError.GetEr rorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), true, p luginError.GetProcessId(), pluginError.GetThreadId()); 432 LogQueue::LogPluginError(pluginError.GetErrorCode(), pluginError.GetEr rorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), true, p luginError.GetProcessId(), pluginError.GetThreadId());
405 } 433 }
406 434
407 // Non-hanging sleep 435 // Non-hanging sleep
408 Sleep(50); 436 Sleep(50);
409 } 437 }
410 438
411 tabLoopIteration++; 439 tabLoopIteration++;
412 } 440 }
413 } 441 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld