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_attributes.h" | |
| 6 | |
| 7 #include <type_traits> | |
| 8 | |
| 9 #include "base/atomicops.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "build/build_config.h" | |
| 13 #if defined(OS_WIN) | |
|
Sergey Ulanov
2016/11/08 20:11:05
nit: add empty line here
Hzj_jie
2016/11/09 02:37:52
Done.
| |
| 14 #include "third_party/webrtc/modules/desktop_capture/win/screen_capturer_win_dir ectx.h" | |
| 15 #endif | |
| 16 | |
| 17 namespace remoting { | |
| 18 | |
| 19 namespace { | |
| 20 static constexpr char kSeparator[] = ","; | |
| 21 | |
| 22 struct DynamicAttribute { | |
| 23 const char* name; | |
| 24 bool(* get_value_func)(); | |
| 25 }; | |
| 26 | |
| 27 // This structure requires to be POD, so use EvaluatedValue() instead of |value| | |
| 28 // or |attribute| variables directly. | |
| 29 // This class is thread-safe, but |attribute|.value() may be evaluated several | |
| 30 // times concurrently if EvaluatedValue() function is called concurrently. | |
| 31 struct StaticAttribute { | |
| 32 static StaticAttribute Create(DynamicAttribute&& attribute) { | |
| 33 // std::move is not constexpr in c++ 11. | |
| 34 // TODO(zijiehe): Add std::move once we move to c++ 14. | |
| 35 return StaticAttribute { attribute, false, false }; | |
| 36 } | |
| 37 | |
| 38 const char* name() const; | |
| 39 bool EvaluatedValue(); | |
| 40 | |
| 41 DynamicAttribute attribute; | |
| 42 bool evaluated; | |
| 43 bool value; | |
| 44 }; | |
| 45 | |
| 46 inline constexpr bool IsDebug() { | |
| 47 #if defined(NDEBUG) | |
| 48 return false; | |
| 49 #else | |
| 50 return true; | |
| 51 #endif | |
| 52 } | |
| 53 | |
| 54 // By using arraysize() macro in base/macros.h, it's illegal to have empty | |
| 55 // arrays. | |
| 56 // | |
| 57 // error: no matching function for call to 'ArraySizeHelper' | |
| 58 // note: candidate template ignored: substitution failure | |
| 59 // [with T = const remoting::StaticAttribute, N = 0]: | |
| 60 // zero-length arrays are not permitted in C++. | |
| 61 // | |
| 62 // So we need ReturnTrue() function, and "Host-Attributes" | |
| 63 // DynamicAttribute. | |
| 64 | |
| 65 inline constexpr bool ReturnTrue() { | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 static StaticAttribute kStaticAttributes[] = { | |
| 70 StaticAttribute::Create( { "Debug-Build", &IsDebug } ), | |
| 71 }; | |
| 72 | |
| 73 static constexpr DynamicAttribute kDynamicAttributes[] = { | |
| 74 { "Host-Attributes", &ReturnTrue }, | |
| 75 #if defined(OS_WIN) | |
| 76 { "DirectX-Capturer", &webrtc::ScreenCapturerWinDirectx::IsSupported }, | |
| 77 // TODO(zijiehe): Add DirectX version attributes. Blocked by change | |
| 78 // https://codereview.chromium.org/2468083002/. | |
| 79 #endif | |
| 80 }; | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 static_assert(std::is_pod<DynamicAttribute>::value, | |
| 85 "DynamicAttribute should be POD."); | |
| 86 static_assert(std::is_pod<StaticAttribute>::value, | |
| 87 "StaticAttribute should be POD."); | |
| 88 | |
| 89 const char* StaticAttribute::name() const { | |
| 90 return attribute.name; | |
| 91 } | |
| 92 | |
| 93 bool StaticAttribute::EvaluatedValue() { | |
| 94 if (evaluated) { | |
| 95 return value; | |
| 96 } else { | |
| 97 value = attribute.get_value_func(); | |
|
Sergey Ulanov
2016/11/08 20:11:05
There is a data race because value is not atomic.
Hzj_jie
2016/11/09 02:37:52
Done.
| |
| 98 base::subtle::MemoryBarrier(); | |
| 99 evaluated = true; | |
| 100 return value; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 std::string GetHostAttributes() { | |
| 105 std::string result; | |
| 106 // By using ranged for-loop, MSVC throws error C3316: | |
| 107 // 'const remoting::StaticAttribute [0]': | |
| 108 // an array of unknown size cannot be used in a range-based for statement. | |
| 109 for (size_t i = 0; i < arraysize(kStaticAttributes); i++) { | |
| 110 auto& static_attribute = kStaticAttributes[i]; | |
| 111 DCHECK(std::string(static_attribute.name()).find(kSeparator) == | |
| 112 std::string::npos); | |
| 113 if (static_attribute.EvaluatedValue()) { | |
| 114 if (!result.empty()) { | |
| 115 result.append(kSeparator); | |
| 116 } | |
| 117 result.append(static_attribute.name()); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 for (size_t i = 0; i < arraysize(kDynamicAttributes); i++) { | |
| 122 const auto& dynamic_attribute = kDynamicAttributes[i]; | |
| 123 DCHECK(std::string(dynamic_attribute.name).find(kSeparator) == | |
| 124 std::string::npos); | |
| 125 if (dynamic_attribute.get_value_func()) { | |
| 126 if (!result.empty()) { | |
| 127 result.append(kSeparator); | |
| 128 } | |
| 129 result.append(dynamic_attribute.name); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 return result; | |
| 134 } | |
| 135 | |
| 136 } // namespace remoting | |
| OLD | NEW |