Skip to content

Commit

Permalink
[android] Remove GcmNetworkManager based BackgroundTaskScheduler
Browse files Browse the repository at this point in the history
When Android Oreo was introduced, we implemented a way to use a
shared API for scheduling background tasks across various versions of
Android. From Android Marshmallow and above we used the system
JobScheduler, and for earlier versions of Android we used
GcmNetworkManager provided by Google Play Services.

Now that support for Android Marshmallow and earlier versions of
Android is deprecated, it is unnecessary to keep this code around, since
it is not in use.

This CL removes the GCM based task scheduler and related functionality,
as well as other code that provides functionality for versions of
Android we no longer support.

The persistence layer for storing the last OS version we have migrated
to is deleted as part of this CL, since it was only used to reschedule
tasks across GcmNetworkManager -> JobScheduler migration boundaries.

The delegation interface itself is not removed since the functionality
for exact scheduling time using the AlarmManager is kept around.

Bug: 1295935, 1385893
Change-Id: Ic71c430c340be3c3f95f3d31bc0cc99638e9d5a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4114948
Reviewed-by: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Tommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1091961}
  • Loading branch information
tommynyquist authored and Chromium LUCI CQ committed Jan 12, 2023
1 parent ee93e9c commit 5b665ff
Show file tree
Hide file tree
Showing 32 changed files with 47 additions and 1,503 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.chromium.base.compat;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
Expand All @@ -26,7 +25,6 @@
import android.webkit.WebViewClient;

import androidx.annotation.RequiresApi;
import androidx.annotation.RequiresPermission;

/**
* Utility class to use new APIs that were added in M (API level 23). These need to exist in a
Expand Down Expand Up @@ -140,13 +138,6 @@ public static int getActionButton(MotionEvent event) {
return event.getActionButton();
}

/** See {@link AlarmManager#setExactAndAllowWhileIdle(int, long, PendingIntent) }. */
@RequiresPermission(android.Manifest.permission.SCHEDULE_EXACT_ALARM)
public static void setAlarmManagerExactAndAllowWhileIdle(AlarmManager alarmManager, int type,
long triggerAtMillis, PendingIntent pendingIntent) {
alarmManager.setExactAndAllowWhileIdle(type, triggerAtMillis, pendingIntent);
}

/** See {@link Display.Mode#getPhysicalWidth() }. */
public static int getModePhysicalWidth(Display.Mode mode) {
return mode.getPhysicalWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1298,14 +1298,6 @@
<action android:name="org.chromium.intent.action.INSTALL_WEB_APK"/>
</intent-filter> # DIFF-ANCHOR: 875093ee
</service> # DIFF-ANCHOR: 878b448b
<service # DIFF-ANCHOR: 8fc286d0
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskGcmTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter> # DIFF-ANCHOR: 99686c45
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
</intent-filter> # DIFF-ANCHOR: 99686c45
</service> # DIFF-ANCHOR: 8fc286d0
<service # DIFF-ANCHOR: a550decc
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskJobService"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,6 @@
<action android:name="org.chromium.intent.action.INSTALL_WEB_APK"/>
</intent-filter> # DIFF-ANCHOR: 875093ee
</service> # DIFF-ANCHOR: 878b448b
<service # DIFF-ANCHOR: 8fc286d0
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskGcmTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter> # DIFF-ANCHOR: 99686c45
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
</intent-filter> # DIFF-ANCHOR: 99686c45
</service> # DIFF-ANCHOR: 8fc286d0
<service # DIFF-ANCHOR: a550decc
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskJobService"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,14 +1154,6 @@
<action android:name="org.chromium.intent.action.INSTALL_WEB_APK"/>
</intent-filter> # DIFF-ANCHOR: 875093ee
</service> # DIFF-ANCHOR: 878b448b
<service # DIFF-ANCHOR: 8fc286d0
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskGcmTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter> # DIFF-ANCHOR: 99686c45
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
</intent-filter> # DIFF-ANCHOR: 99686c45
</service> # DIFF-ANCHOR: 8fc286d0
<service # DIFF-ANCHOR: a550decc
android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskJobService"
android:exported="false"
Expand Down
9 changes: 0 additions & 9 deletions chrome/android/java/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,6 @@ by a child template that "extends" this file.
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>

<!-- Background Task Scheduler GCM task service -->
<service android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskGcmTaskService"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
</intent-filter>
</service>

<!-- Background Task Scheduler alarm receiver -->
<receiver android:name="org.chromium.components.background_task_scheduler.internal.BackgroundTaskBroadcastReceiver"
android:exported="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ public void run() {
});

