130 lines
3.9 KiB
C++
130 lines
3.9 KiB
C++
// 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 <limits>
|
|
|
|
#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<uint64_t>(std::numeric_limits<int64_t>::max());
|
|
constexpr int64_t kMaxValue = std::numeric_limits<int64_t>::max();
|
|
|
|
template <typename T>
|
|
constexpr bool WillOverflow(T value) {
|
|
return static_cast<uint64_t>(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 <typename T>
|
|
constexpr int64_t SafeAssign(T value) {
|
|
return WillOverflow(value) ? kMaxValue : static_cast<int64_t>(value);
|
|
}
|
|
} // namespace
|
|
|
|
template <>
|
|
void SetValue<int>(drm_metrics::ValueMetric* value_proto, const int& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<long>(drm_metrics::ValueMetric* value_proto, const long& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<long long>(drm_metrics::ValueMetric* value_proto,
|
|
const long long& value) {
|
|
value_proto->set_int_value(SafeAssign(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<unsigned int>(drm_metrics::ValueMetric* value_proto,
|
|
const unsigned int& value) {
|
|
value_proto->set_int_value(SafeAssign(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<unsigned short>(drm_metrics::ValueMetric* value_proto,
|
|
const unsigned short& value) {
|
|
value_proto->set_int_value(SafeAssign(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<unsigned long>(drm_metrics::ValueMetric* value_proto,
|
|
const unsigned long& value) {
|
|
value_proto->set_int_value(SafeAssign(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<unsigned long long>(drm_metrics::ValueMetric* value_proto,
|
|
const unsigned long long& value) {
|
|
value_proto->set_int_value(SafeAssign(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<bool>(drm_metrics::ValueMetric* value_proto, const bool& value) {
|
|
value_proto->set_int_value(value ? 1 : 0);
|
|
}
|
|
|
|
template <>
|
|
void SetValue<OEMCrypto_HDCP_Capability>(
|
|
drm_metrics::ValueMetric* value_proto,
|
|
const OEMCrypto_HDCP_Capability& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<OEMCrypto_ProvisioningMethod>(
|
|
drm_metrics::ValueMetric* value_proto,
|
|
const OEMCrypto_ProvisioningMethod& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<OEMCryptoInitializationMode>(
|
|
drm_metrics::ValueMetric* value_proto,
|
|
const OEMCryptoInitializationMode& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<CdmSecurityLevel>(drm_metrics::ValueMetric* value_proto,
|
|
const CdmSecurityLevel& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<CdmUsageSupportType>(drm_metrics::ValueMetric* value_proto,
|
|
const CdmUsageSupportType& value) {
|
|
value_proto->set_int_value(static_cast<int64_t>(value));
|
|
}
|
|
|
|
template <>
|
|
void SetValue<double>(drm_metrics::ValueMetric* value_proto,
|
|
const double& value) {
|
|
value_proto->set_double_value(value);
|
|
}
|
|
|
|
template <>
|
|
void SetValue<std::string>(drm_metrics::ValueMetric* value_proto,
|
|
const std::string& value) {
|
|
value_proto->set_string_value(value);
|
|
}
|
|
} // namespace internal
|
|
} // namespace metrics
|
|
} // namespace wvcdm
|