OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.precache; |
| 6 |
| 7 import android.content.Context; |
| 8 |
| 9 import org.chromium.base.CalledByNative; |
| 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager
; |
| 12 |
| 13 import java.util.concurrent.Callable; |
| 14 |
| 15 /** Class that interacts with the PrecacheManager to control precache cycles. */ |
| 16 public abstract class PrecacheLauncher { |
| 17 /** Pointer to the native PrecacheLauncher object. Set to 0 when uninitializ
ed. */ |
| 18 private long mNativePrecacheLauncher; |
| 19 |
| 20 /** Destroy the native PrecacheLauncher, releasing the memory that it was us
ing. */ |
| 21 public void destroy() { |
| 22 if (mNativePrecacheLauncher != 0) { |
| 23 nativeDestroy(mNativePrecacheLauncher); |
| 24 mNativePrecacheLauncher = 0; |
| 25 } |
| 26 } |
| 27 |
| 28 /** Starts a precache cycle. */ |
| 29 public void start() { |
| 30 // Lazily initialize the native PrecacheLauncher. |
| 31 if (mNativePrecacheLauncher == 0) { |
| 32 mNativePrecacheLauncher = nativeInit(); |
| 33 } |
| 34 nativeStart(mNativePrecacheLauncher); |
| 35 } |
| 36 |
| 37 /** Cancel the precache cycle if one is ongoing. */ |
| 38 public void cancel() { |
| 39 // Lazily initialize the native PrecacheLauncher. |
| 40 if (mNativePrecacheLauncher == 0) { |
| 41 mNativePrecacheLauncher = nativeInit(); |
| 42 } |
| 43 nativeCancel(mNativePrecacheLauncher); |
| 44 } |
| 45 |
| 46 /** Called when a precache cycle completes. */ |
| 47 protected abstract void onPrecacheCompleted(); |
| 48 |
| 49 /** |
| 50 * Called by native code when the precache cycle completes. This method exis
ts because an |
| 51 * abstract method cannot be directly called from native. |
| 52 */ |
| 53 @CalledByNative |
| 54 private void onPrecacheCompletedCallback() { |
| 55 onPrecacheCompleted(); |
| 56 } |
| 57 |
| 58 /** |
| 59 * Returns true if precaching is enabled by either Finch or the command line
flag, and also by |
| 60 * the predictive network actions preference and current network connection
type. |
| 61 * |
| 62 * @param privacyPreferencesManager Singleton that manages the prefetch band
width preference. |
| 63 */ |
| 64 public static boolean isPrecachingEnabled( |
| 65 final PrivacyPreferencesManager privacyPreferencesManager) { |
| 66 if (!nativeIsPrecachingEnabled()) return false; |
| 67 // privacyPreferencesManager.shouldPrerender() can only be executed on t
he UI thread. |
| 68 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean
>() { |
| 69 @Override |
| 70 public Boolean call() { |
| 71 return privacyPreferencesManager.shouldPrerender(); |
| 72 } |
| 73 }); |
| 74 } |
| 75 |
| 76 /** |
| 77 * If precaching is enabled, then allow the PrecacheService to be launched a
nd signal Chrome |
| 78 * when conditions are right to start precaching. If precaching is disabled,
prevent the |
| 79 * PrecacheService from ever starting. |
| 80 * |
| 81 * @param privacyPreferencesManager Singleton that manages the prefetch band
width preference. |
| 82 * @param context The context of the PrecacheServiceLauncher. |
| 83 */ |
| 84 public static void updatePrecachingEnabled(PrivacyPreferencesManager privacy
PreferencesManager, |
| 85 Context context) { |
| 86 PrecacheServiceLauncher.setIsPrecachingEnabled(context, |
| 87 isPrecachingEnabled(privacyPreferencesManager)); |
| 88 } |
| 89 |
| 90 private native long nativeInit(); |
| 91 private native void nativeDestroy(long nativePrecacheLauncher); |
| 92 private native void nativeStart(long nativePrecacheLauncher); |
| 93 private native void nativeCancel(long nativePrecacheLauncher); |
| 94 private static native boolean nativeIsPrecachingEnabled(); |
| 95 } |
OLD | NEW |