From 86efc7534ae9b7201d4013523248ea52b9d2d35d Mon Sep 17 00:00:00 2001 From: "John W. Bruce" Date: Tue, 15 Jan 2019 16:24:45 -0800 Subject: [PATCH] Make Properties::Init() Take Action Only Once (This is a merge of http://go/wvgerrit/70383) Up until now, implementations of Properties::Init() have had to handle potentially being called multiple times, at any point during runtime. In practice, this has meant little for the actual implementations, and all of them have committed the error of blowing away mutated property state if the method is re-run at the wrong time. This patch makes the platform implementations a private function, Properties::InitOnce(), which Properties::Init() ensures will never be called more than once per run. Bug: 112046733 Test: CE CDM Unit Tests Test: Android Unit Tests Change-Id: If33f5e37abfad5d26da8380b4bc25fc018450970 --- libwvdrmengine/cdm/core/include/properties.h | 19 ++++++++++++++----- libwvdrmengine/cdm/core/src/properties.cpp | 2 ++ libwvdrmengine/cdm/src/properties_android.cpp | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libwvdrmengine/cdm/core/include/properties.h b/libwvdrmengine/cdm/core/include/properties.h index ec797df9..8840f07c 100644 --- a/libwvdrmengine/cdm/core/include/properties.h +++ b/libwvdrmengine/cdm/core/include/properties.h @@ -7,6 +7,7 @@ #include #include +#include #include #include "cdm_client_property_set.h" @@ -30,11 +31,14 @@ typedef std::map // Setter methods are provided but their only planned use is for testing. class Properties { public: - // Called at least once before any properties are used. Depending on the - // platform, this function may be called multiple times. It is called each - // time a CdmEngine is created, and when running unit tests it is called in - // many tests' SetUp function. - static void Init(); + static void Init() { + std::unique_lock lock(init_mutex_); + + if (!is_initialized_) { + InitOnce(); + is_initialized_ = true; + } + } static inline bool oem_crypto_use_secure_buffers() { return oem_crypto_use_secure_buffers_; @@ -108,6 +112,11 @@ class Properties { #endif private: + // Called at least once before any properties are used. + static void InitOnce(); + + static std::mutex init_mutex_; + static bool is_initialized_; static bool oem_crypto_use_secure_buffers_; static bool oem_crypto_use_fifo_; static bool oem_crypto_use_userspace_buffers_; diff --git a/libwvdrmengine/cdm/core/src/properties.cpp b/libwvdrmengine/cdm/core/src/properties.cpp index 84778df9..e6f685c3 100644 --- a/libwvdrmengine/cdm/core/src/properties.cpp +++ b/libwvdrmengine/cdm/core/src/properties.cpp @@ -11,6 +11,8 @@ const char* kSecurityLevelDirs[] = {"L1/", "L3/"}; } // namespace namespace wvcdm { +std::mutex Properties::init_mutex_; +bool Properties::is_initialized_ = false; bool Properties::oem_crypto_use_secure_buffers_; bool Properties::oem_crypto_use_fifo_; bool Properties::oem_crypto_use_userspace_buffers_; diff --git a/libwvdrmengine/cdm/src/properties_android.cpp b/libwvdrmengine/cdm/src/properties_android.cpp index b469858e..0ca0881b 100644 --- a/libwvdrmengine/cdm/src/properties_android.cpp +++ b/libwvdrmengine/cdm/src/properties_android.cpp @@ -45,7 +45,7 @@ bool GetAndroidProperty(const char* key, std::string* value) { namespace wvcdm { -void Properties::Init() { +void Properties::InitOnce() { oem_crypto_use_secure_buffers_ = kPropertyOemCryptoUseSecureBuffers; oem_crypto_use_fifo_ = kPropertyOemCryptoUseFifo; oem_crypto_use_userspace_buffers_ = kPropertyOemCryptoUseUserSpaceBuffers;