Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/IECommandExecutor.h
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
#ifndef WEBDRIVER_IE_IECOMMANDEXECUTOR_H_
18
#define WEBDRIVER_IE_IECOMMANDEXECUTOR_H_
19
20
#include <ctime>
21
#include <map>
22
#include <mutex>
23
#include <string>
24
#include <unordered_map>
25
#include <unordered_set>
26
27
#include "command.h"
28
#include "CustomTypes.h"
29
#include "IElementManager.h"
30
#include "messages.h"
31
32
namespace webdriver {
33
34
// Forward declaration of classes.
35
class BrowserFactory;
36
class CommandHandlerRepository;
37
class ElementFinder;
38
class ElementRepository;
39
class InputManager;
40
class ProxyManager;
41
42
// We use a CWindowImpl (creating a hidden window) here because we
43
// want to synchronize access to the command handler. For that we
44
// use SendMessage() most of the time, and SendMessage() requires
45
// a window handle.
46
class IECommandExecutor : public CWindowImpl<IECommandExecutor>, public IElementManager {
47
public:
48
DECLARE_WND_CLASS(L"WebDriverWndClass")
49
50
BEGIN_MSG_MAP(Session)
51
MESSAGE_HANDLER(WM_CREATE, OnCreate)
52
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
53
MESSAGE_HANDLER(WD_SET_COMMAND, OnSetCommand)
54
MESSAGE_HANDLER(WD_EXEC_COMMAND, OnExecCommand)
55
MESSAGE_HANDLER(WD_GET_RESPONSE_LENGTH, OnGetResponseLength)
56
MESSAGE_HANDLER(WD_GET_RESPONSE, OnGetResponse)
57
MESSAGE_HANDLER(WD_WAIT, OnWait)
58
MESSAGE_HANDLER(WD_BROWSER_NEW_WINDOW, OnBrowserNewWindow)
59
MESSAGE_HANDLER(WD_BEFORE_NEW_WINDOW, OnBeforeNewWindow)
60
MESSAGE_HANDLER(WD_AFTER_NEW_WINDOW, OnAfterNewWindow)
61
MESSAGE_HANDLER(WD_BROWSER_QUIT, OnBrowserQuit)
62
MESSAGE_HANDLER(WD_BROWSER_CLOSE_WAIT, OnBrowserCloseWait)
63
MESSAGE_HANDLER(WD_BEFORE_BROWSER_REATTACH, OnBeforeBrowserReattach)
64
MESSAGE_HANDLER(WD_BROWSER_REATTACH, OnBrowserReattach)
65
MESSAGE_HANDLER(WD_IS_SESSION_VALID, OnIsSessionValid)
66
MESSAGE_HANDLER(WD_NEW_HTML_DIALOG, OnNewHtmlDialog)
67
MESSAGE_HANDLER(WD_GET_QUIT_STATUS, OnGetQuitStatus)
68
MESSAGE_HANDLER(WD_REFRESH_MANAGED_ELEMENTS, OnRefreshManagedElements)
69
MESSAGE_HANDLER(WD_HANDLE_UNEXPECTED_ALERTS, OnHandleUnexpectedAlerts)
70
MESSAGE_HANDLER(WD_QUIT, OnQuit)
71
MESSAGE_HANDLER(WD_SCRIPT_WAIT, OnScriptWait)
72
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_TRANSFER_MANAGED_ELEMENT, OnTransferManagedElement)
73
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SCHEDULE_REMOVE_MANAGED_ELEMENT, OnScheduleRemoveManagedElement)
74
MESSAGE_HANDLER(WD_ADD_CHROMIUM_WINDOW_HANDLE, OnAddChromiumWindowHandle)
75
MESSAGE_HANDLER(WD_SESSION_QUIT_WAIT, OnSessionQuitWait)
76
END_MSG_MAP()
77
78
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
79
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
80
LRESULT OnSetCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
81
LRESULT OnExecCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
82
LRESULT OnGetResponseLength(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
83
LRESULT OnGetResponse(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
84
LRESULT OnWait(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
85
LRESULT OnBrowserNewWindow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
86
LRESULT OnBeforeNewWindow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
87
LRESULT OnAfterNewWindow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
88
LRESULT OnBrowserQuit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
89
LRESULT OnBrowserCloseWait(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
90
LRESULT OnBeforeBrowserReattach(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
91
LRESULT OnBrowserReattach(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
92
LRESULT OnIsSessionValid(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
93
LRESULT OnNewHtmlDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
94
LRESULT OnGetQuitStatus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
95
LRESULT OnRefreshManagedElements(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
96
LRESULT OnHandleUnexpectedAlerts(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
97
LRESULT OnQuit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
98
LRESULT OnScriptWait(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
99
LRESULT OnTransferManagedElement(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
100
LRESULT OnScheduleRemoveManagedElement(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
101
LRESULT OnAddChromiumWindowHandle(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
102
LRESULT OnSessionQuitWait(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
103
104
std::string session_id(void) const { return this->session_id_; }
105
106
static unsigned int WINAPI ThreadProc(LPVOID lpParameter);
107
static unsigned int WINAPI WaitThreadProc(LPVOID lpParameter);
108
static unsigned int WINAPI ScriptWaitThreadProc(LPVOID lpParameter);
109
static unsigned int WINAPI DelayPostMessageThreadProc(LPVOID lpParameter);
110
111
std::string current_browser_id(void) const {
112
return this->current_browser_id_;
113
}
114
void set_current_browser_id(const std::string& browser_id) {
115
this->current_browser_id_ = browser_id;
116
}
117
118
int CreateNewBrowser(std::string* error_message);
119
std::string OpenNewBrowsingContext(const std::string& window_type);
120
121
int GetManagedBrowser(const std::string& browser_id,
122
BrowserHandle* browser_wrapper) const;
123
int GetCurrentBrowser(BrowserHandle* browser_wrapper) const;
124
void GetManagedBrowserHandles(
125
std::vector<std::string> *managed_browser_handles) const;
126
127
int GetManagedElement(const std::string& element_id,
128
ElementHandle* element_wrapper) const;
129
bool AddManagedElement(IHTMLElement* element,
130
ElementHandle* element_wrapper);
131
void RemoveManagedElement(const std::string& element_id);
132
void ListManagedElements(void);
133
134
int GetElementFindMethod(const std::string& mechanism,
135
std::wstring* translation) const;
136
int LocateElement(const ElementHandle parent_wrapper,
137
const std::string& mechanism,
138
const std::string& criteria,
139
Json::Value* found_element) const;
140
int LocateElements(const ElementHandle parent_wrapper,
141
const std::string& mechanism,
142
const std::string& criteria,
143
Json::Value* found_elements) const;
144
145
HWND window_handle(void) const { return this->m_hWnd; }
146
IElementManager* element_manager(void) { return this; }
147
148
unsigned long long implicit_wait_timeout(void) const {
149
return this->implicit_wait_timeout_;
150
}
151
void set_implicit_wait_timeout(const unsigned long long timeout) {
152
this->implicit_wait_timeout_ = timeout;
153
}
154
155
long long async_script_timeout(void) const { return this->async_script_timeout_; }
156
void set_async_script_timeout(const long long timeout) {
157
this->async_script_timeout_ = timeout;
158
}
159
160
unsigned long long page_load_timeout(void) const { return this->page_load_timeout_; }
161
void set_page_load_timeout(const unsigned long long timeout) {
162
this->page_load_timeout_ = timeout;
163
}
164
165
bool is_valid(void) const { return this->is_valid_; }
166
void set_is_valid(const bool session_is_valid) {
167
this->is_valid_ = session_is_valid;
168
}
169
170
bool is_quitting(void) const { return this->is_quitting_; }
171
void set_is_quitting(const bool session_is_quitting) {
172
this->is_quitting_ = session_is_quitting;
173
}
174
175
std::string unexpected_alert_behavior(void) const {
176
return this->unexpected_alert_behavior_;
177
}
178
void set_unexpected_alert_behavior(const std::string& unexpected_alert_behavior) {
179
this->unexpected_alert_behavior_ = unexpected_alert_behavior;
180
}
181
182
std::string page_load_strategy(void) const {
183
return this->page_load_strategy_;
184
}
185
void set_page_load_strategy(const std::string& page_load_strategy) {
186
this->page_load_strategy_ = page_load_strategy;
187
}
188
189
bool use_legacy_file_upload_dialog_handling(void) const {
190
return this->use_legacy_file_upload_dialog_handling_;
191
}
192
void set_use_legacy_file_upload_dialog_handling(const bool use_legacy_dialog_handling) {
193
this->use_legacy_file_upload_dialog_handling_ = use_legacy_dialog_handling;
194
}
195
196
int file_upload_dialog_timeout(void) const {
197
return this->file_upload_dialog_timeout_;
198
}
199
void set_file_upload_dialog_timeout(const int file_upload_dialog_timeout) {
200
this->file_upload_dialog_timeout_ = file_upload_dialog_timeout;
201
}
202
203
bool is_edge_mode(void) const {
204
return this->is_edge_chromium_;
205
}
206
void set_is_edge_mode(bool value) {
207
this->is_edge_chromium_ = value;
208
}
209
210
std::string edge_executable_path(void) const {
211
return this->edge_executable_path_;
212
}
213
void set_edge_executable_path(std::string path) {
214
this->edge_executable_path_ = path;
215
}
216
217
bool use_strict_file_interactability(void) const {
218
return this->use_strict_file_interactability_;
219
}
220
void set_use_strict_file_interactability(const bool use_strict_file_interactability) {
221
this->use_strict_file_interactability_ = use_strict_file_interactability;
222
}
223
224
ElementFinder* element_finder(void) const { return this->element_finder_; }
225
InputManager* input_manager(void) const { return this->input_manager_; }
226
ProxyManager* proxy_manager(void) const { return this->proxy_manager_; }
227
BrowserFactory* browser_factory(void) const { return this->factory_; }
228
229
int port(void) const { return this->port_; }
230
231
size_t managed_window_count(void) const {
232
return this->managed_browsers_.size();
233
}
234
235
private:
236
typedef std::unordered_map<std::string, BrowserHandle> BrowserMap;
237
typedef std::map<std::string, std::wstring> ElementFindMethodMap;
238
239
void AddManagedBrowser(BrowserHandle browser_wrapper);
240
241
void DispatchCommand(void);
242
243
void PopulateElementFinderMethods(void);
244
245
void CreateWaitThread(const std::string& deferred_response);
246
void CreateWaitThread(const std::string& deferred_response,
247
const bool is_deferred_command_execution);
248
void CreateDelayPostMessageThread(const DWORD delay_time,
249
const HWND window_handle,
250
const UINT message_to_post);
251
bool IsCommandValidWithAlertPresent(void);
252
bool IsAlertActive(BrowserHandle browser, HWND* alert_handle);
253
bool HandleUnexpectedAlert(BrowserHandle browser,
254
HWND alert_handle,
255
bool force_use_dismiss,
256
std::string* alert_text);
257
258
void PostBrowserReattachMessage(const DWORD current_process_id,
259
const std::string& browser_id,
260
const std::vector<DWORD>& known_process_ids);
261
void GetNewBrowserProcessIds(std::vector<DWORD>* known_process_ids,
262
std::vector<DWORD>* new_process_ids);
263
264
std::string OpenNewBrowsingContext(const std::string& window_type,
265
const std::string& url);
266
std::string OpenNewBrowserWindow(const std::wstring& url);
267
std::string OpenNewBrowserTab(const std::wstring& url);
268
269
BrowserMap managed_browsers_;
270
ElementRepository* managed_elements_;
271
ElementFindMethodMap element_find_methods_;
272
CommandHandlerRepository* command_handlers_;
273
274
std::string current_browser_id_;
275
276
ElementFinder* element_finder_;
277
278
unsigned long long implicit_wait_timeout_;
279
unsigned long long async_script_timeout_;
280
unsigned long long page_load_timeout_;
281
unsigned long long reattach_browser_timeout_;
282
clock_t wait_timeout_;
283
clock_t reattach_wait_timeout_;
284
285
std::string session_id_;
286
int port_;
287
bool ignore_zoom_setting_;
288
std::string initial_browser_url_;
289
std::string unexpected_alert_behavior_;
290
std::string page_load_strategy_;
291
int file_upload_dialog_timeout_;
292
bool use_legacy_file_upload_dialog_handling_;
293
bool enable_full_page_screenshot_;
294
bool use_strict_file_interactability_;
295
bool is_edge_chromium_;
296
std::string edge_executable_path_;
297
std::wstring edge_temp_dir_;
298
std::unordered_set<HWND> chromium_window_handles_;
299
300
Command current_command_;
301
std::string serialized_response_;
302
bool is_waiting_;
303
bool is_valid_;
304
bool is_quitting_;
305
bool is_awaiting_new_window_;
306
std::mutex set_command_mutex_;
307
308
BrowserFactory* factory_;
309
InputManager* input_manager_;
310
ProxyManager* proxy_manager_;
311
};
312
313
} // namespace webdriver
314
315
#endif // WEBDRIVER_IE_IECOMMANDEXECUTOR_H_
316
317