Index: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java |
index fd92e55d2472212edefede449996a7359c56d661..141590ca5314cc08b4579cbc222ceb7b598d61c1 100644 |
--- a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java |
+++ b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java |
@@ -4,97 +4,43 @@ |
package org.chromium.content.browser; |
-import android.app.IntentService; |
import android.content.Context; |
-import android.content.Intent; |
-import android.net.ConnectivityManager; |
-import android.net.NetworkInfo; |
-import android.support.v4.content.WakefulBroadcastReceiver; |
+ |
+import com.google.android.gms.gcm.GcmNetworkManager; |
+import com.google.android.gms.gcm.GcmTaskService; |
+import com.google.android.gms.gcm.TaskParams; |
import org.chromium.base.Log; |
import org.chromium.base.ThreadUtils; |
-import org.chromium.base.VisibleForTesting; |
import org.chromium.base.annotations.SuppressFBWarnings; |
import org.chromium.base.library_loader.LibraryProcessType; |
import org.chromium.base.library_loader.ProcessInitException; |
import org.chromium.content.app.ContentApplication; |
/** |
- * {@link BackgroundSyncLauncherService} monitors network connectivity and launches |
- * the browser when it goes online if the {@link BackgroundSyncLauncher} requested it. |
+ * {@link BackgroundSyncLauncherService} is scheduled through the {@link GcmNetworkManager} |
+ * when the browser needs to be launched in response to changing network or power conditions. |
*/ |
-public class BackgroundSyncLauncherService extends IntentService { |
+public class BackgroundSyncLauncherService extends GcmTaskService { |
private static final String TAG = "cr.BgSyncLauncher"; |
- /** |
- * Receiver for network connection change broadcasts. If the device is online |
- * and the browser isn't running it starts the BackgroundSyncLauncherService. |
- * The service will then launch the browser if necessary. |
- * |
- * This class is public so that it can be instantiated by the Android runtime. |
- */ |
- public static class Receiver extends WakefulBroadcastReceiver { |
- @Override |
- public void onReceive(Context context, Intent intent) { |
- // If online, the browser isn't running, and the browser has requested |
- // it be launched the next time the device is online, start the browser. |
- if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()) |
- && isOnline(context) && !BackgroundSyncLauncher.hasInstance()) { |
- startService(context); |
- } |
- } |
- |
- @VisibleForTesting |
- protected void startService(Context context) { |
- Intent serviceIntent = new Intent(context, BackgroundSyncLauncherService.class); |
- startWakefulService(context, serviceIntent); |
- } |
- |
- @VisibleForTesting |
- protected boolean isOnline(Context context) { |
- ConnectivityManager connectivityManager = |
- (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
- NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); |
- return networkInfo != null && networkInfo.isConnected(); |
- } |
- } |
- |
- public BackgroundSyncLauncherService() { |
- super("BackgroundSyncLauncherService"); |
- } |
- |
@Override |
- public void onHandleIntent(Intent intent) { |
- try { |
+ public int onRunTask(TaskParams params) { |
+ // Start the browser. The browser's BackgroundSyncManager (for the active profile) will |
+ // start, check the network, and run any necessary sync events. This task runs with a wake |
+ // lock, but has a three minute timeout, so we need to start the browser in its own task. |
+ // TODO(jkarlin): Protect the browser sync event with a wake lock. See crbug.com/486020. |
jkarlin
2015/09/04 13:21:35
This comment is a bit confusing. The "onRunTask" f
iclelland
2015/09/10 20:40:46
Acknowledged. I'll see if I can verify this.
|
+ Log.v(TAG, "Starting Browser after coming online"); |
+ if (!BackgroundSyncLauncher.hasInstance()) { |
jkarlin
2015/09/04 13:30:58
hasInstance is being run on the wrong thread. It's
iclelland
2015/09/10 20:40:46
Done.
|
+ final Context context = this; |
ThreadUtils.runOnUiThread(new Runnable() { |
@Override |
public void run() { |
- onOnline(getApplicationContext()); |
+ launchBrowser(context); |
} |
}); |
- } finally { |
- WakefulBroadcastReceiver.completeWakefulIntent(intent); |
} |
- } |
- |
- private void onOnline(final Context context) { |
- ThreadUtils.assertOnUiThread(); |
- |
- BackgroundSyncLauncher.ShouldLaunchCallback callback = |
- new BackgroundSyncLauncher.ShouldLaunchCallback() { |
- @Override |
- public void run(Boolean shouldLaunch) { |
- if (!shouldLaunch) return; |
- // Start the browser. The browser's BackgroundSyncManager (for the active |
- // profile) will start, check the network, and run any necessary sync |
- // events. It runs without a wake lock. |
- // TODO(jkarlin): Protect the browser sync event with a wake lock. See |
- // crbug.com/486020. |
- Log.v(TAG, "Starting Browser after coming online"); |
- launchBrowser(context); |
- } |
- }; |
- BackgroundSyncLauncher.shouldLaunchWhenNextOnline(context, callback); |
+ return GcmNetworkManager.RESULT_SUCCESS; |
} |
@SuppressFBWarnings("DM_EXIT") |