Commit fb380be6 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

[Media Engagement] Add storage for AudioContext

Add a media element playbacks and an audio
context playbacks field to the
MediaEngagmentScore. The media element playbacks
field should be pre-populated with the current
media playbacks value (as all previous playbacks
would be from media elements).

BUG=878460

Change-Id: If34b468bddf1cefaf753e0acd220afa1f100331b
Reviewed-on: https://chromium-review.googlesource.com/1194690
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587888}
parent 85110147
......@@ -24,6 +24,10 @@ const char MediaEngagementScore::kSignificantPlaybacksKey[] =
const char MediaEngagementScore::kVisitsWithMediaTagKey[] =
"visitsWithMediaTag";
const char MediaEngagementScore::kHighScoreChanges[] = "highScoreChanges";
const char MediaEngagementScore::kSignificantMediaPlaybacksKey[] =
"mediaElementPlaybacks";
const char MediaEngagementScore::kSignificantAudioContextPlaybacksKey[] =
"audioContextPlaybacks";
const char MediaEngagementScore::kScoreMinVisitsParamName[] = "min_visits";
const char MediaEngagementScore::kHighScoreLowerThresholdParamName[] =
......@@ -82,24 +86,18 @@ MediaEngagementScore::MediaEngagementScore(base::Clock* clock,
: MediaEngagementScore(
clock,
origin,
GetMediaEngagementScoreDictForSettings(settings, origin)) {
settings_map_ = settings;
}
GetMediaEngagementScoreDictForSettings(settings, origin),
settings) {}
MediaEngagementScore::MediaEngagementScore(
base::Clock* clock,
const GURL& origin,
std::unique_ptr<base::DictionaryValue> score_dict,
HostContentSettingsMap* settings)
: MediaEngagementScore(clock, origin, std::move(score_dict)) {
settings_map_ = settings;
}
MediaEngagementScore::MediaEngagementScore(
base::Clock* clock,
const GURL& origin,
std::unique_ptr<base::DictionaryValue> score_dict)
: origin_(origin), clock_(clock), score_dict_(score_dict.release()) {
: origin_(origin),
clock_(clock),
score_dict_(score_dict.release()),
settings_map_(settings) {
if (!score_dict_)
return;
......@@ -110,6 +108,10 @@ MediaEngagementScore::MediaEngagementScore(
score_dict_->GetInteger(kSignificantPlaybacksKey, &significant_playbacks_);
score_dict_->GetInteger(kVisitsWithMediaTagKey, &visits_with_media_tag_);
score_dict_->GetInteger(kHighScoreChanges, &high_score_changes_);
score_dict_->GetInteger(kSignificantMediaPlaybacksKey,
&media_element_playbacks_);
score_dict_->GetInteger(kSignificantAudioContextPlaybacksKey,
&audio_context_playbacks_);
double internal_time;
if (score_dict_->GetDouble(kLastMediaPlaybackTimeKey, &internal_time))
......@@ -120,14 +122,23 @@ MediaEngagementScore::MediaEngagementScore(
// or if we have data from before the bit was introduced.
bool was_high = is_high_;
Recalculate();
if (is_high_ != was_high) {
if (settings_map_) {
Commit();
} else {
// Update the internal dictionary for testing.
score_dict_->SetBoolean(kHasHighScoreKey, is_high_);
}
bool needs_commit = is_high_ != was_high;
// If we have a score for media playbacks and we do not have a value for
// both media element playbacks and audio context playbacks then we should
// copy media playbacks into media element playbacks.
bool has_new_playbacks_key_ =
score_dict_->FindKey(kSignificantMediaPlaybacksKey) ||
score_dict_->FindKey(kSignificantAudioContextPlaybacksKey);
if (!has_new_playbacks_key_) {
media_element_playbacks_ = media_playbacks_;
needs_commit = true;
}
// If we need to commit because of a migration and we have the settings map
// then we should commit.
if (needs_commit && settings_map_)
Commit();
}
// TODO(beccahughes): Add typemap.
......@@ -169,6 +180,8 @@ bool MediaEngagementScore::UpdateScoreDict() {
int stored_significant_playbacks = 0;
int stored_visits_with_media_tag = 0;
int high_score_changes = 0;
int stored_media_element_playbacks = 0;
int stored_audio_context_playbacks = 0;
score_dict_->GetInteger(kVisitsKey, &stored_visits);
score_dict_->GetInteger(kMediaPlaybacksKey, &stored_media_playbacks);
......@@ -181,6 +194,10 @@ bool MediaEngagementScore::UpdateScoreDict() {
score_dict_->GetInteger(kVisitsWithMediaTagKey,
&stored_visits_with_media_tag);
score_dict_->GetInteger(kHighScoreChanges, &high_score_changes);
score_dict_->GetInteger(kSignificantMediaPlaybacksKey,
&stored_media_element_playbacks);
score_dict_->GetInteger(kSignificantAudioContextPlaybacksKey,
&stored_audio_context_playbacks);
bool changed = stored_visits != visits() ||
stored_media_playbacks != media_playbacks() ||
......@@ -188,6 +205,8 @@ bool MediaEngagementScore::UpdateScoreDict() {
stored_audible_playbacks != audible_playbacks() ||
stored_significant_playbacks != significant_playbacks() ||
stored_visits_with_media_tag != visits_with_media_tag() ||
stored_media_element_playbacks != media_element_playbacks() ||
stored_audio_context_playbacks != audio_context_playbacks() ||
stored_last_media_playback_internal !=
last_media_playback_time_.ToInternalValue();
......@@ -210,6 +229,10 @@ bool MediaEngagementScore::UpdateScoreDict() {
score_dict_->SetInteger(kSignificantPlaybacksKey, significant_playbacks_);
score_dict_->SetInteger(kVisitsWithMediaTagKey, visits_with_media_tag_);
score_dict_->SetInteger(kHighScoreChanges, high_score_changes_);
score_dict_->SetInteger(kSignificantMediaPlaybacksKey,
media_element_playbacks_);
score_dict_->SetInteger(kSignificantAudioContextPlaybacksKey,
audio_context_playbacks_);
return true;
}
......
......@@ -28,6 +28,11 @@ class MediaEngagementScore final {
// kSignificantPlaybacksKey will store the number of significant playbacks
// on an origin. kVisitsWithMediaTagKey will store the number of visits to
// an origin where at least one page had a media tag.
// kSignificantMediaPlaybacksKey will store significant playbacks that
// originated from media elements and kSignificantAudioContextPlaybacksKey
// will store significant playbacks originated from audio contexts. The
// sum of the two may be higher than kMediaPlaybacksKey as a visit may have
// both an audio context playback and a media element playback.
static const char kVisitsKey[];
static const char kMediaPlaybacksKey[];
static const char kLastMediaPlaybackTimeKey[];
......@@ -36,6 +41,8 @@ class MediaEngagementScore final {
static const char kSignificantPlaybacksKey[];
static const char kVisitsWithMediaTagKey[];
static const char kHighScoreChanges[];
static const char kSignificantMediaPlaybacksKey[];
static const char kSignificantAudioContextPlaybacksKey[];
// Origins with a number of visits less than this number will recieve
// a score of zero.
......@@ -102,6 +109,20 @@ class MediaEngagementScore final {
set_visits_with_media_tag(visits_with_media_tag_ + 1);
}
// Get/increment the number of significant playbacks from media elements this
// origin had.
int media_element_playbacks() const { return media_element_playbacks_; }
void IncrementMediaElementPlaybacks() {
set_media_element_playbacks(media_element_playbacks_ + 1);
}
// Get/increment the number of significant playbacks from audio contexts this
// origin had.
int audio_context_playbacks() const { return audio_context_playbacks_; }
void IncrementAudioContextPlaybacks() {
set_audio_context_playbacks(audio_context_playbacks_ + 1);
}
// Get a breakdown of the score that can be serialized by Mojo.
media::mojom::MediaEngagementScoreDetailsPtr GetScoreDetails() const;
......@@ -134,16 +155,17 @@ class MediaEngagementScore final {
void set_last_media_playback_time(base::Time new_time) {
last_media_playback_time_ = new_time;
}
void set_media_element_playbacks(int playbacks) {
media_element_playbacks_ = playbacks;
}
void set_audio_context_playbacks(int playbacks) {
audio_context_playbacks_ = playbacks;
}
private:
friend class MediaEngagementServiceTest;
friend class MediaEngagementScoreTest;
// Used for tests.
MediaEngagementScore(base::Clock* clock,
const GURL& origin,
std::unique_ptr<base::DictionaryValue> score_dict);
// Update the dictionary continaing the latest score values and return whether
// they have changed or not (since what was last retrieved from content
// settings).
......@@ -180,6 +202,12 @@ class MediaEngagementScore final {
// The last time media was played back on this origin.
base::Time last_media_playback_time_;
// The number of significant playbacks from a media element this origin had.
int media_element_playbacks_ = 0;
// The number of significant playbacks from an audio context this origin had.
int audio_context_playbacks_ = 0;
// The origin this score represents.
GURL origin_;
......
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