deferredStartupHandler.addDeferredTask(
()
-> BackgroundTaskSchedulerFactory.getScheduler().checkForOSUpgrade(
ContextUtils.getApplicationContext()));
() -> BackgroundTaskSchedulerFactory.getScheduler().doMaintenance());

deferredStartupHandler.addDeferredTask(
() -> MediaViewerUtils.updateMediaLauncherActivityEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public boolean isScheduled(Context context, int taskId) {
}

@Override
public void checkForOSUpgrade(Context context) {}
public void doMaintenance() {}

@Override
public void reschedule(Context context) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean isScheduled(Context context, int taskId) {
}

@Override
public void checkForOSUpgrade(Context context) {}
public void doMaintenance() {}

@Override
public void reschedule(Context context) {}
Expand Down
8 changes: 0 additions & 8 deletions components/background_task_scheduler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,20 @@ if (is_android) {
"internal/android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerJobServiceTest.java",
"internal/android/java/src/org/chromium/components/background_task_scheduler/internal/BundleToPersistableBundleConverterTest.java",
"internal/android/java/src/org/chromium/components/background_task_scheduler/internal/MockBackgroundTaskSchedulerDelegate.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskGcmTaskServiceTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskJobServiceTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerAlarmManagerTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerGcmNetworkManagerTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerImplTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerPrefsTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerUmaTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/BroadcastReceiverRobolectricTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/ExtrasToProtoConverterTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/ShadowGcmNetworkManager.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/TaskInfoTest.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/TestBackgroundTask.java",
"internal/android/junit/src/org/chromium/components/background_task_scheduler/internal/TestBackgroundTaskFactory.java",
]

deps = [
":background_task_scheduler_task_ids_java",
"$google_play_services_package:google_play_services_auth_base_java",
"$google_play_services_package:google_play_services_base_java",
"$google_play_services_package:google_play_services_basement_java",
"$google_play_services_package:google_play_services_gcm_java",
"$google_play_services_package:google_play_services_tasks_java",
"internal:internal_java",
"internal:proto_java",
"//base:base_java",
Expand Down
34 changes: 4 additions & 30 deletions components/background_task_scheduler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,14 @@ Java, the exact same API is available in C++ as well.

## Background

In Android M+ it is encouraged to use `JobScheduler` for all background jobs,
In Android it is encouraged to use `JobScheduler` for all background jobs,
instead of using things like `IntentService` or polling using alarms. Using the
system API is beneficial as it has a full view of what goes on in the system and
can schedule jobs accordingly.

However, that leaves an API gap for Android L and below. Prior to Android L, the
`JobScheduler` API was not available at all. It was introduced in Android L; but
is not recommended on that platform, because it limits task execution time to 1
minute. This is not really practically usable. For example, merely setting up a
network connection will often burn through much of that budget. Android M+
extends this execution time limit to 10 minutes.

For these older platforms, we can leverage the GcmNetworkManager API provided by
Google Play services to implement a suitable replacement for the JobScheduler
API. The `background_task_scheduler` component provides a new framework for use
within Chromium to schedule and execute background jobs using the frameworks
available on a given version of Android. The public API of the framework is
similar to that of the Android `JobScheduler`, but it is backed by either the
system `JobScheduler` API or by GcmNetworkManager. What service is used to back
the framework remains a black box to callers of the API.

In practice, we prefer to use system APIs, since they do not require including
external libraries, which would bloat the APK size of Chrome and add unnecessary
complexity. Thus, the GcmNetworkManager is only used when the system API is not
available (or available but not considered stable enough). That is, the
JobScheduler API is used on Android M+; and the GcmNetworkManager is used
otherwise.

> NOTE: Some of the pre-M devices do not include Google Play services and
> therefore remain unsupported by `background_task_scheduler`.
> Ultimately, this component hopes to provide a full compatibility
> layer on top of `JobScheduler`. However, until that is implemented, please be
> thoughtful about whether this component provides the coverage that your
> background task needs.
The `background_task_scheduler` component provides a framework for use within
Chromium to schedule and execute background jobs using the system API. The
API of the framework is similar to that of the Android `JobScheduler`.

## What is a task

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

/**
* A BackgroundTaskScheduler is used to schedule jobs that run in the background.
* It is backed by system APIs ({@link android.app.job.JobScheduler}) on newer platforms
* and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older platforms.
* It is backed by the system API ({@link android.app.job.JobScheduler}).
*
* To get an instance of this class, use {@link BackgroundTaskSchedulerFactory#getScheduler()}.
*/
Expand Down Expand Up @@ -47,14 +46,11 @@ public interface BackgroundTaskScheduler {
boolean isScheduled(Context context, int taskId);

/**
* Checks whether OS was upgraded and triggers rescheduling if it is necessary.
* Rescheduling is necessary if type of background task scheduler delegate is different for a
* new version of the OS.
*
* @param context the current context.
* Performs data migrations and flushes cached UMA data. Must not be invoked until native has
* been loaded.
*/
@MainThread
void checkForOSUpgrade(Context context);
void doMaintenance();

/**
* Reschedules all the tasks currently scheduler through BackgroundTaskSheduler.
Expand Down
6 changes: 0 additions & 6 deletions components/background_task_scheduler/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ if (is_android) {
android_library("internal_java") {
sources = [
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskBroadcastReceiver.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskGcmTaskService.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskJobService.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerAlarmManager.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerDelegate.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerFactoryInternal.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerGcmNetworkManager.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerImpl.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerJobService.java",
"android/java/src/org/chromium/components/background_task_scheduler/internal/BackgroundTaskSchedulerPrefs.java",
Expand All @@ -27,10 +25,6 @@ if (is_android) {

deps = [
":proto_java",
"$google_play_services_package:google_play_services_base_java",
"$google_play_services_package:google_play_services_basement_java",
"$google_play_services_package:google_play_services_gcm_java",
"$google_play_services_package:google_play_services_tasks_java",
"//base:base_java",
"//build/android:build_java",
"//components/background_task_scheduler:background_task_scheduler_task_ids_java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import android.os.SystemClock;
import android.text.format.DateUtils;
Expand Down Expand Up @@ -39,7 +37,8 @@ public class BackgroundTaskBroadcastReceiver extends BroadcastReceiver {
private static final String WAKELOCK_TAG = "Chromium:" + TAG;

// Wakelock is only held for 3 minutes, in order to be consistent with the restrictions of
// the GcmTaskService:
// the GcmTaskService, which was used in earlier versions of Chrome on pre-Android M versions
// of Android:
// https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmTaskService.
// Here the waiting is done for only 90% of this time.
private static final long MAX_TIMEOUT_MS = 162 * DateUtils.SECOND_IN_MILLIS;
Expand Down Expand Up @@ -169,13 +168,8 @@ private boolean networkRequirementsMet(Context context, int taskId,
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = ApiHelperForM.getActiveNetwork(connectivityManager);
if (requiredNetworkType == TaskInfo.NetworkType.ANY) return (network != null);
} else {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (requiredNetworkType == TaskInfo.NetworkType.ANY) return (networkInfo != null);
}
Network network = ApiHelperForM.getActiveNetwork(connectivityManager);
if (requiredNetworkType == TaskInfo.NetworkType.ANY) return (network != null);

return (!connectivityManager.isActiveNetworkMetered());
}
Expand Down
Loading

0 comments on commit 5b665ff

Please sign in to comment.