Commit e77291a7 authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

android: Remove initial binding

Use moderate binding instead which has the exact same flags, and also
supports refcounting.

Bug: 813232
Change-Id: I22a56345a835c5916e6604bfb7cb2fc5cae6ceb5
Reviewed-on: https://chromium-review.googlesource.com/1033799Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554795}
parent 41a2a781
...@@ -199,14 +199,12 @@ public class ChildProcessConnection { ...@@ -199,14 +199,12 @@ public class ChildProcessConnection {
// Process ID of the corresponding child process. // Process ID of the corresponding child process.
private int mPid; private int mPid;
// Inital moderate binding.
private final ChildServiceConnection mInitialBinding;
// Strong binding will make the service priority equal to the priority of the activity. // Strong binding will make the service priority equal to the priority of the activity.
private final ChildServiceConnection mStrongBinding; private final ChildServiceConnection mStrongBinding;
// Moderate binding will make the service priority equal to the priority of a visible process // Moderate binding will make the service priority equal to the priority of a visible process
// while the app is in the foreground. // while the app is in the foreground.
// This is also used as the initial binding before any priorities are set.
private final ChildServiceConnection mModerateBinding; private final ChildServiceConnection mModerateBinding;
// Low priority binding maintained in the entire lifetime of the connection, i.e. between calls // Low priority binding maintained in the entire lifetime of the connection, i.e. between calls
...@@ -287,7 +285,6 @@ public class ChildProcessConnection { ...@@ -287,7 +285,6 @@ public class ChildProcessConnection {
int defaultFlags = Context.BIND_AUTO_CREATE int defaultFlags = Context.BIND_AUTO_CREATE
| (bindAsExternalService ? Context.BIND_EXTERNAL_SERVICE : 0); | (bindAsExternalService ? Context.BIND_EXTERNAL_SERVICE : 0);
mInitialBinding = connectionFactory.createConnection(intent, defaultFlags, delegate);
mModerateBinding = connectionFactory.createConnection(intent, defaultFlags, delegate); mModerateBinding = connectionFactory.createConnection(intent, defaultFlags, delegate);
mStrongBinding = connectionFactory.createConnection( mStrongBinding = connectionFactory.createConnection(
intent, defaultFlags | Context.BIND_IMPORTANT, delegate); intent, defaultFlags | Context.BIND_IMPORTANT, delegate);
...@@ -520,7 +517,13 @@ public class ChildProcessConnection { ...@@ -520,7 +517,13 @@ public class ChildProcessConnection {
assert isRunningOnLauncherThread(); assert isRunningOnLauncherThread();
assert !mUnbound; assert !mUnbound;
boolean success = useStrongBinding ? mStrongBinding.bind() : mInitialBinding.bind(); boolean success;
if (useStrongBinding) {
success = mStrongBinding.bind();
} else {
mModerateBindingCount++;
success = mModerateBinding.bind();
}
if (!success) return false; if (!success) return false;
updateWaivedBoundOnlyState(); updateWaivedBoundOnlyState();
...@@ -537,7 +540,6 @@ public class ChildProcessConnection { ...@@ -537,7 +540,6 @@ public class ChildProcessConnection {
mStrongBinding.unbind(); mStrongBinding.unbind();
mWaivedBinding.unbind(); mWaivedBinding.unbind();
mModerateBinding.unbind(); mModerateBinding.unbind();
mInitialBinding.unbind();
// Note that we don't update the waived bound only state here as to preserve the state when // Note that we don't update the waived bound only state here as to preserve the state when
// disconnected. // disconnected.
...@@ -548,28 +550,11 @@ public class ChildProcessConnection { ...@@ -548,28 +550,11 @@ public class ChildProcessConnection {
} }
} }
public boolean isInitialBindingBound() {
assert isRunningOnLauncherThread();
return mInitialBinding.isBound();
}
public void addInitialBinding() {
assert isRunningOnLauncherThread();
mInitialBinding.bind();
updateWaivedBoundOnlyState();
}
public boolean isStrongBindingBound() { public boolean isStrongBindingBound() {
assert isRunningOnLauncherThread(); assert isRunningOnLauncherThread();
return mStrongBinding.isBound(); return mStrongBinding.isBound();
} }
public void removeInitialBinding() {
assert isRunningOnLauncherThread();
mInitialBinding.unbind();
updateWaivedBoundOnlyState();
}
public void addStrongBinding() { public void addStrongBinding() {
assert isRunningOnLauncherThread(); assert isRunningOnLauncherThread();
if (!isConnected()) { if (!isConnected()) {
...@@ -650,11 +635,10 @@ public class ChildProcessConnection { ...@@ -650,11 +635,10 @@ public class ChildProcessConnection {
return mKilledByUs; return mKilledByUs;
} }
// Should be called every time the mInitialBinding or mStrongBinding are bound/unbound. // Should be called every time the mModerateBinding or mStrongBinding are bound/unbound.
private void updateWaivedBoundOnlyState() { private void updateWaivedBoundOnlyState() {
if (!mUnbound) { if (!mUnbound) {
mWaivedBoundOnly = !mInitialBinding.isBound() && !mStrongBinding.isBound() mWaivedBoundOnly = !mStrongBinding.isBound() && !mModerateBinding.isBound();
&& !mModerateBinding.isBound();
} }
} }
......
...@@ -193,7 +193,7 @@ public class ChildProcessConnectionTest { ...@@ -193,7 +193,7 @@ public class ChildProcessConnectionTest {
ChildProcessConnection connection = createDefaultTestConnection(); ChildProcessConnection connection = createDefaultTestConnection();
assertNotNull(mFirstServiceConnection); assertNotNull(mFirstServiceConnection);
connection.start(false /* useStrongBinding */, mServiceCallback); connection.start(false /* useStrongBinding */, mServiceCallback);
Assert.assertTrue(connection.isInitialBindingBound()); Assert.assertTrue(connection.isModerateBindingBound());
Assert.assertFalse(connection.didOnServiceConnectedForTesting()); Assert.assertFalse(connection.didOnServiceConnectedForTesting());
verify(mServiceCallback, never()).onChildStarted(); verify(mServiceCallback, never()).onChildStarted();
verify(mServiceCallback, never()).onChildStartFailed(any()); verify(mServiceCallback, never()).onChildStartFailed(any());
...@@ -216,7 +216,7 @@ public class ChildProcessConnectionTest { ...@@ -216,7 +216,7 @@ public class ChildProcessConnectionTest {
doReturn(false).when(mFirstServiceConnection).bind(); doReturn(false).when(mFirstServiceConnection).bind();
connection.start(false /* useStrongBinding */, mServiceCallback); connection.start(false /* useStrongBinding */, mServiceCallback);
Assert.assertFalse(connection.isInitialBindingBound()); Assert.assertFalse(connection.isModerateBindingBound());
Assert.assertFalse(connection.didOnServiceConnectedForTesting()); Assert.assertFalse(connection.didOnServiceConnectedForTesting());
verify(mServiceCallback, never()).onChildStarted(); verify(mServiceCallback, never()).onChildStarted();
verify(mServiceCallback, never()).onChildStartFailed(any()); verify(mServiceCallback, never()).onChildStartFailed(any());
......
...@@ -484,7 +484,7 @@ public class ChildProcessLauncherHelper { ...@@ -484,7 +484,7 @@ public class ChildProcessLauncherHelper {
manager.increaseRecency(connection); manager.increaseRecency(connection);
} }
} }
if (!mBoostPriorityForPendingViews && boostForPendingViews) connection.addInitialBinding(); if (!mBoostPriorityForPendingViews && boostForPendingViews) connection.addModerateBinding();
if (mImportance != importance) { if (mImportance != importance) {
switch (importance) { switch (importance) {
case ChildProcessImportance.NORMAL: case ChildProcessImportance.NORMAL:
...@@ -506,7 +506,7 @@ public class ChildProcessLauncherHelper { ...@@ -506,7 +506,7 @@ public class ChildProcessLauncherHelper {
if (mForeground && !foreground) connection.removeStrongBinding(); if (mForeground && !foreground) connection.removeStrongBinding();
if (mBoostPriorityForPendingViews && !boostForPendingViews) { if (mBoostPriorityForPendingViews && !boostForPendingViews) {
connection.removeInitialBinding(); connection.removeModerateBinding();
} }
if (mRanking != null) { if (mRanking != null) {
......
...@@ -57,7 +57,7 @@ public class ChildProcessLauncherIntegrationTest { ...@@ -57,7 +57,7 @@ public class ChildProcessLauncherIntegrationTest {
} }
private static class TestChildProcessConnection extends ChildProcessConnection { private static class TestChildProcessConnection extends ChildProcessConnection {
private RuntimeException mRemovedBothInitialAndStrongBinding; private RuntimeException mRemovedBothModerateAndStrongBinding;
public TestChildProcessConnection(Context context, ComponentName serviceName, public TestChildProcessConnection(Context context, ComponentName serviceName,
boolean bindToCaller, boolean bindAsExternalService, boolean bindToCaller, boolean bindAsExternalService,
...@@ -69,30 +69,31 @@ public class ChildProcessLauncherIntegrationTest { ...@@ -69,30 +69,31 @@ public class ChildProcessLauncherIntegrationTest {
@Override @Override
protected void unbind() { protected void unbind() {
super.unbind(); super.unbind();
if (mRemovedBothInitialAndStrongBinding == null) { if (mRemovedBothModerateAndStrongBinding == null) {
mRemovedBothInitialAndStrongBinding = new RuntimeException("unbind"); mRemovedBothModerateAndStrongBinding = new RuntimeException("unbind");
} }
} }
@Override @Override
public void removeInitialBinding() { public void removeModerateBinding() {
super.removeInitialBinding(); super.removeModerateBinding();
if (mRemovedBothInitialAndStrongBinding == null && !isStrongBindingBound()) { if (mRemovedBothModerateAndStrongBinding == null && !isStrongBindingBound()) {
mRemovedBothInitialAndStrongBinding = new RuntimeException("removeInitialBinding"); mRemovedBothModerateAndStrongBinding =
new RuntimeException("removeModerateBinding");
} }
} }
@Override @Override
public void removeStrongBinding() { public void removeStrongBinding() {
super.removeStrongBinding(); super.removeStrongBinding();
if (mRemovedBothInitialAndStrongBinding == null && !isInitialBindingBound()) { if (mRemovedBothModerateAndStrongBinding == null && !isModerateBindingBound()) {
mRemovedBothInitialAndStrongBinding = new RuntimeException("removeStrongBinding"); mRemovedBothModerateAndStrongBinding = new RuntimeException("removeStrongBinding");
} }
} }
public void throwIfDroppedBothInitialAndStrongBinding() { public void throwIfDroppedBothModerateAndStrongBinding() {
if (mRemovedBothInitialAndStrongBinding != null) { if (mRemovedBothModerateAndStrongBinding != null) {
throw mRemovedBothInitialAndStrongBinding; throw mRemovedBothModerateAndStrongBinding;
} }
} }
} }
...@@ -119,7 +120,7 @@ public class ChildProcessLauncherIntegrationTest { ...@@ -119,7 +120,7 @@ public class ChildProcessLauncherIntegrationTest {
@Override @Override
public void run() { public void run() {
Assert.assertEquals(1, connections.size()); Assert.assertEquals(1, connections.size());
connections.get(0).throwIfDroppedBothInitialAndStrongBinding(); connections.get(0).throwIfDroppedBothModerateAndStrongBinding();
} }
}); });
...@@ -130,7 +131,7 @@ public class ChildProcessLauncherIntegrationTest { ...@@ -130,7 +131,7 @@ public class ChildProcessLauncherIntegrationTest {
public void run() { public void run() {
Assert.assertEquals(2, connections.size()); Assert.assertEquals(2, connections.size());
// connections.get(0).didDropBothInitialAndImportantBindings(); // connections.get(0).didDropBothInitialAndImportantBindings();
connections.get(1).throwIfDroppedBothInitialAndStrongBinding(); connections.get(1).throwIfDroppedBothModerateAndStrongBinding();
} }
}); });
} }
......
...@@ -47,9 +47,8 @@ public class BindingManagerImplTest { ...@@ -47,9 +47,8 @@ public class BindingManagerImplTest {
null /* serviceBundle */); null /* serviceBundle */);
connection.setPid(pid); connection.setPid(pid);
connection.start(false /* useStrongBinding */, null /* serviceCallback */); connection.start(false /* useStrongBinding */, null /* serviceCallback */);
if (manager != null) { manager.increaseRecency(connection);
manager.increaseRecency(connection); connection.removeModerateBinding(); // Remove initial binding.
}
return connection; return connection;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment