Index: remoting/host/host_attributes.cc |
diff --git a/remoting/host/host_attributes.cc b/remoting/host/host_attributes.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8eee6db9abd6f6b517e4335586ea81ac8477fd6 |
--- /dev/null |
+++ b/remoting/host/host_attributes.cc |
@@ -0,0 +1,136 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/host/host_attributes.h" |
+ |
+#include <type_traits> |
+ |
+#include "base/atomicops.h" |
+#include "base/logging.h" |
+#include "base/macros.h" |
+#include "build/build_config.h" |
+#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.
|
+#include "third_party/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
+#endif |
+ |
+namespace remoting { |
+ |
+namespace { |
+static constexpr char kSeparator[] = ","; |
+ |
+struct DynamicAttribute { |
+ const char* name; |
+ bool(* get_value_func)(); |
+}; |
+ |
+// This structure requires to be POD, so use EvaluatedValue() instead of |value| |
+// or |attribute| variables directly. |
+// This class is thread-safe, but |attribute|.value() may be evaluated several |
+// times concurrently if EvaluatedValue() function is called concurrently. |
+struct StaticAttribute { |
+ static StaticAttribute Create(DynamicAttribute&& attribute) { |
+ // std::move is not constexpr in c++ 11. |
+ // TODO(zijiehe): Add std::move once we move to c++ 14. |
+ return StaticAttribute { attribute, false, false }; |
+ } |
+ |
+ const char* name() const; |
+ bool EvaluatedValue(); |
+ |
+ DynamicAttribute attribute; |
+ bool evaluated; |
+ bool value; |
+}; |
+ |
+inline constexpr bool IsDebug() { |
+#if defined(NDEBUG) |
+ return false; |
+#else |
+ return true; |
+#endif |
+} |
+ |
+// By using arraysize() macro in base/macros.h, it's illegal to have empty |
+// arrays. |
+// |
+// error: no matching function for call to 'ArraySizeHelper' |
+// note: candidate template ignored: substitution failure |
+// [with T = const remoting::StaticAttribute, N = 0]: |
+// zero-length arrays are not permitted in C++. |
+// |
+// So we need ReturnTrue() function, and "Host-Attributes" |
+// DynamicAttribute. |
+ |
+inline constexpr bool ReturnTrue() { |
+ return true; |
+} |
+ |
+static StaticAttribute kStaticAttributes[] = { |
+ StaticAttribute::Create( { "Debug-Build", &IsDebug } ), |
+}; |
+ |
+static constexpr DynamicAttribute kDynamicAttributes[] = { |
+ { "Host-Attributes", &ReturnTrue }, |
+#if defined(OS_WIN) |
+ { "DirectX-Capturer", &webrtc::ScreenCapturerWinDirectx::IsSupported }, |
+ // TODO(zijiehe): Add DirectX version attributes. Blocked by change |
+ // https://codereview.chromium.org/2468083002/. |
+#endif |
+}; |
+ |
+} // namespace |
+ |
+static_assert(std::is_pod<DynamicAttribute>::value, |
+ "DynamicAttribute should be POD."); |
+static_assert(std::is_pod<StaticAttribute>::value, |
+ "StaticAttribute should be POD."); |
+ |
+const char* StaticAttribute::name() const { |
+ return attribute.name; |
+} |
+ |
+bool StaticAttribute::EvaluatedValue() { |
+ if (evaluated) { |
+ return value; |
+ } else { |
+ 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.
|
+ base::subtle::MemoryBarrier(); |
+ evaluated = true; |
+ return value; |
+ } |
+} |
+ |
+std::string GetHostAttributes() { |
+ std::string result; |
+ // By using ranged for-loop, MSVC throws error C3316: |
+ // 'const remoting::StaticAttribute [0]': |
+ // an array of unknown size cannot be used in a range-based for statement. |
+ for (size_t i = 0; i < arraysize(kStaticAttributes); i++) { |
+ auto& static_attribute = kStaticAttributes[i]; |
+ DCHECK(std::string(static_attribute.name()).find(kSeparator) == |
+ std::string::npos); |
+ if (static_attribute.EvaluatedValue()) { |
+ if (!result.empty()) { |
+ result.append(kSeparator); |
+ } |
+ result.append(static_attribute.name()); |
+ } |
+ } |
+ |
+ for (size_t i = 0; i < arraysize(kDynamicAttributes); i++) { |
+ const auto& dynamic_attribute = kDynamicAttributes[i]; |
+ DCHECK(std::string(dynamic_attribute.name).find(kSeparator) == |
+ std::string::npos); |
+ if (dynamic_attribute.get_value_func()) { |
+ if (!result.empty()) { |
+ result.append(kSeparator); |
+ } |
+ result.append(dynamic_attribute.name); |
+ } |
+ } |
+ |
+ return result; |
+} |
+ |
+} // namespace remoting |