Commit 60b52be5 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

[Media Engagement] Add checks in UpdateScoreDict

UpdateScoreDict is causing a crash when we are getting
keys and this could be caused by old data that does
not have all the keys. This hardens the checks when
loading the data.

BUG=934165

Change-Id: I35eafc9b2a232ff8cc79fd48b48de9cc09cfc65d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1508596Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638629}
parent 1ba12aee
...@@ -57,6 +57,13 @@ std::unique_ptr<base::DictionaryValue> GetMediaEngagementScoreDictForSettings( ...@@ -57,6 +57,13 @@ std::unique_ptr<base::DictionaryValue> GetMediaEngagementScoreDictForSettings(
return std::make_unique<base::DictionaryValue>(); return std::make_unique<base::DictionaryValue>();
} }
void GetIntegerFromScore(base::DictionaryValue* dict,
base::StringPiece key,
int* out) {
if (base::Value* v = dict->FindKeyOfType(key, base::Value::Type::INTEGER))
*out = v->GetInt();
}
} // namespace } // namespace
// static. // static.
...@@ -101,21 +108,31 @@ MediaEngagementScore::MediaEngagementScore( ...@@ -101,21 +108,31 @@ MediaEngagementScore::MediaEngagementScore(
if (!score_dict_) if (!score_dict_)
return; return;
score_dict_->GetInteger(kVisitsKey, &visits_); GetIntegerFromScore(score_dict_.get(), kVisitsKey, &visits_);
score_dict_->GetInteger(kMediaPlaybacksKey, &media_playbacks_); GetIntegerFromScore(score_dict_.get(), kMediaPlaybacksKey, &media_playbacks_);
score_dict_->GetBoolean(kHasHighScoreKey, &is_high_); GetIntegerFromScore(score_dict_.get(), kAudiblePlaybacksKey,
score_dict_->GetInteger(kAudiblePlaybacksKey, &audible_playbacks_); &audible_playbacks_);
score_dict_->GetInteger(kSignificantPlaybacksKey, &significant_playbacks_); GetIntegerFromScore(score_dict_.get(), kSignificantPlaybacksKey,
score_dict_->GetInteger(kVisitsWithMediaTagKey, &visits_with_media_tag_); &significant_playbacks_);
score_dict_->GetInteger(kHighScoreChanges, &high_score_changes_); GetIntegerFromScore(score_dict_.get(), kVisitsWithMediaTagKey,
score_dict_->GetInteger(kSignificantMediaPlaybacksKey, &visits_with_media_tag_);
GetIntegerFromScore(score_dict_.get(), kHighScoreChanges,
&high_score_changes_);
GetIntegerFromScore(score_dict_.get(), kSignificantMediaPlaybacksKey,
&media_element_playbacks_); &media_element_playbacks_);
score_dict_->GetInteger(kSignificantAudioContextPlaybacksKey, GetIntegerFromScore(score_dict_.get(), kSignificantAudioContextPlaybacksKey,
&audio_context_playbacks_); &audio_context_playbacks_);
double internal_time; if (base::Value* value = score_dict_->FindKeyOfType(
if (score_dict_->GetDouble(kLastMediaPlaybackTimeKey, &internal_time)) kHasHighScoreKey, base::Value::Type::BOOLEAN)) {
last_media_playback_time_ = base::Time::FromInternalValue(internal_time); is_high_ = value->GetBool();
}
if (base::Value* value = score_dict_->FindKeyOfType(
kLastMediaPlaybackTimeKey, base::Value::Type::DOUBLE)) {
last_media_playback_time_ =
base::Time::FromInternalValue(value->GetDouble());
}
// Recalculate the total score and high bit. If the high bit changed we // Recalculate the total score and high bit. If the high bit changed we
// should commit this. This should only happen if we change the threshold // should commit this. This should only happen if we change the threshold
...@@ -184,20 +201,33 @@ bool MediaEngagementScore::UpdateScoreDict() { ...@@ -184,20 +201,33 @@ bool MediaEngagementScore::UpdateScoreDict() {
int stored_media_element_playbacks = 0; int stored_media_element_playbacks = 0;
int stored_audio_context_playbacks = 0; int stored_audio_context_playbacks = 0;
score_dict_->GetInteger(kVisitsKey, &stored_visits); if (!score_dict_)
score_dict_->GetInteger(kMediaPlaybacksKey, &stored_media_playbacks); return false;
score_dict_->GetDouble(kLastMediaPlaybackTimeKey,
&stored_last_media_playback_internal); if (base::Value* value = score_dict_->FindKeyOfType(
score_dict_->GetBoolean(kHasHighScoreKey, &is_high); kHasHighScoreKey, base::Value::Type::BOOLEAN)) {
score_dict_->GetInteger(kAudiblePlaybacksKey, &stored_audible_playbacks); is_high = value->GetBool();
score_dict_->GetInteger(kSignificantPlaybacksKey, }
if (base::Value* value = score_dict_->FindKeyOfType(
kLastMediaPlaybackTimeKey, base::Value::Type::DOUBLE)) {
stored_last_media_playback_internal = value->GetDouble();
}
GetIntegerFromScore(score_dict_.get(), kVisitsKey, &stored_visits);
GetIntegerFromScore(score_dict_.get(), kMediaPlaybacksKey,
&stored_media_playbacks);
GetIntegerFromScore(score_dict_.get(), kAudiblePlaybacksKey,
&stored_audible_playbacks);
GetIntegerFromScore(score_dict_.get(), kSignificantPlaybacksKey,
&stored_significant_playbacks); &stored_significant_playbacks);
score_dict_->GetInteger(kVisitsWithMediaTagKey, GetIntegerFromScore(score_dict_.get(), kVisitsWithMediaTagKey,
&stored_visits_with_media_tag); &stored_visits_with_media_tag);
score_dict_->GetInteger(kHighScoreChanges, &high_score_changes); GetIntegerFromScore(score_dict_.get(), kHighScoreChanges,
score_dict_->GetInteger(kSignificantMediaPlaybacksKey, &high_score_changes);
GetIntegerFromScore(score_dict_.get(), kSignificantMediaPlaybacksKey,
&stored_media_element_playbacks); &stored_media_element_playbacks);
score_dict_->GetInteger(kSignificantAudioContextPlaybacksKey, GetIntegerFromScore(score_dict_.get(), kSignificantAudioContextPlaybacksKey,
&stored_audio_context_playbacks); &stored_audio_context_playbacks);
bool changed = stored_visits != visits() || bool changed = stored_visits != visits() ||
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment