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

Unified Diff: content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java

Issue 1214193006: Delay cleanup of moderate bindings when the app goes to background (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Apply Yaron's comments Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/public/android/javatests/src/org/chromium/content/browser/BindingManagerImplTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java b/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
index f8fd519aec937c2222f3d642ae4b585b84e8527f..02635dddce64c7a3ad6bf38e2d906664225673ed 100644
--- a/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
+++ b/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
@@ -9,6 +9,7 @@ import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
+import android.os.Handler;
import android.util.LruCache;
import android.util.SparseArray;
@@ -30,6 +31,10 @@ class BindingManagerImpl implements BindingManager {
// non-low-memory devices).
private static final long DETACH_AS_ACTIVE_HIGH_END_DELAY_MILLIS = 1 * 1000;
+ // Delays used when clearing moderate binding pool when onSentToBackground happens.
+ private static final long MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS = 10 * 1000;
+ private static final long MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS_ON_TESTING = 100;
+
// These fields allow to override the parameters for testing - see
// createBindingManagerForTesting().
private final long mRemoveStrongBindingDelay;
@@ -39,6 +44,9 @@ class BindingManagerImpl implements BindingManager {
extends LruCache<Integer, ManagedConnection> implements ComponentCallbacks2 {
private final float mLowReduceRatio;
private final float mHighReduceRatio;
+ private final Object mDelayedClearerLock = new Object();
+ private Runnable mDelayedClearer;
+ private final Handler mHandler = new Handler(ThreadUtils.getUiThreadLooper());
public ModerateBindingPool(int maxSize, float lowReduceRatio, float highReduceRatio) {
super(maxSize);
@@ -55,6 +63,9 @@ class BindingManagerImpl implements BindingManager {
reduce(mLowReduceRatio);
} else if (level <= TRIM_MEMORY_RUNNING_LOW) {
reduce(mHighReduceRatio);
+ } else if (level == TRIM_MEMORY_UI_HIDDEN) {
+ // This will be handled by |mDelayedClearer|.
+ return;
} else {
evictAll();
}
@@ -117,6 +128,38 @@ class BindingManagerImpl implements BindingManager {
oldValue.removeModerateBinding();
}
}
+
+ void onSentToBackground(final boolean onTesting) {
+ if (size() == 0) return;
+ synchronized (mDelayedClearerLock) {
+ mDelayedClearer = new Runnable() {
+ @Override
+ public void run() {
+ synchronized (mDelayedClearerLock) {
+ if (mDelayedClearer == null) return;
+ mDelayedClearer = null;
+ }
+ Log.i(TAG, "Release moderate connections: %d", size());
+ if (!onTesting) {
+ RecordHistogram.recordCountHistogram(
+ "Android.ModerateBindingCount", size());
+ }
+ evictAll();
+ }
+ };
+ mHandler.postDelayed(mDelayedClearer, onTesting
+ ? MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS_ON_TESTING
+ : MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS);
+ }
+ }
+
+ void onBroughtToForeground() {
+ synchronized (mDelayedClearerLock) {
+ if (mDelayedClearer == null) return;
+ mHandler.removeCallbacks(mDelayedClearer);
+ mDelayedClearer = null;
+ }
+ }
}
private ModerateBindingPool mModerateBindingPool;
@@ -379,14 +422,7 @@ class BindingManagerImpl implements BindingManager {
mBoundForBackgroundPeriod = mLastInForeground;
}
}
- if (mModerateBindingPool != null) {
- Log.i(TAG, "Release moderate connections: %d", mModerateBindingPool.size());
- if (!mOnTesting) {
- RecordHistogram.recordCountHistogram(
- "Android.ModerateBindingCount", mModerateBindingPool.size());
- }
- mModerateBindingPool.evictAll();
- }
+ if (mModerateBindingPool != null) mModerateBindingPool.onSentToBackground(mOnTesting);
}
@Override
@@ -395,6 +431,7 @@ class BindingManagerImpl implements BindingManager {
mBoundForBackgroundPeriod.setBoundForBackgroundPeriod(false);
mBoundForBackgroundPeriod = null;
}
+ if (mModerateBindingPool != null) mModerateBindingPool.onBroughtToForeground();
}
@Override
« no previous file with comments | « no previous file | content/public/android/javatests/src/org/chromium/content/browser/BindingManagerImplTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698