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..cb5c1f6a4aa912f29a0e5336e10fa63920420c8b |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchBackgroundTask.java |
| @@ -0,0 +1,145 @@ |
| +// 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.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| +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; |
| + |
| + public PrefetchBackgroundTask() {} |
| + |
| + /** |
| + * 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. |
|
carlosk
2017/04/19 23:01:38
It is unfortunate that battery level is not an And
dewittj
2017/04/20 21:41:44
Acknowledged.
|
| + */ |
| + @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. |
|
carlosk
2017/04/19 23:01:37
Why this TODO? Do you intend to change the API so
dewittj
2017/04/20 21:41:45
I tried a REALLY absurdly long time, Long.MAX_VALU
carlosk
2017/05/01 17:26:29
I think it is still important to not do work -- es
|
| + 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 antive library and then calls into the PrefetchService, which then manages the |
|
carlosk
2017/04/19 23:01:38
s/antive/native/
dewittj
2017/04/20 21:41:45
Done.
|
| + * 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; |
| + Profile profile = Profile.getLastUsedProfile(); |
| + return nativeStartPrefetchTask(profile); |
| + } |
| + |
| + @Override |
| + public boolean onStopTask(Context context, TaskParameters taskParameters) { |
| + assert mNativeTask != 0; |
|
carlosk
2017/04/19 23:01:38
Duplicate here the taskId assert from onStartTask.
dewittj
2017/04/20 21:41:45
Done.
|
| + |
| + return nativeOnStopTask(mNativeTask); |
| + } |
| + |
| + /** |
| + * Called during construction of the native task. |
| + * |
| + * PrefetchBackgroundTask#onStartTask constructs the native task. |
| + */ |
| + @CalledByNative |
| + private void setNativeTask(long nativeTask) { |
| + mNativeTask = nativeTask; |
| + } |
| + |
| + /** |
| + * Invoked by the native task when it is destroyed. |
| + */ |
| + @CalledByNative |
| + private void doneProcessing(boolean needsReschedule) { |
| + assert mTaskFinishedCallback != null; |
| + mTaskFinishedCallback.taskFinished(needsReschedule); |
| + setNativeTask(0); |
| + } |
| + |
| + private static 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 |
| + // 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); |
| + } |
| + } |
| + |
| + private native boolean nativeStartPrefetchTask(Profile profile); |
| + private native boolean nativeOnStopTask(long nativePrefetchBackgroundTask); |
| +} |