Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eb61ec6d196b10172eece09053fe0c1586ce3be5 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java |
| @@ -0,0 +1,161 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.offlinepages.prefetch; |
| + |
| +import android.content.Context; |
| + |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.base.Log; |
| +import org.chromium.base.VisibleForTesting; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.base.annotations.SuppressFBWarnings; |
| +import org.chromium.base.library_loader.LibraryProcessType; |
| +import org.chromium.base.library_loader.ProcessInitException; |
| +import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| +import org.chromium.components.background_task_scheduler.BackgroundTask; |
| +import org.chromium.components.background_task_scheduler.BackgroundTask.TaskFinishedCallback; |
| +import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler; |
| +import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory; |
| +import org.chromium.components.background_task_scheduler.TaskIds; |
| +import org.chromium.components.background_task_scheduler.TaskInfo; |
| +import org.chromium.components.background_task_scheduler.TaskParameters; |
| +import org.chromium.content.browser.BrowserStartupController; |
| + |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * Handles servicing background offlining requests. |
| + * |
| + * Can schedule or cancel tasks, and handles the actual initialization that |
| + * happens when a task fires. |
| + */ |
| +@JNINamespace("offline_pages::prefetch") |
| +public class PrefetchBackgroundTask implements BackgroundTask { |
| + private static final String TAG = "OPPrefetchBGTask"; |
| + |
| + private long mNativeTask = 0; |
| + private TaskFinishedCallback mTaskFinishedCallback = null; |
| + |
| + private Profile mProfile; |
| + |
| + public PrefetchBackgroundTask() { |
| + mProfile = Profile.getLastUsedProfile(); |
| + } |
| + |
| + public PrefetchBackgroundTask(Profile profile) { |
| + mProfile = profile; |
| + } |
| + |
| + /** |
| + * Schedules the default 'NWake' task for the prefetching service. |
| + * |
| + * This task will only be scheduled on a good network type. |
| + * TODO(dewittj): Handle skipping work if the battery percentage is too low. |
| + */ |
| + @CalledByNative |
| + public static void scheduleTask() { |
| + BackgroundTaskScheduler scheduler = BackgroundTaskSchedulerFactory.getScheduler(); |
| + TaskInfo taskInfo = |
| + TaskInfo.createOneOffTask(TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID, |
| + PrefetchBackgroundTask.class, |
| + // Minimum time to wait |
| + TimeUnit.MINUTES.toMillis(15), |
| + // Maximum time to wait. After this interval the event will fire |
| + // regardless of whether the conditions are right. |
| + // TODO(dewittj): Remove the maximum time to wait. |
|
Dmitry Titov
2017/05/01 18:14:56
If this is a reasonable upper limit, then why have
dewittj
2017/05/01 22:16:35
Done.
|
| + TimeUnit.DAYS.toMillis(7)) |
| + .setRequiredNetworkType(TaskInfo.NETWORK_TYPE_UNMETERED) |
| + .setIsPersisted(true) |
| + .setUpdateCurrent(true) |
| + .build(); |
| + scheduler.schedule(ContextUtils.getApplicationContext(), taskInfo); |
| + } |
| + |
| + /** |
| + * Cancels the default 'NWake' task for the prefetching service. |
| + */ |
| + @CalledByNative |
| + public static void cancelTask() { |
| + BackgroundTaskScheduler scheduler = BackgroundTaskSchedulerFactory.getScheduler(); |
| + scheduler.cancel( |
| + ContextUtils.getApplicationContext(), TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID); |
| + } |
| + |
| + /** |
| + * Initializer that runs when the task wakes up Chrome. |
| + * |
| + * Loads the native library and then calls into the PrefetchService, which then manages the |
| + * lifetime of this task. |
| + */ |
| + @Override |
| + public boolean onStartTask( |
| + Context context, TaskParameters taskParameters, TaskFinishedCallback callback) { |
| + assert taskParameters.getTaskId() == TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID; |
| + assert mNativeTask == 0; |
| + |
| + // Ensures that native potion of the browser is launched. |
| + launchBrowserIfNecessary(context); |
| + |
| + mTaskFinishedCallback = callback; |
| + return nativeStartPrefetchTask(mProfile); |
| + } |
| + |
| + @Override |
| + public boolean onStopTask(Context context, TaskParameters taskParameters) { |
| + assert taskParameters.getTaskId() == TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID; |
| + assert mNativeTask != 0; |
|
Dmitry Titov
2017/05/01 18:14:56
lets replace assert with 'if' here, above as well.
dewittj
2017/05/01 22:16:34
What should we return here? the return value is wh
Dmitry Titov
2017/05/01 23:07:43
onStartTask we should return false which means "re
dewittj
2017/05/02 22:42:22
Done.
|
| + |
| + return nativeOnStopTask(mNativeTask); |
| + } |
| + |
| + /** |
| + * Called during construction of the native task. |
| + * |
| + * PrefetchBackgroundTask#onStartTask constructs the native task. |
| + */ |
| + @VisibleForTesting |
| + @CalledByNative |
| + void setNativeTask(long nativeTask) { |
| + mNativeTask = nativeTask; |
| + } |
| + |
| + /** |
| + * Invoked by the native task when it is destroyed. |
| + */ |
| + @VisibleForTesting |
| + @CalledByNative |
| + void doneProcessing(boolean needsReschedule) { |
| + assert mTaskFinishedCallback != null; |
| + mTaskFinishedCallback.taskFinished(needsReschedule); |
| + setNativeTask(0); |
| + } |
| + |
| + @VisibleForTesting |
| + @SuppressFBWarnings("DM_EXIT") |
| + void launchBrowserIfNecessary(Context context) { |
| + if (BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER) |
| + .isStartupSuccessfullyCompleted()) { |
| + return; |
| + } |
| + |
| + // TODO(fgorski): This method is taken from ChromeBackgroundService as a local fix and will |
|
Dmitry Titov
2017/05/01 18:14:56
as a local fix for what exactly? Also, is Backgrou
dewittj
2017/05/01 22:16:34
The BackgroundTaskScheduler will eventually be abl
|
| + // be removed with BackgroundTaskScheduler supporting GcmNetworkManager scheduling. |
| + try { |
| + ChromeBrowserInitializer.getInstance(context).handleSynchronousStartup(); |
| + } catch (ProcessInitException e) { |
| + Log.e(TAG, "ProcessInitException while starting the browser process."); |
| + // Since the library failed to initialize nothing in the application can work, so kill |
| + // the whole application not just the activity. |
| + System.exit(-1); |
| + } |
| + } |
| + |
| + @VisibleForTesting |
| + native boolean nativeStartPrefetchTask(Profile profile); |
| + @VisibleForTesting |
| + native boolean nativeOnStopTask(long nativePrefetchBackgroundTask); |
| +} |