summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortwifkak <twifkak@chromium.org>2015-10-02 13:18:06 -0700
committerCommit bot <commit-bot@chromium.org>2015-10-02 20:18:59 +0000
commit83df8cc17d2fb6d6936b4841b624f86306829750 (patch)
treeede3e8227338168e95c06b985632c31654c283fa
parentf7530c056309d44cee18533276f4237f97205686 (diff)
downloadchromium_src-83df8cc17d2fb6d6936b4841b624f86306829750.zip
chromium_src-83df8cc17d2fb6d6936b4841b624f86306829750.tar.gz
chromium_src-83df8cc17d2fb6d6936b4841b624f86306829750.tar.bz2
Allow precache to run when device is interactive.
This is part of an attempt to increase the triggering rate for precache. Since only one URL is fetched at a time, it should have minimal impact on interactivity. BUG=309216 Review URL: https://codereview.chromium.org/1374043004 Cr-Commit-Position: refs/heads/master@{#352112}
-rw-r--r--chrome/android/java/src/org/chromium/chrome/browser/precache/FailureReason.java3
-rw-r--r--chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheService.java6
-rw-r--r--chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java43
-rw-r--r--chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncherTest.java16
-rw-r--r--chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceTest.java27
-rw-r--r--components/precache/android/java/src/org/chromium/components/precache/DeviceState.java5
-rw-r--r--components/precache/android/javatests/src/org/chromium/components/precache/MockDeviceState.java11
7 files changed, 36 insertions, 75 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/precache/FailureReason.java b/chrome/android/java/src/org/chromium/chrome/browser/precache/FailureReason.java
index 1ce2f2c..df2ee85 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/precache/FailureReason.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/precache/FailureReason.java
@@ -26,8 +26,7 @@ enum FailureReason {
/** DeviceState#isWifiAvailable() returns false. */
NO_WIFI(5),
- /** DeviceState#isInteractive() returns true. */
- SCREEN_ON(6),
+ // Deprecated: SCREEN_ON(6).
/** PrecacheServiceLauncher#timeSinceLastPrecacheMs() is too recent. */
NOT_ENOUGH_TIME_SINCE_LAST_PRECACHE(7),
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheService.java b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheService.java
index b1cc381..e245d85 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheService.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheService.java
@@ -63,9 +63,8 @@ public class PrecacheService extends Service {
private final BroadcastReceiver mDeviceStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
- if (sIsPrecaching && (mDeviceState.isInteractive(context)
- || !mDeviceState.isPowerConnected(context)
- || !mDeviceState.isWifiAvailable(context))) {
+ if (sIsPrecaching && (!mDeviceState.isPowerConnected(context)
+ || !mDeviceState.isWifiAvailable(context))) {
cancelPrecaching();
}
}
@@ -195,7 +194,6 @@ public class PrecacheService extends Service {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
- filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mDeviceStateReceiver, filter);
}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
index e698dcd..5895d0c 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncher.java
@@ -146,7 +146,6 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
reasons.addAll(mPrecacheLauncher.failureReasons());
if (!mDeviceState.isPowerConnected(context)) reasons.add(FailureReason.NO_POWER);
if (!mDeviceState.isWifiAvailable(context)) reasons.add(FailureReason.NO_WIFI);
- if (mDeviceState.isInteractive(context)) reasons.add(FailureReason.SCREEN_ON);
if (timeSinceLastPrecacheMs(context) < WAIT_UNTIL_NEXT_PRECACHE_MS) {
reasons.add(FailureReason.NOT_ENOUGH_TIME_SINCE_LAST_PRECACHE);
}
@@ -183,50 +182,38 @@ public class PrecacheServiceLauncher extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
resetLastPrecacheMsIfDeviceRebooted(context);
+ // TODO(twifkak): Make these triggering conditions (power, screen, wifi, time) controllable
+ // via variation parameters. Also change the cancel conditions in PrecacheService.
boolean isPowerConnected = mDeviceState.isPowerConnected(context);
boolean isWifiAvailable = mDeviceState.isWifiAvailable(context);
- boolean isInteractive = mDeviceState.isInteractive(context);
- boolean areConditionsGoodForPrecaching =
- isPowerConnected && isWifiAvailable && !isInteractive;
+ boolean areConditionsGoodForPrecaching = isPowerConnected && isWifiAvailable;
boolean hasEnoughTimePassedSinceLastPrecache =
timeSinceLastPrecacheMs(context) >= WAIT_UNTIL_NEXT_PRECACHE_MS;
-
// Do nothing if precaching is disabled.
if (!isPrecachingEnabled(context.getApplicationContext())) {
recordFailureReasons(context);
return;
}
- // Only start precaching when an alarm action is received. This is to prevent situations
- // such as power being connected, precaching starting, then precaching being immediately
- // canceled because the screen turns on in response to power being connected.
- if (ACTION_ALARM.equals(intent.getAction())
- && areConditionsGoodForPrecaching
- && hasEnoughTimePassedSinceLastPrecache) {
- recordFailureReasons(context); // Record success.
- acquireWakeLockAndStartService(context);
- } else {
- if (isPowerConnected && isWifiAvailable) {
+ if (areConditionsGoodForPrecaching) {
+ if (hasEnoughTimePassedSinceLastPrecache) {
+ recordFailureReasons(context); // Record success.
+ acquireWakeLockAndStartService(context);
+ } else {
+ // If we're just waiting for for enough time to pass after Wi-Fi or power has been
+ // connected, then set an alarm for the next time to check the device state.
// Don't call record failure reasons when setting an alarm to retry. These cases are
// uninteresting.
-
- // If we're just waiting for non-interactivity (e.g., the screen to be off), or for
- // enough time to pass after Wi-Fi or power has been connected, then set an alarm
- // for the next time to check the device state. We can't receive SCREEN_ON/OFF
- // intents as is done for detecting changes in power and connectivity, because
- // SCREEN_ON/OFF intents are only delivered to BroadcastReceivers that are
- // registered dynamically in code, but the PrecacheServiceLauncher is registered in
- // the Android manifest.
setAlarm(context,
Math.max(INTERACTIVE_STATE_POLLING_PERIOD_MS,
WAIT_UNTIL_NEXT_PRECACHE_MS - timeSinceLastPrecacheMs(context)));
- } else {
- recordFailureReasons(context);
- // If the device doesn't have connected power or doesn't have Wi-Fi, then there's no
- // point in setting an alarm.
- cancelAlarm(context);
}
+ } else {
+ recordFailureReasons(context);
+ // If the device doesn't have connected power or doesn't have Wi-Fi, then there's no
+ // point in setting an alarm.
+ cancelAlarm(context);
}
}
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncherTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncherTest.java
index 81f0d28..80d6c46 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncherTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceLauncherTest.java
@@ -115,7 +115,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
setPrecachingEnabled(false);
mPrecacheLauncher.setFailureReasons(EnumSet.of(FailureReason.NATIVE_SHOULD_RUN_IS_FALSE));
mLauncher.setDeviceState(new MockDeviceState(0 /* stickyBatteryStatus */,
- true /* powerIsConnected */, false /* interactive */, true /* wifiIsAvailable */));
+ true /* powerIsConnected */, true /* wifiIsAvailable */));
mLauncher.setElapsedRealtime(PrecacheServiceLauncher.WAIT_UNTIL_NEXT_PRECACHE_MS);
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
@@ -130,7 +130,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
@SmallTest
@Feature({"Precache"})
public void testGoodConditions() {
- mLauncher.setDeviceState(new MockDeviceState(0, true, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, true, true));
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
@@ -143,7 +143,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
@SmallTest
@Feature({"Precache"})
public void testNotEnoughTimeButGoodConditionsOtherwise() {
- mLauncher.setDeviceState(new MockDeviceState(0, true, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, true, true));
setLastPrecacheMs(0L);
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
@@ -158,7 +158,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
@SmallTest
@Feature({"Precache"})
public void testEnoughTimeButNoPower() {
- mLauncher.setDeviceState(new MockDeviceState(0, false, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, false, true));
mLauncher.setElapsedRealtime(PrecacheServiceLauncher.WAIT_UNTIL_NEXT_PRECACHE_MS);
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
@@ -177,7 +177,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
FailureReason.SYNC_NOT_INITIALIZED,
FailureReason.PRERENDER_PRIVACY_PREFERENCE_NOT_ENABLED,
FailureReason.NATIVE_SHOULD_RUN_IS_FALSE));
- mLauncher.setDeviceState(new MockDeviceState(0, false, true, false));
+ mLauncher.setDeviceState(new MockDeviceState(0, false, false));
setLastPrecacheMs(0L);
PrecacheService.setIsPrecaching(true);
@@ -193,7 +193,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
@Feature({"Precache"})
public void testStateIsResetAfterReboot() {
// 1. Precache is successfully run at time X.
- mLauncher.setDeviceState(new MockDeviceState(0, true, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, true, true));
mLauncher.setElapsedRealtime(PrecacheServiceLauncher.WAIT_UNTIL_NEXT_PRECACHE_MS);
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
@@ -204,7 +204,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
mLauncher.mStartPrecacheCalled = false;
// 2. The device loses power and reboots.
- mLauncher.setDeviceState(new MockDeviceState(0, false /* power */, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, false /* power */, true));
mLauncher.setElapsedRealtime(0);
// 3. Some intent is triggered, which allows the reciever to notice the reboot and reset
@@ -216,7 +216,7 @@ public class PrecacheServiceLauncherTest extends InstrumentationTestCase {
mLauncher.mCancelAlarmCalled = false;
// 4. Precache is successfully run at time X+1.
- mLauncher.setDeviceState(new MockDeviceState(0, true, false, true));
+ mLauncher.setDeviceState(new MockDeviceState(0, true, true));
mLauncher.setElapsedRealtime(PrecacheServiceLauncher.WAIT_UNTIL_NEXT_PRECACHE_MS + 1);
mLauncher.onReceive(mContext, new Intent(PrecacheServiceLauncher.ACTION_ALARM));
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceTest.java
index 1e4ab38..239a835 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/precache/PrecacheServiceTest.java
@@ -56,10 +56,10 @@ public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> {
getService().setPrecacheLauncher(mPrecacheLauncher);
}
- private void startAndChangeDeviceState(MockPrecacheService service, boolean powerIsConnected,
- boolean isInteractive, boolean wifiIsAvailable) {
+ private void startAndChangeDeviceState(
+ MockPrecacheService service, boolean powerIsConnected, boolean wifiIsAvailable) {
AdvancedMockContext context = new AdvancedMockContext();
- getService().setDeviceState(new MockDeviceState(0, true, false, true));
+ getService().setDeviceState(new MockDeviceState(0, true, true));
assertFalse("Precaching should not be in progress initially", service.isPrecaching());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
@@ -70,8 +70,7 @@ public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> {
}
});
assertTrue("Precaching should be in progress after start", service.isPrecaching());
- getService().setDeviceState(
- new MockDeviceState(0, powerIsConnected, isInteractive, wifiIsAvailable));
+ getService().setDeviceState(new MockDeviceState(0, powerIsConnected, wifiIsAvailable));
service.getDeviceStateReceiver().onReceive(context, new Intent());
}
@@ -80,28 +79,18 @@ public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> {
@Feature({"Precache"})
public void testPrecacheWhenPowerDisconnects() {
setupService();
- startAndChangeDeviceState(getService(), false, false, true);
+ startAndChangeDeviceState(getService(), false, true);
assertFalse("Precaching should not be in progress when power is disconnected",
getService().isPrecaching());
}
- /** Tests that the device becoming interactive stops a precache. */
- @SmallTest
- @Feature({"Precache"})
- public void testPrecacheWhenDeviceBecomesInteractive() {
- setupService();
- startAndChangeDeviceState(getService(), true, true, true);
- assertFalse("Precaching should not be in progress when the device is interactive",
- getService().isPrecaching());
- }
-
/** Tests that going off of Wi-Fi stops a precache. */
@SmallTest
@Feature({"Precache"})
public void testPrecacheWhenNoLongerWifi() {
setupService();
- startAndChangeDeviceState(getService(), true, true, true);
+ startAndChangeDeviceState(getService(), true, false);
assertFalse("Precaching should not be in progress when the network is not Wi-Fi",
getService().isPrecaching());
}
@@ -111,7 +100,7 @@ public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> {
@Feature({"Precache"})
public void testPrecacheWhenPrerequisitesStillMet() {
setupService();
- startAndChangeDeviceState(getService(), true, false, true);
+ startAndChangeDeviceState(getService(), true, true);
assertTrue("Precaching should be in progress", getService().isPrecaching());
}
@@ -123,7 +112,7 @@ public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> {
@Feature({"Precache"})
public void testPrecacheCompleted() {
setupService();
- startAndChangeDeviceState(getService(), true, false, true);
+ startAndChangeDeviceState(getService(), true, true);
assertTrue("Precaching should be in progress", getService().isPrecaching());
mPrecacheLauncher.onPrecacheCompleted(true);
assertFalse("Precaching should not be in progress after completion",
diff --git a/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java b/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
index 70995ca..4342537 100644
--- a/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
+++ b/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
@@ -10,7 +10,6 @@ import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
-import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.VisibleForTesting;
/**
@@ -60,10 +59,6 @@ public class DeviceState {
|| status == BatteryManager.BATTERY_STATUS_FULL;
}
- public boolean isInteractive(Context context) {
- return ApiCompatibilityUtils.isInteractive(context);
- }
-
/** @return whether the currently active network is Wi-Fi, not roaming, and not metered. */
public boolean isWifiAvailable(Context context) {
NetworkInfoDelegate networkInfo =
diff --git a/components/precache/android/javatests/src/org/chromium/components/precache/MockDeviceState.java b/components/precache/android/javatests/src/org/chromium/components/precache/MockDeviceState.java
index 61f601f..1a69141 100644
--- a/components/precache/android/javatests/src/org/chromium/components/precache/MockDeviceState.java
+++ b/components/precache/android/javatests/src/org/chromium/components/precache/MockDeviceState.java
@@ -12,14 +12,12 @@ import android.content.Context;
public class MockDeviceState extends DeviceState {
private final int mStickyBatteryStatus;
private final boolean mPowerIsConnected;
- private final boolean mInteractive;
private final boolean mWifiIsAvailable;
- public MockDeviceState(int stickyBatteryStatus, boolean powerIsConnected,
- boolean interactive, boolean wifiIsAvailable) {
+ public MockDeviceState(
+ int stickyBatteryStatus, boolean powerIsConnected, boolean wifiIsAvailable) {
mStickyBatteryStatus = stickyBatteryStatus;
mPowerIsConnected = powerIsConnected;
- mInteractive = interactive;
mWifiIsAvailable = wifiIsAvailable;
}
@@ -34,11 +32,6 @@ public class MockDeviceState extends DeviceState {
}
@Override
- public boolean isInteractive(Context context) {
- return mInteractive;
- }
-
- @Override
public boolean isWifiAvailable(Context context) {
return mWifiIsAvailable;
}