Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_traits.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/string_piece.h" | |
| 9 #include "remoting/host/directx_traits.h" | |
| 10 | |
| 11 namespace remoting { | |
| 12 | |
| 13 namespace { | |
| 14 static constexpr char kSeparator[] = ","; | |
| 15 } // namespace | |
| 16 | |
| 17 HostTraits& host_traits() { | |
| 18 // This instance does not release to ensure it can still work during the | |
| 19 // ending of the process. | |
| 20 static HostTraits* instance = new HostTraits(); | |
|
Sergey Ulanov
2016/11/02 20:10:32
base::Singleton should be used for singletons. Chr
Hzj_jie
2016/11/03 02:29:34
I think this is a typical scenario of singleton. O
Sergey Ulanov
2016/11/03 19:05:34
There are many other classes for which we have onl
Hzj_jie
2016/11/03 21:33:30
Done.
| |
| 21 return *instance; | |
| 22 } | |
| 23 | |
| 24 HostTraits::HostTraits() { | |
| 25 RegisterDirectxTraits(); | |
| 26 } | |
| 27 | |
| 28 HostTraits::~HostTraits() = default; | |
| 29 | |
| 30 std::string HostTraits::ToString() const { | |
| 31 std::string result; | |
| 32 for (const auto& str : static_values_) { | |
| 33 DCHECK(!str.empty()); | |
| 34 if (!result.empty()) { | |
| 35 result.append(kSeparator); | |
| 36 } | |
| 37 result.append(str); | |
| 38 } | |
| 39 | |
| 40 for (const auto& pair : dynamic_values_) { | |
| 41 DCHECK(!pair.first.empty()); | |
| 42 DCHECK(!pair.second.is_null()); | |
| 43 if (pair.second.Run()) { | |
| 44 if (!result.empty()) { | |
| 45 result.append(kSeparator); | |
| 46 } | |
| 47 result.append(pair.first); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 return result; | |
| 52 } | |
| 53 | |
| 54 void HostTraits::Register(const std::string& key) { | |
| 55 DCHECK(key.find(kSeparator) == std::string::npos); | |
| 56 DCHECK(dynamic_values_.find(key) == dynamic_values_.end()); | |
| 57 DCHECK(static_values_.find(key) == static_values_.end()); | |
| 58 static_values_.insert(key); | |
| 59 } | |
| 60 | |
| 61 void HostTraits::Register(const std::string& key, | |
| 62 const base::Callback<bool(void)>& value) { | |
| 63 DCHECK(key.find(kSeparator) == std::string::npos); | |
| 64 DCHECK(dynamic_values_.find(key) == dynamic_values_.end()); | |
| 65 DCHECK(static_values_.find(key) == static_values_.end()); | |
| 66 dynamic_values_[key] = value; | |
| 67 } | |
| 68 | |
| 69 void HostTraits::Erase(const std::string& key) { | |
| 70 size_t removed = dynamic_values_.erase(key) + static_values_.erase(key); | |
| 71 DCHECK(removed == 1); | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 bool HostTraits::ContainsTrait(const std::string& output, | |
| 76 const std::string& key) { | |
| 77 return output == key || | |
| 78 base::StringPiece(output).ends_with(std::string(kSeparator) + key) || | |
| 79 base::StringPiece(output).starts_with(key + kSeparator) || | |
| 80 output.find(std::string(kSeparator) + key + kSeparator) | |
| 81 != std::string::npos; | |
| 82 } | |
| 83 | |
| 84 } // namespace remoting | |
| OLD | NEW |