Commit b40f5ce9 authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

Implemented NotificationBackgroundTaskSchedulerAndroid hook

- Hooked NotificationBackgroundTaskScheduler onStart and onStop
callback from native to Java side
- Injected SchedulerTaskTime bundle to scheduler TaskInfo
- Marked "internal_types" as public

- TODO in next CLs:
1. Create SchedulerTaskTime enum counter part in Java side
2. Remove file "intenal_types" and move the enum to the public

TBR=peter@chromium.org

Bug: 963287
Change-Id: I20cdcc7d7149ac92885e6a5810dfea7664416e57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1625911
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#663830}
parent 643cb720
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.notifications.scheduler; package org.chromium.chrome.browser.notifications.scheduler;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import android.support.annotation.MainThread; import android.support.annotation.MainThread;
import org.chromium.base.Callback; import org.chromium.base.Callback;
...@@ -23,6 +24,8 @@ import org.chromium.components.background_task_scheduler.TaskParameters; ...@@ -23,6 +24,8 @@ import org.chromium.components.background_task_scheduler.TaskParameters;
* notifications. * notifications.
*/ */
public class NotificationSchedulerTask extends NativeBackgroundTask { public class NotificationSchedulerTask extends NativeBackgroundTask {
public static final String EXTRA_SCHEDULER_TASK_TIME = "extra_scheduler_task_time";
@Override @Override
public void reschedule(Context context) {} public void reschedule(Context context) {}
...@@ -62,19 +65,24 @@ public class NotificationSchedulerTask extends NativeBackgroundTask { ...@@ -62,19 +65,24 @@ public class NotificationSchedulerTask extends NativeBackgroundTask {
/** /**
* Schedules a notification scheduler background task to display scheduled notifications if * Schedules a notification scheduler background task to display scheduled notifications if
* needed. * needed.
* @param schedulerTaskTime The time for the task to scheduling e.g. in the morning or evening.
* @param windowStartMs The starting time of a time window to run the background job in * @param windowStartMs The starting time of a time window to run the background job in
* milliseconds. * milliseconds.
* @param windowEndMs The end time of a time window to run the background job in milliseconds. * @param windowEndMs The end time of a time window to run the background job in milliseconds.
*/ */
@MainThread @MainThread
@CalledByNative @CalledByNative
public static void schedule(long windowStartMs, long windowEndMs) { public static void schedule(
int /*@SchedulerTaskTime*/ schedulerTaskTime, long windowStartMs, long windowEndMs) {
BackgroundTaskScheduler scheduler = BackgroundTaskSchedulerFactory.getScheduler(); BackgroundTaskScheduler scheduler = BackgroundTaskSchedulerFactory.getScheduler();
Bundle bundle = new Bundle();
bundle.putInt(EXTRA_SCHEDULER_TASK_TIME, schedulerTaskTime);
TaskInfo taskInfo = TaskInfo taskInfo =
TaskInfo.createOneOffTask(TaskIds.NOTIFICATION_SCHEDULER_JOB_ID, TaskInfo.createOneOffTask(TaskIds.NOTIFICATION_SCHEDULER_JOB_ID,
NotificationSchedulerTask.class, windowStartMs, windowEndMs) NotificationSchedulerTask.class, windowStartMs, windowEndMs)
.setUpdateCurrent(true) .setUpdateCurrent(true)
.setIsPersisted(true) .setIsPersisted(true)
.setExtras(bundle)
.build(); .build();
scheduler.schedule(ContextUtils.getApplicationContext(), taskInfo); scheduler.schedule(ContextUtils.getApplicationContext(), taskInfo);
} }
......
...@@ -20,6 +20,7 @@ group("scheduler") { ...@@ -20,6 +20,7 @@ group("scheduler") {
source_set("public") { source_set("public") {
sources = [ sources = [
"internal_types.h",
"notification_background_task_scheduler.h", "notification_background_task_scheduler.h",
"notification_data.cc", "notification_data.cc",
"notification_data.h", "notification_data.h",
......
...@@ -90,7 +90,7 @@ class BackgroundTaskCoordinatorHelper { ...@@ -90,7 +90,7 @@ class BackgroundTaskCoordinatorHelper {
} }
} }
ScheduleBackgroundTaskInternal(); ScheduleBackgroundTaskInternal(task_start_time);
} }
private: private:
...@@ -118,7 +118,7 @@ class BackgroundTaskCoordinatorHelper { ...@@ -118,7 +118,7 @@ class BackgroundTaskCoordinatorHelper {
background_task_time_ = time; background_task_time_ = time;
} }
void ScheduleBackgroundTaskInternal() { void ScheduleBackgroundTaskInternal(SchedulerTaskTime task_start_time) {
if (!background_task_time_.has_value()) if (!background_task_time_.has_value())
return; return;
...@@ -127,10 +127,8 @@ class BackgroundTaskCoordinatorHelper { ...@@ -127,10 +127,8 @@ class BackgroundTaskCoordinatorHelper {
window_start_time = base::ClampToRange(window_start_time, base::TimeDelta(), window_start_time = base::ClampToRange(window_start_time, base::TimeDelta(),
base::TimeDelta::Max()); base::TimeDelta::Max());
// TODO(xingliu): Pass the SchedulerTaskTime tag to background task to
// support arbitrary time background task.
background_task_->Schedule( background_task_->Schedule(
window_start_time, task_start_time, window_start_time,
window_start_time + config_->background_task_window_duration); window_start_time + config_->background_task_window_duration);
} }
......
...@@ -48,7 +48,10 @@ class MockNotificationBackgroundTaskScheduler ...@@ -48,7 +48,10 @@ class MockNotificationBackgroundTaskScheduler
public: public:
MockNotificationBackgroundTaskScheduler() = default; MockNotificationBackgroundTaskScheduler() = default;
~MockNotificationBackgroundTaskScheduler() override = default; ~MockNotificationBackgroundTaskScheduler() override = default;
MOCK_METHOD2(Schedule, void(base::TimeDelta, base::TimeDelta)); MOCK_METHOD3(Schedule,
void(notifications::SchedulerTaskTime,
base::TimeDelta,
base::TimeDelta));
MOCK_METHOD0(Cancel, void()); MOCK_METHOD0(Cancel, void());
private: private:
...@@ -155,7 +158,7 @@ class BackgroundTaskCoordinatorTest : public testing::Test { ...@@ -155,7 +158,7 @@ class BackgroundTaskCoordinatorTest : public testing::Test {
// And current task should be canceled. // And current task should be canceled.
TEST_F(BackgroundTaskCoordinatorTest, NoNotification) { TEST_F(BackgroundTaskCoordinatorTest, NoNotification) {
EXPECT_CALL(*background_task(), Cancel()); EXPECT_CALL(*background_task(), Cancel());
EXPECT_CALL(*background_task(), Schedule(_, _)).Times(0); EXPECT_CALL(*background_task(), Schedule(_, _, _)).Times(0);
TestData test_data; TestData test_data;
test_data.impression_test_data = kSingleClientImpressionTestData; test_data.impression_test_data = kSingleClientImpressionTestData;
ScheduleTask(test_data); ScheduleTask(test_data);
...@@ -169,7 +172,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningScheduleEvening) { ...@@ -169,7 +172,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningScheduleEvening) {
// Expected to run task this evening. // Expected to run task this evening.
auto expected_window_start = GetTime("04/25/20 18:00:00 PM") - GetTime(kNow); auto expected_window_start = GetTime("04/25/20 18:00:00 PM") - GetTime(kNow);
EXPECT_CALL(*background_task(), EXPECT_CALL(*background_task(),
Schedule(expected_window_start, Schedule(_, expected_window_start,
expected_window_start + expected_window_start +
config()->background_task_window_duration)); config()->background_task_window_duration));
...@@ -188,7 +191,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningScheduleEveningThrottled) { ...@@ -188,7 +191,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningScheduleEveningThrottled) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run task next morning. // Expected to run task next morning.
EXPECT_CALL(*background_task(), EXPECT_CALL(*background_task(),
Schedule(GetTime("04/26/20 06:00:00 AM") - GetTime(kNow), _)); Schedule(_, GetTime("04/26/20 06:00:00 AM") - GetTime(kNow), _));
auto impression_data = kSingleClientImpressionTestData; auto impression_data = kSingleClientImpressionTestData;
Impression impression; Impression impression;
...@@ -207,7 +210,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InEveningScheduleNextMorning) { ...@@ -207,7 +210,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InEveningScheduleNextMorning) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run task next morning. // Expected to run task next morning.
auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow); auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
NotificationEntry entry(SchedulerClientType::kTest1, kGuid); NotificationEntry entry(SchedulerClientType::kTest1, kGuid);
TestData test_data{ TestData test_data{
...@@ -223,7 +226,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InEveningScheduleNextMorningThrottled) { ...@@ -223,7 +226,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InEveningScheduleNextMorningThrottled) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run task next morning. // Expected to run task next morning.
auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow); auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
// We have reached daily max. // We have reached daily max.
auto impression_data = kSingleClientImpressionTestData; auto impression_data = kSingleClientImpressionTestData;
...@@ -245,7 +248,7 @@ TEST_F(BackgroundTaskCoordinatorTest, Suppression) { ...@@ -245,7 +248,7 @@ TEST_F(BackgroundTaskCoordinatorTest, Suppression) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run task in the morning after suppression expired. // Expected to run task in the morning after suppression expired.
auto expected_window_start = GetTime("04/28/20 06:00:00 AM") - GetTime(kNow); auto expected_window_start = GetTime("04/28/20 06:00:00 AM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
auto impression_data = kSingleClientImpressionTestData; auto impression_data = kSingleClientImpressionTestData;
impression_data.back().suppression_info = SuppressionInfo( impression_data.back().suppression_info = SuppressionInfo(
...@@ -265,7 +268,7 @@ TEST_F(BackgroundTaskCoordinatorTest, ScheduleEarlierTime) { ...@@ -265,7 +268,7 @@ TEST_F(BackgroundTaskCoordinatorTest, ScheduleEarlierTime) {
// kTest1 type will run this evening, kTest2 will run task 3 days later. // kTest1 type will run this evening, kTest2 will run task 3 days later.
// Expected to run the earilier task. // Expected to run the earilier task.
auto expected_window_start = GetTime("04/25/20 18:00:00 PM") - GetTime(kNow); auto expected_window_start = GetTime("04/25/20 18:00:00 PM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
NotificationEntry entry1(SchedulerClientType::kTest1, kGuid); NotificationEntry entry1(SchedulerClientType::kTest1, kGuid);
NotificationEntry entry2(SchedulerClientType::kTest2, "guid_entry2"); NotificationEntry entry2(SchedulerClientType::kTest2, "guid_entry2");
...@@ -284,7 +287,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningThrottledAllTypes) { ...@@ -284,7 +287,7 @@ TEST_F(BackgroundTaskCoordinatorTest, InMorningThrottledAllTypes) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run task next morning. // Expected to run task next morning.
auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow); auto expected_window_start = GetTime("04/26/20 06:00:00 AM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
auto impression_data = kClientsImpressionTestData; auto impression_data = kClientsImpressionTestData;
Impression impression; Impression impression;
...@@ -307,7 +310,7 @@ TEST_F(BackgroundTaskCoordinatorTest, ThrottledAllTypesAndSuppression) { ...@@ -307,7 +310,7 @@ TEST_F(BackgroundTaskCoordinatorTest, ThrottledAllTypesAndSuppression) {
EXPECT_CALL(*background_task(), Cancel()).Times(0); EXPECT_CALL(*background_task(), Cancel()).Times(0);
// Expected to run after 3 days suppression ends. // Expected to run after 3 days suppression ends.
auto expected_window_start = GetTime("04/28/20 06:00:00 AM") - GetTime(kNow); auto expected_window_start = GetTime("04/28/20 06:00:00 AM") - GetTime(kNow);
EXPECT_CALL(*background_task(), Schedule(expected_window_start, _)); EXPECT_CALL(*background_task(), Schedule(_, expected_window_start, _));
auto impression_data = kClientsImpressionTestData; auto impression_data = kClientsImpressionTestData;
Impression impression; Impression impression;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/internal_types.h"
namespace notifications { namespace notifications {
...@@ -36,8 +37,10 @@ class NotificationBackgroundTaskScheduler { ...@@ -36,8 +37,10 @@ class NotificationBackgroundTaskScheduler {
// Schedules a background task in a time window between |window_start| and // Schedules a background task in a time window between |window_start| and
// |window_end|. This will update the current background task. Only one // |window_end|. This will update the current background task. Only one
// background task exists for notification scheduler. // background task exists for notification scheduler. |scheduler_task_time|
virtual void Schedule(base::TimeDelta window_start, // tag is passed to background task go support arbitrary time background task.
virtual void Schedule(notifications::SchedulerTaskTime scheduler_task_time,
base::TimeDelta window_start,
base::TimeDelta window_end) = 0; base::TimeDelta window_end) = 0;
// Cancels the background task. // Cancels the background task.
......
...@@ -14,7 +14,8 @@ void JNI_NotificationSchedulerTask_OnStartTask( ...@@ -14,7 +14,8 @@ void JNI_NotificationSchedulerTask_OnStartTask(
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jobject>& profile, const base::android::JavaParamRef<jobject>& profile,
const base::android::JavaParamRef<jobject>& callback) { const base::android::JavaParamRef<jobject>& callback) {
NOTIMPLEMENTED(); Java_org_chromium_chrome_browser_notifications_scheduler_NotificationSchedulerTask_nativeOnStartTask(
env, jcaller, profile, callback);
} }
// static // static
...@@ -22,8 +23,8 @@ jboolean JNI_NotificationSchedulerTask_OnStopTask( ...@@ -22,8 +23,8 @@ jboolean JNI_NotificationSchedulerTask_OnStopTask(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jobject>& profile) { const base::android::JavaParamRef<jobject>& profile) {
NOTIMPLEMENTED(); return Java_org_chromium_chrome_browser_notifications_scheduler_NotificationSchedulerTask_nativeOnStopTask(
return true; env, jcaller, profile);
} }
NotificationBackgroundTaskSchedulerAndroid:: NotificationBackgroundTaskSchedulerAndroid::
...@@ -33,10 +34,12 @@ NotificationBackgroundTaskSchedulerAndroid:: ...@@ -33,10 +34,12 @@ NotificationBackgroundTaskSchedulerAndroid::
~NotificationBackgroundTaskSchedulerAndroid() = default; ~NotificationBackgroundTaskSchedulerAndroid() = default;
void NotificationBackgroundTaskSchedulerAndroid::Schedule( void NotificationBackgroundTaskSchedulerAndroid::Schedule(
notifications::SchedulerTaskTime scheduler_task_time,
base::TimeDelta window_start, base::TimeDelta window_start,
base::TimeDelta window_end) { base::TimeDelta window_end) {
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
Java_NotificationSchedulerTask_schedule( Java_NotificationSchedulerTask_schedule(
env, base::saturated_cast<jlong>(window_start.InMilliseconds()), env, static_cast<jint>(scheduler_task_time),
base::saturated_cast<jlong>(window_start.InMilliseconds()),
base::saturated_cast<jlong>(window_end.InMilliseconds())); base::saturated_cast<jlong>(window_end.InMilliseconds()));
} }
...@@ -22,7 +22,8 @@ class NotificationBackgroundTaskSchedulerAndroid ...@@ -22,7 +22,8 @@ class NotificationBackgroundTaskSchedulerAndroid
private: private:
// NotificationBackgroundTaskScheduler implementation. // NotificationBackgroundTaskScheduler implementation.
void Schedule(base::TimeDelta window_start, void Schedule(notifications::SchedulerTaskTime scheduler_task_time,
base::TimeDelta window_start,
base::TimeDelta window_end) override; base::TimeDelta window_end) override;
DISALLOW_COPY_AND_ASSIGN(NotificationBackgroundTaskSchedulerAndroid); DISALLOW_COPY_AND_ASSIGN(NotificationBackgroundTaskSchedulerAndroid);
......
...@@ -13,6 +13,7 @@ NotificationBackgroundTaskSchedulerImpl:: ...@@ -13,6 +13,7 @@ NotificationBackgroundTaskSchedulerImpl::
~NotificationBackgroundTaskSchedulerImpl() = default; ~NotificationBackgroundTaskSchedulerImpl() = default;
void NotificationBackgroundTaskSchedulerImpl::Schedule( void NotificationBackgroundTaskSchedulerImpl::Schedule(
notifications::SchedulerTaskTime scheduler_task_time,
base::TimeDelta window_start, base::TimeDelta window_start,
base::TimeDelta window_end) { base::TimeDelta window_end) {
// TODO(xingliu): Implements this for non-Android platform. // TODO(xingliu): Implements this for non-Android platform.
......
...@@ -18,7 +18,8 @@ class NotificationBackgroundTaskSchedulerImpl ...@@ -18,7 +18,8 @@ class NotificationBackgroundTaskSchedulerImpl
private: private:
// NotificationBackgroundTaskScheduler implementation. // NotificationBackgroundTaskScheduler implementation.
void Schedule(base::TimeDelta window_start, void Schedule(notifications::SchedulerTaskTime scheduler_task_time,
base::TimeDelta window_start,
base::TimeDelta window_end) override; base::TimeDelta window_end) override;
void Cancel() override; void Cancel() override;
......
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