Commit 7152da3b authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Notification: Background task scheduler for notification scheduler.

This CL adds glue interfaces and setup unimplemented classes for
notification scheduler system to use platform background task scheduler
API.

TBR=peter@chromium.org

Bug: 930968
Change-Id: I01abc4edc15f4d2569ec7fb0abf012341cfa4ee5
Reviewed-on: https://chromium-review.googlesource.com/c/1470718Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636185}
parent dd86adaf
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.notifications.scheduler;
import android.content.Context;
import android.support.annotation.MainThread;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.background_task_scheduler.NativeBackgroundTask;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler;
import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory;
import org.chromium.components.background_task_scheduler.TaskIds;
import org.chromium.components.background_task_scheduler.TaskInfo;
import org.chromium.components.background_task_scheduler.TaskParameters;
/**
* A background task used by notification scheduler system to process and display scheduled
* notifications.
*/
public class NotificationSchedulerTask extends NativeBackgroundTask {
@Override
public void reschedule(Context context) {}
@Override
protected int onStartTaskBeforeNativeLoaded(
Context context, TaskParameters taskParameters, TaskFinishedCallback callback) {
return StartBeforeNativeResult.LOAD_NATIVE;
}
@Override
protected void onStartTaskWithNative(
Context context, TaskParameters taskParameters, TaskFinishedCallback callback) {
// Wrap to a Callback<Boolean> because JNI generator can't recognize TaskFinishedCallback as
// a Java interface in the function parameter.
Callback<Boolean> taskCallback = new Callback<Boolean>() {
@Override
public void onResult(Boolean needsReschedule) {
callback.taskFinished(needsReschedule);
}
};
nativeOnStartTask(Profile.getLastUsedProfile().getOriginalProfile(), taskCallback);
}
@Override
protected boolean onStopTaskBeforeNativeLoaded(Context context, TaskParameters taskParameters) {
// Reschedule the background task if native is not even loaded, that we don't know if any
// notification needs to be processed.
return true;
}
@Override
protected boolean onStopTaskWithNative(Context context, TaskParameters taskParameters) {
// TODO(xingliu): Check with native to see if we need to reschedule.
return nativeOnStopTask(Profile.getLastUsedProfile().getOriginalProfile());
}
/**
* Schedules a notification scheduler background task to display scheduled notifications if
* needed.
* @param windowStartMs The starting 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
@CalledByNative
public static void schedule(long windowStartMs, long windowEndMs) {
BackgroundTaskScheduler scheduler = BackgroundTaskSchedulerFactory.getScheduler();
TaskInfo taskInfo =
TaskInfo.createOneOffTask(TaskIds.NOTIFICATION_SCHEDULER_JOB_ID,
NotificationSchedulerTask.class, windowStartMs, windowEndMs)
.setUpdateCurrent(true)
.setIsPersisted(true)
.build();
scheduler.schedule(ContextUtils.getApplicationContext(), taskInfo);
}
private native void nativeOnStartTask(Profile profile, Callback<Boolean> callback);
private native boolean nativeOnStopTask(Profile profile);
}
......@@ -981,6 +981,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/notifications/channels/ChannelsInitializer.java",
"java/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdater.java",
"java/src/org/chromium/chrome/browser/notifications/channels/SiteChannelsManager.java",
"java/src/org/chromium/chrome/browser/notifications/scheduler/NotificationSchedulerTask.java",
"java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotifier.java",
"java/src/org/chromium/chrome/browser/ntp/FakeRecentlyClosedTabManager.java",
"java/src/org/chromium/chrome/browser/ntp/ForeignSessionHelper.java",
......
......@@ -893,6 +893,8 @@ jumbo_split_static_library("browser") {
"notifications/metrics/notification_metrics_logger_factory.h",
"notifications/non_persistent_notification_handler.cc",
"notifications/non_persistent_notification_handler.h",
"notifications/notification_background_task_scheduler_impl.cc",
"notifications/notification_background_task_scheduler_impl.h",
"notifications/notification_channels_provider_android.cc",
"notifications/notification_channels_provider_android.h",
"notifications/notification_common.cc",
......@@ -920,10 +922,6 @@ jumbo_split_static_library("browser") {
"notifications/persistent_notification_handler.h",
"notifications/platform_notification_service_impl.cc",
"notifications/platform_notification_service_impl.h",
"notifications/scheduler/notification_data.cc",
"notifications/scheduler/notification_data.h",
"notifications/scheduler/schedule_params.cc",
"notifications/scheduler/schedule_params.h",
"notifications/system_notification_helper.cc",
"notifications/system_notification_helper.h",
"ntp_snippets/bookmark_last_visit_updater.cc",
......@@ -2623,6 +2621,8 @@ jumbo_split_static_library("browser") {
"metrics/android_metrics_provider.h",
"metrics/page_load_metrics_provider.cc",
"metrics/page_load_metrics_provider.h",
"notifications/notification_background_task_scheduler_android.cc",
"notifications/notification_background_task_scheduler_android.h",
"notifications/notification_platform_bridge_android.cc",
"notifications/notification_platform_bridge_android.h",
"page_load_metrics/observers/android_page_load_metrics_observer.cc",
......@@ -5038,6 +5038,7 @@ if (is_android) {
"../android/java/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridge.java",
"../android/java/src/org/chromium/chrome/browser/notifications/NotificationSettingsBridge.java",
"../android/java/src/org/chromium/chrome/browser/notifications/NotificationSystemStatusUtil.java",
"../android/java/src/org/chromium/chrome/browser/notifications/scheduler/NotificationSchedulerTask.java",
"../android/java/src/org/chromium/chrome/browser/ntp/ContentSuggestionsNotifier.java",
"../android/java/src/org/chromium/chrome/browser/ntp/ForeignSessionHelper.java",
"../android/java/src/org/chromium/chrome/browser/ntp/LogoBridge.java",
......
......@@ -5,5 +5,6 @@
group("notifications") {
deps = [
"//chrome/browser/notifications/proto",
"//chrome/browser/notifications/scheduler",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_background_task_scheduler_android.h"
#include "base/android/jni_android.h"
#include "base/logging.h"
#include "jni/NotificationSchedulerTask_jni.h"
// static
void JNI_NotificationSchedulerTask_OnStartTask(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jobject>& profile,
const base::android::JavaParamRef<jobject>& callback) {
NOTIMPLEMENTED();
}
// static
jboolean JNI_NotificationSchedulerTask_OnStopTask(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jobject>& profile) {
NOTIMPLEMENTED();
return true;
}
NotificationBackgroundTaskSchedulerAndroid::
NotificationBackgroundTaskSchedulerAndroid() = default;
NotificationBackgroundTaskSchedulerAndroid::
~NotificationBackgroundTaskSchedulerAndroid() = default;
void NotificationBackgroundTaskSchedulerAndroid::Schedule(
base::TimeDelta window_start,
base::TimeDelta window_end) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NotificationSchedulerTask_schedule(
env, base::saturated_cast<jlong>(window_start.InMilliseconds()),
base::saturated_cast<jlong>(window_end.InMilliseconds()));
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_ANDROID_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_ANDROID_H_
#include "base/macros.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/notification_background_task_scheduler.h"
// This class contains:
// 1. Android implementation of NotificationBackgroundTaskScheduler, which
// asks Android API to schedule background task.
// 2. JNI calls to route background task events to native.
// The life cycle of this object is owned by a keyed service in native.
class NotificationBackgroundTaskSchedulerAndroid
: public NotificationBackgroundTaskScheduler {
public:
NotificationBackgroundTaskSchedulerAndroid();
~NotificationBackgroundTaskSchedulerAndroid() override;
private:
// NotificationBackgroundTaskScheduler implementation.
void Schedule(base::TimeDelta window_start,
base::TimeDelta window_end) override;
DISALLOW_COPY_AND_ASSIGN(NotificationBackgroundTaskSchedulerAndroid);
};
#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_ANDROID_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_background_task_scheduler_impl.h"
#include "base/logging.h"
NotificationBackgroundTaskSchedulerImpl::
NotificationBackgroundTaskSchedulerImpl() = default;
NotificationBackgroundTaskSchedulerImpl::
~NotificationBackgroundTaskSchedulerImpl() = default;
void NotificationBackgroundTaskSchedulerImpl::Schedule(
base::TimeDelta window_start,
base::TimeDelta window_end) {
// TODO(xingliu): Implements this for non-Android platform.
NOTIMPLEMENTED();
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_IMPL_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_IMPL_H_
#include "base/macros.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/notification_background_task_scheduler.h"
// Default implementation of NotificatioNBackgroundTaskScheduler.
class NotificationBackgroundTaskSchedulerImpl
: public NotificationBackgroundTaskScheduler {
public:
NotificationBackgroundTaskSchedulerImpl();
~NotificationBackgroundTaskSchedulerImpl() override;
private:
// NotificationBackgroundTaskScheduler implementation.
void Schedule(base::TimeDelta window_start,
base::TimeDelta window_end) override;
DISALLOW_COPY_AND_ASSIGN(NotificationBackgroundTaskSchedulerImpl);
};
#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_IMPL_H_
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/buildflag_header.gni")
if (is_android) {
import("//build/config/android/rules.gni")
}
source_set("scheduler") {
sources = [
"notification_background_task_scheduler.h",
"notification_data.cc",
"notification_data.h",
"schedule_params.cc",
"schedule_params.h",
]
deps = [
"//base",
"//skia",
"//ui/message_center/public/cpp",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_H_
#define CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_H_
#include "base/macros.h"
#include "base/time/time.h"
// Interface to schedule a background task on platform OS to run the
// notification scheduler job.
class NotificationBackgroundTaskScheduler {
public:
// Interface used to handle background task events.
class Handler {
public:
// Called when the background task is started.
virtual void OnStartTask() = 0;
// Called when the background task is stopped by the OS when it wants to
// reallocate resources, our task is not finished yet in this case. The
// handler implementation should explicitly decide whether the task should
// be rescheduled and run later.
virtual void OnStopTask() = 0;
protected:
virtual ~Handler() = default;
private:
DISALLOW_COPY_AND_ASSIGN(Handler);
};
// Schedules a background task in a time window between |window_start| and
// |window_end|.
virtual void Schedule(base::TimeDelta window_start,
base::TimeDelta window_end) = 0;
protected:
NotificationBackgroundTaskScheduler() = default;
virtual ~NotificationBackgroundTaskScheduler() = default;
private:
DISALLOW_COPY_AND_ASSIGN(NotificationBackgroundTaskScheduler);
};
#endif // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_NOTIFICATION_BACKGROUND_TASK_SCHEDULER_H_
......@@ -37,8 +37,9 @@ class BackgroundTaskSchedulerUma {
static final int BACKGROUND_TASK_EXPLORE_SITES_REFRESH = 17;
static final int BACKGROUND_TASK_DOWNLOAD_AUTO_RESUMPTION = 18;
static final int BACKGROUND_TASK_ONE_SHOT_SYNC_WAKE_UP = 19;
static final int BACKGROUND_TASK_NOTIFICATION_SCHEDULER = 20;
// Keep this one at the end and increment appropriately when adding new tasks.
static final int BACKGROUND_TASK_COUNT = 20;
static final int BACKGROUND_TASK_COUNT = 21;
static final String KEY_CACHED_UMA = "bts_cached_uma";
......@@ -282,6 +283,8 @@ class BackgroundTaskSchedulerUma {
return BACKGROUND_TASK_EXPLORE_SITES_REFRESH;
case TaskIds.BACKGROUND_SYNC_ONE_SHOT_JOB_ID:
return BACKGROUND_TASK_ONE_SHOT_SYNC_WAKE_UP;
case TaskIds.NOTIFICATION_SCHEDULER_JOB_ID:
return BACKGROUND_TASK_NOTIFICATION_SCHEDULER;
default:
assert false;
}
......
......@@ -32,6 +32,7 @@ public final class TaskIds {
public static final int DEPRECATED_EXPLORE_SITES_REFRESH_JOB_ID = 100;
public static final int EXPLORE_SITES_REFRESH_JOB_ID = 101;
public static final int BACKGROUND_SYNC_ONE_SHOT_JOB_ID = 102;
public static final int NOTIFICATION_SCHEDULER_JOB_ID = 103;
private TaskIds() {}
}
......@@ -99,7 +99,10 @@ public class BackgroundTaskSchedulerUmaTest {
assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_ONE_SHOT_SYNC_WAKE_UP,
BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
TaskIds.BACKGROUND_SYNC_ONE_SHOT_JOB_ID));
assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_COUNT, 20);
assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_NOTIFICATION_SCHEDULER,
BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
TaskIds.NOTIFICATION_SCHEDULER_JOB_ID));
assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_COUNT, 21);
}
@Test
......
......@@ -3686,6 +3686,7 @@ uploading your change for review. These are checked by presubmit scripts.
<int value="17" label="Explore Sites refresh task"/>
<int value="18" label="Download auto-resumption task"/>
<int value="19" label="One shot Background Sync wake up task"/>
<int value="20" label="Notification scheduler task"/>
</enum>
<enum name="BackgroundTracingState">
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