blob: 8511f278ceddc6d297a55a4378d16ee19bf00edf [file] [log] [blame]
zijiehe21d8d3912016-11-18 20:14:211// 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
13namespace remoting {
zijiehe21d8d3912016-11-18 20:14:2114
15namespace {
16
17static constexpr char kSeparator = ',';
18static constexpr char kKeyValueSeparator = ':';
19
20// Whether |value| is good to be added to HostSessionOptions as a value.
21bool 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.
28bool KeyIsValid(const std::string& key) {
29 return !key.empty() && ValueIsValid(key);
30}
31
32} // namespace
33
34HostSessionOptions::HostSessionOptions() = default;
35HostSessionOptions::~HostSessionOptions() = default;
36
zijiehef38c9722017-02-08 02:05:2937HostSessionOptions::HostSessionOptions(const std::string& parameter) {
38 Import(parameter);
39}
40
zijiehe21d8d3912016-11-18 20:14:2141void 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
48base::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
57std::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
72void 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
zijiehe21d8d3912016-11-18 20:14:2186} // namespace remoting