| 444 | } |
| 445 | |
| 446 | bool DocumentHost::SetFocusToBrowser() { |
| 447 | LOG(TRACE) << "Entering DocumentHost::SetFocusToBrowser"; |
| 448 | |
| 449 | HWND top_level_window_handle = this->GetTopLevelWindowHandle(); |
| 450 | HWND foreground_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); |
| 451 | if (foreground_window != top_level_window_handle) { |
| 452 | LOG(TRACE) << "Top-level IE window is " << top_level_window_handle |
| 453 | << " foreground window is " << foreground_window; |
| 454 | CComPtr<IUIAutomation> ui_automation; |
| 455 | HRESULT hr = ::CoCreateInstance(CLSID_CUIAutomation, |
| 456 | NULL, |
| 457 | CLSCTX_INPROC_SERVER, |
| 458 | IID_IUIAutomation, |
| 459 | reinterpret_cast<void**>(&ui_automation)); |
| 460 | if (SUCCEEDED(hr)) { |
| 461 | LOG(TRACE) << "Using UI Automation to set window focus"; |
| 462 | CComPtr<IUIAutomationElement> parent_window; |
| 463 | hr = ui_automation->ElementFromHandle(top_level_window_handle, |
| 464 | &parent_window); |
| 465 | if (SUCCEEDED(hr)) { |
| 466 | CComPtr<IUIAutomationWindowPattern> window_pattern; |
| 467 | hr = parent_window->GetCurrentPatternAs(UIA_WindowPatternId, |
| 468 | IID_PPV_ARGS(&window_pattern)); |
| 469 | if (SUCCEEDED(hr) && window_pattern != nullptr) { |
| 470 | BOOL is_topmost; |
| 471 | hr = window_pattern->get_CurrentIsTopmost(&is_topmost); |
| 472 | WindowVisualState visual_state; |
| 473 | hr = window_pattern->get_CurrentWindowVisualState(&visual_state); |
| 474 | if (visual_state == WindowVisualState::WindowVisualState_Maximized || |
| 475 | visual_state == WindowVisualState::WindowVisualState_Normal) { |
| 476 | parent_window->SetFocus(); |
| 477 | window_pattern->SetWindowVisualState(visual_state); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | foreground_window = ::GetAncestor(::GetForegroundWindow(), GA_ROOT); |
| 485 | if (foreground_window != top_level_window_handle) { |
| 486 | HWND content_window_handle = this->GetContentWindowHandle(); |
| 487 | LOG(TRACE) << "Top-level IE window is " << top_level_window_handle |
| 488 | << " foreground window is " << foreground_window; |
| 489 | LOG(TRACE) << "Window still not in foreground; " |
| 490 | << "attempting to use SetForegroundWindow API"; |
| 491 | UINT_PTR lock_timeout = 0; |
| 492 | DWORD process_id = 0; |
| 493 | DWORD thread_id = ::GetWindowThreadProcessId(top_level_window_handle, |
| 494 | &process_id); |
| 495 | DWORD current_thread_id = ::GetCurrentThreadId(); |
| 496 | DWORD current_process_id = ::GetCurrentProcessId(); |
| 497 | if (current_thread_id != thread_id) { |
| 498 | ::AttachThreadInput(current_thread_id, thread_id, TRUE); |
| 499 | ::SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, |
| 500 | 0, |
| 501 | &lock_timeout, |
| 502 | 0); |
| 503 | ::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, |
no test coverage detected