Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 5 | #include "remoting/base/session_options.h" |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "base/logging.h" |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 11 | #include "base/strings/string_split.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | |
| 14 | namespace remoting { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | static constexpr char kSeparator = ','; |
| 19 | static constexpr char kKeyValueSeparator = ':'; |
| 20 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 21 | // Whether |value| is good to be added to SessionOptions as a value. |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 22 | bool ValueIsValid(const std::string& value) { |
| 23 | return value.find(kSeparator) == std::string::npos && |
| 24 | value.find(kKeyValueSeparator) == std::string::npos && |
| 25 | base::IsStringASCII(value); |
| 26 | } |
| 27 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 28 | // Whether |key| is good to be added to SessionOptions as a key. |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 29 | bool KeyIsValid(const std::string& key) { |
| 30 | return !key.empty() && ValueIsValid(key); |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 35 | SessionOptions::SessionOptions() = default; |
Zijie He | ba57151 | 2017-11-20 20:22:23 | [diff] [blame] | 36 | SessionOptions::SessionOptions(const SessionOptions& other) = default; |
| 37 | SessionOptions::SessionOptions(SessionOptions&& other) = default; |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 38 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 39 | SessionOptions::SessionOptions(const std::string& parameter) { |
zijiehe | f38c972 | 2017-02-08 02:05:29 | [diff] [blame] | 40 | Import(parameter); |
| 41 | } |
| 42 | |
Zijie He | ba57151 | 2017-11-20 20:22:23 | [diff] [blame] | 43 | SessionOptions::~SessionOptions() = default; |
| 44 | |
Joe Downing | bc8bf50a | 2023-01-11 20:18:35 | [diff] [blame] | 45 | SessionOptions& SessionOptions::operator=(const SessionOptions& other) = |
| 46 | default; |
Zijie He | ba57151 | 2017-11-20 20:22:23 | [diff] [blame] | 47 | SessionOptions& SessionOptions::operator=(SessionOptions&& other) = default; |
| 48 | |
Joe Downing | bc8bf50a | 2023-01-11 20:18:35 | [diff] [blame] | 49 | void SessionOptions::Append(const std::string& key, const std::string& value) { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 50 | DCHECK(KeyIsValid(key)); |
| 51 | DCHECK(ValueIsValid(value)); |
| 52 | options_[key] = value; |
| 53 | } |
| 54 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 55 | absl::optional<std::string> SessionOptions::Get(const std::string& key) const { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 56 | auto it = options_.find(key); |
| 57 | if (it == options_.end()) { |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 58 | return absl::nullopt; |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 59 | } |
| 60 | return it->second; |
| 61 | } |
| 62 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 63 | absl::optional<bool> SessionOptions::GetBool(const std::string& key) const { |
| 64 | absl::optional<std::string> value = Get(key); |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 65 | if (!value) { |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 66 | return absl::nullopt; |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const std::string lowercase_value = base::ToLowerASCII(*value); |
Joe Downing | bc8bf50a | 2023-01-11 20:18:35 | [diff] [blame] | 70 | if (lowercase_value.empty() || lowercase_value == "true" || |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 71 | lowercase_value == "1") { |
| 72 | return true; |
| 73 | } |
| 74 | if (lowercase_value == "false" || lowercase_value == "0") { |
| 75 | return false; |
| 76 | } |
| 77 | LOG(WARNING) << "Unexpected option received " << *value |
| 78 | << " which cannot be converted to bool."; |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 79 | return absl::nullopt; |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 80 | } |
| 81 | |
Yuwei Huang | dabd8ea | 2020-04-28 00:44:31 | [diff] [blame] | 82 | bool SessionOptions::GetBoolValue(const std::string& key) const { |
| 83 | return GetBool(key).value_or(false); |
| 84 | } |
| 85 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 86 | absl::optional<int> SessionOptions::GetInt(const std::string& key) const { |
| 87 | absl::optional<std::string> value = Get(key); |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 88 | if (!value) { |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 89 | return absl::nullopt; |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int result; |
| 93 | if (base::StringToInt(*value, &result)) { |
| 94 | return result; |
| 95 | } |
| 96 | LOG(WARNING) << "Unexpected option received " << *value |
| 97 | << " which cannot be converted to integer."; |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 98 | return absl::nullopt; |
zijiehe | e892fa9 | 2017-05-08 19:49:55 | [diff] [blame] | 99 | } |
| 100 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 101 | std::string SessionOptions::Export() const { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 102 | std::string result; |
| 103 | for (const auto& pair : options_) { |
| 104 | if (!result.empty()) { |
| 105 | result += kSeparator; |
| 106 | } |
| 107 | if (!pair.first.empty()) { |
| 108 | result += pair.first; |
| 109 | result.push_back(kKeyValueSeparator); |
| 110 | result += pair.second; |
| 111 | } |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
Zijie He | c18adab8 | 2017-11-17 21:08:47 | [diff] [blame] | 116 | void SessionOptions::Import(const std::string& parameter) { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 117 | options_.clear(); |
| 118 | std::vector<std::pair<std::string, std::string>> result; |
Joe Downing | bc8bf50a | 2023-01-11 20:18:35 | [diff] [blame] | 119 | base::SplitStringIntoKeyValuePairs(parameter, kKeyValueSeparator, kSeparator, |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 120 | &result); |
| 121 | for (const auto& pair : result) { |
| 122 | if (KeyIsValid(pair.first) && ValueIsValid(pair.second)) { |
| 123 | Append(pair.first, pair.second); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 128 | } // namespace remoting |