Change DeviceFiles Helpers to Accept std::string
(This is a merge of http://go/wvgerrit/13922 from the Widevine CDM repository.) I'm not sure why we chose to pass char* instead of std::string to the helper functions in DeviceFiles, but it seems to require a lot of gymnastics of the calling code for minimal gain. Change-Id: Ie0cdec80ab77c94370648dd74249124aed6e8be1
This commit is contained in:
@@ -85,12 +85,15 @@ class DeviceFiles {
|
||||
private:
|
||||
// Helpers that wrap the File interface and automatically handle hashing, as
|
||||
// well as adding the device files base path to to the file name.
|
||||
bool StoreFileWithHash(const char* name, const std::string& serialized_file);
|
||||
bool StoreFileRaw(const char* name, const std::string& serialized_file);
|
||||
bool RetrieveHashedFile(const char* name, std::string* serialized_file);
|
||||
bool FileExists(const char* name);
|
||||
bool RemoveFile(const char* name);
|
||||
ssize_t GetFileSize(const char* name);
|
||||
bool StoreFileWithHash(const std::string& name,
|
||||
const std::string& serialized_file);
|
||||
bool StoreFileRaw(const std::string& name,
|
||||
const std::string& serialized_file);
|
||||
bool RetrieveHashedFile(const std::string& name,
|
||||
std::string* serialized_file);
|
||||
bool FileExists(const std::string& name);
|
||||
bool RemoveFile(const std::string& name);
|
||||
ssize_t GetFileSize(const std::string& name);
|
||||
|
||||
// Certificate and offline licenses are now stored in security
|
||||
// level specific directories. In an earlier version they were
|
||||
|
||||
@@ -197,8 +197,7 @@ bool DeviceFiles::StoreLicense(const std::string& key_set_id,
|
||||
std::string serialized_file;
|
||||
file.SerializeToString(&serialized_file);
|
||||
|
||||
std::string file_name = key_set_id + kLicenseFileNameExt;
|
||||
return StoreFileWithHash(file_name.c_str(), serialized_file);
|
||||
return StoreFileWithHash(key_set_id + kLicenseFileNameExt, serialized_file);
|
||||
}
|
||||
|
||||
bool DeviceFiles::RetrieveLicense(
|
||||
@@ -213,8 +212,9 @@ bool DeviceFiles::RetrieveLicense(
|
||||
}
|
||||
|
||||
std::string serialized_file;
|
||||
std::string file_name = key_set_id + kLicenseFileNameExt;
|
||||
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) return false;
|
||||
if (!RetrieveHashedFile(key_set_id + kLicenseFileNameExt, &serialized_file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
video_widevine_client::sdk::File file;
|
||||
if (!file.ParseFromString(serialized_file)) {
|
||||
@@ -268,10 +268,7 @@ bool DeviceFiles::DeleteLicense(const std::string& key_set_id) {
|
||||
LOGW("DeviceFiles::DeleteLicense: not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file_name(key_set_id);
|
||||
file_name.append(kLicenseFileNameExt);
|
||||
return RemoveFile(file_name.c_str());
|
||||
return RemoveFile(key_set_id + kLicenseFileNameExt);
|
||||
}
|
||||
|
||||
bool DeviceFiles::DeleteAllLicenses() {
|
||||
@@ -279,10 +276,7 @@ bool DeviceFiles::DeleteAllLicenses() {
|
||||
LOGW("DeviceFiles::DeleteAllLicenses: not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file_name(kWildcard);
|
||||
file_name.append(kLicenseFileNameExt);
|
||||
return RemoveFile(file_name.c_str());
|
||||
return RemoveFile(std::string(kWildcard) + kLicenseFileNameExt);
|
||||
}
|
||||
|
||||
bool DeviceFiles::DeleteAllFiles() {
|
||||
@@ -301,10 +295,7 @@ bool DeviceFiles::LicenseExists(const std::string& key_set_id) {
|
||||
LOGW("DeviceFiles::LicenseExists: not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file_name(key_set_id);
|
||||
file_name.append(kLicenseFileNameExt);
|
||||
return FileExists(file_name.c_str());
|
||||
return FileExists(key_set_id + kLicenseFileNameExt);
|
||||
}
|
||||
|
||||
bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) {
|
||||
@@ -312,9 +303,7 @@ bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) {
|
||||
LOGW("DeviceFiles::ReserveLicenseId: not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file_name = key_set_id + kLicenseFileNameExt;
|
||||
return StoreFileRaw(file_name.c_str(), kBlankFileData);
|
||||
return StoreFileRaw(key_set_id + kLicenseFileNameExt, kBlankFileData);
|
||||
}
|
||||
|
||||
bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
|
||||
@@ -329,7 +318,7 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
|
||||
std::string serialized_file;
|
||||
video_widevine_client::sdk::File file;
|
||||
std::string file_name = GetUsageInfoFileName(app_id);
|
||||
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) {
|
||||
if (!RetrieveHashedFile(file_name, &serialized_file)) {
|
||||
file.set_type(video_widevine_client::sdk::File::USAGE_INFO);
|
||||
file.set_version(video_widevine_client::sdk::File::VERSION_1);
|
||||
} else {
|
||||
@@ -348,7 +337,7 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
|
||||
provider_session->set_license(key_response.data(), key_response.size());
|
||||
|
||||
file.SerializeToString(&serialized_file);
|
||||
return StoreFileWithHash(file_name.c_str(), serialized_file);
|
||||
return StoreFileWithHash(file_name, serialized_file);
|
||||
}
|
||||
|
||||
bool DeviceFiles::DeleteUsageInfo(const std::string& app_id,
|
||||
@@ -359,7 +348,7 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& app_id,
|
||||
}
|
||||
std::string serialized_file;
|
||||
std::string file_name = GetUsageInfoFileName(app_id);
|
||||
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) return false;
|
||||
if (!RetrieveHashedFile(file_name, &serialized_file)) return false;
|
||||
|
||||
video_widevine_client::sdk::File file;
|
||||
if (!file.ParseFromString(serialized_file)) {
|
||||
@@ -393,7 +382,7 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& app_id,
|
||||
sessions->RemoveLast();
|
||||
|
||||
file.SerializeToString(&serialized_file);
|
||||
return StoreFileWithHash(file_name.c_str(), serialized_file);
|
||||
return StoreFileWithHash(file_name, serialized_file);
|
||||
}
|
||||
|
||||
bool DeviceFiles::DeleteAllUsageInfoForApp(
|
||||
@@ -410,9 +399,9 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
|
||||
provider_session_tokens->clear();
|
||||
|
||||
std::string file_name = GetUsageInfoFileName(app_id);
|
||||
if (!FileExists(file_name.c_str())) return true;
|
||||
if (!FileExists(file_name)) return true;
|
||||
std::string serialized_file;
|
||||
if (RetrieveHashedFile(file_name.c_str(), &serialized_file)) {
|
||||
if (RetrieveHashedFile(file_name, &serialized_file)) {
|
||||
video_widevine_client::sdk::File file_proto;
|
||||
if (!file_proto.ParseFromString(serialized_file)) {
|
||||
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to parse file");
|
||||
@@ -425,7 +414,7 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
|
||||
} else {
|
||||
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to retrieve file");
|
||||
}
|
||||
return RemoveFile(file_name.c_str());
|
||||
return RemoveFile(file_name);
|
||||
}
|
||||
|
||||
bool DeviceFiles::RetrieveUsageInfo(
|
||||
@@ -445,8 +434,8 @@ bool DeviceFiles::RetrieveUsageInfo(
|
||||
|
||||
std::string serialized_file;
|
||||
std::string file_name = GetUsageInfoFileName(app_id);
|
||||
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) {
|
||||
if (!FileExists(file_name.c_str()) || GetFileSize(file_name.c_str()) == 0) {
|
||||
if (!RetrieveHashedFile(file_name, &serialized_file)) {
|
||||
if (!FileExists(file_name) || GetFileSize(file_name) == 0) {
|
||||
usage_info->resize(0);
|
||||
return true;
|
||||
}
|
||||
@@ -480,7 +469,7 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& app_id,
|
||||
}
|
||||
std::string serialized_file;
|
||||
std::string file_name = GetUsageInfoFileName(app_id);
|
||||
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) return false;
|
||||
if (!RetrieveHashedFile(file_name, &serialized_file)) return false;
|
||||
|
||||
video_widevine_client::sdk::File file;
|
||||
if (!file.ParseFromString(serialized_file)) {
|
||||
@@ -505,18 +494,13 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& app_id,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeviceFiles::StoreFileWithHash(const char* name,
|
||||
bool DeviceFiles::StoreFileWithHash(const std::string& name,
|
||||
const std::string& serialized_file) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::StoreFileWithHash: Invalid file handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::StoreFileWithHash: Unspecified file name parameter");
|
||||
return false;
|
||||
}
|
||||
|
||||
// calculate SHA hash
|
||||
std::string hash;
|
||||
if (!Hash(serialized_file, &hash)) {
|
||||
@@ -535,18 +519,13 @@ bool DeviceFiles::StoreFileWithHash(const char* name,
|
||||
return StoreFileRaw(name, serialized_hash_file);
|
||||
}
|
||||
|
||||
bool DeviceFiles::StoreFileRaw(const char* name,
|
||||
bool DeviceFiles::StoreFileRaw(const std::string& name,
|
||||
const std::string& serialized_file) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::StoreFileRaw: Invalid file handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::StoreFileRaw: Unspecified file name parameter");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string path;
|
||||
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
|
||||
LOGW("DeviceFiles::StoreFileRaw: Unable to get base path");
|
||||
@@ -580,18 +559,13 @@ bool DeviceFiles::StoreFileRaw(const char* name,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeviceFiles::RetrieveHashedFile(const char* name,
|
||||
bool DeviceFiles::RetrieveHashedFile(const std::string& name,
|
||||
std::string* serialized_file) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::RetrieveHashedFile: Invalid file handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::RetrieveHashedFile: Unspecified file name parameter");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!serialized_file) {
|
||||
LOGW(
|
||||
"DeviceFiles::RetrieveHashedFile: Unspecified serialized_file "
|
||||
@@ -663,17 +637,12 @@ bool DeviceFiles::RetrieveHashedFile(const char* name,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeviceFiles::FileExists(const char* name) {
|
||||
bool DeviceFiles::FileExists(const std::string& name) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::FileExists: Invalid file handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::FileExists: Unspecified file name parameter");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string path;
|
||||
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
|
||||
LOGW("DeviceFiles::FileExists: Unable to get base path");
|
||||
@@ -684,17 +653,12 @@ bool DeviceFiles::FileExists(const char* name) {
|
||||
return file_->Exists(path);
|
||||
}
|
||||
|
||||
bool DeviceFiles::RemoveFile(const char* name) {
|
||||
bool DeviceFiles::RemoveFile(const std::string& name) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::RemoveFile: Invalid file handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::RemoveFile: Unspecified file name parameter");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string path;
|
||||
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
|
||||
LOGW("DeviceFiles::RemoveFile: Unable to get base path");
|
||||
@@ -705,17 +669,12 @@ bool DeviceFiles::RemoveFile(const char* name) {
|
||||
return file_->Remove(path);
|
||||
}
|
||||
|
||||
ssize_t DeviceFiles::GetFileSize(const char* name) {
|
||||
ssize_t DeviceFiles::GetFileSize(const std::string& name) {
|
||||
if (!file_.get()) {
|
||||
LOGW("DeviceFiles::GetFileSize: Invalid file handle");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
LOGW("DeviceFiles::GetFileSize: Unspecified file name parameter");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string path;
|
||||
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
|
||||
LOGW("DeviceFiles::GetFileSize: Unable to get base path");
|
||||
|
||||
Reference in New Issue
Block a user