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:
John "Juce" Bruce
2015-03-31 17:29:44 -07:00
parent c66badec60
commit 69a9f8edb2
2 changed files with 33 additions and 71 deletions

View File

@@ -85,12 +85,15 @@ class DeviceFiles {
private: private:
// Helpers that wrap the File interface and automatically handle hashing, as // Helpers that wrap the File interface and automatically handle hashing, as
// well as adding the device files base path to to the file name. // well as adding the device files base path to to the file name.
bool StoreFileWithHash(const char* name, const std::string& serialized_file); bool StoreFileWithHash(const std::string& name,
bool StoreFileRaw(const char* name, const std::string& serialized_file); const std::string& serialized_file);
bool RetrieveHashedFile(const char* name, std::string* serialized_file); bool StoreFileRaw(const std::string& name,
bool FileExists(const char* name); const std::string& serialized_file);
bool RemoveFile(const char* name); bool RetrieveHashedFile(const std::string& name,
ssize_t GetFileSize(const char* 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 // Certificate and offline licenses are now stored in security
// level specific directories. In an earlier version they were // level specific directories. In an earlier version they were

View File

@@ -197,8 +197,7 @@ bool DeviceFiles::StoreLicense(const std::string& key_set_id,
std::string serialized_file; std::string serialized_file;
file.SerializeToString(&serialized_file); file.SerializeToString(&serialized_file);
std::string file_name = key_set_id + kLicenseFileNameExt; return StoreFileWithHash(key_set_id + kLicenseFileNameExt, serialized_file);
return StoreFileWithHash(file_name.c_str(), serialized_file);
} }
bool DeviceFiles::RetrieveLicense( bool DeviceFiles::RetrieveLicense(
@@ -213,8 +212,9 @@ bool DeviceFiles::RetrieveLicense(
} }
std::string serialized_file; std::string serialized_file;
std::string file_name = key_set_id + kLicenseFileNameExt; if (!RetrieveHashedFile(key_set_id + kLicenseFileNameExt, &serialized_file)) {
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) return false; return false;
}
video_widevine_client::sdk::File file; video_widevine_client::sdk::File file;
if (!file.ParseFromString(serialized_file)) { if (!file.ParseFromString(serialized_file)) {
@@ -268,10 +268,7 @@ bool DeviceFiles::DeleteLicense(const std::string& key_set_id) {
LOGW("DeviceFiles::DeleteLicense: not initialized"); LOGW("DeviceFiles::DeleteLicense: not initialized");
return false; return false;
} }
return RemoveFile(key_set_id + kLicenseFileNameExt);
std::string file_name(key_set_id);
file_name.append(kLicenseFileNameExt);
return RemoveFile(file_name.c_str());
} }
bool DeviceFiles::DeleteAllLicenses() { bool DeviceFiles::DeleteAllLicenses() {
@@ -279,10 +276,7 @@ bool DeviceFiles::DeleteAllLicenses() {
LOGW("DeviceFiles::DeleteAllLicenses: not initialized"); LOGW("DeviceFiles::DeleteAllLicenses: not initialized");
return false; return false;
} }
return RemoveFile(std::string(kWildcard) + kLicenseFileNameExt);
std::string file_name(kWildcard);
file_name.append(kLicenseFileNameExt);
return RemoveFile(file_name.c_str());
} }
bool DeviceFiles::DeleteAllFiles() { bool DeviceFiles::DeleteAllFiles() {
@@ -301,10 +295,7 @@ bool DeviceFiles::LicenseExists(const std::string& key_set_id) {
LOGW("DeviceFiles::LicenseExists: not initialized"); LOGW("DeviceFiles::LicenseExists: not initialized");
return false; return false;
} }
return FileExists(key_set_id + kLicenseFileNameExt);
std::string file_name(key_set_id);
file_name.append(kLicenseFileNameExt);
return FileExists(file_name.c_str());
} }
bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) { 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"); LOGW("DeviceFiles::ReserveLicenseId: not initialized");
return false; return false;
} }
return StoreFileRaw(key_set_id + kLicenseFileNameExt, kBlankFileData);
std::string file_name = key_set_id + kLicenseFileNameExt;
return StoreFileRaw(file_name.c_str(), kBlankFileData);
} }
bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token, 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; std::string serialized_file;
video_widevine_client::sdk::File file; video_widevine_client::sdk::File file;
std::string file_name = GetUsageInfoFileName(app_id); 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_type(video_widevine_client::sdk::File::USAGE_INFO);
file.set_version(video_widevine_client::sdk::File::VERSION_1); file.set_version(video_widevine_client::sdk::File::VERSION_1);
} else { } else {
@@ -348,7 +337,7 @@ bool DeviceFiles::StoreUsageInfo(const std::string& provider_session_token,
provider_session->set_license(key_response.data(), key_response.size()); provider_session->set_license(key_response.data(), key_response.size());
file.SerializeToString(&serialized_file); 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, 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 serialized_file;
std::string file_name = GetUsageInfoFileName(app_id); 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; video_widevine_client::sdk::File file;
if (!file.ParseFromString(serialized_file)) { if (!file.ParseFromString(serialized_file)) {
@@ -393,7 +382,7 @@ bool DeviceFiles::DeleteUsageInfo(const std::string& app_id,
sessions->RemoveLast(); sessions->RemoveLast();
file.SerializeToString(&serialized_file); file.SerializeToString(&serialized_file);
return StoreFileWithHash(file_name.c_str(), serialized_file); return StoreFileWithHash(file_name, serialized_file);
} }
bool DeviceFiles::DeleteAllUsageInfoForApp( bool DeviceFiles::DeleteAllUsageInfoForApp(
@@ -410,9 +399,9 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
provider_session_tokens->clear(); provider_session_tokens->clear();
std::string file_name = GetUsageInfoFileName(app_id); 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; 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; video_widevine_client::sdk::File file_proto;
if (!file_proto.ParseFromString(serialized_file)) { if (!file_proto.ParseFromString(serialized_file)) {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to parse file"); LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to parse file");
@@ -425,7 +414,7 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
} else { } else {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to retrieve file"); LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to retrieve file");
} }
return RemoveFile(file_name.c_str()); return RemoveFile(file_name);
} }
bool DeviceFiles::RetrieveUsageInfo( bool DeviceFiles::RetrieveUsageInfo(
@@ -445,8 +434,8 @@ bool DeviceFiles::RetrieveUsageInfo(
std::string serialized_file; std::string serialized_file;
std::string file_name = GetUsageInfoFileName(app_id); std::string file_name = GetUsageInfoFileName(app_id);
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) { if (!RetrieveHashedFile(file_name, &serialized_file)) {
if (!FileExists(file_name.c_str()) || GetFileSize(file_name.c_str()) == 0) { if (!FileExists(file_name) || GetFileSize(file_name) == 0) {
usage_info->resize(0); usage_info->resize(0);
return true; return true;
} }
@@ -480,7 +469,7 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& app_id,
} }
std::string serialized_file; std::string serialized_file;
std::string file_name = GetUsageInfoFileName(app_id); 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; video_widevine_client::sdk::File file;
if (!file.ParseFromString(serialized_file)) { if (!file.ParseFromString(serialized_file)) {
@@ -505,18 +494,13 @@ bool DeviceFiles::RetrieveUsageInfo(const std::string& app_id,
return true; return true;
} }
bool DeviceFiles::StoreFileWithHash(const char* name, bool DeviceFiles::StoreFileWithHash(const std::string& name,
const std::string& serialized_file) { const std::string& serialized_file) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::StoreFileWithHash: Invalid file handle"); LOGW("DeviceFiles::StoreFileWithHash: Invalid file handle");
return false; return false;
} }
if (!name) {
LOGW("DeviceFiles::StoreFileWithHash: Unspecified file name parameter");
return false;
}
// calculate SHA hash // calculate SHA hash
std::string hash; std::string hash;
if (!Hash(serialized_file, &hash)) { if (!Hash(serialized_file, &hash)) {
@@ -535,18 +519,13 @@ bool DeviceFiles::StoreFileWithHash(const char* name,
return StoreFileRaw(name, serialized_hash_file); return StoreFileRaw(name, serialized_hash_file);
} }
bool DeviceFiles::StoreFileRaw(const char* name, bool DeviceFiles::StoreFileRaw(const std::string& name,
const std::string& serialized_file) { const std::string& serialized_file) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::StoreFileRaw: Invalid file handle"); LOGW("DeviceFiles::StoreFileRaw: Invalid file handle");
return false; return false;
} }
if (!name) {
LOGW("DeviceFiles::StoreFileRaw: Unspecified file name parameter");
return false;
}
std::string path; std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) { if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::StoreFileRaw: Unable to get base path"); LOGW("DeviceFiles::StoreFileRaw: Unable to get base path");
@@ -580,18 +559,13 @@ bool DeviceFiles::StoreFileRaw(const char* name,
return true; return true;
} }
bool DeviceFiles::RetrieveHashedFile(const char* name, bool DeviceFiles::RetrieveHashedFile(const std::string& name,
std::string* serialized_file) { std::string* serialized_file) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::RetrieveHashedFile: Invalid file handle"); LOGW("DeviceFiles::RetrieveHashedFile: Invalid file handle");
return false; return false;
} }
if (!name) {
LOGW("DeviceFiles::RetrieveHashedFile: Unspecified file name parameter");
return false;
}
if (!serialized_file) { if (!serialized_file) {
LOGW( LOGW(
"DeviceFiles::RetrieveHashedFile: Unspecified serialized_file " "DeviceFiles::RetrieveHashedFile: Unspecified serialized_file "
@@ -663,17 +637,12 @@ bool DeviceFiles::RetrieveHashedFile(const char* name,
return true; return true;
} }
bool DeviceFiles::FileExists(const char* name) { bool DeviceFiles::FileExists(const std::string& name) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::FileExists: Invalid file handle"); LOGW("DeviceFiles::FileExists: Invalid file handle");
return false; return false;
} }
if (!name) {
LOGW("DeviceFiles::FileExists: Unspecified file name parameter");
return false;
}
std::string path; std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) { if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::FileExists: Unable to get base path"); LOGW("DeviceFiles::FileExists: Unable to get base path");
@@ -684,17 +653,12 @@ bool DeviceFiles::FileExists(const char* name) {
return file_->Exists(path); return file_->Exists(path);
} }
bool DeviceFiles::RemoveFile(const char* name) { bool DeviceFiles::RemoveFile(const std::string& name) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::RemoveFile: Invalid file handle"); LOGW("DeviceFiles::RemoveFile: Invalid file handle");
return false; return false;
} }
if (!name) {
LOGW("DeviceFiles::RemoveFile: Unspecified file name parameter");
return false;
}
std::string path; std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) { if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::RemoveFile: Unable to get base path"); LOGW("DeviceFiles::RemoveFile: Unable to get base path");
@@ -705,17 +669,12 @@ bool DeviceFiles::RemoveFile(const char* name) {
return file_->Remove(path); return file_->Remove(path);
} }
ssize_t DeviceFiles::GetFileSize(const char* name) { ssize_t DeviceFiles::GetFileSize(const std::string& name) {
if (!file_.get()) { if (!file_.get()) {
LOGW("DeviceFiles::GetFileSize: Invalid file handle"); LOGW("DeviceFiles::GetFileSize: Invalid file handle");
return -1; return -1;
} }
if (!name) {
LOGW("DeviceFiles::GetFileSize: Unspecified file name parameter");
return -1;
}
std::string path; std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) { if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::GetFileSize: Unable to get base path"); LOGW("DeviceFiles::GetFileSize: Unable to get base path");