Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/ExecuteAsyncScriptCommandHandler.cpp
2868 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 "ExecuteAsyncScriptCommandHandler.h"
18
#include "errorcodes.h"
19
#include "../Browser.h"
20
#include "../IECommandExecutor.h"
21
#include "../Script.h"
22
#include "../StringUtilities.h"
23
24
#define GUID_STRING_LEN 40
25
26
namespace webdriver {
27
28
ExecuteAsyncScriptCommandHandler::ExecuteAsyncScriptCommandHandler(void) {
29
}
30
31
ExecuteAsyncScriptCommandHandler::~ExecuteAsyncScriptCommandHandler(void) {
32
}
33
34
void ExecuteAsyncScriptCommandHandler::ExecuteInternal(
35
const IECommandExecutor& executor,
36
const ParametersMap& command_parameters,
37
Response* response) {
38
ParametersMap::const_iterator script_parameter_iterator = command_parameters.find("script");
39
ParametersMap::const_iterator args_parameter_iterator = command_parameters.find("args");
40
if (script_parameter_iterator == command_parameters.end()) {
41
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
42
"Missing parameter: script");
43
return;
44
}
45
46
if (!script_parameter_iterator->second.isString()) {
47
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
48
"script parameter must be a string");
49
return;
50
}
51
52
if (args_parameter_iterator == command_parameters.end()) {
53
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
54
"Missing parameter: args");
55
return;
56
}
57
58
if (!args_parameter_iterator->second.isArray()) {
59
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
60
"args parameter must be an array");
61
return;
62
}
63
64
std::vector<wchar_t> page_id_buffer(GUID_STRING_LEN);
65
GUID page_id_guid;
66
::CoCreateGuid(&page_id_guid);
67
::StringFromGUID2(page_id_guid, &page_id_buffer[0], GUID_STRING_LEN);
68
std::wstring page_id = &page_id_buffer[0];
69
70
std::vector<wchar_t> pending_id_buffer(GUID_STRING_LEN);
71
GUID pending_id_guid;
72
::CoCreateGuid(&pending_id_guid);
73
::StringFromGUID2(pending_id_guid, &pending_id_buffer[0], GUID_STRING_LEN);
74
std::wstring pending_id = &pending_id_buffer[0];
75
76
Json::Value json_args = args_parameter_iterator->second;
77
78
unsigned long long timeout_value = executor.async_script_timeout();
79
std::wstring timeout = std::to_wstring(static_cast<long long>(timeout_value));
80
81
std::wstring script_body = StringUtilities::ToWString(script_parameter_iterator->second.asString());
82
83
std::wstring async_script = L"(function() { return function(){\n";
84
async_script += L"document.__$webdriverAsyncExecutor = {\n";
85
async_script += L" pageId: '" + page_id + L"',\n";
86
async_script += L" asyncTimeout: 0\n";
87
async_script += L"};\n";
88
async_script += L"var timeoutId = window.setTimeout(function() {\n";
89
async_script += L" window.setTimeout(function() {\n";
90
async_script += L" document.__$webdriverAsyncExecutor.asyncTimeout = 1;\n";
91
async_script += L" }, 0);\n";
92
async_script += L"}," + timeout + L");\n";
93
async_script += L"var callback = function(value) {\n";
94
async_script += L" document.__$webdriverAsyncExecutor.asyncTimeout = 0;\n";
95
async_script += L" document.__$webdriverAsyncExecutor.asyncScriptResult = value;\n";
96
async_script += L" window.clearTimeout(timeoutId);\n";
97
async_script += L"};\n";
98
async_script += L"var argsArray = Array.prototype.slice.call(arguments);\n";
99
async_script += L"argsArray.push(callback);\n";
100
async_script += L"if (document.__$webdriverAsyncExecutor.asyncScriptResult !== undefined) {\n";
101
async_script += L" delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
102
async_script += L"}\n";
103
async_script += L"(function() {\n" + script_body + L"\n}).apply(null, argsArray);\n";
104
async_script += L"};})();";
105
106
std::wstring polling_script = L"(function() { return function(){\n";
107
polling_script += L"var pendingId = '" + pending_id + L"';\n";
108
polling_script += L"if ('__$webdriverAsyncExecutor' in document) {\n";
109
polling_script += L" if (document.__$webdriverAsyncExecutor.pageId != '" + page_id + L"') {\n";
110
polling_script += L" return {'status': 'reload', 'id': pendingId, 'value': -1};\n";
111
polling_script += L" } else if ('asyncScriptResult' in document.__$webdriverAsyncExecutor) {\n";
112
polling_script += L" var value = document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
113
polling_script += L" delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
114
polling_script += L" return {'status': 'complete', 'id': pendingId, 'value': value};\n";
115
polling_script += L" } else if (document.__$webdriverAsyncExecutor.asyncTimeout == 0) {\n";
116
polling_script += L" return {'status': 'pending', 'id': pendingId, 'value': document.__$webdriverAsyncExecutor.asyncTimeout};\n";
117
polling_script += L" } else {\n";
118
polling_script += L" return {'status': 'timeout', 'id': pendingId, 'value': document.__$webdriverAsyncExecutor.asyncTimeout};\n";
119
polling_script += L" }\n";
120
polling_script += L"} else {\n";
121
polling_script += L" return {'status': 'reload', 'id': pendingId, 'value': -1};\n";
122
polling_script += L"}\n";
123
polling_script += L"};})();";
124
125
BrowserHandle browser_wrapper;
126
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
127
if (status_code != WD_SUCCESS) {
128
response->SetErrorResponse(status_code, "Unable to get browser");
129
return;
130
}
131
132
CComPtr<IHTMLDocument2> doc;
133
browser_wrapper->GetDocument(&doc);
134
135
HWND async_executor_handle;
136
Script async_script_wrapper(doc, async_script);
137
async_script_wrapper.set_polling_source_code(polling_script);
138
status_code = async_script_wrapper.ExecuteAsync(executor,
139
json_args,
140
&async_executor_handle);
141
browser_wrapper->set_script_executor_handle(async_executor_handle);
142
143
if (status_code != WD_SUCCESS) {
144
response->SetErrorResponse(status_code, "JavaScript error");
145
}
146
}
147
148
} // namespace webdriver
149
150