Path: blob/trunk/cpp/iedriver/CommandHandlers/ExecuteScriptCommandHandler.cpp
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#include "ExecuteScriptCommandHandler.h"17#include "errorcodes.h"18#include "logging.h"19#include "../Browser.h"20#include "../IECommandExecutor.h"21#include "../Script.h"2223namespace webdriver {2425ExecuteScriptCommandHandler::ExecuteScriptCommandHandler(void) {26}2728ExecuteScriptCommandHandler::~ExecuteScriptCommandHandler(void) {29}3031void ExecuteScriptCommandHandler::ExecuteInternal(32const IECommandExecutor& executor,33const ParametersMap& command_parameters,34Response* response) {35ParametersMap::const_iterator script_parameter_iterator = command_parameters.find("script");36ParametersMap::const_iterator args_parameter_iterator = command_parameters.find("args");37if (script_parameter_iterator == command_parameters.end()) {38response->SetErrorResponse(ERROR_INVALID_ARGUMENT,39"Missing parameter: script");40return;41}4243if (!script_parameter_iterator->second.isString()) {44response->SetErrorResponse(ERROR_INVALID_ARGUMENT,45"script parameter must be a string");46return;47}4849if (args_parameter_iterator == command_parameters.end()) {50response->SetErrorResponse(ERROR_INVALID_ARGUMENT,51"Missing parameter: args");52return;53}5455if (!args_parameter_iterator->second.isArray()) {56response->SetErrorResponse(ERROR_INVALID_ARGUMENT,57"args parameter must be an array");58return;59}6061std::string script_body = script_parameter_iterator->second.asString();62const std::string script_source = "(function() { return function(){\n" + script_body + "\n};})();";6364Json::Value json_args(args_parameter_iterator->second);6566BrowserHandle browser_wrapper;67int status_code = executor.GetCurrentBrowser(&browser_wrapper);68if (status_code != WD_SUCCESS) {69response->SetErrorResponse(status_code, "Unable to get browser");70return;71}7273CComPtr<IHTMLDocument2> doc;74browser_wrapper->GetDocument(&doc);75Script script_wrapper(doc, script_source);7677HWND async_executor_handle;78status_code = script_wrapper.ExecuteAsync(executor,79json_args,80&async_executor_handle);81browser_wrapper->set_script_executor_handle(async_executor_handle);8283if (status_code != WD_SUCCESS) {84response->SetErrorResponse(status_code, "JavaScript error");85return;86}87}8889} // namespace webdriver909192