Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/AsyncScriptExecutor.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_ASYNCSCRIPTEXECUTOR_H_
18
#define WEBDRIVER_IE_ASYNCSCRIPTEXECUTOR_H_
19
20
#include <vector>
21
22
#include "IElementManager.h"
23
#include "messages.h"
24
#include "json.h"
25
26
namespace webdriver {
27
28
// Structure to be used for comunication between threads
29
struct AsyncScriptExecutorThreadContext {
30
HWND hwnd;
31
LPCTSTR script_source;
32
int script_argument_count;
33
HWND main_element_repository_handle;
34
LPCTSTR serialized_script_args;
35
};
36
37
class ElementRepository;
38
39
// We use a CWindowImpl (creating a hidden window) here because we
40
// want to synchronize access to the command handler. For that we
41
// use SendMessage() most of the time, and SendMessage() requires
42
// a window handle.
43
class AsyncScriptExecutor : public CWindowImpl<AsyncScriptExecutor>, public IElementManager {
44
public:
45
DECLARE_WND_CLASS(L"WebDriverAsyncAtomWndClass")
46
47
BEGIN_MSG_MAP(Session)
48
MESSAGE_HANDLER(WM_CREATE, OnCreate)
49
MESSAGE_HANDLER(WM_CLOSE, OnClose)
50
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
51
MESSAGE_HANDLER(WD_INIT, OnInit)
52
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_DOCUMENT, OnSetDocument)
53
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_ELEMENT_ARGUMENT, OnSetElementArgument)
54
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_IS_EXECUTION_READY, OnIsExecutionReady)
55
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_EXECUTE, OnExecuteScript)
56
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_IS_EXECUTION_COMPLETE, OnIsExecutionComplete)
57
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_DETACH_LISTENTER, OnDetachListener)
58
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_GET_RESULT, OnGetResult)
59
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_NOTIFY_ELEMENT_TRANSFERRED, OnNotifyElementTransferred)
60
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_SET_POLLING_SCRIPT, OnSetPollingScript)
61
MESSAGE_HANDLER(WD_ASYNC_SCRIPT_GET_REQUIRED_ELEMENT_LIST, OnGetRequiredElementList)
62
END_MSG_MAP()
63
64
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
65
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
66
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
67
LRESULT OnInit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
68
LRESULT OnSetDocument(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
69
LRESULT OnSetElementArgument(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
70
LRESULT OnIsExecutionReady(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
71
LRESULT OnExecuteScript(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
72
LRESULT OnIsExecutionComplete(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
73
LRESULT OnDetachListener(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
74
LRESULT OnGetResult(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
75
LRESULT OnNotifyElementTransferred(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
76
LRESULT OnSetPollingScript(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
77
LRESULT OnGetRequiredElementList(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
78
79
int GetManagedElement(const std::string& element_id,
80
ElementHandle* element_wrapper) const;
81
bool AddManagedElement(IHTMLElement* element,
82
ElementHandle* element_wrapper);
83
void RemoveManagedElement(const std::string& element_id);
84
85
static unsigned int WINAPI ThreadProc(LPVOID lpParameter);
86
87
private:
88
void GetElementIdList(const Json::Value& json_object);
89
bool WaitForPollingScript(void);
90
void TransferReturnedElements(void);
91
void ReplaceTransferredElementResult(std::string original_element_id,
92
std::string element_id,
93
Json::Value* result);
94
95
ElementRepository* element_repository_;
96
HWND main_element_repository_handle_;
97
CComPtr<IHTMLDocument2> script_host_;
98
std::vector<CComVariant> script_arguments_;
99
std::vector<std::string> element_id_list_;
100
std::wstring script_source_code_;
101
std::wstring polling_script_source_code_;
102
Json::Value script_args_;
103
Json::Value script_result_;
104
int script_argument_count_;
105
int script_argument_index_;
106
int status_code_;
107
bool is_execution_completed_;
108
bool is_listener_attached_;
109
};
110
111
} // namespace webdriver
112
113
#endif // WEBDRIVER_IE_ASYNCSCRIPTEXECUTOR_H_
114
115
116