| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Bundle; | 8 import android.os.Bundle; |
| 9 | 9 |
| 10 import com.google.android.gms.gcm.GcmNetworkManager; | 10 import com.google.android.gms.gcm.GcmNetworkManager; |
| 11 import com.google.android.gms.gcm.GcmTaskService; | 11 import com.google.android.gms.gcm.GcmTaskService; |
| 12 import com.google.android.gms.gcm.TaskParams; | 12 import com.google.android.gms.gcm.TaskParams; |
| 13 | 13 |
| 14 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
| 15 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 17 import org.chromium.base.annotations.SuppressFBWarnings; | 17 import org.chromium.base.annotations.SuppressFBWarnings; |
| 18 import org.chromium.base.library_loader.LibraryLoader; | 18 import org.chromium.base.library_loader.LibraryLoader; |
| 19 import org.chromium.base.library_loader.ProcessInitException; | 19 import org.chromium.base.library_loader.ProcessInitException; |
| 20 import org.chromium.chrome.browser.download.DownloadResumptionScheduler; | 20 import org.chromium.chrome.browser.download.DownloadResumptionScheduler; |
| 21 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 21 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
| 22 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 22 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
| 23 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; | 23 import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher; |
| 24 import org.chromium.chrome.browser.offlinepages.BackgroundOfflinerTask; | 24 import org.chromium.chrome.browser.offlinepages.BackgroundOfflinerTask; |
| 25 import org.chromium.chrome.browser.offlinepages.BackgroundScheduler; |
| 25 import org.chromium.chrome.browser.offlinepages.BackgroundSchedulerProcessorImpl
; | 26 import org.chromium.chrome.browser.offlinepages.BackgroundSchedulerProcessorImpl
; |
| 26 import org.chromium.chrome.browser.offlinepages.OfflinePageUtils; | 27 import org.chromium.chrome.browser.offlinepages.OfflinePageUtils; |
| 27 import org.chromium.chrome.browser.precache.PrecacheController; | 28 import org.chromium.chrome.browser.precache.PrecacheController; |
| 28 import org.chromium.chrome.browser.precache.PrecacheUMA; | 29 import org.chromium.chrome.browser.precache.PrecacheUMA; |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * {@link ChromeBackgroundService} is scheduled through the {@link GcmNetworkMan
ager} when the | 32 * {@link ChromeBackgroundService} is scheduled through the {@link GcmNetworkMan
ager} when the |
| 32 * browser needs to be launched for scheduled tasks, or in response to changing
network or power | 33 * browser needs to be launched for scheduled tasks, or in response to changing
network or power |
| 33 * conditions. | 34 * conditions. |
| 34 * | 35 * |
| 35 * If HOLD_WAKELOCK is set to true in a bundle in the task params, then the Chro
meBackgroundService | 36 * If HOLD_WAKELOCK is set to true in a bundle in the task params, then the Chro
meBackgroundService |
| 36 * will wait until the task reports done before returning control to the {@link
GcmNetworkManager}. | 37 * will wait until the task reports done before returning control to the {@link
GcmNetworkManager}. |
| 37 * This both guarantees that the wakelock keeps chrome awake and that the GcmNet
workManager does not | 38 * This both guarantees that the wakelock keeps chrome awake and that the GcmNet
workManager does not |
| 38 * start another task in place of the one just started. The GcmNetworkManager c
an start more than | 39 * start another task in place of the one just started. The GcmNetworkManager c
an start more than |
| 39 * one task concurrently, thought, so it does not guarantee that a different tas
k won't start. | 40 * one task concurrently, thought, so it does not guarantee that a different tas
k won't start. |
| 40 */ | 41 */ |
| 41 public class ChromeBackgroundService extends GcmTaskService { | 42 public class ChromeBackgroundService extends GcmTaskService { |
| 42 private static final String TAG = "BackgroundService"; | 43 private static final String TAG = "BackgroundService"; |
| 43 /** Bundle key to use to specify we should block the GcmNetworkManager threa
d until the task on | 44 /** Bundle key to use to specify we should block the GcmNetworkManager threa
d until the task on |
| 44 * the UI thread is done before returning to the GcmNetworkManager. | 45 * the UI thread is done before returning to the GcmNetworkManager. |
| 45 */ | 46 */ |
| 46 public static final String HOLD_WAKELOCK = "HoldWakelock"; | 47 public static final String HOLD_WAKELOCK = "HoldWakelock"; |
| 47 private static final int WAKELOCK_TIMEOUT_SECONDS = 4 * 60; | 48 // GCM will return our wakelock after 3 minutes, we should be a second less
than that. |
| 49 private static final int WAKELOCK_TIMEOUT_SECONDS = 3 * 60 - 1; |
| 48 | 50 |
| 49 private BackgroundOfflinerTask mBackgroundOfflinerTask; | 51 private BackgroundOfflinerTask mBackgroundOfflinerTask; |
| 50 | 52 |
| 51 @Override | 53 @Override |
| 52 @VisibleForTesting | 54 @VisibleForTesting |
| 53 public int onRunTask(final TaskParams params) { | 55 public int onRunTask(final TaskParams params) { |
| 54 final String taskTag = params.getTag(); | 56 final String taskTag = params.getTag(); |
| 55 Log.i(TAG, "[" + taskTag + "] Woken up at " + new java.util.Date().toStr
ing()); | 57 Log.i(TAG, "[" + taskTag + "] Woken up at " + new java.util.Date().toStr
ing()); |
| 56 final ChromeBackgroundServiceWaiter waiter = getWaiterIfNeeded(params.ge
tExtras()); | 58 final ChromeBackgroundServiceWaiter waiter = getWaiterIfNeeded(params.ge
tExtras()); |
| 57 final Context context = this; | 59 final Context context = this; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 220 |
| 219 private void rescheduleSnippetsTasksOnUpgrade() { | 221 private void rescheduleSnippetsTasksOnUpgrade() { |
| 220 if (SnippetsLauncher.shouldRescheduleTasksOnUpgrade()) { | 222 if (SnippetsLauncher.shouldRescheduleTasksOnUpgrade()) { |
| 221 if (!SnippetsLauncher.hasInstance()) { | 223 if (!SnippetsLauncher.hasInstance()) { |
| 222 launchBrowser(this, /*tag=*/""); // The |tag| doesn't matter her
e. | 224 launchBrowser(this, /*tag=*/""); // The |tag| doesn't matter her
e. |
| 223 } | 225 } |
| 224 rescheduleFetching(); | 226 rescheduleFetching(); |
| 225 } | 227 } |
| 226 } | 228 } |
| 227 | 229 |
| 230 protected void rescheduleOfflinePagesTasksOnUpgrade() { |
| 231 BackgroundScheduler.getInstance(this).rescheduleOfflinePagesTasksOnUpgra
de(); |
| 232 } |
| 233 |
| 228 @Override | 234 @Override |
| 229 public void onInitializeTasks() { | 235 public void onInitializeTasks() { |
| 230 rescheduleBackgroundSyncTasksOnUpgrade(); | 236 rescheduleBackgroundSyncTasksOnUpgrade(); |
| 231 reschedulePrecacheTasksOnUpgrade(); | 237 reschedulePrecacheTasksOnUpgrade(); |
| 232 rescheduleSnippetsTasksOnUpgrade(); | 238 rescheduleSnippetsTasksOnUpgrade(); |
| 239 rescheduleOfflinePagesTasksOnUpgrade(); |
| 233 } | 240 } |
| 234 } | 241 } |
| OLD | NEW |