Commit a06b143a authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Notification scheduler: Rewrite BackgroundTaskCoordinator.

This CL rewrites the logic to schedule background task in
BackgroundTaskCoordinator. Now it's based on deliver time window.

The old unit tests are deleted, new unit tests will be added in
following CL.

Bug: 991010
Change-Id: I8a05e8178d94b1daadf87d8abc8f9e0bac783e37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1756439
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689105}
parent 3bcd14d3
...@@ -39,15 +39,13 @@ class BackgroundTaskCoordinator { ...@@ -39,15 +39,13 @@ class BackgroundTaskCoordinator {
static std::unique_ptr<BackgroundTaskCoordinator> Create( static std::unique_ptr<BackgroundTaskCoordinator> Create(
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task, std::unique_ptr<NotificationBackgroundTaskScheduler> background_task,
const SchedulerConfig* config, const SchedulerConfig* config,
TimeRandomizer time_randomizer,
base::Clock* clock); base::Clock* clock);
virtual ~BackgroundTaskCoordinator(); virtual ~BackgroundTaskCoordinator();
// Schedule background task based on current notification in the storage. // Schedule background task based on current notification in the storage.
virtual void ScheduleBackgroundTask(Notifications notifications, virtual void ScheduleBackgroundTask(Notifications notifications,
ClientStates client_states, ClientStates client_states) = 0;
SchedulerTaskTime task_start_time) = 0;
}; };
} // namespace notifications } // namespace notifications
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string>
#include <vector> #include <vector>
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -75,8 +76,6 @@ class DisplayDeciderTest : public testing::Test { ...@@ -75,8 +76,6 @@ class DisplayDeciderTest : public testing::Test {
void SetUp() override { void SetUp() override {
// Setup configuration used by this test. // Setup configuration used by this test.
config_.morning_task_hour = 7;
config_.evening_task_hour = 18;
config_.max_daily_shown_all_type = 3; config_.max_daily_shown_all_type = 3;
} }
......
...@@ -267,7 +267,7 @@ class NotificationSchedulerImpl : public NotificationScheduler, ...@@ -267,7 +267,7 @@ class NotificationSchedulerImpl : public NotificationScheduler,
context_->impression_tracker()->GetClientStates(&client_states); context_->impression_tracker()->GetClientStates(&client_states);
context_->background_task_coordinator()->ScheduleBackgroundTask( context_->background_task_coordinator()->ScheduleBackgroundTask(
std::move(notifications), std::move(client_states), task_start_time_); std::move(notifications), std::move(client_states));
} }
void OnUserAction(const UserActionData& action_data) override { void OnUserAction(const UserActionData& action_data) override {
......
...@@ -207,7 +207,7 @@ TEST_F(NotificationSchedulerTest, Schedule) { ...@@ -207,7 +207,7 @@ TEST_F(NotificationSchedulerTest, Schedule) {
auto param = std::unique_ptr<NotificationParams>(); auto param = std::unique_ptr<NotificationParams>();
EXPECT_CALL(*notification_manager(), ScheduleNotification(_)); EXPECT_CALL(*notification_manager(), ScheduleNotification(_));
EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _, _)); EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
scheduler()->Schedule(std::move(param)); scheduler()->Schedule(std::move(param));
} }
...@@ -217,7 +217,7 @@ TEST_F(NotificationSchedulerTest, DeleteAllNotifications) { ...@@ -217,7 +217,7 @@ TEST_F(NotificationSchedulerTest, DeleteAllNotifications) {
// Currently we don't reschedule background task even if all the notifications // Currently we don't reschedule background task even if all the notifications
// are deleted. // are deleted.
EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _, _)).Times(0); EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _)).Times(0);
EXPECT_CALL(*notification_manager(), EXPECT_CALL(*notification_manager(),
DeleteNotifications(SchedulerClientType::kTest1)); DeleteNotifications(SchedulerClientType::kTest1));
scheduler()->DeleteAllNotifications(SchedulerClientType::kTest1); scheduler()->DeleteAllNotifications(SchedulerClientType::kTest1);
...@@ -260,7 +260,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNothing) { ...@@ -260,7 +260,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNothing) {
EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0); EXPECT_CALL(*display_agent(), ShowNotification(_, _)).Times(0);
EXPECT_CALL(*notification_manager(), DisplayNotification(_)).Times(0); EXPECT_CALL(*notification_manager(), DisplayNotification(_)).Times(0);
EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _, _)); EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
scheduler()->OnStartTask(SchedulerTaskTime::kMorning, base::DoNothing()); scheduler()->OnStartTask(SchedulerTaskTime::kMorning, base::DoNothing());
} }
...@@ -308,7 +308,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNotification) { ...@@ -308,7 +308,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNotification) {
notification_manager_delegate()->DisplayNotification(std::move(entry)); notification_manager_delegate()->DisplayNotification(std::move(entry));
})); }));
EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _, _)); EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
scheduler()->OnStartTask(SchedulerTaskTime::kMorning, base::DoNothing()); scheduler()->OnStartTask(SchedulerTaskTime::kMorning, base::DoNothing());
loop.Run(); loop.Run();
...@@ -317,7 +317,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNotification) { ...@@ -317,7 +317,7 @@ TEST_F(NotificationSchedulerTest, BackgroundTaskStartShowNotification) {
// Test to simulate a background task stopped by the OS. // Test to simulate a background task stopped by the OS.
TEST_F(NotificationSchedulerTest, BackgroundTaskStop) { TEST_F(NotificationSchedulerTest, BackgroundTaskStop) {
Init(); Init();
EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _, _)); EXPECT_CALL(*task_coordinator(), ScheduleBackgroundTask(_, _));
scheduler()->OnStopTask(SchedulerTaskTime::kMorning); scheduler()->OnStopTask(SchedulerTaskTime::kMorning);
} }
......
...@@ -18,12 +18,6 @@ constexpr base::TimeDelta kDefaultImpressionExpiration = ...@@ -18,12 +18,6 @@ constexpr base::TimeDelta kDefaultImpressionExpiration =
constexpr base::TimeDelta kDefaultSuppressionDuration = constexpr base::TimeDelta kDefaultSuppressionDuration =
base::TimeDelta::FromDays(56); base::TimeDelta::FromDays(56);
// The morning task by default will run at 7am.
constexpr int kDefaultMorningTaskHour = 7;
// The evening task by default will run at 6pm.
constexpr int kDefaultEveningTaskHour = 18;
// Check consecutive notification dismisses in this duration to generate a // Check consecutive notification dismisses in this duration to generate a
// dismiss event. // dismiss event.
constexpr base::TimeDelta kDefaultDimissDuration = base::TimeDelta::FromDays(7); constexpr base::TimeDelta kDefaultDimissDuration = base::TimeDelta::FromDays(7);
...@@ -33,8 +27,8 @@ constexpr base::TimeDelta kDefaultBackgroundTaskWindowDuration = ...@@ -33,8 +27,8 @@ constexpr base::TimeDelta kDefaultBackgroundTaskWindowDuration =
base::TimeDelta::FromHours(1); base::TimeDelta::FromHours(1);
// Default randomized time window to distribute load from user actions. // Default randomized time window to distribute load from user actions.
constexpr base::TimeDelta kDefaultBackgroundTaskRandomTimeWindow = constexpr base::TimeDelta kDefaultBackgroundTaskMinInterval =
base::TimeDelta::FromHours(1); base::TimeDelta::FromMinutes(10);
// static // static
std::unique_ptr<SchedulerConfig> SchedulerConfig::Create() { std::unique_ptr<SchedulerConfig> SchedulerConfig::Create() {
...@@ -50,11 +44,8 @@ SchedulerConfig::SchedulerConfig() ...@@ -50,11 +44,8 @@ SchedulerConfig::SchedulerConfig()
suppression_duration(kDefaultSuppressionDuration), suppression_duration(kDefaultSuppressionDuration),
dismiss_count(3), dismiss_count(3),
dismiss_duration(kDefaultDimissDuration), dismiss_duration(kDefaultDimissDuration),
morning_task_hour(kDefaultMorningTaskHour),
evening_task_hour(kDefaultEveningTaskHour),
background_task_window_duration(kDefaultBackgroundTaskWindowDuration), background_task_window_duration(kDefaultBackgroundTaskWindowDuration),
background_task_random_time_window( background_task_min_interval(kDefaultBackgroundTaskMinInterval) {
kDefaultBackgroundTaskRandomTimeWindow) {
// TODO(xingliu): Add constructor using finch data. // TODO(xingliu): Add constructor using finch data.
} }
......
...@@ -49,19 +49,11 @@ struct SchedulerConfig { ...@@ -49,19 +49,11 @@ struct SchedulerConfig {
// in this duration, to generate a dismiss event. // in this duration, to generate a dismiss event.
base::TimeDelta dismiss_duration; base::TimeDelta dismiss_duration;
// The hour (from 0 to 23) to run the morning background task for notification
// scheduler.
int morning_task_hour;
// The hour (from 0 to 23) to run the evening background task for notification
// scheduler.
int evening_task_hour;
// The time window to launch the background task. // The time window to launch the background task.
base::TimeDelta background_task_window_duration; base::TimeDelta background_task_window_duration;
// A random time delta to distribute the user clicks to a time window. // The minimum interval between background tasks.
base::TimeDelta background_task_random_time_window; base::TimeDelta background_task_min_interval;
private: private:
DISALLOW_COPY_AND_ASSIGN(SchedulerConfig); DISALLOW_COPY_AND_ASSIGN(SchedulerConfig);
......
...@@ -97,8 +97,6 @@ KeyedService* CreateNotificationScheduleService( ...@@ -97,8 +97,6 @@ KeyedService* CreateNotificationScheduleService(
auto background_task_coordinator = BackgroundTaskCoordinator::Create( auto background_task_coordinator = BackgroundTaskCoordinator::Create(
std::move(background_task_scheduler), config.get(), std::move(background_task_scheduler), config.get(),
base::BindRepeating(&BackgroundTaskCoordinator::DefaultTimeRandomizer,
config->background_task_random_time_window),
base::DefaultClock::GetInstance()); base::DefaultClock::GetInstance());
auto display_decider = DisplayDecider::Create( auto display_decider = DisplayDecider::Create(
......
...@@ -15,10 +15,9 @@ class MockBackgroundTaskCoordinator : public BackgroundTaskCoordinator { ...@@ -15,10 +15,9 @@ class MockBackgroundTaskCoordinator : public BackgroundTaskCoordinator {
public: public:
MockBackgroundTaskCoordinator(); MockBackgroundTaskCoordinator();
~MockBackgroundTaskCoordinator() override; ~MockBackgroundTaskCoordinator() override;
MOCK_METHOD3(ScheduleBackgroundTask, MOCK_METHOD2(ScheduleBackgroundTask,
void(BackgroundTaskCoordinator::Notifications notifications, void(BackgroundTaskCoordinator::Notifications notifications,
BackgroundTaskCoordinator::ClientStates client_states, BackgroundTaskCoordinator::ClientStates client_states));
SchedulerTaskTime task_start_time));
}; };
} // namespace test } // namespace test
......
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