Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/ExecuteScriptCommandHandler.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 "ExecuteScriptCommandHandler.h"
18
#include "errorcodes.h"
19
#include "logging.h"
20
#include "../Browser.h"
21
#include "../IECommandExecutor.h"
22
#include "../Script.h"
23
24
namespace webdriver {
25
26
ExecuteScriptCommandHandler::ExecuteScriptCommandHandler(void) {
27
}
28
29
ExecuteScriptCommandHandler::~ExecuteScriptCommandHandler(void) {
30
}
31
32
void ExecuteScriptCommandHandler::ExecuteInternal(
33
const IECommandExecutor& executor,
34
const ParametersMap& command_parameters,
35
Response* response) {
36
ParametersMap::const_iterator script_parameter_iterator = command_parameters.find("script");
37
ParametersMap::const_iterator args_parameter_iterator = command_parameters.find("args");
38
if (script_parameter_iterator == command_parameters.end()) {
39
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
40
"Missing parameter: script");
41
return;
42
}
43
44
if (!script_parameter_iterator->second.isString()) {
45
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
46
"script parameter must be a string");
47
return;
48
}
49
50
if (args_parameter_iterator == command_parameters.end()) {
51
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
52
"Missing parameter: args");
53
return;
54
}
55
56
if (!args_parameter_iterator->second.isArray()) {
57
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
58
"args parameter must be an array");
59
return;
60
}
61
62
std::string script_body = script_parameter_iterator->second.asString();
63
const std::string script_source = "(function() { return function(){\n" + script_body + "\n};})();";
64
65
Json::Value json_args(args_parameter_iterator->second);
66
67
BrowserHandle browser_wrapper;
68
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
69
if (status_code != WD_SUCCESS) {
70
response->SetErrorResponse(status_code, "Unable to get browser");
71
return;
72
}
73
74
CComPtr<IHTMLDocument2> doc;
75
browser_wrapper->GetDocument(&doc);
76
Script script_wrapper(doc, script_source);
77
78
HWND async_executor_handle;
79
status_code = script_wrapper.ExecuteAsync(executor,
80
json_args,
81
&async_executor_handle);
82
browser_wrapper->set_script_executor_handle(async_executor_handle);
83
84
if (status_code != WD_SUCCESS) {
85
response->SetErrorResponse(status_code, "JavaScript error");
86
return;
87
}
88
}
89
90
} // namespace webdriver
91
92