Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheLauncher.java

Issue 1266243003: Tweaks to the precache triggering code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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.precache; 5 package org.chromium.chrome.browser.precache;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 8
9 import org.chromium.base.Log;
9 import org.chromium.base.ThreadUtils; 10 import org.chromium.base.ThreadUtils;
11 import org.chromium.base.VisibleForTesting;
10 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.CalledByNative;
11 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager ; 13 import org.chromium.chrome.browser.preferences.privacy.PrivacyPreferencesManager ;
12 14 import org.chromium.chrome.browser.sync.ProfileSyncService;
13 import java.util.concurrent.Callable;
14 15
15 /** Class that interacts with the PrecacheManager to control precache cycles. */ 16 /** Class that interacts with the PrecacheManager to control precache cycles. */
16 public abstract class PrecacheLauncher { 17 public abstract class PrecacheLauncher {
18 private static final String TAG = "cr.Precache";
19
17 /** Pointer to the native PrecacheLauncher object. Set to 0 when uninitializ ed. */ 20 /** Pointer to the native PrecacheLauncher object. Set to 0 when uninitializ ed. */
18 private long mNativePrecacheLauncher; 21 private long mNativePrecacheLauncher;
19 22
20 /** Destroy the native PrecacheLauncher, releasing the memory that it was us ing. */ 23 /** Destroy the native PrecacheLauncher, releasing the memory that it was us ing. */
21 public void destroy() { 24 public void destroy() {
22 if (mNativePrecacheLauncher != 0) { 25 if (mNativePrecacheLauncher != 0) {
23 nativeDestroy(mNativePrecacheLauncher); 26 nativeDestroy(mNativePrecacheLauncher);
24 mNativePrecacheLauncher = 0; 27 mNativePrecacheLauncher = 0;
25 } 28 }
26 } 29 }
(...skipping 22 matching lines...) Expand all
49 /** 52 /**
50 * Called by native code when the precache cycle completes. This method exis ts because an 53 * Called by native code when the precache cycle completes. This method exis ts because an
51 * abstract method cannot be directly called from native. 54 * abstract method cannot be directly called from native.
52 */ 55 */
53 @CalledByNative 56 @CalledByNative
54 private void onPrecacheCompletedCallback() { 57 private void onPrecacheCompletedCallback() {
55 onPrecacheCompleted(); 58 onPrecacheCompleted();
56 } 59 }
57 60
58 /** 61 /**
59 * Returns true if precaching is enabled by either Finch or the command line flag, and also by 62 * Updates the PrecacheServiceLauncher with whether conditions are right for precaching. All of
60 * the predictive network actions preference and current network connection type. 63 * the following must be true:
61 * 64 *
62 * @param privacyPreferencesManager Singleton that manages the prefetch band width preference. 65 * <ul>
66 * <li>The predictive network actions preference is enabled.</li>
67 * <li>The current network type is suitable for predictive network actions .</li>
68 * <li>Sync is enabled for sessions and it is not encrypted with a seconda ry passphrase.</li>
69 * <li>Either the Precache field trial or the precache commandline flag is enabled.</li>
70 * </ul>
71 *
72 * This should be called only after the sync backend has been initialized. M ust be called on the
73 * UI thread.
74 *
75 * @param context The application context.
63 */ 76 */
64 public static boolean isPrecachingEnabled( 77 private void updateEnabledSync(Context context) {
65 final PrivacyPreferencesManager privacyPreferencesManager) { 78 PrivacyPreferencesManager privacyPreferencesManager =
66 if (!nativeIsPrecachingEnabled()) return false; 79 PrivacyPreferencesManager.getInstance(context);
67 // privacyPreferencesManager.shouldPrerender() can only be executed on t he UI thread. 80
68 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean >() { 81 // privacyPreferencesManager.shouldPrerender() and nativeShouldRun() can only be executed on
82 // the UI thread.
83 PrecacheServiceLauncher.setIsPrecachingEnabled(
84 context, privacyPreferencesManager.shouldPrerender() && nativeSh ouldRun());
85 Log.v(TAG, "updateEnabledSync complete");
86 }
87
88 /**
89 * If precaching is enabled, then allow the PrecacheService to be launched a nd signal Chrome
90 * when conditions are right to start precaching. If precaching is disabled, prevent the
91 * PrecacheService from ever starting.
92 *
93 * @param context Any context within the application.
94 */
95 @VisibleForTesting
96 void updateEnabled(final Context context) {
97 Log.v(TAG, "updateEnabled starting");
98 ThreadUtils.postOnUiThread(new Runnable() {
69 @Override 99 @Override
70 public Boolean call() { 100 public void run() {
71 return privacyPreferencesManager.shouldPrerender(); 101 final ProfileSyncService sync = ProfileSyncService.get(context);
102
103 if (mListener == null) {
104 mListener = new ProfileSyncService.SyncStateChangedListener( ) {
105 public void syncStateChanged() {
106 if (sync.isSyncInitialized()) {
107 updateEnabledSync(context);
108 }
109 }
110 };
111 sync.addSyncStateChangedListener(mListener);
112 }
113
114 // Call the listener once, in case the sync backend is already i nitialized.
115 mListener.syncStateChanged();
116 Log.v(TAG, "updateEnabled complete");
72 } 117 }
73 }); 118 });
74 } 119 }
75 120
76 /** 121 /**
77 * If precaching is enabled, then allow the PrecacheService to be launched a nd signal Chrome 122 * 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 123 * when conditions are right to start precaching. If precaching is disabled, prevent the
79 * PrecacheService from ever starting. 124 * PrecacheService from ever starting.
80 * 125 *
81 * @param privacyPreferencesManager Singleton that manages the prefetch band width preference. 126 * @param context Any context within the application.
82 * @param context The context of the PrecacheServiceLauncher.
83 */ 127 */
84 public static void updatePrecachingEnabled(PrivacyPreferencesManager privacy PreferencesManager, 128 public static void updatePrecachingEnabled(final Context context) {
85 Context context) { 129 sInstance.updateEnabled(context);
86 PrecacheServiceLauncher.setIsPrecachingEnabled(context,
87 isPrecachingEnabled(privacyPreferencesManager));
88 } 130 }
89 131
132 private static final PrecacheLauncher sInstance = new PrecacheLauncher() {
133 @Override
134 protected void onPrecacheCompleted() {}
135 };
136
137 // Initialized by updateEnabled to call updateEnabledSync when the sync
138 // backend is initialized. Only accessed on the UI thread.
139 private ProfileSyncService.SyncStateChangedListener mListener = null;
140
90 private native long nativeInit(); 141 private native long nativeInit();
91 private native void nativeDestroy(long nativePrecacheLauncher); 142 private native void nativeDestroy(long nativePrecacheLauncher);
92 private native void nativeStart(long nativePrecacheLauncher); 143 private native void nativeStart(long nativePrecacheLauncher);
93 private native void nativeCancel(long nativePrecacheLauncher); 144 private native void nativeCancel(long nativePrecacheLauncher);
94 private static native boolean nativeIsPrecachingEnabled(); 145
146 @VisibleForTesting native boolean nativeShouldRun();
95 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698