2015-04-13 12:09:26 -04:00
|
|
|
// 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");
|
2013-01-11 22:18:32 +01:00
|
|
|
// 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 "command.h"
|
|
|
|
|
#include "command_types.h"
|
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
|
|
namespace webdriver {
|
|
|
|
|
|
2014-07-01 11:42:36 -04:00
|
|
|
Command::Command() : command_type_(webdriver::CommandType::NoCommand),
|
|
|
|
|
session_id_("") {
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::~Command() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 11:42:36 -04:00
|
|
|
void Command::Deserialize(const std::string& json) {
|
|
|
|
|
LOG(TRACE) << "Entering Command::Deserialize";
|
2013-01-11 22:18:32 +01:00
|
|
|
|
|
|
|
|
// Clear the existing maps.
|
|
|
|
|
this->command_parameters_.clear();
|
|
|
|
|
|
2014-07-01 11:42:36 -04:00
|
|
|
LOG(DEBUG) << "Raw JSON command: " << json;
|
2013-01-11 22:18:32 +01:00
|
|
|
|
|
|
|
|
Json::Value root;
|
2018-03-22 13:55:29 -07:00
|
|
|
std::string parse_errors;
|
|
|
|
|
std::stringstream json_stream;
|
|
|
|
|
json_stream.str(json);
|
|
|
|
|
bool successful_parse = Json::parseFromStream(Json::CharReaderBuilder(),
|
|
|
|
|
json_stream,
|
|
|
|
|
&root,
|
|
|
|
|
&parse_errors);
|
|
|
|
|
|
2013-01-11 22:18:32 +01:00
|
|
|
if (!successful_parse) {
|
|
|
|
|
// report to the user the failure and their locations in the document.
|
2018-03-22 13:55:29 -07:00
|
|
|
LOG(WARN) << "Failed to parse configuration due to "
|
|
|
|
|
<< parse_errors << std::endl
|
2014-07-01 11:42:36 -04:00
|
|
|
<< "JSON command: '" << json << "'";
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-01 11:42:36 -04:00
|
|
|
this->command_type_ = root.get("name", webdriver::CommandType::NoCommand).asString();
|
2013-01-11 22:18:32 +01:00
|
|
|
if (this->command_type_ != webdriver::CommandType::NoCommand) {
|
|
|
|
|
Json::Value locator_parameter_object = root["locator"];
|
|
|
|
|
Json::Value::iterator it = locator_parameter_object.begin();
|
|
|
|
|
Json::Value::iterator end = locator_parameter_object.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
std::string key = it.key().asString();
|
|
|
|
|
std::string value = locator_parameter_object[key].asString();
|
2014-07-01 11:42:36 -04:00
|
|
|
if (key == "sessionid") {
|
|
|
|
|
this->session_id_ = value;
|
|
|
|
|
} else {
|
|
|
|
|
this->command_parameters_[key] = value;
|
|
|
|
|
}
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 11:20:48 -07:00
|
|
|
this->is_valid_parameters_ = true;
|
2013-01-11 22:18:32 +01:00
|
|
|
Json::Value command_parameter_object = root["parameters"];
|
2014-11-18 13:33:16 -05:00
|
|
|
if (!command_parameter_object.isObject()) {
|
|
|
|
|
LOG(WARN) << "The value of the 'parameters' attribute is not a JSON "
|
|
|
|
|
<< "object. This is invalid for the WebDriver JSON Wire "
|
|
|
|
|
<< "Protocol.";
|
2018-08-20 11:20:48 -07:00
|
|
|
this->is_valid_parameters_ = false;
|
2014-11-18 13:33:16 -05:00
|
|
|
} else {
|
|
|
|
|
it = command_parameter_object.begin();
|
|
|
|
|
end = command_parameter_object.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
std::string key = it.key().asString();
|
|
|
|
|
Json::Value value = command_parameter_object[key];
|
|
|
|
|
this->command_parameters_[key] = value;
|
|
|
|
|
}
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-01 11:42:36 -04:00
|
|
|
LOG(DEBUG) << "Command type is zero, no 'name' attribute in JSON object";
|
2013-01-11 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 11:42:36 -04:00
|
|
|
std::string Command::Serialize() {
|
|
|
|
|
LOG(TRACE) << "Entering Command::Serialize";
|
|
|
|
|
Json::Value json_object;
|
|
|
|
|
json_object["name"] = this->command_type_;
|
|
|
|
|
if (this->session_id_.length() == 0) {
|
|
|
|
|
json_object["sessionId"] = Json::nullValue;
|
|
|
|
|
} else {
|
|
|
|
|
json_object["sessionId"] = this->session_id_;
|
|
|
|
|
}
|
2014-09-05 02:00:00 +00:00
|
|
|
Json::Value parameters_object(Json::objectValue);
|
2014-07-01 11:42:36 -04:00
|
|
|
ParametersMap::const_iterator it = this->command_parameters_.begin();
|
|
|
|
|
ParametersMap::const_iterator end = this->command_parameters_.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
parameters_object[it->first] = it->second;
|
|
|
|
|
}
|
|
|
|
|
json_object["parameters"] = parameters_object;
|
2018-03-22 13:55:29 -07:00
|
|
|
Json::StreamWriterBuilder writer;
|
|
|
|
|
std::string output(Json::writeString(writer, json_object));
|
2014-07-01 11:42:36 -04:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 11:04:06 -08:00
|
|
|
void Command::Copy(const Command& source) {
|
|
|
|
|
this->command_type_ = source.command_type_;
|
|
|
|
|
this->command_parameters_ = source.command_parameters_;
|
|
|
|
|
this->is_valid_parameters_ = source.is_valid_parameters_;
|
|
|
|
|
this->session_id_ = source.session_id_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::Reset() {
|
|
|
|
|
this->command_type_ = CommandType::NoCommand;
|
|
|
|
|
this->session_id_ = "";
|
|
|
|
|
this->command_parameters_.clear();
|
|
|
|
|
this->is_valid_parameters_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-11 22:18:32 +01:00
|
|
|
} // namespace webdriver
|