Skip to content

Commit

Permalink
Merge pull request #32 from GoogleChrome/NullSession
Browse files Browse the repository at this point in the history
Launch CCT when Session creation fails.
  • Loading branch information
PEConn authored Oct 7, 2019
2 parents 8460510 + 451e37e commit 9f0a0f0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public class TwaLauncherTest {
@Before
public void setUp() {
TwaProviderPicker.restrictToPackageForTesting(mContext.getPackageName());

// TODO(peconn): Maybe make this a test rule?
TestCustomTabsService.setCanCreateSessions(true);
mActivity = mActivityTestRule.getActivity();
mTwaLauncher = new TwaLauncher(mActivity);
}
Expand Down Expand Up @@ -129,6 +132,16 @@ public void fallsBackToCustomTab() {
assertFalse(intent.hasExtra(EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY));
}

@Test
public void fallsBackToCustomTab_whenSessionCreationFails() {
TestCustomTabsService.setCanCreateSessions(false);

Runnable launchRunnable = () -> mTwaLauncher.launch(URL);
TestBrowser browser = getBrowserActivityWhenLaunched(launchRunnable);
assertFalse(browser.getIntent().getBooleanExtra(EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY,
false));
}

@Test
public void customTabFallbackUsesStatusBarColor() {
mEnableComponents.manuallyDisable(TestCustomTabsServiceSupportsTwas.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -41,6 +42,9 @@ public class TestCustomTabsService extends CustomTabsService {
public static final String CALLBACK_BIND_TO_POST_MESSAGE = "BindToPostMessageService";
private static TestCustomTabsService sInstance;

// The TestCustomTabsService isn't necessarily created when we want to set this.
private static final AtomicBoolean sCanCreateSessions = new AtomicBoolean(true);

private final CountDownLatch mFileReceivingLatch = new CountDownLatch(1);

private boolean mPostMessageRequested;
Expand All @@ -64,8 +68,12 @@ protected boolean warmup(long flags) {

@Override
protected boolean newSession(CustomTabsSessionToken sessionToken) {
mSession = sessionToken;
return true;
if (sCanCreateSessions.get()) {
mSession = sessionToken;
return true;
} else {
return false;
}
}

@Override
Expand Down Expand Up @@ -140,4 +148,8 @@ public boolean waitForSplashImageFile(int timeoutMillis) {
return false;
}
}

public static void setCanCreateSessions(boolean canCreateSessions) {
sCanCreateSessions.set(canCreateSessions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public class TwaLauncher {
@Nullable
private CustomTabsSession mSession;

@Nullable
private Runnable mOnSessionCreatedRunnable;

private boolean mDestroyed;

/**
Expand Down Expand Up @@ -154,17 +151,28 @@ private void launchTwa(TrustedWebActivityIntentBuilder twaBuilder,

Runnable onSessionCreatedRunnable = () ->
launchWhenSessionEstablished(twaBuilder, splashScreenStrategy, completionCallback);

if (mSession != null) {
onSessionCreatedRunnable.run();
return;
}

mOnSessionCreatedRunnable = onSessionCreatedRunnable;
Runnable onSessionCreationFailedRunnable = () -> {
// The provider has been unable to create a session for us, we can't launch a
// Trusted Web Activity. We could either exit, forcing the user to try again,
// hopefully successfully this time or we could launch a Custom Tab giving the user
// a subpar experience (compared to a TWA).
// We'll open in a CCT, but pay attention to what users want.
launchCct(twaBuilder, completionCallback);
};

if (mServiceConnection == null) {
mServiceConnection = new TwaCustomTabsServiceConnection();
}
CustomTabsClient.bindCustomTabsService(mContext, mProviderPackage,
mServiceConnection);

mServiceConnection.setSessionCreationRunnables(
onSessionCreatedRunnable, onSessionCreationFailedRunnable);
CustomTabsClient.bindCustomTabsService(mContext, mProviderPackage, mServiceConnection);
}

private void launchWhenSessionEstablished(TrustedWebActivityIntentBuilder twaBuilder,
Expand Down Expand Up @@ -214,6 +222,15 @@ public String getProviderPackage() {
}

private class TwaCustomTabsServiceConnection extends CustomTabsServiceConnection {
private Runnable mOnSessionCreatedRunnable;
private Runnable mOnSessionCreationFailedRunnable;

private void setSessionCreationRunnables(@Nullable Runnable onSuccess,
@Nullable Runnable onFailure) {
mOnSessionCreatedRunnable = onSuccess;
mOnSessionCreationFailedRunnable = onFailure;
}

@Override
public void onCustomTabsServiceConnected(ComponentName componentName,
CustomTabsClient client) {
Expand All @@ -222,10 +239,15 @@ public void onCustomTabsServiceConnected(ComponentName componentName,
client.warmup(0);
}
mSession = client.newSession(null, mSessionId);
if (mOnSessionCreatedRunnable != null) {

if (mSession != null && mOnSessionCreatedRunnable != null) {
mOnSessionCreatedRunnable.run();
mOnSessionCreatedRunnable = null;
} else if (mSession == null && mOnSessionCreationFailedRunnable != null) {
mOnSessionCreationFailedRunnable.run();
}

mOnSessionCreatedRunnable = null;
mOnSessionCreationFailedRunnable = null;
}

@Override
Expand Down

0 comments on commit 9f0a0f0

Please sign in to comment.