More policy engine/timers refactoring

[ Merge of http://go/wvgerrit/93838 ]

Some more rework of policy engine/policy timers code to support
timer and clock value handling introduced by OEMCrypto v16.

Changes are
* renamed methods to include rental duration since policies for v16 use
  rental and playback duration for all licenses. Previously rental and
  playback durations enforced timing for persistent licenses and license
  duration was used for streaming licenses.
* Moved some common code to the base PolicyTimer class from
  PolicyTimerV15.
* Corrected data member naming (policy_timers -> policy_timers_)
* Updated comments

Bug: 139372190
Test: Android WV unit/integration tests
Change-Id: Id925ddcc14608a8500f30c2c68486d91608a9abe
This commit is contained in:
Rahul Frias
2020-02-13 02:39:50 -08:00
parent 3012ff29bd
commit 68587be8a0
7 changed files with 97 additions and 79 deletions

View File

@@ -33,7 +33,7 @@ PolicyEngine::PolicyEngine(CdmSessionId session_id,
session_id_(session_id),
event_listener_(event_listener),
license_keys_(new LicenseKeys(crypto_session->GetSecurityLevel())),
policy_timers(new PolicyTimersV15),
policy_timers_(new PolicyTimersV15),
clock_(new Clock) {
InitDevice(crypto_session);
}
@@ -90,12 +90,13 @@ void PolicyEngine::OnTimerEvent() {
const int64_t current_time = GetCurrentTime();
// If we have passed the grace period, the expiration will update.
if (policy_timers->HasPassedGracePeriod(current_time)) {
if (policy_timers_->HasPassedGracePeriod(current_time)) {
NotifyExpirationUpdate(current_time);
}
// License expiration trumps all.
if (policy_timers->HasLicenseOrPlaybackDurationExpired(current_time) &&
if (policy_timers_->HasLicenseOrRentalOrPlaybackDurationExpired(
current_time) &&
license_state_ != kLicenseStateExpired) {
license_state_ = kLicenseStateExpired;
NotifyKeysChange(kKeyStatusExpired);
@@ -110,7 +111,7 @@ void PolicyEngine::OnTimerEvent() {
// Test to determine if renewal should be attempted.
switch (license_state_) {
case kLicenseStateCanPlay: {
if (policy_timers->HasRenewalDelayExpired(current_time)) {
if (policy_timers_->HasRenewalDelayExpired(current_time)) {
renewal_needed = true;
}
// HDCP may change, so force a check.
@@ -124,14 +125,14 @@ void PolicyEngine::OnTimerEvent() {
}
case kLicenseStateWaitingLicenseUpdate: {
if (policy_timers->HasRenewalRetryIntervalExpired(current_time)) {
if (policy_timers_->HasRenewalRetryIntervalExpired(current_time)) {
renewal_needed = true;
}
break;
}
case kLicenseStatePending: {
if (!policy_timers->IsLicenseForFuture(current_time)) {
if (!policy_timers_->IsLicenseForFuture(current_time)) {
license_state_ = kLicenseStateCanPlay;
NotifyKeysChange(kKeyStatusUsable);
}
@@ -157,10 +158,9 @@ void PolicyEngine::OnTimerEvent() {
}
void PolicyEngine::SetLicense(const License& license) {
license_id_.Clear();
license_id_.CopyFrom(license.id());
license_keys_->SetFromLicense(license);
policy_timers->SetLicense(license);
policy_timers_->SetLicense(license);
UpdateLicense(license);
}
@@ -170,12 +170,11 @@ void PolicyEngine::SetEntitledLicenseKeys(
}
void PolicyEngine::SetLicenseForRelease(const License& license) {
license_id_.Clear();
license_id_.CopyFrom(license.id());
// Expire any old keys.
NotifyKeysChange(kKeyStatusExpired);
policy_timers->SetLicense(license);
policy_timers_->SetLicense(license);
UpdateLicense(license);
}
@@ -199,18 +198,19 @@ void PolicyEngine::UpdateLicense(const License& license) {
}
const int64_t current_time = GetCurrentTime();
policy_timers->UpdateLicense(current_time, license);
policy_timers_->UpdateLicense(current_time, license);
// Update time information
if (!policy_timers->get_policy().can_play() ||
policy_timers->HasLicenseOrPlaybackDurationExpired(current_time)) {
if (!policy_timers_->get_policy().can_play() ||
policy_timers_->HasLicenseOrRentalOrPlaybackDurationExpired(
current_time)) {
license_state_ = kLicenseStateExpired;
NotifyKeysChange(kKeyStatusExpired);
return;
}
// Update state
if (!policy_timers->IsLicenseForFuture(current_time)) {
if (!policy_timers_->IsLicenseForFuture(current_time)) {
license_state_ = kLicenseStateCanPlay;
NotifyKeysChange(kKeyStatusUsable);
} else {
@@ -222,14 +222,14 @@ void PolicyEngine::UpdateLicense(const License& license) {
bool PolicyEngine::BeginDecryption() {
const int64_t current_time = GetCurrentTime();
if (!policy_timers->HasPlaybackStarted(current_time)) {
if (!policy_timers_->HasPlaybackStarted(current_time)) {
switch (license_state_) {
case kLicenseStateCanPlay:
case kLicenseStateNeedRenewal:
case kLicenseStateWaitingLicenseUpdate:
policy_timers->BeginDecryption(current_time);
policy_timers_->BeginDecryption(current_time);
if (policy_timers->get_policy().renew_with_usage()) {
if (policy_timers_->get_policy().renew_with_usage()) {
license_state_ = kLicenseStateNeedRenewal;
}
NotifyExpirationUpdate(current_time);
@@ -246,7 +246,7 @@ bool PolicyEngine::BeginDecryption() {
}
void PolicyEngine::DecryptionEvent() {
policy_timers->DecryptionEvent(GetCurrentTime());
policy_timers_->DecryptionEvent(GetCurrentTime());
}
void PolicyEngine::NotifyResolution(uint32_t width, uint32_t height) {
@@ -270,20 +270,20 @@ CdmResponseType PolicyEngine::Query(CdmQueryMap* query_response) {
license_id_.type() == video_widevine::STREAMING ? QUERY_VALUE_STREAMING
: QUERY_VALUE_OFFLINE;
(*query_response)[QUERY_KEY_PLAY_ALLOWED] =
policy_timers->get_policy().can_play() ? QUERY_VALUE_TRUE
: QUERY_VALUE_FALSE;
(*query_response)[QUERY_KEY_PERSIST_ALLOWED] =
policy_timers->get_policy().can_persist() ? QUERY_VALUE_TRUE
: QUERY_VALUE_FALSE;
(*query_response)[QUERY_KEY_RENEW_ALLOWED] =
policy_timers->get_policy().can_renew() ? QUERY_VALUE_TRUE
policy_timers_->get_policy().can_play() ? QUERY_VALUE_TRUE
: QUERY_VALUE_FALSE;
(*query_response)[QUERY_KEY_PERSIST_ALLOWED] =
policy_timers_->get_policy().can_persist() ? QUERY_VALUE_TRUE
: QUERY_VALUE_FALSE;
(*query_response)[QUERY_KEY_RENEW_ALLOWED] =
policy_timers_->get_policy().can_renew() ? QUERY_VALUE_TRUE
: QUERY_VALUE_FALSE;
(*query_response)[QUERY_KEY_LICENSE_DURATION_REMAINING] = std::to_string(
policy_timers->GetLicenseOrRentalDurationRemaining(current_time));
(*query_response)[QUERY_KEY_PLAYBACK_DURATION_REMAINING] =
std::to_string(policy_timers->GetPlaybackDurationRemaining(current_time));
policy_timers_->GetLicenseOrRentalDurationRemaining(current_time));
(*query_response)[QUERY_KEY_PLAYBACK_DURATION_REMAINING] = std::to_string(
policy_timers_->GetPlaybackDurationRemaining(current_time));
(*query_response)[QUERY_KEY_RENEWAL_SERVER_URL] =
policy_timers->get_policy().renewal_server_url();
policy_timers_->get_policy().renewal_server_url();
return NO_ERROR;
}
@@ -305,54 +305,57 @@ bool PolicyEngine::CanUseKeyForSecurityLevel(const KeyId& key_id) {
}
bool PolicyEngine::GetSecondsSinceStarted(int64_t* seconds_since_started) {
return policy_timers->GetSecondsSinceStarted(GetCurrentTime(),
seconds_since_started);
return policy_timers_->GetSecondsSinceStarted(GetCurrentTime(),
seconds_since_started);
}
bool PolicyEngine::GetSecondsSinceLastPlayed(
int64_t* seconds_since_last_played) {
return policy_timers->GetSecondsSinceLastPlayed(GetCurrentTime(),
seconds_since_last_played);
return policy_timers_->GetSecondsSinceLastPlayed(GetCurrentTime(),
seconds_since_last_played);
}
int64_t PolicyEngine::GetLicenseOrPlaybackDurationRemaining() {
return policy_timers->GetLicenseOrPlaybackDurationRemaining(GetCurrentTime());
int64_t PolicyEngine::GetLicenseOrRentalOrPlaybackDurationRemaining() {
return policy_timers_->GetLicenseOrRentalOrPlaybackDurationRemaining(
GetCurrentTime());
}
bool PolicyEngine::CanRenew() const {
return policy_timers->get_policy().can_renew();
return policy_timers_->get_policy().can_renew();
}
int64_t PolicyEngine::GetPlaybackStartTime() {
return policy_timers->GetPlaybackStartTime();
return policy_timers_->GetPlaybackStartTime();
}
int64_t PolicyEngine::GetLastPlaybackTime() {
return policy_timers->GetLastPlaybackTime();
return policy_timers_->GetLastPlaybackTime();
}
int64_t PolicyEngine::GetGracePeriodEndTime() {
return policy_timers->GetGracePeriodEndTime();
return policy_timers_->GetGracePeriodEndTime();
}
void PolicyEngine::RestorePlaybackTimes(int64_t playback_start_time,
int64_t last_playback_time,
int64_t grace_period_end_time) {
const int64_t current_time = GetCurrentTime();
policy_timers->RestorePlaybackTimes(current_time, playback_start_time,
last_playback_time,
grace_period_end_time);
policy_timers_->RestorePlaybackTimes(current_time, playback_start_time,
last_playback_time,
grace_period_end_time);
NotifyExpirationUpdate(current_time);
}
void PolicyEngine::UpdateRenewalRequest(int64_t current_time) {
license_state_ = kLicenseStateWaitingLicenseUpdate;
policy_timers->UpdateRenewalRequest(current_time);
policy_timers_->UpdateRenewalRequest(current_time);
}
bool PolicyEngine::HasLicenseOrPlaybackDurationExpired(int64_t current_time) {
return policy_timers->HasLicenseOrPlaybackDurationExpired(current_time);
bool PolicyEngine::HasLicenseOrRentalOrPlaybackDurationExpired(
int64_t current_time) {
return policy_timers_->HasLicenseOrRentalOrPlaybackDurationExpired(
current_time);
}
// Apply a key status to the current keys.
@@ -377,7 +380,7 @@ void PolicyEngine::NotifyKeysChange(CdmKeyStatus new_status) {
void PolicyEngine::NotifyExpirationUpdate(int64_t current_time) {
int64_t expiry_time;
if (policy_timers->UpdateExpirationTime(current_time, &expiry_time)) {
if (policy_timers_->UpdateExpirationTime(current_time, &expiry_time)) {
if (event_listener_)
event_listener_->OnExpirationUpdate(session_id_, expiry_time);
}