| 123 | } |
| 124 | |
| 125 | void IESession::ShutDown(void) { |
| 126 | LOG(TRACE) << "Entering IESession::ShutDown"; |
| 127 | |
| 128 | // Kill the background thread first - otherwise the IE process crashes. |
| 129 | ::SendMessage(this->executor_window_handle_, WD_QUIT, NULL, NULL); |
| 130 | |
| 131 | // Don't terminate the thread until the browsers have all been deallocated. |
| 132 | // Note: Loop count of 6, because the timeout is 5 seconds, giving us a nice, |
| 133 | // round 30 seconds. |
| 134 | int retry_count = 6; |
| 135 | bool has_quit = this->WaitForCommandExecutorExit(EXECUTOR_EXIT_WAIT_TIMEOUT); |
| 136 | while (!has_quit && retry_count > 0) { |
| 137 | // ASSUMPTION! If all browsers haven't been deallocated by the timeout |
| 138 | // specified, they're blocked from quitting by something. We'll assume |
| 139 | // that something is an alert blocking close, and ask the executor to |
| 140 | // attempt another close after closing the offending alert. |
| 141 | // N.B., this could probably be made more robust by modifying |
| 142 | // IECommandExecutor::OnGetQuitStatus(), but that would require some |
| 143 | // fairly complex synchronization code, to make sure a browser isn't |
| 144 | // deallocated while the "close the alert and close the browser again" |
| 145 | // code is still running, since the deallocation happens in response |
| 146 | // to the DWebBrowserEvents2::OnQuit event. |
| 147 | LOG(DEBUG) << "Not all browsers have been deallocated!"; |
| 148 | ::PostMessage(this->executor_window_handle_, |
| 149 | WD_HANDLE_UNEXPECTED_ALERTS, |
| 150 | NULL, |
| 151 | NULL); |
| 152 | has_quit = this->WaitForCommandExecutorExit(EXECUTOR_EXIT_WAIT_TIMEOUT); |
| 153 | retry_count--; |
| 154 | } |
| 155 | |
| 156 | if (has_quit) { |
| 157 | LOG(DEBUG) << "Executor shutdown successful!"; |
| 158 | } else { |
| 159 | LOG(ERROR) << "Still running browsers after handling alerts! This is likely to lead to a crash."; |
| 160 | } |
| 161 | DWORD process_id; |
| 162 | DWORD thread_id = ::GetWindowThreadProcessId(this->executor_window_handle_, |
| 163 | &process_id); |
| 164 | HANDLE thread_handle = ::OpenThread(SYNCHRONIZE, FALSE, thread_id); |
| 165 | LOG(DEBUG) << "Posting thread shutdown message"; |
| 166 | ::PostThreadMessage(thread_id, WD_SHUTDOWN, NULL, NULL); |
| 167 | if (thread_handle != NULL) { |
| 168 | LOG(DEBUG) << "Starting wait for thread completion"; |
| 169 | DWORD wait_result = ::WaitForSingleObject(&thread_handle, 30000); |
| 170 | if (wait_result != WAIT_OBJECT_0) { |
| 171 | LOG(DEBUG) << "Waiting for thread to end returned " << wait_result; |
| 172 | } else { |
| 173 | LOG(DEBUG) << "Wait for thread handle complete"; |
| 174 | } |
| 175 | ::CloseHandle(thread_handle); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool IESession::WaitForCommandExecutorExit(int timeout_in_milliseconds) { |
| 180 | LOG(TRACE) << "Entering IESession::WaitForCommandExecutorExit"; |
nothing calls this directly
no test coverage detected