Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/webdriver-server/command.cc
2867 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 "command.h"
18
#include "command_types.h"
19
#include "logging.h"
20
21
namespace webdriver {
22
23
Command::Command() : command_type_(webdriver::CommandType::NoCommand),
24
session_id_("") {
25
}
26
27
Command::~Command() {
28
}
29
30
void Command::Deserialize(const std::string& json) {
31
LOG(TRACE) << "Entering Command::Deserialize";
32
33
// Clear the existing maps.
34
this->command_parameters_.clear();
35
36
LOG(DEBUG) << "Raw JSON command: " << json;
37
38
Json::Value root;
39
std::string parse_errors;
40
std::stringstream json_stream;
41
json_stream.str(json);
42
bool successful_parse = Json::parseFromStream(Json::CharReaderBuilder(),
43
json_stream,
44
&root,
45
&parse_errors);
46
47
if (!successful_parse) {
48
// report to the user the failure and their locations in the document.
49
LOG(WARN) << "Failed to parse configuration due to "
50
<< parse_errors << std::endl
51
<< "JSON command: '" << json << "'";
52
}
53
54
this->command_type_ = root.get("name", webdriver::CommandType::NoCommand).asString();
55
if (this->command_type_ != webdriver::CommandType::NoCommand) {
56
Json::Value locator_parameter_object = root["locator"];
57
Json::Value::iterator it = locator_parameter_object.begin();
58
Json::Value::iterator end = locator_parameter_object.end();
59
for (; it != end; ++it) {
60
std::string key = it.key().asString();
61
std::string value = locator_parameter_object[key].asString();
62
if (key == "sessionid") {
63
this->session_id_ = value;
64
} else {
65
this->command_parameters_[key] = value;
66
}
67
}
68
69
this->is_valid_parameters_ = true;
70
Json::Value command_parameter_object = root["parameters"];
71
if (!command_parameter_object.isObject()) {
72
LOG(WARN) << "The value of the 'parameters' attribute is not a JSON "
73
<< "object. This is invalid for the WebDriver JSON Wire "
74
<< "Protocol.";
75
this->is_valid_parameters_ = false;
76
} else {
77
it = command_parameter_object.begin();
78
end = command_parameter_object.end();
79
for (; it != end; ++it) {
80
std::string key = it.key().asString();
81
Json::Value value = command_parameter_object[key];
82
this->command_parameters_[key] = value;
83
}
84
}
85
} else {
86
LOG(DEBUG) << "Command type is zero, no 'name' attribute in JSON object";
87
}
88
}
89
90
std::string Command::Serialize() {
91
LOG(TRACE) << "Entering Command::Serialize";
92
Json::Value json_object;
93
json_object["name"] = this->command_type_;
94
if (this->session_id_.length() == 0) {
95
json_object["sessionId"] = Json::nullValue;
96
} else {
97
json_object["sessionId"] = this->session_id_;
98
}
99
Json::Value parameters_object(Json::objectValue);
100
ParametersMap::const_iterator it = this->command_parameters_.begin();
101
ParametersMap::const_iterator end = this->command_parameters_.end();
102
for (; it != end; ++it) {
103
parameters_object[it->first] = it->second;
104
}
105
json_object["parameters"] = parameters_object;
106
Json::StreamWriterBuilder writer;
107
std::string output(Json::writeString(writer, json_object));
108
return output;
109
}
110
111
void Command::Copy(const Command& source) {
112
this->command_type_ = source.command_type_;
113
this->command_parameters_ = source.command_parameters_;
114
this->is_valid_parameters_ = source.is_valid_parameters_;
115
this->session_id_ = source.session_id_;
116
}
117
118
void Command::Reset() {
119
this->command_type_ = CommandType::NoCommand;
120
this->session_id_ = "";
121
this->command_parameters_.clear();
122
this->is_valid_parameters_ = false;
123
}
124
125
} // namespace webdriver
126
127