Log cleanup and reformatting for core/ (Part 2-6)

[ Merge of http://go/wvgerrit/83423 ]
[ Merge of http://go/wvgerrit/83424 ]
[ Merge of http://go/wvgerrit/83425 ]
[ Merge of http://go/wvgerrit/83426 ]
[ Merge of http://go/wvgerrit/83427 ]

Types of cleanup:
  - Removed function / class prefixes from the logs.
  - Fixed log string format options to match the types passed
  - Corrected small spelling mistakes / typos
  - _Tried_ to make the log format more consistent
  - Added static_cast<int> conversion on enumerations when logged
  - Changed several LOGE to LOGW and vice versa
      - Used LOGE if the triggering condition stops the method/function
        from completing its task
      - Used LOGW if the triggering condition changes the expected
        outcome but does not stop the rest of the method/function's
        task
  - Changed several instances of `NULL` to `nullptr`
  - Ran clang-format on files after cleanup

This is part of a larger code quality effort in Widevine DRM.

Test: WV linux unittests and WV Android unit tests
Bug: 134460638
Bug: 134365840
Bug: 136123217
Change-Id: I958ec70ef99eef95c38dbebd7a1acd62ef304145
This commit is contained in:
Alex Dale
2019-08-01 11:18:12 -07:00
parent 79a28e5ddb
commit f4360552b7
15 changed files with 871 additions and 1132 deletions

View File

@@ -40,11 +40,34 @@ using video_widevine_client::sdk::
using video_widevine::DrmDeviceCertificate;
using video_widevine::SignedDrmDeviceCertificate;
#define RETURN_FALSE_IF_NULL(PARAM) \
if (PARAM == nullptr) { \
LOGE("|PARAM| not provided"); \
*result = kParameterNull; \
return false; \
// Stringify turns macro arguments into static C strings.
// Example: STRINGIFY(this_argument) -> "this_argument"
#define STRINGIFY(PARAM...) #PARAM
#define RETURN_FALSE_IF_NULL(PARAM) \
if ((PARAM) == nullptr) { \
LOGE("Output parameter |" STRINGIFY(PARAM) "| not provided"); \
return false; \
}
#define RETURN_FALSE_WITH_RESULT_IF_NULL(PARAM, result) \
if ((PARAM) == nullptr) { \
LOGE("Output parameter |" STRINGIFY(PARAM) "| not provided"); \
*result = kParameterNull; \
return false; \
}
#define RETURN_FALSE_IF_UNINITIALIZED() \
if (!initialized_) { \
LOGE("Device files is not initialized"); \
return false; \
}
#define RETURN_FALSE_WITH_RESULT_IF_UNINITIALIZED(result) \
if (!initialized_) { \
LOGE("Device files is not initialized"); \
*result = kObjectNotInitialized; \
return false; \
}
namespace {
@@ -74,13 +97,13 @@ DeviceFiles::~DeviceFiles() {}
bool DeviceFiles::Init(CdmSecurityLevel security_level) {
if (!file_system_) {
LOGD("DeviceFiles::Init: Invalid FileSystem given.");
LOGE("Invalid FileSystem given");
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level, &path)) {
LOGW("DeviceFiles::Init: Unsupported security level %d", security_level);
LOGE("Unsupported security level: %d", security_level);
return false;
}
security_level_ = security_level;
@@ -90,10 +113,7 @@ bool DeviceFiles::Init(CdmSecurityLevel security_level) {
bool DeviceFiles::StoreCertificate(const std::string& certificate,
const std::string& wrapped_private_key) {
if (!initialized_) {
LOGW("DeviceFiles::StoreCertificate: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
// Fill in file information
video_widevine_client::sdk::File file;
@@ -116,29 +136,28 @@ bool DeviceFiles::RetrieveCertificate(std::string* certificate,
std::string* wrapped_private_key,
std::string* serial_number,
uint32_t* system_id) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveCertificate: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(GetCertificateFileName(), &file) != kNoError) {
LOGW("DeviceFiles::RetrieveCertificate: unable to retrieve file");
LOGE("Unable to retrieve certificate file");
return false;
}
if (file.type() != video_widevine_client::sdk::File::DEVICE_CERTIFICATE) {
LOGW("DeviceFiles::RetrieveCertificate: Incorrect file type");
LOGE("Certificate file is of incorrect file type: type = %d",
static_cast<int>(file.type()));
return false;
}
if (file.version() != video_widevine_client::sdk::File::VERSION_1) {
LOGW("DeviceFiles::RetrieveCertificate: Incorrect file version");
LOGE("Certificate file is of incorrect file version: version = %d",
static_cast<int>(file.version()));
return false;
}
if (!file.has_device_certificate()) {
LOGW("DeviceFiles::RetrieveCertificate: Certificate not present");
LOGE("Certificate not present");
return false;
}
@@ -152,9 +171,9 @@ bool DeviceFiles::RetrieveCertificate(std::string* certificate,
bool DeviceFiles::ExtractDeviceInfo(const std::string& device_certificate,
std::string* serial_number,
uint32_t* system_id) {
LOGI("ExtractDeviceInfo Entry");
if (!serial_number && !system_id) {
LOGE("DeviceFiles::ExtractDeviceInfo: invalid parameter.");
LOGV("Extracting device info");
if (serial_number == nullptr && system_id == nullptr) {
LOGE("Output parameters |serial_number| and |system_id| not provided");
return false;
}
@@ -162,9 +181,7 @@ bool DeviceFiles::ExtractDeviceInfo(const std::string& device_certificate,
SignedDrmDeviceCertificate signed_drm_device_certificate;
if (!signed_drm_device_certificate.ParseFromString(device_certificate) ||
!signed_drm_device_certificate.has_drm_certificate()) {
LOGE(
"DeviceFiles::ExtractDeviceInfo: fails parsing signed drm device "
"certificate.");
LOGE("Failed to parse signed DRM device certificate");
return false;
}
DrmDeviceCertificate drm_device_certificate;
@@ -172,9 +189,7 @@ bool DeviceFiles::ExtractDeviceInfo(const std::string& device_certificate,
signed_drm_device_certificate.drm_certificate()) ||
(drm_device_certificate.type() !=
video_widevine::DrmDeviceCertificate::DRM_USER_DEVICE)) {
LOGE(
"DeviceFiles::ExtractDeviceInfo: fails parsing drm device "
"certificate message.");
LOGE("Failed to parse DRM device certificate message");
return false;
}
if (serial_number != NULL) {
@@ -187,20 +202,12 @@ bool DeviceFiles::ExtractDeviceInfo(const std::string& device_certificate,
}
bool DeviceFiles::HasCertificate() {
if (!initialized_) {
LOGW("DeviceFiles::HasCertificate: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return FileExists(GetCertificateFileName());
}
bool DeviceFiles::RemoveCertificate() {
if (!initialized_) {
LOGW("DeviceFiles::RemoveCertificate: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED()
return RemoveFile(GetCertificateFileName());
}
@@ -214,18 +221,10 @@ bool DeviceFiles::StoreLicense(
int64_t last_playback_time, int64_t grace_period_end_time,
const CdmAppParameterMap& app_parameters, const CdmUsageEntry& usage_entry,
const uint32_t usage_entry_number, ResponseType* result) {
if (result == nullptr) {
LOGE("DeviceFiles::StoreLicense: |result| not provided");
return false;
}
RETURN_FALSE_IF_NULL(result);
*result = kNoError;
if (!initialized_) {
LOGW("DeviceFiles::StoreLicense: not initialized");
*result = kObjectNotInitialized;
return false;
}
RETURN_FALSE_WITH_RESULT_IF_UNINITIALIZED(result);
// Fill in file information
video_widevine_client::sdk::File file;
@@ -242,7 +241,7 @@ bool DeviceFiles::StoreLicense(
license->set_state(License_LicenseState_RELEASING);
break;
default:
LOGW("DeviceFiles::StoreLicense: Unknown license state: %u", state);
LOGE("Unknown license state: %d", static_cast<int>(state));
*result = kUnknownLicenseState;
return false;
break;
@@ -283,52 +282,54 @@ bool DeviceFiles::RetrieveLicense(
int64_t* last_playback_time, int64_t* grace_period_end_time,
CdmAppParameterMap* app_parameters, CdmUsageEntry* usage_entry,
uint32_t* usage_entry_number, ResponseType* result) {
// This check must be made first as the RETURN_FALSE_IF_NULL() macro
// will assign NULL_PARAMETER to |result|.
if (result == nullptr) {
LOGE("DeviceFiles::RetrieveLicense: |result| not provided");
LOGE("Output parameter |result| not provided");
return false;
}
RETURN_FALSE_WITH_RESULT_IF_UNINITIALIZED(result);
if (!initialized_) {
LOGW("DeviceFiles::RetrieveLicense: not initialized");
*result = kObjectNotInitialized;
return false;
}
RETURN_FALSE_IF_NULL(state);
RETURN_FALSE_IF_NULL(pssh_data);
RETURN_FALSE_IF_NULL(license_request);
RETURN_FALSE_IF_NULL(license_message);
RETURN_FALSE_IF_NULL(license_renewal_request);
RETURN_FALSE_IF_NULL(license_renewal);
RETURN_FALSE_IF_NULL(release_server_url);
RETURN_FALSE_IF_NULL(playback_start_time);
RETURN_FALSE_IF_NULL(last_playback_time);
RETURN_FALSE_IF_NULL(grace_period_end_time);
RETURN_FALSE_IF_NULL(app_parameters);
RETURN_FALSE_IF_NULL(usage_entry);
RETURN_FALSE_IF_NULL(usage_entry_number);
RETURN_FALSE_WITH_RESULT_IF_NULL(state, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(pssh_data, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(license_request, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(license_message, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(license_renewal_request, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(license_renewal, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(release_server_url, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(playback_start_time, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(last_playback_time, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(grace_period_end_time, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(app_parameters, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(usage_entry, result);
RETURN_FALSE_WITH_RESULT_IF_NULL(usage_entry_number, result);
video_widevine_client::sdk::File file;
*result = RetrieveHashedFile(key_set_id + kLicenseFileNameExt, &file);
if (*result != kNoError) {
LOGW("DeviceFiles::RetrieveLicense: unable to retrieve file: %d", *result);
LOGE("Unable to retrieve key set license file: result = %d",
static_cast<int>(*result));
return false;
}
if (file.type() != video_widevine_client::sdk::File::LICENSE) {
LOGW("DeviceFiles::RetrieveLicense: Incorrect file type");
LOGE("Incorrect file type: type = %d, expected_type = %d",
static_cast<int>(file.type()),
static_cast<int>(video_widevine_client::sdk::File::LICENSE));
*result = kIncorrectFileType;
return false;
}
if (file.version() != video_widevine_client::sdk::File::VERSION_1) {
LOGW("DeviceFiles::RetrieveLicense: Incorrect file version");
LOGE("Incorrect file version: version = %d, expected_version = %d",
static_cast<int>(file.version()),
static_cast<int>(video_widevine_client::sdk::File::VERSION_1));
*result = kIncorrectFileVersion;
return false;
}
if (!file.has_license()) {
LOGW("DeviceFiles::RetrieveLicense: License not present");
LOGE("License not present");
*result = kLicenseNotPresent;
return false;
}
@@ -343,8 +344,7 @@ bool DeviceFiles::RetrieveLicense(
*state = kLicenseStateReleasing;
break;
default:
LOGW("DeviceFiles::RetrieveLicense: Unrecognized license state: %u",
kLicenseStateUnknown);
LOGW("Unrecognized license state: %d", static_cast<int>(license.state()));
*state = kLicenseStateUnknown;
break;
}
@@ -367,23 +367,13 @@ bool DeviceFiles::RetrieveLicense(
}
bool DeviceFiles::DeleteLicense(const std::string& key_set_id) {
if (!initialized_) {
LOGW("DeviceFiles::DeleteLicense: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return RemoveFile(key_set_id + kLicenseFileNameExt);
}
bool DeviceFiles::ListLicenses(std::vector<std::string>* key_set_ids) {
if (!initialized_) {
LOGW("DeviceFiles::ListLicenses: not initialized");
return false;
}
if (key_set_ids == NULL) {
LOGW("DeviceFiles::ListLicenses: key_set_ids parameter not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(key_set_ids);
// Get list of filenames
std::vector<std::string> filenames;
@@ -408,47 +398,31 @@ bool DeviceFiles::ListLicenses(std::vector<std::string>* key_set_ids) {
}
bool DeviceFiles::DeleteAllLicenses() {
if (!initialized_) {
LOGW("DeviceFiles::DeleteAllLicenses: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return RemoveFile(std::string(kWildcard) + kLicenseFileNameExt);
}
bool DeviceFiles::DeleteAllFiles() {
if (!initialized_) {
LOGW("DeviceFiles::DeleteAllFiles: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
// We pass an empty string to RemoveFile to delete the device files base
// directory itself.
return RemoveFile(kEmptyFileName);
}
bool DeviceFiles::LicenseExists(const std::string& key_set_id) {
if (!initialized_) {
LOGW("DeviceFiles::LicenseExists: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return reserved_license_ids_.count(key_set_id) ||
FileExists(key_set_id + kLicenseFileNameExt);
}
bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) {
if (!initialized_) {
LOGW("DeviceFiles::ReserveLicenseId: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
reserved_license_ids_.insert(key_set_id);
return true;
}
bool DeviceFiles::UnreserveLicenseId(const std::string& key_set_id) {
if (!initialized_) {
LOGW("DeviceFiles::UnreserveLicenseId: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
reserved_license_ids_.erase(key_set_id);
return true;
}
@@ -460,18 +434,14 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
const std::string& key_set_id,
const std::string& usage_entry,
uint32_t usage_entry_number) {
if (!initialized_) {
LOGW("DeviceFiles::StoreUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (!FileExists(usage_info_file_name)) {
file.set_type(video_widevine_client::sdk::File::USAGE_INFO);
file.set_version(video_widevine_client::sdk::File::VERSION_1);
} else {
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::StoreUsageInfo: Unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
}
@@ -495,40 +465,39 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
bool DeviceFiles::ListUsageIds(
const std::string& app_id, std::vector<std::string>* ksids,
std::vector<std::string>* provider_session_tokens) {
if (!initialized_) {
LOGW("DeviceFiles::ListUsageIds: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
if (ksids == NULL && provider_session_tokens == NULL) {
LOGW("DeviceFiles::ListUsageIds: ksids or pst parameter not provided");
if (ksids == nullptr && provider_session_tokens == nullptr) {
LOGE(
"Both output parameters |ksids| and |provider_session_tokens| are "
"not provided");
return false;
}
// Empty or non-existent file == no usage records.
std::string file_name = GetUsageInfoFileName(app_id);
if (!FileExists(file_name) || GetFileSize(file_name) == 0) {
if (ksids != NULL) ksids->clear();
if (provider_session_tokens != NULL) provider_session_tokens->clear();
if (ksids != nullptr) ksids->clear();
if (provider_session_tokens != nullptr) provider_session_tokens->clear();
return true;
}
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(file_name, &file) != kNoError) {
LOGW("DeviceFiles::ListUsageRecords: Unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
if (ksids != NULL) ksids->clear();
if (provider_session_tokens != NULL) provider_session_tokens->clear();
if (ksids != nullptr) ksids->clear();
if (provider_session_tokens != nullptr) provider_session_tokens->clear();
size_t num_records = file.usage_info().sessions_size();
for (size_t i = 0; i < num_records; ++i) {
if ((ksids != NULL) &&
if ((ksids != nullptr) &&
!file.usage_info().sessions(i).key_set_id().empty()) {
ksids->push_back(file.usage_info().sessions(i).key_set_id());
}
if ((provider_session_tokens != NULL) &&
if ((provider_session_tokens != nullptr) &&
!file.usage_info().sessions(i).token().empty()) {
provider_session_tokens->push_back(file.usage_info().sessions(i).token());
}
@@ -539,25 +508,18 @@ bool DeviceFiles::ListUsageIds(
bool DeviceFiles::GetProviderSessionToken(const std::string& app_id,
const std::string& key_set_id,
std::string* provider_session_token) {
if (!initialized_) {
LOGW("DeviceFiles::GetProviderSessionToken: not initialized");
return false;
}
if (provider_session_token == NULL) {
LOGW("DeviceFiles::GetProviderSessionToken: NULL return argument pointer");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(provider_session_token);
std::string file_name = GetUsageInfoFileName(app_id);
if (!FileExists(file_name) || GetFileSize(file_name) == 0) {
LOGW("DeviceFiles::GetProviderSessionToken: empty file");
LOGE("Usage info file does not exists or is an empty file");
return false;
}
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(file_name, &file) != kNoError) {
LOGW("DeviceFiles::GetProviderSessionToken: unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -573,13 +535,10 @@ bool DeviceFiles::GetProviderSessionToken(const std::string& app_id,
bool DeviceFiles::DeleteUsageInfo(const std::string& usage_info_file_name,
const std::string& provider_session_token) {
if (!initialized_) {
LOGW("DeviceFiles::DeleteUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::DeleteUsageInfo: Unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -594,10 +553,8 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& usage_info_file_name,
}
if (!found) {
LOGW(
"DeviceFiles::DeleteUsageInfo: Unable to find provider session "
"token: %s",
b2a_hex(provider_session_token).c_str());
LOGE("Unable to find provider session token: pst = %s",
b2a_hex(provider_session_token).c_str());
return false;
}
@@ -616,14 +573,9 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& usage_info_file_name,
bool DeviceFiles::DeleteAllUsageInfoForApp(
const std::string& usage_info_file_name,
std::vector<std::string>* provider_session_tokens) {
if (!initialized_) {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: not initialized");
return false;
}
if (NULL == provider_session_tokens) {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: pst destination not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(provider_session_tokens);
provider_session_tokens->clear();
if (!FileExists(usage_info_file_name)) return true;
@@ -634,16 +586,13 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
provider_session_tokens->push_back(file.usage_info().sessions(i).token());
}
} else {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to retrieve file");
LOGW("Unable to retrieve usage info file");
}
return RemoveFile(usage_info_file_name);
}
bool DeviceFiles::DeleteAllUsageInfo() {
if (!initialized_) {
LOGW("DeviceFiles::DeleteAllUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return RemoveFile(kUsageInfoFileNamePrefix + std::string(kWildcard) +
kUsageInfoFileNameExt);
}
@@ -651,17 +600,8 @@ bool DeviceFiles::DeleteAllUsageInfo() {
bool DeviceFiles::RetrieveUsageInfo(
const std::string& usage_info_file_name,
std::vector<std::pair<CdmKeyMessage, CdmKeyResponse> >* usage_info) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageInfo: not initialized");
return false;
}
if (NULL == usage_info) {
LOGW(
"DeviceFiles::RetrieveUsageInfo: license destination not "
"provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(usage_info);
if (!FileExists(usage_info_file_name) ||
GetFileSize(usage_info_file_name) == 0) {
@@ -671,7 +611,7 @@ bool DeviceFiles::RetrieveUsageInfo(
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: Unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -691,14 +631,11 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
CdmKeyResponse* license_response,
std::string* usage_entry,
uint32_t* usage_entry_number) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -722,14 +659,11 @@ bool DeviceFiles::RetrieveUsageInfoByKeySetId(
std::string* provider_session_token, CdmKeyMessage* license_request,
CdmKeyResponse* license_response, std::string* usage_entry,
uint32_t* usage_entry_number) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageInfoByKeySetId: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfoByKeySetId: unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -751,10 +685,7 @@ bool DeviceFiles::RetrieveUsageInfoByKeySetId(
bool DeviceFiles::StoreUsageInfo(const std::string& usage_info_file_name,
const std::vector<CdmUsageData>& usage_data) {
if (!initialized_) {
LOGW("DeviceFiles::StoreUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
file.set_type(video_widevine_client::sdk::File::USAGE_INFO);
@@ -784,19 +715,16 @@ bool DeviceFiles::StoreUsageInfo(const std::string& usage_info_file_name,
bool DeviceFiles::UpdateUsageInfo(const std::string& usage_info_file_name,
const std::string& provider_session_token,
const CdmUsageData& usage_data) {
if (!initialized_) {
LOGW("DeviceFiles::UpdateUsageInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (!FileExists(usage_info_file_name)) {
LOGW("DeviceFiles::UpdateUsageInfo: Usage file does not exist");
LOGE("Usage info file does not exist");
return false;
}
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::UpdateUsageInfo: Unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -824,15 +752,8 @@ bool DeviceFiles::UpdateUsageInfo(const std::string& usage_info_file_name,
bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
std::vector<CdmUsageData>* usage_data) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageInfo: not initialized");
return false;
}
if (usage_data == NULL) {
LOGW("DeviceFiles::RetrieveUsageInfo: usage_data not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(usage_data);
if (!FileExists(usage_info_file_name) ||
GetFileSize(usage_info_file_name) == 0) {
@@ -842,7 +763,7 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -865,19 +786,12 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
const std::string& provider_session_token,
CdmUsageData* usage_data) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageInfo: not initialized");
return false;
}
if (usage_data == NULL) {
LOGW("DeviceFiles::RetrieveUsageInfo: usage_data not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(usage_data);
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(usage_info_file_name, &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageInfo: unable to retrieve file");
LOGE("Unable to retrieve usage info file");
return false;
}
@@ -902,15 +816,8 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& usage_info_file_name,
bool DeviceFiles::ListUsageInfoFiles(
std::vector<std::string>* usage_info_file_names) {
if (!initialized_) {
LOGW("DeviceFiles::ListUsageInfoFiles: not initialized");
return false;
}
if (usage_info_file_names == NULL) {
LOGW("DeviceFiles::ListUsageInfoFiles: usage_info_file_names not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(usage_info_file_names);
// Get list of filenames
std::vector<std::string> filenames;
@@ -937,10 +844,7 @@ bool DeviceFiles::ListUsageInfoFiles(
bool DeviceFiles::StoreHlsAttributes(
const std::string& key_set_id, const CdmHlsMethod method,
const std::vector<uint8_t>& media_segment_iv) {
if (!initialized_) {
LOGW("DeviceFiles::StoreHlsAttributes: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
// Fill in file information
video_widevine_client::sdk::File file;
@@ -958,8 +862,7 @@ bool DeviceFiles::StoreHlsAttributes(
break;
case kHlsMethodNone:
default:
LOGW("DeviceFiles::StoreHlsAttributeInfo: Unknown HLS method: %u",
method);
LOGE("Unknown HLS method: %d", method);
return false;
break;
}
@@ -976,32 +879,28 @@ bool DeviceFiles::StoreHlsAttributes(
bool DeviceFiles::RetrieveHlsAttributes(
const std::string& key_set_id, CdmHlsMethod* method,
std::vector<uint8_t>* media_segment_iv) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveHlsAttributes: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(key_set_id + kHlsAttributesFileNameExt, &file) !=
kNoError) {
LOGW("DeviceFiles::RetrieveHlsAttributes: unable to retrieve file");
LOGE("Unable to retrieve key set HLS attributes file");
return false;
}
if (file.type() != video_widevine_client::sdk::File::HLS_ATTRIBUTES) {
LOGW("DeviceFiles::RetrieveHlsAttributes: Incorrect file type: %u",
file.type());
LOGE("Incorrect file type: type = %d", static_cast<int>(file.type()));
return false;
}
if (file.version() != video_widevine_client::sdk::File::VERSION_1) {
LOGW("DeviceFiles::RetrieveHlsAttributes: Incorrect file version: %u",
file.version());
LOGE("Incorrect file version: version = %d",
static_cast<int>(file.version()));
return false;
}
if (!file.has_hls_attributes()) {
LOGW("DeviceFiles::RetrieveHlsAttributes: HLS attributes not present");
LOGE("HLS attributes not present");
return false;
}
@@ -1015,8 +914,8 @@ bool DeviceFiles::RetrieveHlsAttributes(
*method = kHlsMethodSampleAes;
break;
default:
LOGW("DeviceFiles::RetrieveHlsAttributes: Unrecognized HLS method: %u",
attributes.method());
LOGW("Unrecognized HLS method: %d",
static_cast<int>(attributes.method()));
*method = kHlsMethodNone;
break;
}
@@ -1026,20 +925,14 @@ bool DeviceFiles::RetrieveHlsAttributes(
}
bool DeviceFiles::DeleteHlsAttributes(const std::string& key_set_id) {
if (!initialized_) {
LOGW("DeviceFiles::DeleteHlsAttributes: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return RemoveFile(key_set_id + kHlsAttributesFileNameExt);
}
bool DeviceFiles::StoreUsageTableInfo(
const CdmUsageTableHeader& usage_table_header,
const std::vector<CdmUsageEntryInfo>& usage_entry_info) {
if (!initialized_) {
LOGW("DeviceFiles::StoreUsageTableInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
// Fill in file information
video_widevine_client::sdk::File file;
@@ -1082,40 +975,32 @@ bool DeviceFiles::StoreUsageTableInfo(
bool DeviceFiles::RetrieveUsageTableInfo(
CdmUsageTableHeader* usage_table_header,
std::vector<CdmUsageEntryInfo>* usage_entry_info) {
if (!initialized_) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: not initialized");
return false;
}
if (usage_table_header == NULL) {
LOGW(
"DeviceFiles::RetrieveUsageTableInfo: usage_table_header not provided");
return false;
}
if (usage_entry_info == NULL) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: usage_entry_info not provided");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
RETURN_FALSE_IF_NULL(usage_table_header);
RETURN_FALSE_IF_NULL(usage_entry_info);
video_widevine_client::sdk::File file;
if (RetrieveHashedFile(GetUsageTableFileName(), &file) != kNoError) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: unable to retrieve file");
LOGE("Unable to retrieve usage table file");
return false;
}
if (file.type() != video_widevine_client::sdk::File::USAGE_TABLE_INFO) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: Incorrect file type");
LOGE("Incorrect file type: type = %d, expected_type = %d",
static_cast<int>(file.type()),
static_cast<int>(video_widevine_client::sdk::File::USAGE_TABLE_INFO));
return false;
}
if (file.version() != video_widevine_client::sdk::File::VERSION_1) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: Incorrect file version");
LOGE("Incorrect file version: version = %d, expected_version = %d",
static_cast<int>(file.version()),
static_cast<int>(video_widevine_client::sdk::File::VERSION_1));
return false;
}
if (!file.has_usage_table_info()) {
LOGW("DeviceFiles::RetrieveUsageTableInfo: Usage table info not present");
LOGE("Usage table info not present in file");
return false;
}
@@ -1147,10 +1032,7 @@ bool DeviceFiles::RetrieveUsageTableInfo(
}
bool DeviceFiles::DeleteUsageTableInfo() {
if (!initialized_) {
LOGW("DeviceFiles::DeleteUsageTableInfo: not initialized");
return false;
}
RETURN_FALSE_IF_UNINITIALIZED();
return RemoveFile(GetUsageTableFileName());
}
@@ -1173,7 +1055,7 @@ DeviceFiles::ResponseType DeviceFiles::StoreFileRaw(
const std::string& name, const std::string& serialized_file) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::StoreFileRaw: Unable to get base path");
LOGE("Unable to get base path");
return kBasePathUnavailable;
}
@@ -1182,21 +1064,26 @@ DeviceFiles::ResponseType DeviceFiles::StoreFileRaw(
auto file =
file_system_->Open(path, FileSystem::kCreate | FileSystem::kTruncate);
if (!file) {
LOGW("DeviceFiles::StoreFileRaw: File open failed: %s", path.c_str());
LOGE("Failed to open file: path = %s", path.c_str());
return kFileOpenFailed;
}
ssize_t bytes = file->Write(serialized_file.data(), serialized_file.size());
const ssize_t bytes_written =
file->Write(serialized_file.data(), serialized_file.size());
if (bytes != static_cast<ssize_t>(serialized_file.size())) {
LOGW(
"DeviceFiles::StoreFileRaw: write failed: (actual: %d, "
"expected: %d)",
bytes, serialized_file.size());
if (bytes_written < 0) {
LOGE("Failed to write to file: path = %s", path.c_str());
return kFileWriteError;
}
if (bytes_written != static_cast<ssize_t>(serialized_file.size())) {
LOGE(
"Failed to fully write to file: path = %s, "
"bytes_written = %zd, bytes_attempted = %zu",
path.c_str(), bytes_written, serialized_file.size());
return kFileWriteError;
}
LOGV("DeviceFiles::StoreFileRaw: success: %s (%db)", path.c_str(),
LOGV("Successfully stored raw file: path = %s, size = %zu", path.c_str(),
serialized_file.size());
return kNoError;
}
@@ -1206,28 +1093,27 @@ DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
video_widevine_client::sdk::File* deserialized_file) {
std::string serialized_file;
if (!deserialized_file) {
LOGW("DeviceFiles::RetrieveHashedFile: Unspecified file parameter");
if (deserialized_file == nullptr) {
LOGE("File handle parameter |deserialized_file| not provided");
return kParameterNull;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::RetrieveHashedFile: Unable to get base path");
LOGE("Unable to get base path");
return kBasePathUnavailable;
}
path += name;
if (!file_system_->Exists(path)) {
LOGW("DeviceFiles::RetrieveHashedFile: %s does not exist", path.c_str());
LOGE("File does not exist: path = %s", path.c_str());
return kFileNotFound;
}
ssize_t bytes = file_system_->FileSize(path);
if (bytes <= 0) {
LOGW("DeviceFiles::RetrieveHashedFile: File size invalid: %s",
path.c_str());
const ssize_t file_size = file_system_->FileSize(path);
if (file_size <= 0) {
LOGE("File size is invalid: %s", path.c_str());
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
@@ -1240,23 +1126,31 @@ DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
}
std::string serialized_hash_file;
serialized_hash_file.resize(bytes);
bytes = file->Read(&serialized_hash_file[0], serialized_hash_file.size());
serialized_hash_file.resize(file_size);
const ssize_t bytes_read =
file->Read(&serialized_hash_file[0], serialized_hash_file.size());
if (bytes != static_cast<ssize_t>(serialized_hash_file.size())) {
LOGW("DeviceFiles::RetrieveHashedFile: read failed: %d", bytes);
if (bytes_read != file_size) {
if (bytes_read < 0) {
LOGE("Failed to read from file: path = %s", path.c_str());
} else {
LOGE(
"Failed to fully read from file: "
"path = %s, bytes_read = %zd, bytes_attempted = %zd",
path.c_str(), bytes_read, file_size);
}
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
return kFileReadError;
}
LOGV("DeviceFiles::RetrieveHashedFile: success: %s (%db)", path.c_str(),
LOGV("Successfully read file: path = %s, size = %zu", path.c_str(),
serialized_hash_file.size());
HashedFile hash_file;
if (!hash_file.ParseFromString(serialized_hash_file)) {
LOGW("DeviceFiles::RetrieveHashedFile: Unable to parse hash file");
LOGE("Unable to parse hash file");
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
@@ -1265,7 +1159,7 @@ DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
std::string hash = Sha256Hash(hash_file.file());
if (hash != hash_file.hash()) {
LOGW("DeviceFiles::RetrieveHashedFile: Hash mismatch");
LOGE("File hash mismatch: path = %s", path.c_str());
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
@@ -1273,7 +1167,7 @@ DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
}
if (!deserialized_file->ParseFromString(hash_file.file())) {
LOGW("DeviceFiles::RetrieveHashedFile: Unable to parse file");
LOGE("Unable to parse hashed file");
// Remove the corrupted file so the caller will not get the same error
// when trying to access the file repeatedly, causing the system to stall.
file_system_->Remove(path);
@@ -1285,7 +1179,7 @@ DeviceFiles::ResponseType DeviceFiles::RetrieveHashedFile(
bool DeviceFiles::FileExists(const std::string& name) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::FileExists: Unable to get base path");
LOGE("Unable to get base path");
return false;
}
path += name;
@@ -1296,7 +1190,7 @@ bool DeviceFiles::FileExists(const std::string& name) {
bool DeviceFiles::ListFiles(std::vector<std::string>* names) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::ListFiles: Unable to get base path");
LOGE("Unable to get base path");
return false;
}
return file_system_->List(path, names);
@@ -1305,7 +1199,7 @@ bool DeviceFiles::ListFiles(std::vector<std::string>* names) {
bool DeviceFiles::RemoveFile(const std::string& name) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::RemoveFile: Unable to get base path");
LOGE("Unable to get base path");
return false;
}
path += name;
@@ -1316,7 +1210,7 @@ bool DeviceFiles::RemoveFile(const std::string& name) {
ssize_t DeviceFiles::GetFileSize(const std::string& name) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::GetFileSize: Unable to get base path");
LOGE("Unable to get base path");
return -1;
}
path += name;