Index: chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java |
index e0b8ee62732d2ba3dc1eb9db56bc271116f0860a..efad30ae8e4f9382fff931510de73c8577c592a8 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java |
@@ -6,14 +6,17 @@ package org.chromium.chrome.browser.precache; |
import android.content.Context; |
+import org.chromium.base.Log; |
import org.chromium.base.ThreadUtils; |
+import org.chromium.base.VisibleForTesting; |
import org.chromium.base.annotations.CalledByNative; |
import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager; |
- |
-import java.util.concurrent.Callable; |
+import org.chromium.chrome.browser.sync.ProfileSyncService; |
/** Class that interacts with the PrecacheManager to control precache cycles. */ |
public abstract class PrecacheLauncher { |
+ private static final String TAG = "cr.Precache"; |
+ |
/** Pointer to the native PrecacheLauncher object. Set to 0 when uninitialized. */ |
private long mNativePrecacheLauncher; |
@@ -56,19 +59,61 @@ public abstract class PrecacheLauncher { |
} |
/** |
- * Returns true if precaching is enabled by either Finch or the command line flag, and also by |
- * the predictive network actions preference and current network connection type. |
+ * Updates the PrecacheServiceLauncher with whether conditions are right for precaching. All of |
+ * the following must be true: |
+ * |
+ * <ul> |
+ * <li>The predictive network actions preference is enabled.</li> |
+ * <li>The current network type is suitable for predictive network actions.</li> |
+ * <li>Sync is enabled for sessions and it is not encrypted with a secondary passphrase.</li> |
+ * <li>Either the Precache field trial or the precache commandline flag is enabled.</li> |
+ * </ul> |
+ * |
+ * This should be called only after the sync backend has been initialized. Must be called on the |
+ * UI thread. |
+ * |
+ * @param context The application context. |
+ */ |
+ private void updateEnabledSync(Context context) { |
+ PrivacyPreferencesManager privacyPreferencesManager = |
+ PrivacyPreferencesManager.getInstance(context); |
+ |
+ // privacyPreferencesManager.shouldPrerender() and nativeShouldRun() can only be executed on |
+ // the UI thread. |
+ PrecacheServiceLauncher.setIsPrecachingEnabled( |
+ context, privacyPreferencesManager.shouldPrerender() && nativeShouldRun()); |
+ Log.v(TAG, "updateEnabledSync complete"); |
+ } |
+ |
+ /** |
+ * If precaching is enabled, then allow the PrecacheService to be launched and signal Chrome |
+ * when conditions are right to start precaching. If precaching is disabled, prevent the |
+ * PrecacheService from ever starting. |
* |
- * @param privacyPreferencesManager Singleton that manages the prefetch bandwidth preference. |
+ * @param context Any context within the application. |
*/ |
- public static boolean isPrecachingEnabled( |
- final PrivacyPreferencesManager privacyPreferencesManager) { |
- if (!nativeIsPrecachingEnabled()) return false; |
- // privacyPreferencesManager.shouldPrerender() can only be executed on the UI thread. |
- return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean>() { |
+ @VisibleForTesting |
+ void updateEnabled(final Context context) { |
+ Log.v(TAG, "updateEnabled starting"); |
+ ThreadUtils.postOnUiThread(new Runnable() { |
@Override |
- public Boolean call() { |
- return privacyPreferencesManager.shouldPrerender(); |
+ public void run() { |
+ final ProfileSyncService sync = ProfileSyncService.get(context); |
+ |
+ if (mListener == null) { |
+ mListener = new ProfileSyncService.SyncStateChangedListener() { |
+ public void syncStateChanged() { |
+ if (sync.isSyncInitialized()) { |
+ updateEnabledSync(context); |
+ } |
+ } |
+ }; |
+ sync.addSyncStateChangedListener(mListener); |
+ } |
+ |
+ // Call the listener once, in case the sync backend is already initialized. |
+ mListener.syncStateChanged(); |
+ Log.v(TAG, "updateEnabled complete"); |
} |
}); |
} |
@@ -78,18 +123,25 @@ public abstract class PrecacheLauncher { |
* when conditions are right to start precaching. If precaching is disabled, prevent the |
* PrecacheService from ever starting. |
* |
- * @param privacyPreferencesManager Singleton that manages the prefetch bandwidth preference. |
- * @param context The context of the PrecacheServiceLauncher. |
+ * @param context Any context within the application. |
*/ |
- public static void updatePrecachingEnabled(PrivacyPreferencesManager privacyPreferencesManager, |
- Context context) { |
- PrecacheServiceLauncher.setIsPrecachingEnabled(context, |
- isPrecachingEnabled(privacyPreferencesManager)); |
+ public static void updatePrecachingEnabled(final Context context) { |
+ sInstance.updateEnabled(context); |
} |
+ private static final PrecacheLauncher sInstance = new PrecacheLauncher() { |
+ @Override |
+ protected void onPrecacheCompleted() {} |
+ }; |
+ |
+ // Initialized by updateEnabled to call updateEnabledSync when the sync |
+ // backend is initialized. Only accessed on the UI thread. |
+ private ProfileSyncService.SyncStateChangedListener mListener = null; |
+ |
private native long nativeInit(); |
private native void nativeDestroy(long nativePrecacheLauncher); |
private native void nativeStart(long nativePrecacheLauncher); |
private native void nativeCancel(long nativePrecacheLauncher); |
- private static native boolean nativeIsPrecachingEnabled(); |
+ |
+ @VisibleForTesting native boolean nativeShouldRun(); |
} |