Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/BackgroundSyncLauncher.java

Issue 1324173002: [Background Sync] Use GcmNetworkManager to start the browser for sync events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@bgsync-fix-background5
Patch Set: Move the BackgroundSyncLauncher into /chrome Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/BackgroundSyncLauncher.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/BackgroundSyncLauncher.java
similarity index 55%
rename from content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
rename to chrome/android/java/src/org/chromium/chrome/browser/BackgroundSyncLauncher.java
index bad267185182d208dcaf14efc6b12dae6704278a..4079bc6001687217638d336d96ffdf17e1b7b794 100644
--- a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/BackgroundSyncLauncher.java
@@ -2,13 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-package org.chromium.content.browser;
+package org.chromium.chrome.browser;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
+import com.google.android.gms.gcm.GcmNetworkManager;
+import com.google.android.gms.gcm.OneoffTask;
+import com.google.android.gms.gcm.Task;
+
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
@@ -20,14 +24,15 @@ import org.chromium.base.annotations.JNINamespace;
*
* Thread model: This class is to be run on the UI thread only.
*/
-@JNINamespace("content")
+@JNINamespace("chrome")
jkarlin 2015/10/14 18:30:28 chrome code exists in the top level namespace
iclelland 2015/10/14 19:31:24 Done.
public class BackgroundSyncLauncher {
static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch_next_online";
-
// The instance of BackgroundSyncLauncher currently owned by a C++
// BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running.
private static BackgroundSyncLauncher sInstance;
+ private GcmNetworkManager mScheduler;
+
/**
* Create a BackgroundSyncLauncher object, which is owned by C++.
* @param context The app context.
@@ -82,15 +87,15 @@ public class BackgroundSyncLauncher {
}
/**
- * Set interest (or disinterest) in launching the browser the next time the device goes online
- * after the browser closes. On creation of the {@link BackgroundSyncLauncher} class (on browser
- * start) this value is reset to false. This is set by C++ and reset to false each time
- * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started). This
- * call is asynchronous.
+ * Manages the scheduled tasks which re-launch the browser when the device next goes online.
+ * This method is called by C++ as background sync registrations are added and removed. When the
+ * {@link BackgroundSyncLauncher} singleton is created (on browser start), this is called to
+ * remove any pre-existing scheduled tasks.
*/
@VisibleForTesting
@CalledByNative
- protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) {
+ protected void launchBrowserWhenNextOnlineIfStopped(
+ final Context context, final boolean shouldLaunch) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
@@ -101,6 +106,11 @@ public class BackgroundSyncLauncher {
return null;
}
}.execute();
+ if (shouldLaunch) {
+ scheduleLaunchTask(mScheduler);
+ } else {
+ removeScheduledTasks(mScheduler);
+ }
}
/**
@@ -112,6 +122,50 @@ public class BackgroundSyncLauncher {
}
private BackgroundSyncLauncher(Context context) {
- setLaunchWhenNextOnline(context, false);
+ mScheduler = GcmNetworkManager.getInstance(context);
+ launchBrowserWhenNextOnlineIfStopped(context, false);
+ }
+
+ private static void scheduleLaunchTask(GcmNetworkManager scheduler) {
+ // Google Play Services may not be up to date, if the application was not installed through
+ // the Play Store. In this case, scheduling the task will fail silently.
+ // TODO(iclelland): Check whether the Play Services client library matches the requirements
+ // in the manifest, and respond appropriately if it does not.
+ OneoffTask oneoff = new OneoffTask.Builder()
+ .setService(BackgroundSyncLauncherService.class)
+ .setTag("BackgroundSync Event")
+ .setExecutionWindow(0, 1)
+ .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
+ .setPersisted(true)
+ .setUpdateCurrent(true)
+ .build();
+ scheduler.schedule(oneoff);
+ }
+
+ private static void removeScheduledTasks(GcmNetworkManager scheduler) {
+ scheduler.cancelAllTasks(BackgroundSyncLauncherService.class);
+ }
+
+ /**
+ * Reschedule any required background sync tasks, if they have been removed due to an
+ * application upgrade.
+ *
+ * This method checks the saved preferences, and reschedules the sync tasks as appropriate
+ * to match the preferences.
+ * This method is static so that it can be run without actually instantiating a
+ * BackgroundSyncLauncher.
+ */
+ protected static void rescheduleTasksOnUpgrade(final Context context) {
+ final GcmNetworkManager scheduler = GcmNetworkManager.getInstance(context);
+ BackgroundSyncLauncher.ShouldLaunchCallback callback =
+ new BackgroundSyncLauncher.ShouldLaunchCallback() {
+ @Override
+ public void run(Boolean shouldLaunch) {
+ if (shouldLaunch) {
+ scheduleLaunchTask(scheduler);
+ }
+ }
+ };
+ BackgroundSyncLauncher.shouldLaunchWhenNextOnline(context, callback);
}
-}
+}

Powered by Google App Engine
This is Rietveld 408576698