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

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

Issue 5750789393874944: [IE] First round of ATL removal (Closed)
Patch Set: Created June 20, 2014, 9:22 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 #ifdef SUPPORT_CONFIG 5 #ifdef SUPPORT_CONFIG
6 #include "PluginConfig.h" 6 #include "PluginConfig.h"
7 #endif 7 #endif
8 #include "PluginTab.h" 8 #include "PluginTab.h"
9 #include "PluginDomTraverser.h" 9 #include "PluginDomTraverser.h"
10 #include "PluginClass.h" 10 #include "PluginClass.h"
(...skipping 28 matching lines...) Expand all
39 { 39 {
40 m_isActivated = true; 40 m_isActivated = true;
41 } 41 }
42 42
43 try 43 try
44 { 44 {
45 m_thread = std::thread(&CPluginTabBase::ThreadProc, this); 45 m_thread = std::thread(&CPluginTabBase::ThreadProc, this);
46 } 46 }
47 catch (const std::system_error& ex) 47 catch (const std::system_error& ex)
48 { 48 {
49 auto errDescription = std::string("Tab::Thread - Failed to create tab thread ") + 49 std::wstring errDescription( L"Tab::Thread - Failed to create tab thread" );
50 ex.code().message() + ex.what(); 50 errDescription += ABP::debug::widen( ex.code().message() + ex.what() );
51 DEBUG_ERROR_LOG(ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_TAB_THR EAD_CREATE_PROCESS, errDescription.c_str()); 51 DEBUG_ERROR_LOG( ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_TAB_TH READ_CREATE_PROCESS, errDescription );
52 } 52 }
53 53
54 #ifdef SUPPORT_DOM_TRAVERSER 54 #ifdef SUPPORT_DOM_TRAVERSER
55 m_traverser = new CPluginDomTraverser(static_cast<CPluginTab*>(this)); 55 m_traverser = new CPluginDomTraverser(static_cast<CPluginTab*>(this));
56 #endif // SUPPORT_DOM_TRAVERSER 56 #endif // SUPPORT_DOM_TRAVERSER
57 } 57 }
58 58
59 59
60 CPluginTabBase::~CPluginTabBase() 60 CPluginTabBase::~CPluginTabBase()
61 { 61 {
(...skipping 16 matching lines...) Expand all
78 78
79 void CPluginTabBase::OnUpdate() 79 void CPluginTabBase::OnUpdate()
80 { 80 {
81 m_isActivated = true; 81 m_isActivated = true;
82 } 82 }
83 83
84 namespace 84 namespace
85 { 85 {
86 void FilterLoader(CPluginTabBase* tabBase) 86 void FilterLoader(CPluginTabBase* tabBase)
87 { 87 {
88 tabBase->m_filter->LoadHideFilters(CPluginClient::GetInstance()->GetElementH idingSelectors(tabBase->GetDocumentDomain().GetString())); 88 tabBase->m_filter->LoadHideFilters(CPluginClient::GetInstance()->GetElementHid ingSelectors(tabBase->GetDocumentDomain()));
89 SetEvent(tabBase->m_filter->hideFiltersLoadedEvent); 89 SetEvent(tabBase->m_filter->hideFiltersLoadedEvent);
90 } 90 }
91 } 91 }
92 92
93 void CPluginTabBase::OnNavigate(const CString& url) 93 void CPluginTabBase::OnNavigate(const std::wstring & url)
94 { 94 {
95 SetDocumentUrl(url); 95 SetDocumentUrl(url);
96 96
97
98 #ifdef SUPPORT_FRAME_CACHING 97 #ifdef SUPPORT_FRAME_CACHING
99 ClearFrameCache(GetDocumentDomain()); 98 ClearFrameCache(GetDocumentDomain());
100 #endif 99 #endif
101 100
102 std::wstring domainString = GetDocumentDomain(); 101 std::wstring domainString = GetDocumentDomain();
103 ResetEvent(m_filter->hideFiltersLoadedEvent); 102 ResetEvent(m_filter->hideFiltersLoadedEvent);
104 try 103 try
105 { 104 {
106 std::thread filterLoaderThread(&FilterLoader, this); 105 std::thread filterLoaderThread(&FilterLoader, this);
107 filterLoaderThread.detach(); // TODO: but actually we should wait for the th read in the dtr. 106 filterLoaderThread.detach(); // TODO: but actually we should wait for the th read in the dtr.
108 } 107 }
109 catch (const std::system_error& ex) 108 catch (const std::system_error& ex)
110 { 109 {
111 auto errDescription = std::string("Class::Thread - Failed to start filter lo ader thread, ") + 110 std::wstring errDescription( L"Class::Thread - Failed to start filter loader thread, ");
112 ex.code().message() + ex.what(); 111 errDescription += ABP::debug::widen( ex.code().message() + ex.what() );
113 DEBUG_ERROR_LOG(ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_TH READ_CREATE_PROCESS, errDescription.c_str()); 112 DEBUG_ERROR_LOG( ex.code().value(), PLUGIN_ERROR_THREAD, PLUGIN_ERROR_MAIN_T HREAD_CREATE_PROCESS, errDescription );
114 } 113 }
115 114
116 #ifdef SUPPORT_DOM_TRAVERSER 115 #ifdef SUPPORT_DOM_TRAVERSER
117 m_traverser->ClearCache(); 116 m_traverser->ClearCache();
118 #endif 117 #endif
119 } 118 }
120 119
120 namespace {
121 }
122
121 void CPluginTabBase::InjectABP(IWebBrowser2* browser) 123 void CPluginTabBase::InjectABP(IWebBrowser2* browser)
122 { 124 {
123 CriticalSection::Lock lock(m_csInject); 125 CriticalSection::Lock lock(m_csInject);
124 CString url = GetDocumentUrl(); 126 std::wstring url = GetDocumentUrl();
125 CString log; 127
126 log.Format(L"InjectABP. Current URL: %s, settings URL: %s", url, UserSettingsF ileUrl().c_str()); 128 std::wostringstream log;
127 DEBUG_GENERAL(log); 129 log << L"InjectABP. Current URL: " << url << L", settings URL: " << UserSettin gsFileUrl();
128 if (!(0 == url.CompareNoCase(CString(UserSettingsFileUrl().c_str())) || 130 DEBUG_GENERAL( log.str() );
129 0 == url.CompareNoCase(CString(FirstRunPageFileUrl().c_str())))) 131
132 if( !ABP::util::wstring_equal_ci( url, UserSettingsFileUrl() )
133 && !ABP::util::wstring_equal_ci( url, FirstRunPageFileUrl() ) )
130 { 134 {
131 DEBUG_GENERAL(L"Not injecting"); 135 DEBUG_GENERAL(L"Not injecting");
132 return; 136 return;
133 } 137 }
134 DEBUG_GENERAL(L"Going to inject"); 138 DEBUG_GENERAL(L"Going to inject");
135 CComPtr<IDispatch> pDocDispatch; 139 CComPtr<IDispatch> pDocDispatch;
136 browser->get_Document(&pDocDispatch); 140 browser->get_Document(&pDocDispatch);
137 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch; 141 CComQIPtr<IHTMLDocument2> pDoc2 = pDocDispatch;
138 if (!pDoc2) 142 if (!pDoc2)
139 { 143 {
140 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI docume nt"); 144 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, L"CPluginTabBase::InjectABP - Failed to QI docum ent");
141 return; 145 return;
142 } 146 }
143 CComPtr<IHTMLWindow2> pWnd2; 147 CComPtr<IHTMLWindow2> pWnd2;
144 pDoc2->get_parentWindow(&pWnd2); 148 pDoc2->get_parentWindow(&pWnd2);
145 if (!pWnd2) 149 if (!pWnd2)
146 { 150 {
147 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to get paren t window"); 151 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, L"CPluginTabBase::InjectABP - Failed to get pare nt window");
148 return; 152 return;
149 } 153 }
150 CComQIPtr<IDispatchEx> pWndEx = pWnd2; 154 CComQIPtr<IDispatchEx> pWndEx = pWnd2;
151 if (!pWndEx) 155 if (!pWndEx)
152 { 156 {
153 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to QI dispat ch"); 157 DEBUG_ERROR_LOG(0, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CRE ATE_SETTINGS_JAVASCRIPT_INVOKE, L"CPluginTabBase::InjectABP - Failed to QI dispa tch");
154 return; 158 return;
155 } 159 }
156 // Create "Settings" object in JavaScript. 160 // Create "Settings" object in JavaScript.
157 // A method call of "Settings" in JavaScript, transfered to "Invoke" of m_plug inUserSettings 161 // A method call of "Settings" in JavaScript, transfered to "Invoke" of m_plug inUserSettings
158 DISPID dispid; 162 DISPID dispid;
159 HRESULT hr = pWndEx->GetDispID(L"Settings", fdexNameEnsure, &dispid); 163 HRESULT hr = pWndEx->GetDispID(L"Settings", fdexNameEnsure, &dispid);
160 if (FAILED(hr)) 164 if (FAILED(hr))
161 { 165 {
162 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR EATE_SETTINGS_JAVASCRIPT_INVOKE, "CPluginTabBase::InjectABP - Failed to get disp atch"); 166 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR EATE_SETTINGS_JAVASCRIPT_INVOKE, L"CPluginTabBase::InjectABP - Failed to get dis patch");
163 return; 167 return;
164 } 168 }
165 CComVariant var((IDispatch*)&m_pluginUserSettings);
166 169
167 DEBUG_GENERAL("Injecting"); 170 DEBUG_GENERAL(L"Injecting");
171
172 VARIANT var;
173 var.vt = VT_DISPATCH;
174 var.pdispVal = &m_pluginUserSettings;
168 175
169 DISPPARAMS params; 176 DISPPARAMS params;
170 params.cArgs = 1; 177 params.cArgs = 1;
171 params.cNamedArgs = 0; 178 params.cNamedArgs = 0;
172 params.rgvarg = &var; 179 params.rgvarg = &var;
173 params.rgdispidNamedArgs = 0; 180 params.rgdispidNamedArgs = 0;
174 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0); 181 hr = pWndEx->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPU T | DISPATCH_PROPERTYPUTREF, &params, 0, 0, 0);
175 DEBUG_GENERAL("Invoke"); 182 DEBUG_GENERAL(L"Invoke");
176 if (FAILED(hr)) 183 if (FAILED(hr))
177 { 184 {
178 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"); 185 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_CREATE_SETTINGS_JAVASCRIPT, PLUGIN_ERROR_CR EATE_SETTINGS_JAVASCRIPT_INVOKE, L"CPluginTabBase::InjectABP - Failed to create Settings in JavaScript");
179 } 186 }
180 } 187 }
181 188
182 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser) 189 void CPluginTabBase::OnDownloadComplete(IWebBrowser2* browser)
183 { 190 {
184 #ifdef SUPPORT_DOM_TRAVERSER 191 #ifdef SUPPORT_DOM_TRAVERSER
185 if (!CPluginClient::GetInstance()->IsWhitelistedUrl(std::wstring(GetDocumentUr l()))) 192 if (!CPluginClient::GetInstance()->IsWhitelistedUrl(GetDocumentUrl()))
186 { 193 {
187 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl() ); 194 m_traverser->TraverseDocument(browser, GetDocumentDomain(), GetDocumentUrl() .c_str());
188 } 195 }
189 #endif // SUPPORT_DOM_TRAVERSER 196 #endif // SUPPORT_DOM_TRAVERSER
190 197
191 InjectABP(browser); 198 InjectABP(browser);
192 } 199 }
193 200
194 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const CString& ur l, bool isDocumentBrowser) 201 void CPluginTabBase::OnDocumentComplete(IWebBrowser2* browser, const std::wstrin g url, bool isDocumentBrowser)
195 { 202 {
196 CString documentUrl = GetDocumentUrl(); 203 std::wstring documentUrl = GetDocumentUrl();
197 204
198 if (isDocumentBrowser) 205 if (isDocumentBrowser)
199 { 206 {
200 if (url != documentUrl) 207 if (url != documentUrl)
201 { 208 {
202 SetDocumentUrl(url); 209 SetDocumentUrl( url );
203 } 210 }
204 InjectABP(browser); 211 InjectABP(browser);
205 } 212 }
206 213
207 #ifdef SUPPORT_DOM_TRAVERSER 214 #ifdef SUPPORT_DOM_TRAVERSER
208 if (url.Left(6) != "res://") 215 if ( !ABP::util::begins_with( url, L"res://" ) )
209 { 216 {
210 // Get document 217 // Get document
211 CComPtr<IDispatch> pDocDispatch; 218 CComPtr<IDispatch> pDocDispatch;
212 HRESULT hr = browser->get_Document(&pDocDispatch); 219 HRESULT hr = browser->get_Document(&pDocDispatch);
213 if (FAILED(hr) || !pDocDispatch) 220 if (FAILED(hr) || !pDocDispatch)
214 { 221 {
215 return; 222 return;
216 } 223 }
217 224
218 CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch; 225 CComQIPtr<IHTMLDocument2> pDoc = pDocDispatch;
(...skipping 17 matching lines...) Expand all
236 docHostUIHandler->UpdateUI(); 243 docHostUIHandler->UpdateUI();
237 } 244 }
238 } 245 }
239 246
240 pDoc.Release(); 247 pDoc.Release();
241 pDocDispatch.Release(); 248 pDocDispatch.Release();
242 } 249 }
243 #endif 250 #endif
244 } 251 }
245 252
246 CString CPluginTabBase::GetDocumentDomain() 253 std::wstring CPluginTabBase::GetDocumentDomain()
247 { 254 {
248 CString domain; 255 std::wstring domain;
249 256
250 m_criticalSection.Lock(); 257 m_criticalSection.Lock();
251 { 258 {
252 domain = m_documentDomain; 259 domain = m_documentDomain;
253 } 260 }
254 m_criticalSection.Unlock(); 261 m_criticalSection.Unlock();
255 262
256 return domain; 263 return domain;
257 } 264 }
258 265
259 void CPluginTabBase::SetDocumentUrl(const CString& url) 266 void CPluginTabBase::SetDocumentUrl(const std::wstring & url)
260 { 267 {
261 m_criticalSection.Lock(); 268 m_criticalSection.Lock();
262 { 269 {
263 m_documentUrl = url; 270 m_documentUrl = url;
264 m_documentDomain = CString(CAdblockPlusClient::GetInstance()->GetHostFromUrl (url.GetString()).c_str()); 271 m_documentDomain = CAdblockPlusClient::GetInstance()->GetHostFromUrl(url);
265 } 272 }
266 m_criticalSection.Unlock(); 273 m_criticalSection.Unlock();
267 } 274 }
268 275
269 CString CPluginTabBase::GetDocumentUrl() 276 std::wstring CPluginTabBase::GetDocumentUrl()
270 { 277 {
271 CString url; 278 std::wstring url;
272 279
273 m_criticalSection.Lock(); 280 m_criticalSection.Lock();
274 { 281 {
275 url = m_documentUrl; 282 url = m_documentUrl;
276 } 283 }
277 m_criticalSection.Unlock(); 284 m_criticalSection.Unlock();
278 285
279 return url; 286 return url;
280 } 287 }
281 288
282 289
283 // ============================================================================ 290 // ============================================================================
284 // Frame caching 291 // Frame caching
285 // ============================================================================ 292 // ============================================================================
286 293
287 #ifdef SUPPORT_FRAME_CACHING 294 #ifdef SUPPORT_FRAME_CACHING
288 295
289 bool CPluginTabBase::IsFrameCached(const CString& url) 296 bool CPluginTabBase::IsFrameCached(const std::wstring & url)
290 { 297 {
291 bool isFrame; 298 bool isFrame;
292 299
293 m_criticalSectionCache.Lock(); 300 m_criticalSectionCache.Lock();
294 { 301 {
295 isFrame = m_cacheFrames.find(url) != m_cacheFrames.end(); 302 isFrame = m_cacheFrames.find(url) != m_cacheFrames.end();
296 } 303 }
297 m_criticalSectionCache.Unlock(); 304 m_criticalSectionCache.Unlock();
298 305
299 return isFrame; 306 return isFrame;
300 } 307 }
301 308
302 void CPluginTabBase::CacheFrame(const CString& url) 309 void CPluginTabBase::CacheFrame(const std::wstring & url)
303 { 310 {
304 m_criticalSectionCache.Lock(); 311 m_criticalSectionCache.Lock();
305 { 312 {
306 m_cacheFrames.insert(url); 313 m_cacheFrames.insert(url);
307 } 314 }
308 m_criticalSectionCache.Unlock(); 315 m_criticalSectionCache.Unlock();
309 } 316 }
310 317
311 void CPluginTabBase::ClearFrameCache(const CString& domain) 318 void CPluginTabBase::ClearFrameCache(const std::wstring & domain)
312 { 319 {
313 m_criticalSectionCache.Lock(); 320 m_criticalSectionCache.Lock();
314 { 321 {
315 if (domain.IsEmpty() || domain != m_cacheDomain) 322 if (domain.empty() || domain != m_cacheDomain)
316 { 323 {
317 m_cacheFrames.clear(); 324 m_cacheFrames.clear();
318 m_cacheDomain = domain; 325 m_cacheDomain = domain;
319 } 326 }
320 } 327 }
321 m_criticalSectionCache.Unlock(); 328 m_criticalSectionCache.Unlock();
322 } 329 }
323 330
324 #endif // SUPPORT_FRAME_CACHING 331 #endif // SUPPORT_FRAME_CACHING
325 332
326 333
327 void CPluginTabBase::ThreadProc() 334 void CPluginTabBase::ThreadProc()
328 { 335 {
329 // Force loading/creation of settings 336 // Force loading/creation of settings
330 CPluginSettings* settings = CPluginSettings::GetInstance(); 337 CPluginSettings* settings = CPluginSettings::GetInstance();
331 338
332 settings->SetWorkingThreadId(); 339 settings->SetWorkingThreadId();
333 340
334 CString threadInfo; 341 std::wostringstream thread_info;
335 threadInfo.Format(L"%d.%d", ::GetCurrentProcessId(), ::GetCurrentThreadId()); 342 thread_info << ::GetCurrentProcessId() << L"." << ::GetCurrentThreadId();
336 343
337 CString debugText; 344 std::wstring t[] = {
338 345 L"========================================================================== ======",
339 debugText += L"=============================================================== ================="; 346 L"TAB THREAD " + thread_info.str(),
340 debugText += L"\nTAB THREAD " + threadInfo; 347 L"========================================================================== ======",
341 debugText += L"\n============================================================= ==================="; 348 };
342 349 DEBUG_GENERAL(t);
343 DEBUG_GENERAL(debugText)
344 350
345 // -------------------------------------------------------------------- 351 // --------------------------------------------------------------------
346 // Tab loop 352 // Tab loop
347 // -------------------------------------------------------------------- 353 // --------------------------------------------------------------------
348 354
349 DWORD loopCount = 0; 355 DWORD loopCount = 0;
350 DWORD tabLoopIteration = 1; 356 DWORD tabLoopIteration = 1;
351 357
352 while (this->m_continueThreadRunning) 358 while (this->m_continueThreadRunning)
353 { 359 {
354 #ifdef ENABLE_DEBUG_THREAD 360 #ifdef ENABLE_DEBUG_THREAD
355 CStringA sTabLoopIteration; 361 std::wostringstream message;
356 sTabLoopIteration.Format("%u", tabLoopIteration); 362 message << L"Loop iteration " << tabLoopIteration;
357 363 DEBUG_THREAD( L"------------------------------------------------------------ --------------------" );
358 DEBUG_THREAD("-------------------------------------------------------------- ------------------") 364 DEBUG_THREAD( message.str() );
359 DEBUG_THREAD("Loop iteration " + sTabLoopIteration); 365 DEBUG_THREAD( L"------------------------------------------------------------ --------------------" );
360 DEBUG_THREAD("-------------------------------------------------------------- ------------------")
361 #endif 366 #endif
362 if (this->m_isActivated) 367 if (this->m_isActivated)
363 { 368 {
364 bool isChanged = false; 369 bool isChanged = false;
365 370
366 if (isChanged) 371 if (isChanged)
367 { 372 {
368 this->m_plugin->UpdateStatusBar(); 373 this->m_plugin->UpdateStatusBar();
369 } 374 }
370 375
(...skipping 14 matching lines...) Expand all
385 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId()); 390 CPluginClient::LogPluginError(pluginError.GetErrorCode(), pluginError. GetErrorId(), pluginError.GetErrorSubid(), pluginError.GetErrorDescription(), tr ue, pluginError.GetProcessId(), pluginError.GetThreadId());
386 } 391 }
387 392
388 // Non-hanging sleep 393 // Non-hanging sleep
389 Sleep(50); 394 Sleep(50);
390 } 395 }
391 396
392 tabLoopIteration++; 397 tabLoopIteration++;
393 } 398 }
394 } 399 }
OLDNEW

Powered by Google App Engine
This is Rietveld