Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 809a55c

Browse files
Peter E ConnCommit Bot
Peter E Conn
authored and
Commit Bot
committed
🛃 Launch CCT on Null Session.
This is an upstream of this fix: GoogleChrome/android-browser-helper@451e37e Bug: 988346 Change-Id: I8e9d1eed51fd445cdef1e7f83776e1d437814028 Reviewed-on: https://chromium-review.googlesource.com/c/custom-tabs-client/+/1844173 Reviewed-by: Peter Conn <[email protected]> Commit-Queue: Peter Conn <[email protected]>
1 parent d9aa8b2 commit 809a55c

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

‎customtabs/src/android/support/customtabs/trusted/TwaLauncher.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ public class TwaLauncher {
5454
@Nullable
5555
private CustomTabsSession mSession;
5656

57-
@Nullable
58-
private Runnable mOnSessionCreatedRunnable;
59-
6057
private boolean mDestroyed;
6158

6259
/**
@@ -151,17 +148,28 @@ private void launchTwa(TrustedWebActivityIntentBuilder twaBuilder,
151148

152149
Runnable onSessionCreatedRunnable = () ->
153150
launchWhenSessionEstablished(twaBuilder, splashScreenStrategy, completionCallback);
151+
154152
if (mSession != null) {
155153
onSessionCreatedRunnable.run();
156154
return;
157155
}
158156

159-
mOnSessionCreatedRunnable = onSessionCreatedRunnable;
157+
Runnable onSessionCreationFailedRunnable = () -> {
158+
// The provider has been unable to create a session for us, we can't launch a
159+
// Trusted Web Activity. We could either exit, forcing the user to try again,
160+
// hopefully successfully this time or we could launch a Custom Tab giving the user
161+
// a subpar experience (compared to a TWA).
162+
// We'll open in a CCT, but pay attention to what users want.
163+
launchCct(twaBuilder, completionCallback);
164+
};
165+
160166
if (mServiceConnection == null) {
161167
mServiceConnection = new TwaCustomTabsServiceConnection();
162168
}
163-
CustomTabsClient.bindCustomTabsService(mContext, mProviderPackage,
164-
mServiceConnection);
169+
170+
mServiceConnection.setSessionCreationRunnables(
171+
onSessionCreatedRunnable, onSessionCreationFailedRunnable);
172+
CustomTabsClient.bindCustomTabsService(mContext, mProviderPackage, mServiceConnection);
165173
}
166174

167175
private void launchWhenSessionEstablished(TrustedWebActivityIntentBuilder twaBuilder,
@@ -211,17 +219,31 @@ public String getProviderPackage() {
211219
}
212220

213221
private class TwaCustomTabsServiceConnection extends CustomTabsServiceConnection {
222+
private Runnable mOnSessionCreatedRunnable;
223+
private Runnable mOnSessionCreationFailedRunnable;
224+
225+
private void setSessionCreationRunnables(@Nullable Runnable onSuccess,
226+
@Nullable Runnable onFailure) {
227+
mOnSessionCreatedRunnable = onSuccess;
228+
mOnSessionCreationFailedRunnable = onFailure;
229+
}
230+
214231
@Override
215232
public void onCustomTabsServiceConnected(ComponentName componentName,
216233
CustomTabsClient client) {
217234
if (TrustedWebUtils.warmupIsRequired(mContext, mProviderPackage)) {
218235
client.warmup(0);
219236
}
220237
mSession = client.newSession(null, mSessionId);
221-
if (mOnSessionCreatedRunnable != null) {
238+
239+
if (mSession != null && mOnSessionCreatedRunnable != null) {
222240
mOnSessionCreatedRunnable.run();
223-
mOnSessionCreatedRunnable = null;
241+
} else if (mSession == null && mOnSessionCreationFailedRunnable != null) {
242+
mOnSessionCreationFailedRunnable.run();
224243
}
244+
245+
mOnSessionCreatedRunnable = null;
246+
mOnSessionCreationFailedRunnable = null;
225247
}
226248

227249
@Override

0 commit comments

Comments
 (0)