@@ -306,10 +306,12 @@ bool BrowserFactory::AttachToBrowser(ProcessWindowInfo* process_window_info,
306
306
// ShellWindows might fail if there is an IE modal dialog blocking
307
307
// execution (unverified).
308
308
if (!this ->force_shell_windows_api_ ) {
309
+ LOG (DEBUG) << " Using Active Accessibility to find IWebBrowser2 interface" ;
309
310
attached = this ->AttachToBrowserUsingActiveAccessibility (process_window_info,
310
311
error_message);
311
312
if (!attached) {
312
- LOG (DEBUG) << " Failed to find IWebBrowser2 using ActiveAccessibility: " << *error_message;
313
+ LOG (DEBUG) << " Failed to find IWebBrowser2 using ActiveAccessibility: "
314
+ << *error_message;
313
315
}
314
316
}
315
317
@@ -359,13 +361,28 @@ bool BrowserFactory::AttachToBrowserUsingActiveAccessibility
359
361
process_window_info->dwProcessId ,
360
362
this ->browser_attach_timeout_ );
361
363
return false ;
364
+ } else {
365
+ LOG (DEBUG) << " Found window handle " << process_window_info->hwndBrowser
366
+ << " for window with class 'Internet Explorer_Server' belonging"
367
+ << " to process with id " << process_window_info->dwProcessId ;
362
368
}
363
369
364
370
CComPtr<IHTMLDocument2> document;
365
371
if (this ->GetDocumentFromWindowHandle (process_window_info->hwndBrowser ,
366
372
&document)) {
373
+ int get_parent_window_retry_count = 8 ;
367
374
CComPtr<IHTMLWindow2> window;
368
375
HRESULT hr = document->get_parentWindow (&window);
376
+ while (FAILED (hr) && get_parent_window_retry_count > 0 ) {
377
+ // We know we have a valid document. We *should* be able to do a
378
+ // document.parentWindow call to get the window. However, on the off-
379
+ // chance that the document exists, but IE is slow to initialize all
380
+ // of the COM objects and the full DOM, we'll sleep up to 2 seconds,
381
+ // retrying to get the parent window.
382
+ ::Sleep (250 );
383
+ hr = document->get_parentWindow (&window);
384
+ --get_parent_window_retry_count;
385
+ }
369
386
if (SUCCEEDED (hr)) {
370
387
// http://support.microsoft.com/kb/257717
371
388
CComPtr<IServiceProvider> provider;
@@ -416,9 +433,6 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
416
433
return false ;
417
434
}
418
435
419
- HWND hwnd_browser = NULL ;
420
- CComPtr<IWebBrowser2> browser;
421
-
422
436
clock_t end = clock () + (this ->browser_attach_timeout_ / 1000 * CLOCKS_PER_SEC);
423
437
424
438
CComPtr<IEnumVARIANT> enumerator;
@@ -429,7 +443,7 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
429
443
}
430
444
enumerator->Reset ();
431
445
for (CComVariant shell_window_variant;
432
- enumerator->Next (1 , &shell_window_variant, nullptr ) == S_OK;
446
+ enumerator->Next (1 , &shell_window_variant, NULL ) == S_OK;
433
447
shell_window_variant.Clear ()) {
434
448
435
449
if (shell_window_variant.vt != VT_DISPATCH) {
@@ -448,6 +462,12 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
448
462
&BrowserFactory::FindChildWindowForProcess,
449
463
reinterpret_cast <LPARAM>(process_window_info));
450
464
if (process_window_info->hwndBrowser != NULL ) {
465
+ LOG (DEBUG) << " Found window handle "
466
+ << process_window_info->hwndBrowser
467
+ << " for window with class 'Internet Explorer_Server'"
468
+ << " belonging to process with id "
469
+ << process_window_info->dwProcessId ;
470
+ CComPtr<IWebBrowser2> browser;
451
471
hr = shell_window_variant.pdispVal ->QueryInterface <IWebBrowser2>(&browser);
452
472
if (FAILED (hr)) {
453
473
LOGHR (WARN, hr) << " Found browser window using ShellWindows "
0 commit comments