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

Unified Diff: content/public/android/java/src/org/chromium/content/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: Fix crash caused by zero-length launch window Created 5 years, 3 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: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
index bad267185182d208dcaf14efc6b12dae6704278a..fdba57f2dcebd88ae4d72be422e645bd0e0049f0 100644
--- a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
+++ b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java
@@ -5,9 +5,10 @@
package org.chromium.content.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;
@@ -22,12 +23,12 @@ import org.chromium.base.annotations.JNINamespace;
*/
@JNINamespace("content")
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.
@@ -54,53 +55,20 @@ public class BackgroundSyncLauncher {
}
/**
- * Callback for {@link #shouldLaunchWhenNextOnline}. The run method is invoked on the UI thread.
- */
- public static interface ShouldLaunchCallback { public void run(Boolean shouldLaunch); }
-
- /**
- * Returns whether the browser should be launched when the device next goes online.
- * 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 and will run the callback
- * on the UI thread when complete.
- * @param context The application context.
- * @param sharedPreferences The shared preferences.
- */
- protected static void shouldLaunchWhenNextOnline(
- final Context context, final ShouldLaunchCallback callback) {
- new AsyncTask<Void, Void, Boolean>() {
- @Override
- protected Boolean doInBackground(Void... params) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false);
- }
- @Override
- protected void onPostExecute(Boolean shouldLaunch) {
- callback.run(shouldLaunch);
- }
- }.execute();
- }
-
- /**
* 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.
+ * after the browser closes, by scheduling a one-shot task to launch the browser. On creation
+ * of the {@link BackgroundSyncLauncher} class (on browser start) this is reset to false
+ * (removing any scheduled tasks). This is set by C++ and reset to false each time
+ * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started).
jkarlin 2015/09/04 13:21:35 Any chance you could rephrase the entire above com
iclelland 2015/09/10 20:40:46 Rewritten -- hopefully clearer now.
*/
@VisibleForTesting
@CalledByNative
- protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) {
- new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... params) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- prefs.edit()
- .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, shouldLaunch)
- .apply();
- return null;
- }
- }.execute();
+ protected void setLaunchWhenNextOnline(final boolean shouldLaunch) {
jkarlin 2015/09/04 13:21:35 The name of this function could be clearer, perhap
iclelland 2015/09/10 20:40:46 Done.
+ if (shouldLaunch) {
+ scheduleLaunchTask();
+ } else {
+ removeScheduledTasks();
+ }
}
/**
@@ -112,6 +80,27 @@ public class BackgroundSyncLauncher {
}
private BackgroundSyncLauncher(Context context) {
- setLaunchWhenNextOnline(context, false);
+ mScheduler = GcmNetworkManager.getInstance(context);
+ setLaunchWhenNextOnline(false);
+ }
+
+ private void scheduleLaunchTask() {
+ // 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();
+ mScheduler.schedule(oneoff);
+ }
+
+ private void removeScheduledTasks() {
+ mScheduler.cancelAllTasks(BackgroundSyncLauncherService.class);
}
jkarlin 2015/09/04 13:21:35 I just saw this in the docs: Important: When Goog
iclelland 2015/09/10 20:40:46 I'll add a TODO to handle the update case better.
jkarlin 2015/09/11 18:57:55 We'll also need it for retries as retry has an exp
iclelland 2015/09/24 13:40:53 Done.
-}
+}

Powered by Google App Engine
This is Rietveld 408576698