Commit 8ed534b1 authored by wutao's avatar wutao Committed by Commit Bot

ambient: Change topic batch size

This is a work around to not curate personal photos. Batch size 2 seems
the max number will not get server side curation.

Bug: b/158256201
Test: manual
Change-Id: I9b9dcfd81c32f8db3dadd2552fcb5475f0638080
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2263234
Commit-Queue: Tao Wu <wutao@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782165}
parent 4f97bca4
......@@ -14,6 +14,8 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/optional.h"
#include "base/rand_util.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
......@@ -22,7 +24,21 @@ namespace ash {
namespace {
constexpr int kNumberTopics = 100;
// Topic related numbers.
// The number of requests to fetch topics.
constexpr int kNumberOfRequests = 50;
// The batch size of topics to fetch in one request.
// Magic number 2 is based on experiments that no curation on Google Photos.
constexpr int kTopicsBatchSize = 2;
// The upper bound of delay to the fetch topics. An random value will be
// generated in the range of |kTopicFetchDelayMax|/2 to |kTopicFetchDelayMax|.
// TODO(b/139953713): Change to a correct time interval.
// E.g. it will be max 36 seconds if we want to fetch 50 batches in 30 mins.
constexpr base::TimeDelta kTopicFetchDelayMax = base::TimeDelta::FromSeconds(3);
using DownloadCallback = base::OnceCallback<void(const gfx::ImageSkia&)>;
......@@ -42,24 +58,45 @@ AmbientPhotoController::AmbientPhotoController() {
AmbientPhotoController::~AmbientPhotoController() = default;
void AmbientPhotoController::StartScreenUpdate() {
Shell::Get()
->ambient_controller()
->ambient_backend_controller()
->FetchScreenUpdateInfo(
kNumberTopics,
base::BindOnce(&AmbientPhotoController::OnScreenUpdateInfoFetched,
weak_factory_.GetWeakPtr()));
FetchTopics();
}
void AmbientPhotoController::StopScreenUpdate() {
photo_refresh_timer_.Stop();
topic_index_ = 0;
topics_batch_fetched_ = 0;
ambient_backend_model_.Clear();
weak_factory_.InvalidateWeakPtrs();
}
void AmbientPhotoController::OnTopicsChanged() {
ScheduleRefreshImage();
++topics_batch_fetched_;
if (topics_batch_fetched_ < kNumberOfRequests)
ScheduleFetchTopics();
// The first OnTopicsChanged event triggers the photo refresh.
if (topics_batch_fetched_ == 1)
ScheduleRefreshImage();
}
void AmbientPhotoController::FetchTopics() {
Shell::Get()
->ambient_controller()
->ambient_backend_controller()
->FetchScreenUpdateInfo(
kTopicsBatchSize,
base::BindOnce(&AmbientPhotoController::OnScreenUpdateInfoFetched,
weak_factory_.GetWeakPtr()));
}
void AmbientPhotoController::ScheduleFetchTopics() {
const base::TimeDelta kDelay =
(base::RandDouble() * kTopicFetchDelayMax) / 2 + kTopicFetchDelayMax / 2;
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&AmbientPhotoController::FetchTopics,
weak_factory_.GetWeakPtr()),
kDelay);
}
void AmbientPhotoController::ScheduleRefreshImage() {
......@@ -79,12 +116,13 @@ const AmbientModeTopic& AmbientPhotoController::GetNextTopic() {
const auto& topics = ambient_backend_model_.topics();
DCHECK(!topics.empty());
const auto& topic = topics[topic_index_];
++topic_index_;
// We prefetch the first two photos, which will increase the |topic_index_| to
// 2 in the first batch with size of 2. Then it will reset to 0 if we put this
// block after the increment of |topic_index_|.
if (topic_index_ == topics.size())
topic_index_ = 0;
return topic;
return topics[topic_index_++];
}
void AmbientPhotoController::GetNextImage() {
......@@ -106,9 +144,7 @@ void AmbientPhotoController::OnScreenUpdateInfoFetched(
return;
}
ambient_backend_model_.SetTopics(screen_update.next_topics);
topic_index_ = 0;
ambient_backend_model_.AppendTopics(screen_update.next_topics);
StartDownloadingWeatherConditionIcon(screen_update);
}
......
......@@ -64,6 +64,10 @@ class ASH_EXPORT AmbientPhotoController : public AmbientBackendModelObserver {
private:
friend class AmbientAshTestBase;
void FetchTopics();
void ScheduleFetchTopics();
void ScheduleRefreshImage();
void GetScreenUpdateInfo();
......@@ -87,11 +91,15 @@ class ASH_EXPORT AmbientPhotoController : public AmbientBackendModelObserver {
AmbientBackendModel ambient_backend_model_;
// The timer to refresh photos.
base::OneShotTimer photo_refresh_timer_;
// The index of a topic to download.
size_t topic_index_ = 0;
// Tracking how many batches of topics have been fetched.
int topics_batch_fetched_ = 0;
ScopedObserver<AmbientBackendModel, AmbientBackendModelObserver>
ambient_backedn_model_observer_{this};
......
......@@ -26,9 +26,9 @@ void AmbientBackendModel::RemoveObserver(
observers_.RemoveObserver(observer);
}
void AmbientBackendModel::SetTopics(
void AmbientBackendModel::AppendTopics(
const std::vector<AmbientModeTopic>& topics) {
topics_ = topics;
topics_.insert(topics_.end(), topics.begin(), topics.end());
NotifyTopicsChanged();
}
......
......@@ -31,7 +31,7 @@ class ASH_EXPORT AmbientBackendModel {
void AddObserver(AmbientBackendModelObserver* observer);
void RemoveObserver(AmbientBackendModelObserver* observer);
void SetTopics(const std::vector<AmbientModeTopic>& topics);
void AppendTopics(const std::vector<AmbientModeTopic>& topics);
const std::vector<AmbientModeTopic>& topics() const { return topics_; }
// Prefetch one more image for ShowNextImage animations.
......
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