forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncScriptExecutor.cpp
More file actions
214 lines (190 loc) · 8.77 KB
/
Copy pathAsyncScriptExecutor.cpp
File metadata and controls
214 lines (190 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "AsyncScriptExecutor.h"
#include <vector>
#include "errorcodes.h"
#include "logging.h"
#include "Script.h"
namespace webdriver {
LRESULT AsyncScriptExecutor::OnInit(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnInit";
return 0;
}
LRESULT AsyncScriptExecutor::OnCreate(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnCreate";
CREATESTRUCT* create = reinterpret_cast<CREATESTRUCT*>(lParam);
AsyncScriptExecutorThreadContext* context = reinterpret_cast<AsyncScriptExecutorThreadContext*>(create->lpCreateParams);
this->script_source_code_ = context->script_source;
this->script_argument_count_ = context->script_argument_count;
this->script_argument_index_ = 0;
// Calling vector::resize() is okay here, because the vector
// should be empty when Initialize() is called, and the
// reallocation of variants shouldn't give us too much of a
// negative impact.
this->script_arguments_.resize(this->script_argument_count_);
this->status_code_ = WD_SUCCESS;
this->is_execution_completed_ = false;
this->is_listener_attached_ = true;
return 0;
}
LRESULT AsyncScriptExecutor::OnClose(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnClose";
this->DestroyWindow();
return 0;
}
LRESULT AsyncScriptExecutor::OnDestroy(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncAtomExecutor::OnDestroy";
::PostQuitMessage(0);
return 0;
}
LRESULT AsyncScriptExecutor::OnSetDocument(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnSetDocument";
CComPtr<IHTMLDocument2> doc;
LPSTREAM initializer_payload = reinterpret_cast<LPSTREAM>(lParam);
HRESULT hr = ::CoGetInterfaceAndReleaseStream(initializer_payload,
IID_IHTMLDocument2,
reinterpret_cast<void**>(&this->script_host_));
return 0;
}
LRESULT AsyncScriptExecutor::OnSetArgument(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnSetArgument";
VARTYPE variant_type = static_cast<VARTYPE>(wParam);
switch (variant_type) {
case VT_DISPATCH: {
CComPtr<IDispatch> dispatch;
LPSTREAM message_payload = reinterpret_cast<LPSTREAM>(lParam);
HRESULT hr = ::CoGetInterfaceAndReleaseStream(message_payload,
IID_IDispatch,
reinterpret_cast<void**>(&dispatch));
CComVariant dispatch_variant(dispatch);
this->script_arguments_[this->script_argument_index_] = dispatch_variant;
break;
}
default: {
// TODO: Unmarshal arguments of types other than VT_DISPATCH. At present,
// the asynchronous execution of JavaScript is only used for Automation
// Atoms on an element which take a single argument, an IHTMLElement
// object, which is represented as an IDispatch. This case statement
// will get much more complex should the need arise to execute
// arbitrary scripts in an asynchronous manner.
}
}
++this->script_argument_index_;
return 0;
}
LRESULT AsyncScriptExecutor::OnExecuteScript(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnExecuteScript";
Script script_to_execute(this->script_host_,
this->script_source_code_,
this->script_argument_count_);
for (size_t index = 0; index < this->script_arguments_.size(); ++index) {
script_to_execute.AddArgument(this->script_arguments_[index]);
}
this->status_code_ = script_to_execute.Execute();
this->is_execution_completed_ = true;
if (!this->is_listener_attached_) {
::PostMessage(this->m_hWnd, WM_CLOSE, NULL, NULL);
}
return 0;
}
LRESULT AsyncScriptExecutor::OnDetachListener(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnDetachListener";
this->is_listener_attached_ = false;
return 0;
}
LRESULT AsyncScriptExecutor::OnIsExecutionComplete(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
return this->is_execution_completed_ ? 1 : 0;
}
LRESULT AsyncScriptExecutor::OnGetResult(UINT uMsg,
WPARAM wParam,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering AsyncScriptExecutor::OnGetResult";
// NOTE: We need to tell this window to close itself. If and when marshaling
// of the actual variant result to the calling thread is implemented, posting
// the message to close the window will have to be moved to the method that
// retrieves the marshaled result.
::PostMessage(this->m_hWnd, WM_CLOSE, NULL, NULL);
return this->status_code_;
}
unsigned int WINAPI AsyncScriptExecutor::ThreadProc(LPVOID lpParameter) {
LOG(TRACE) << "Entering AsyncScriptExecutor::ThreadProc";
AsyncScriptExecutorThreadContext* thread_context = reinterpret_cast<AsyncScriptExecutorThreadContext*>(lpParameter);
HWND window_handle = thread_context->hwnd;
DWORD error = 0;
HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) {
LOGHR(DEBUG, hr) << "COM library initialization has some problem";
}
AsyncScriptExecutor async_executor;
HWND async_executor_window_handle = async_executor.Create(/*HWND*/ HWND_MESSAGE,
/*_U_RECT rect*/ CWindow::rcDefault,
/*LPCTSTR szWindowName*/ NULL,
/*DWORD dwStyle*/ NULL,
/*DWORD dwExStyle*/ NULL,
/*_U_MENUorID MenuOrID*/ 0U,
/*LPVOID lpCreateParam*/ reinterpret_cast<LPVOID*>(thread_context));
if (async_executor_window_handle == NULL) {
LOGERR(WARN) << "Unable to create new AsyncScriptExecutor";
}
MSG msg;
::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
// Return the HWND back through lpParameter, and signal that the
// window is ready for messages.
thread_context->hwnd = async_executor_window_handle;
HANDLE event_handle = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, ASYNC_SCRIPT_EVENT_NAME);
if (event_handle != NULL) {
::SetEvent(event_handle);
::CloseHandle(event_handle);
} else {
LOGERR(DEBUG) << "Unable to signal that window is ready";
}
// Run the message loop
while (::GetMessage(&msg, NULL, 0, 0) > 0) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
::CoUninitialize();
return 0;
}
} // namespace webdriver