zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/host/host_session_options.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/strings/string_split.h" |
| 11 | #include "base/strings/string_util.h" |
| 12 | |
| 13 | namespace remoting { |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
| 17 | static constexpr char kSeparator = ','; |
| 18 | static constexpr char kKeyValueSeparator = ':'; |
| 19 | |
| 20 | // Whether |value| is good to be added to HostSessionOptions as a value. |
| 21 | bool ValueIsValid(const std::string& value) { |
| 22 | return value.find(kSeparator) == std::string::npos && |
| 23 | value.find(kKeyValueSeparator) == std::string::npos && |
| 24 | base::IsStringASCII(value); |
| 25 | } |
| 26 | |
| 27 | // Whether |key| is good to be added to HostSessionOptions as a key. |
| 28 | bool KeyIsValid(const std::string& key) { |
| 29 | return !key.empty() && ValueIsValid(key); |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | HostSessionOptions::HostSessionOptions() = default; |
| 35 | HostSessionOptions::~HostSessionOptions() = default; |
| 36 | |
zijiehe | f38c972 | 2017-02-08 02:05:29 | [diff] [blame] | 37 | HostSessionOptions::HostSessionOptions(const std::string& parameter) { |
| 38 | Import(parameter); |
| 39 | } |
| 40 | |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 41 | void HostSessionOptions::Append(const std::string& key, |
| 42 | const std::string& value) { |
| 43 | DCHECK(KeyIsValid(key)); |
| 44 | DCHECK(ValueIsValid(value)); |
| 45 | options_[key] = value; |
| 46 | } |
| 47 | |
| 48 | base::Optional<std::string> HostSessionOptions::Get( |
| 49 | const std::string& key) const { |
| 50 | auto it = options_.find(key); |
| 51 | if (it == options_.end()) { |
| 52 | return base::nullopt; |
| 53 | } |
| 54 | return it->second; |
| 55 | } |
| 56 | |
| 57 | std::string HostSessionOptions::Export() const { |
| 58 | std::string result; |
| 59 | for (const auto& pair : options_) { |
| 60 | if (!result.empty()) { |
| 61 | result += kSeparator; |
| 62 | } |
| 63 | if (!pair.first.empty()) { |
| 64 | result += pair.first; |
| 65 | result.push_back(kKeyValueSeparator); |
| 66 | result += pair.second; |
| 67 | } |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | void HostSessionOptions::Import(const std::string& parameter) { |
| 73 | options_.clear(); |
| 74 | std::vector<std::pair<std::string, std::string>> result; |
| 75 | base::SplitStringIntoKeyValuePairs(parameter, |
| 76 | kKeyValueSeparator, |
| 77 | kSeparator, |
| 78 | &result); |
| 79 | for (const auto& pair : result) { |
| 80 | if (KeyIsValid(pair.first) && ValueIsValid(pair.second)) { |
| 81 | Append(pair.first, pair.second); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
zijiehe | 21d8d391 | 2016-11-18 20:14:21 | [diff] [blame] | 86 | } // namespace remoting |