// Copyright 2017 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine License // Agreement. // // This file contains the specializations for helper methods for the // ValueMetric class. #include "value_metric.h" #include #include "OEMCryptoCENC.h" #include "metrics_collections.h" #include "wv_cdm_types.h" namespace wvcdm { namespace metrics { namespace internal { namespace { // Max value of protobuf's int64 type. constexpr uint64_t kMaxValueCmp = static_cast(std::numeric_limits::max()); constexpr int64_t kMaxValue = std::numeric_limits::max(); template constexpr bool WillOverflow(T value) { return static_cast(value) > kMaxValueCmp; } // Prevents an overflow of very large numbers. If the value attempted to // be assigned is greater than the maximum value supported by protobuf's // int64 type, then the value is capped at the max. template constexpr int64_t SafeAssign(T value) { return WillOverflow(value) ? kMaxValue : static_cast(value); } } // namespace template <> void SetValue(drm_metrics::ValueMetric* value_proto, const int& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const long& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const long long& value) { value_proto->set_int_value(SafeAssign(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const unsigned int& value) { value_proto->set_int_value(SafeAssign(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const unsigned short& value) { value_proto->set_int_value(SafeAssign(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const unsigned long& value) { value_proto->set_int_value(SafeAssign(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const unsigned long long& value) { value_proto->set_int_value(SafeAssign(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const bool& value) { value_proto->set_int_value(value ? 1 : 0); } template <> void SetValue( drm_metrics::ValueMetric* value_proto, const OEMCrypto_HDCP_Capability& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue( drm_metrics::ValueMetric* value_proto, const OEMCrypto_ProvisioningMethod& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue( drm_metrics::ValueMetric* value_proto, const OEMCryptoInitializationMode& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const CdmSecurityLevel& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const CdmUsageSupportType& value) { value_proto->set_int_value(static_cast(value)); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const double& value) { value_proto->set_double_value(value); } template <> void SetValue(drm_metrics::ValueMetric* value_proto, const std::string& value) { value_proto->set_string_value(value); } } // namespace internal } // namespace metrics } // namespace wvcdm