| Index: base/android/java/src/org/chromium/base/process_launcher/BindingManagerImpl.java
|
| diff --git a/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java b/base/android/java/src/org/chromium/base/process_launcher/BindingManagerImpl.java
|
| similarity index 90%
|
| rename from content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
|
| rename to base/android/java/src/org/chromium/base/process_launcher/BindingManagerImpl.java
|
| index 583a76c8a508f442f3553b04748dc1e8b4b23016..2a68edc285481cefe13a2df3d4aef27e5d573292 100644
|
| --- a/content/public/android/java/src/org/chromium/content/browser/BindingManagerImpl.java
|
| +++ b/base/android/java/src/org/chromium/base/process_launcher/BindingManagerImpl.java
|
| @@ -2,13 +2,15 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -package org.chromium.content.browser;
|
| +package org.chromium.base.process_launcher;
|
|
|
| import android.annotation.TargetApi;
|
| import android.content.ComponentCallbacks2;
|
| import android.content.Context;
|
| import android.content.res.Configuration;
|
| import android.os.Build;
|
| +import android.os.Handler;
|
| +import android.os.Looper;
|
| import android.util.LruCache;
|
| import android.util.SparseArray;
|
|
|
| @@ -24,8 +26,8 @@ import java.util.Map;
|
| * Manages oom bindings used to bound child services.
|
| * This object must only be accessed from the launcher thread.
|
| */
|
| -class BindingManagerImpl implements BindingManager {
|
| - private static final String TAG = "cr.BindingManager";
|
| +public class BindingManagerImpl implements BindingManager {
|
| + private static final String TAG = "cr_BindingManager";
|
|
|
| // Low reduce ratio of moderate binding.
|
| private static final float MODERATE_BINDING_LOW_REDUCE_RATIO = 0.25f;
|
| @@ -39,11 +41,14 @@ class BindingManagerImpl implements BindingManager {
|
| // Delays used when clearing moderate binding pool when onSentToBackground happens.
|
| private static final long MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS = 10 * 1000;
|
|
|
| + // The handler for the thread this BindingManager should be accessed on.
|
| + private final Handler mHandler;
|
| +
|
| // These fields allow to override the parameters for testing - see
|
| // createBindingManagerForTesting().
|
| private final boolean mIsLowMemoryDevice;
|
|
|
| - private static class ModerateBindingPool
|
| + private class ModerateBindingPool
|
| extends LruCache<Integer, ManagedConnection> implements ComponentCallbacks2 {
|
| private Runnable mDelayedClearer;
|
|
|
| @@ -54,7 +59,7 @@ class BindingManagerImpl implements BindingManager {
|
| @Override
|
| public void onTrimMemory(final int level) {
|
| ThreadUtils.assertOnUiThread();
|
| - LauncherThread.post(new Runnable() {
|
| + mHandler.post(new Runnable() {
|
| @Override
|
| public void run() {
|
| Log.i(TAG, "onTrimMemory: level=%d, size=%d", level, size());
|
| @@ -78,7 +83,7 @@ class BindingManagerImpl implements BindingManager {
|
| @Override
|
| public void onLowMemory() {
|
| ThreadUtils.assertOnUiThread();
|
| - LauncherThread.post(new Runnable() {
|
| + mHandler.post(new Runnable() {
|
| @Override
|
| public void run() {
|
| Log.i(TAG, "onLowMemory: evict %d bindings", size());
|
| @@ -153,12 +158,12 @@ class BindingManagerImpl implements BindingManager {
|
| evictAll();
|
| }
|
| };
|
| - LauncherThread.postDelayed(mDelayedClearer, MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS);
|
| + mHandler.postDelayed(mDelayedClearer, MODERATE_BINDING_POOL_CLEARER_DELAY_MILLIS);
|
| }
|
|
|
| void onBroughtToForeground() {
|
| if (mDelayedClearer != null) {
|
| - LauncherThread.removeCallbacks(mDelayedClearer);
|
| + mHandler.removeCallbacks(mDelayedClearer);
|
| mDelayedClearer = null;
|
| }
|
| }
|
| @@ -230,7 +235,7 @@ class BindingManagerImpl implements BindingManager {
|
| if (mIsLowMemoryDevice) {
|
| doUnbind.run();
|
| } else {
|
| - LauncherThread.postDelayed(doUnbind, DETACH_AS_ACTIVE_HIGH_END_DELAY_MILLIS);
|
| + mHandler.postDelayed(doUnbind, DETACH_AS_ACTIVE_HIGH_END_DELAY_MILLIS);
|
| }
|
| }
|
|
|
| @@ -338,15 +343,15 @@ class BindingManagerImpl implements BindingManager {
|
| * The constructor is private to hide parameters exposed for testing from the regular consumer.
|
| * Use factory methods to create an instance.
|
| */
|
| - private BindingManagerImpl(boolean isLowMemoryDevice, boolean onTesting) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + private BindingManagerImpl(Handler handler, boolean isLowMemoryDevice, boolean onTesting) {
|
| + mHandler = handler;
|
| + checkOnValidThread();
|
| mIsLowMemoryDevice = isLowMemoryDevice;
|
| mOnTesting = onTesting;
|
| }
|
|
|
| - public static BindingManagerImpl createBindingManager() {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| - return new BindingManagerImpl(SysUtils.isLowEndDevice(), false);
|
| + public static BindingManagerImpl createBindingManager(Handler handler) {
|
| + return new BindingManagerImpl(handler, SysUtils.isLowEndDevice(), false);
|
| }
|
|
|
| /**
|
| @@ -354,14 +359,14 @@ class BindingManagerImpl implements BindingManager {
|
| * set to 0, so that the tests don't need to deal with actual waiting.
|
| * @param isLowEndDevice true iff the created instance should apply low-end binding policies
|
| */
|
| - public static BindingManagerImpl createBindingManagerForTesting(boolean isLowEndDevice) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| - return new BindingManagerImpl(isLowEndDevice, true);
|
| + public static BindingManagerImpl createBindingManagerForTesting(
|
| + Handler handler, boolean isLowEndDevice) {
|
| + return new BindingManagerImpl(handler, isLowEndDevice, true);
|
| }
|
|
|
| @Override
|
| public void addNewConnection(int pid, ManagedChildProcessConnection connection) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| // This will reset the previous entry for the pid in the unlikely event of the OS
|
| // reusing renderer pids.
|
| mManagedConnections.put(pid, new ManagedConnection(connection));
|
| @@ -369,7 +374,7 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void setInForeground(int pid, boolean inForeground) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| ManagedConnection managedConnection = mManagedConnections.get(pid);
|
| if (managedConnection == null) {
|
| Log.w(TAG, "Cannot setInForeground() - never saw a connection for the pid: %d", pid);
|
| @@ -387,11 +392,13 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void onDeterminedVisibility(int pid) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| ManagedConnection managedConnection = mManagedConnections.get(pid);
|
| if (managedConnection == null) {
|
| - Log.w(TAG, "Cannot call determinedVisibility() - never saw a connection for the pid: "
|
| - + "%d", pid);
|
| + Log.w(TAG,
|
| + "Cannot call determinedVisibility() - never saw a connection for the pid: "
|
| + + "%d",
|
| + pid);
|
| return;
|
| }
|
|
|
| @@ -400,7 +407,7 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void onSentToBackground() {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| assert mBoundForBackgroundPeriod == null;
|
| // mLastInForeground can be null at this point as the embedding application could be
|
| // used in foreground without spawning any renderers.
|
| @@ -413,7 +420,7 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void onBroughtToForeground() {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| if (mBoundForBackgroundPeriod != null) {
|
| mBoundForBackgroundPeriod.setBoundForBackgroundPeriod(false);
|
| mBoundForBackgroundPeriod = null;
|
| @@ -423,7 +430,7 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void removeConnection(int pid) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| ManagedConnection managedConnection = mManagedConnections.get(pid);
|
| if (managedConnection != null) {
|
| mManagedConnections.remove(pid);
|
| @@ -434,13 +441,13 @@ class BindingManagerImpl implements BindingManager {
|
| /** @return true iff the connection reference is no longer held */
|
| @VisibleForTesting
|
| public boolean isConnectionCleared(int pid) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| return mManagedConnections.get(pid) == null;
|
| }
|
|
|
| @Override
|
| public void startModerateBindingManagement(Context context, int maxSize) {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| if (mIsLowMemoryDevice) return;
|
|
|
| if (mModerateBindingPool == null) {
|
| @@ -456,10 +463,15 @@ class BindingManagerImpl implements BindingManager {
|
|
|
| @Override
|
| public void releaseAllModerateBindings() {
|
| - assert LauncherThread.runningOnLauncherThread();
|
| + checkOnValidThread();
|
| if (mModerateBindingPool != null) {
|
| Log.i(TAG, "Release all moderate bindings: %d", mModerateBindingPool.size());
|
| mModerateBindingPool.evictAll();
|
| }
|
| }
|
| +
|
| + /** Checks whether the caller is running on the thread specified at creation time. */
|
| + private final void checkOnValidThread() {
|
| + assert mHandler.getLooper() == Looper.myLooper();
|
| + }
|
| }
|
|
|