Commit 576b9aff authored by Sam Bowen's avatar Sam Bowen Committed by Commit Bot

[Kaleidoscope Providers] Add a GetHighWatchTimeOrigins method.

This will be used in Kaleidoscope to match against providers returned
from the API. Providers with matching origins will be considered
high watch time, and we will suggest them to the user.

Change-Id: Ic1181dde499ad39094442bd555a1b3b541f5ca2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2277091Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarBecca Hughes <beccahughes@chromium.org>
Commit-Queue: Sam Bowen <sgbowen@google.com>
Cr-Commit-Position: refs/heads/master@{#786074}
parent 3a84c6bb
......@@ -252,6 +252,16 @@ void MediaHistoryKeyedService::SavePlaybackSession(
}
}
void MediaHistoryKeyedService::GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min,
base::OnceCallback<void(const std::vector<url::Origin>&)> callback) {
base::PostTaskAndReplyWithResult(
store_->GetForRead()->db_task_runner_.get(), FROM_HERE,
base::BindOnce(&MediaHistoryStore::GetHighWatchTimeOrigins,
store_->GetForRead(), audio_video_watchtime_min),
std::move(callback));
}
MediaHistoryKeyedService::GetMediaFeedItemsRequest
MediaHistoryKeyedService::GetMediaFeedItemsRequest::CreateItemsForDebug(
int64_t feed_id) {
......
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_MEDIA_HISTORY_MEDIA_HISTORY_KEYED_SERVICE_H_
#include "base/macros.h"
#include "base/time/time.h"
#include "chrome/browser/media/feeds/media_feeds_store.mojom.h"
#include "chrome/browser/media/history/media_history_store.mojom.h"
#include "components/history/core/browser/history_service_observer.h"
......@@ -88,6 +89,12 @@ class MediaHistoryKeyedService : public KeyedService,
const base::Optional<media_session::MediaPosition>& position,
const std::vector<media_session::MediaImage>& artwork);
// Get origins from the origins table that have watchtime above the given
// threshold value.
void GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min,
base::OnceCallback<void(const std::vector<url::Origin>&)> callback);
// Returns Media Feeds items.
struct GetMediaFeedItemsRequest {
enum class Type {
......
......@@ -8,6 +8,7 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "sql/statement.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace media_history {
......@@ -159,4 +160,30 @@ bool MediaHistoryOriginTable::Delete(const url::Origin& origin) {
return statement.Run();
}
std::vector<url::Origin> MediaHistoryOriginTable::GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min) {
std::vector<url::Origin> origins;
if (!CanAccessDatabase())
return origins;
sql::Statement statement(DB()->GetCachedStatement(
SQL_FROM_HERE,
"SELECT origin, "
"aggregate_watchtime_audio_video_s "
"FROM origin "
"ORDER BY aggregate_watchtime_audio_video_s DESC"));
while (statement.Step()) {
url::Origin origin = url::Origin::Create(GURL(statement.ColumnString(0)));
base::TimeDelta cached_audio_video_watchtime =
base::TimeDelta::FromSeconds(statement.ColumnInt64(1));
if (audio_video_watchtime_min <= cached_audio_video_watchtime)
origins.push_back(std::move(origin));
}
DCHECK(statement.Succeeded());
return origins;
}
} // namespace media_history
......@@ -49,6 +49,10 @@ class MediaHistoryOriginTable : public MediaHistoryTableBase {
// was successful.
bool Delete(const url::Origin& origin);
// Gets the origins which have watchtime above the given threshold.
std::vector<url::Origin> GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min);
DISALLOW_COPY_AND_ASSIGN(MediaHistoryOriginTable);
};
......
......@@ -511,6 +511,12 @@ MediaHistoryStore::GetOriginRowsForDebug() {
return origins;
}
std::vector<url::Origin> MediaHistoryStore::GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min) {
DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
return origin_table_->GetHighWatchTimeOrigins(audio_video_watchtime_min);
}
std::vector<mojom::MediaHistoryPlaybackRowPtr>
MediaHistoryStore::GetMediaHistoryPlaybackRowsForDebug() {
DCHECK(db_task_runner_->RunsTasksInCurrentSequence());
......
......@@ -21,6 +21,7 @@
#include "content/public/browser/media_player_watch_time.h"
#include "sql/init_status.h"
#include "sql/meta_table.h"
#include "url/origin.h"
namespace media_session {
struct MediaImage;
......@@ -137,6 +138,9 @@ class MediaHistoryStore : public base::RefCountedThreadSafe<MediaHistoryStore> {
std::vector<media_feeds::mojom::MediaFeedPtr> GetMediaFeeds(
const MediaHistoryKeyedService::GetMediaFeedsRequest& request);
std::vector<url::Origin> GetHighWatchTimeOrigins(
const base::TimeDelta& audio_video_watchtime_min);
void SavePlaybackSession(
const GURL& url,
const media_session::MediaMetadata& metadata,
......
......@@ -24,6 +24,7 @@
#include "chrome/browser/media/history/media_history_feed_items_table.h"
#include "chrome/browser/media/history/media_history_feeds_table.h"
#include "chrome/browser/media/history/media_history_images_table.h"
#include "chrome/browser/media/history/media_history_keyed_service.h"
#include "chrome/browser/media/history/media_history_origin_table.h"
#include "chrome/browser/media/history/media_history_playback_table.h"
#include "chrome/browser/media/history/media_history_session_images_table.h"
......@@ -642,6 +643,55 @@ TEST_P(MediaHistoryStoreUnitTest,
EXPECT_EQ(origins, GetOriginRowsSync(otr_service()));
}
// TODO(crbug.com/1087974).
#if defined(THREAD_SANITIZER)
#define MAYBE_GetOriginsWithHighWatchTime DISABLED_GetOriginsWithHighWatchTime
#else
#define MAYBE_GetOriginsWithHighWatchTime GetOriginsWithHighWatchTime
#endif
TEST_P(MediaHistoryStoreUnitTest, MAYBE_GetOriginsWithHighWatchTime) {
const GURL url("http://google.com/test");
const GURL url_alt("http://example.org/test");
const base::TimeDelta min_watch_time = base::TimeDelta::FromMinutes(30);
{
// Record a watch time that isn't high enough to get with our request.
content::MediaPlayerWatchTime watch_time(
url, url.GetOrigin(), min_watch_time - base::TimeDelta::FromSeconds(1),
base::TimeDelta(), true /* has_video */, true /* has_audio */);
service()->SavePlayback(watch_time);
WaitForDB();
}
{
// Record a watchtime that we should get with our request.
content::MediaPlayerWatchTime watch_time(
url_alt, url_alt.GetOrigin(), min_watch_time, base::TimeDelta(),
true /* has_video */, true /* has_audio */);
service()->SavePlayback(watch_time);
WaitForDB();
}
base::RunLoop run_loop;
std::vector<url::Origin> out;
service()->GetHighWatchTimeOrigins(
min_watch_time,
base::BindLambdaForTesting([&](const std::vector<url::Origin>& origins) {
out = std::move(origins);
run_loop.Quit();
}));
run_loop.Run();
if (IsReadOnly()) {
EXPECT_TRUE(out.empty());
} else {
std::vector<url::Origin> expected = {url::Origin::Create(url_alt)};
EXPECT_EQ(out, expected);
}
}
#if !defined(OS_ANDROID)
// Runs the tests with the media feeds feature enabled.
......@@ -1434,7 +1484,6 @@ TEST_P(MediaHistoryStoreFeedsTest,
// to ensure a no-op.
const int feed_id = IsReadOnly() ? -1 : GetMediaFeedsSync(service())[0]->id;
{
auto result =
ResultWithItems(feed_id, GetExpectedItems(),
......
......@@ -42,6 +42,11 @@ constexpr int kMediaFeedsItemsMaxCount = 20;
constexpr char kChromeMediaRecommendationsOAuth2Scope[] =
"https://www.googleapis.com/auth/chrome-media-recommendations";
// The minimum watch time needed in media history for a provider to be
// considered high watch time.
constexpr base::TimeDelta kProviderHighWatchTimeMin =
base::TimeDelta::FromMinutes(30);
} // namespace
KaleidoscopeDataProviderImpl::KaleidoscopeDataProviderImpl(
......@@ -90,6 +95,12 @@ void KaleidoscopeDataProviderImpl::GetCredentials(GetCredentialsCallback cb) {
signin::ConsentLevel::kNotRequired);
}
void KaleidoscopeDataProviderImpl::GetHighWatchTimeOrigins(
GetHighWatchTimeOriginsCallback cb) {
GetMediaHistoryService()->GetHighWatchTimeOrigins(kProviderHighWatchTimeMin,
std::move(cb));
}
void KaleidoscopeDataProviderImpl::GetTopMediaFeeds(
GetTopMediaFeedsCallback callback) {
GetMediaHistoryService()->GetMediaFeeds(
......
......@@ -11,6 +11,7 @@
#include "google_apis/gaia/google_service_auth_error.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "url/origin.h"
namespace media_history {
class MediaHistoryKeyedService;
......@@ -40,6 +41,7 @@ class KaleidoscopeDataProviderImpl
void GetMediaFeedContents(int64_t feed_id,
GetMediaFeedContentsCallback callback) override;
void GetCredentials(GetCredentialsCallback cb) override;
void GetHighWatchTimeOrigins(GetHighWatchTimeOriginsCallback cb) override;
private:
media_history::MediaHistoryKeyedService* GetMediaHistoryService();
......
......@@ -7,5 +7,8 @@ import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojom") {
sources = [ "kaleidoscope.mojom" ]
public_deps = [ "//chrome/browser/media/feeds:mojo_bindings" ]
public_deps = [
"//chrome/browser/media/feeds:mojo_bindings",
"//url/mojom:url_mojom_origin",
]
}
......@@ -5,6 +5,7 @@
module media.mojom;
import "chrome/browser/media/feeds/media_feeds_store.mojom";
import "url/mojom/origin.mojom";
// The credentials required to make Google API calls from JS.
struct Credentials {
......@@ -27,4 +28,8 @@ interface KaleidoscopeDataProvider {
// Retrieves the current credentials.
GetCredentials() => (Credentials credentials);
// Returns all the watch time origins from media history store that have
// watch time above a threshold.
GetHighWatchTimeOrigins() => (array<url.mojom.Origin> origins);
};
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