Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/IESession.cpp
2867 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "IESession.h"
18
19
#include "logging.h"
20
21
#include "BrowserFactory.h"
22
#include "CommandExecutor.h"
23
#include "IECommandExecutor.h"
24
#include "messages.h"
25
#include "StringUtilities.h"
26
#include "WebDriverConstants.h"
27
28
#define MUTEX_NAME L"WD_INITIALIZATION_MUTEX"
29
#define MUTEX_WAIT_TIMEOUT 30000
30
#define THREAD_WAIT_TIMEOUT 30000
31
#define EXECUTOR_EXIT_WAIT_TIMEOUT 5000
32
#define EXECUTOR_EXIT_WAIT_INTERVAL 100
33
34
typedef unsigned (__stdcall *ThreadProcedure)(void*);
35
36
namespace webdriver {
37
38
IESession::IESession() {
39
}
40
41
IESession::~IESession(void) {
42
}
43
44
void IESession::Initialize(void* init_params) {
45
LOG(TRACE) << "Entering IESession::Initialize";
46
47
HANDLE mutex = ::CreateMutex(NULL, FALSE, MUTEX_NAME);
48
if (mutex != NULL) {
49
// Wait for up to the timeout (currently 30 seconds) for other sessions
50
// to completely initialize.
51
DWORD mutex_wait_status = ::WaitForSingleObject(mutex, MUTEX_WAIT_TIMEOUT);
52
if (mutex_wait_status == WAIT_ABANDONED) {
53
LOG(WARN) << "Acquired mutex, but received wait abandoned status. This "
54
<< "could mean the process previously owning the mutex was "
55
<< "unexpectedly terminated.";
56
} else if (mutex_wait_status == WAIT_TIMEOUT) {
57
LOG(WARN) << "Could not acquire mutex within the timeout. Multiple "
58
<< "instances may hang or behave unpredictably";
59
} else if (mutex_wait_status == WAIT_OBJECT_0) {
60
LOG(DEBUG) << "Mutex acquired for session initalization";
61
} else if (mutex_wait_status == WAIT_FAILED) {
62
LOGERR(WARN) << "Mutex acquire waiting is failed";
63
}
64
} else {
65
LOGERR(WARN) << "Could not create session initialization mutex. Multiple "
66
<< "instances will behave unpredictably. ";
67
}
68
69
SessionParameters* params = reinterpret_cast<SessionParameters*>(init_params);
70
int port = params->port;
71
72
IECommandExecutorThreadContext thread_context;
73
thread_context.port = port;
74
thread_context.hwnd = NULL;
75
76
unsigned int thread_id = 0;
77
78
HANDLE event_handle = ::CreateEvent(NULL, TRUE, FALSE, WEBDRIVER_START_EVENT_NAME);
79
if (event_handle == NULL) {
80
LOGERR(DEBUG) << "Unable to create event " << WEBDRIVER_START_EVENT_NAME;
81
}
82
83
ThreadProcedure thread_proc = &IECommandExecutor::ThreadProc;
84
HANDLE thread_handle = reinterpret_cast<HANDLE>(_beginthreadex(NULL,
85
0,
86
thread_proc,
87
reinterpret_cast<void*>(&thread_context),
88
0,
89
&thread_id));
90
if (event_handle != NULL) {
91
DWORD thread_wait_status = ::WaitForSingleObject(event_handle, THREAD_WAIT_TIMEOUT);
92
if (thread_wait_status != WAIT_OBJECT_0) {
93
LOGERR(WARN) << "Unable to wait until created thread notification: '" << thread_wait_status << "'.";
94
}
95
::CloseHandle(event_handle);
96
}
97
98
if (thread_handle != NULL) {
99
::CloseHandle(thread_handle);
100
} else {
101
LOG(DEBUG) << "Unable to create thread for command executor";
102
}
103
104
std::string session_id = "";
105
if (thread_context.hwnd != NULL) {
106
LOG(TRACE) << "Created thread for command executor returns HWND: '" << thread_context.hwnd << "'";
107
std::vector<wchar_t> window_text_buffer(37);
108
::GetWindowText(thread_context.hwnd, &window_text_buffer[0], 37);
109
session_id = StringUtilities::ToString(&window_text_buffer[0]);
110
LOG(TRACE) << "Session id is retrived from command executor window: '" << session_id << "'";
111
} else {
112
LOG(DEBUG) << "Created thread does not return HWND of created session";
113
}
114
115
if (mutex != NULL) {
116
LOG(DEBUG) << "Releasing session initialization mutex";
117
::ReleaseMutex(mutex);
118
::CloseHandle(mutex);
119
}
120
121
this->executor_window_handle_ = thread_context.hwnd;
122
this->set_session_id(session_id);
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";
181
int is_quitting = static_cast<int>(::SendMessage(this->executor_window_handle_,
182
WD_GET_QUIT_STATUS,
183
NULL,
184
NULL));
185
int retry_count = timeout_in_milliseconds / EXECUTOR_EXIT_WAIT_INTERVAL;
186
while (is_quitting > 0 && --retry_count > 0) {
187
::Sleep(EXECUTOR_EXIT_WAIT_INTERVAL);
188
is_quitting = static_cast<int>(::SendMessage(this->executor_window_handle_,
189
WD_GET_QUIT_STATUS,
190
NULL,
191
NULL));
192
}
193
return is_quitting == 0;
194
}
195
196
bool IESession::ExecuteCommand(const std::string& serialized_command,
197
std::string* serialized_response) {
198
LOG(TRACE) << "Entering IESession::ExecuteCommand";
199
200
// Sending a command consists of five actions:
201
// 1. Setting the command to be executed
202
// 2. Executing the command
203
// 3. Waiting for the response to be populated
204
// 4. Retrieving the response
205
// 5. Retrieving whether the command sent caused the session to be ready for shutdown
206
LRESULT set_command_result = ::SendMessage(this->executor_window_handle_,
207
WD_SET_COMMAND,
208
NULL,
209
reinterpret_cast<LPARAM>(serialized_command.c_str()));
210
while (set_command_result == 0) {
211
::Sleep(500);
212
set_command_result = ::SendMessage(this->executor_window_handle_,
213
WD_SET_COMMAND,
214
NULL,
215
reinterpret_cast<LPARAM>(serialized_command.c_str()));
216
}
217
::PostMessage(this->executor_window_handle_,
218
WD_EXEC_COMMAND,
219
NULL,
220
NULL);
221
222
int response_length = static_cast<int>(::SendMessage(this->executor_window_handle_,
223
WD_GET_RESPONSE_LENGTH,
224
NULL,
225
NULL));
226
LOG(TRACE) << "Beginning wait for response length to be not zero";
227
while (response_length == 0) {
228
// Sleep a short time to prevent thread starvation on single-core machines.
229
::Sleep(10);
230
response_length = static_cast<int>(::SendMessage(this->executor_window_handle_,
231
WD_GET_RESPONSE_LENGTH,
232
NULL,
233
NULL));
234
}
235
LOG(TRACE) << "Found non-zero response length";
236
237
// Must add one to the length to handle the terminating character.
238
std::vector<char> response_buffer(response_length + 1);
239
::SendMessage(this->executor_window_handle_,
240
WD_GET_RESPONSE,
241
NULL,
242
reinterpret_cast<LPARAM>(&response_buffer[0]));
243
*serialized_response = &response_buffer[0];
244
bool session_is_valid = ::SendMessage(this->executor_window_handle_,
245
WD_IS_SESSION_VALID,
246
NULL,
247
NULL) != 0;
248
return session_is_valid;
249
}
250
251
} // namespace webdriver
252
253