Commit d0bcf099 authored by Pavel Shmakov's avatar Pavel Shmakov Committed by Commit Bot

Extract CustomTabActivityClientConnectionKeeper

The last piece of code that begs to be extracted from CustomTabActivity
one that keeps the client connection alive and reports the status.

Change-Id: I1e24fea219dcaa72ed0a07d3da66f131c9fa5235
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1798705
Commit-Queue: Pavel Shmakov <pshmakov@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696016}
parent 92b2ec59
......@@ -376,6 +376,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/customtabs/CloseButtonNavigator.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabActivityClientConnectionKeeper.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabActivityLifecycleUmaTracker.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabAppMenuPropertiesDelegate.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabBottomBarDelegate.java",
......
......@@ -19,7 +19,6 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Browser;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
......@@ -55,7 +54,6 @@ import org.chromium.chrome.browser.customtabs.features.CustomTabNavigationBarCon
import org.chromium.chrome.browser.customtabs.features.toolbar.CustomTabToolbarCoordinator;
import org.chromium.chrome.browser.dependency_injection.ChromeActivityCommonsModule;
import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor;
import org.chromium.chrome.browser.gsa.GSAState;
import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.night_mode.NightModeStateProvider;
import org.chromium.chrome.browser.night_mode.NightModeUtils;
......@@ -63,7 +61,6 @@ import org.chromium.chrome.browser.night_mode.PowerSavingModeMonitor;
import org.chromium.chrome.browser.night_mode.SystemNightModeMonitor;
import org.chromium.chrome.browser.page_info.PageInfoController;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabAssociatedApp;
import org.chromium.chrome.browser.tabmodel.ChromeTabCreator;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorImpl;
......@@ -73,9 +70,6 @@ import org.chromium.chrome.browser.util.IntentUtils;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.WebContents;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.browser.customtabs.CustomTabsSessionToken;
......@@ -83,21 +77,8 @@ import androidx.browser.customtabs.CustomTabsSessionToken;
* The activity for custom tabs. It will be launched on top of a client's task.
*/
public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent> {
// For CustomTabs.ConnectionStatusOnReturn, see histograms.xml. Append only.
@IntDef({ConnectionStatus.DISCONNECTED, ConnectionStatus.DISCONNECTED_KEEP_ALIVE,
ConnectionStatus.CONNECTED, ConnectionStatus.CONNECTED_KEEP_ALIVE})
@Retention(RetentionPolicy.SOURCE)
private @interface ConnectionStatus {
int DISCONNECTED = 0;
int DISCONNECTED_KEEP_ALIVE = 1;
int CONNECTED = 2;
int CONNECTED_KEEP_ALIVE = 3;
int NUM_ENTRIES = 4;
}
private CustomTabIntentDataProvider mIntentDataProvider;
private CustomTabsSessionToken mSession;
private CustomTabBottomBarDelegate mBottomBarDelegate;
private CustomTabActivityTabController mTabController;
private CustomTabActivityTabProvider mTabProvider;
private CustomTabActivityTabFactory mTabFactory;
......@@ -112,8 +93,6 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
// change the package name.
private boolean mShouldOverridePackage;
private boolean mIsKeepAlive;
private final CustomTabsConnection mConnection = CustomTabsConnection.getInstance();
@Nullable
......@@ -175,20 +154,6 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
"MobileStartup.IntentToCreationTime.CustomTabs", timeMs);
}
@Override
public void onStart() {
super.onStart();
mIsKeepAlive = mConnection.keepAliveForSession(
mIntentDataProvider.getSession(), mIntentDataProvider.getKeepAliveServiceIntent());
}
@Override
public void onStop() {
super.onStop();
mConnection.dontKeepAliveForSession(mIntentDataProvider.getSession());
mIsKeepAlive = false;
}
@Override
public void performPreInflationStartup() {
// Parse the data from the Intent before calling super to allow the Intent to customize
......@@ -238,8 +203,7 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
// Setting task title and icon to be null will preserve the client app's title and icon.
ApiCompatibilityUtils.setTaskDescription(this, null, null,
mIntentDataProvider.getToolbarColor());
mBottomBarDelegate = getComponent().resolveBottomBarDelegate();
mBottomBarDelegate.showBottomBarIfNecessary();
getComponent().resolveBottomBarDelegate().showBottomBarIfNecessary();
}
@Override
......@@ -305,15 +269,11 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
// getIntent() returns the same intent as before.
setIntent(originalIntent);
handleNewIntent(intent);
}
private boolean handleNewIntent(Intent intent) {
// Color scheme doesn't matter here: currently we don't support updating UI using Intents.
CustomTabIntentDataProvider dataProvider = new CustomTabIntentDataProvider(intent, this,
CustomTabsIntent.COLOR_SCHEME_LIGHT);
return mCustomTabIntentHandler.onNewIntent(dataProvider);
mCustomTabIntentHandler.onNewIntent(dataProvider);
}
private void resetPostMessageHandlersForCurrentSession() {
......@@ -428,38 +388,6 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
return mNavigationController.navigateOnBack();
}
private void recordClientConnectionStatus() {
String packageName =
(getActivityTab() == null) ? null : TabAssociatedApp.getAppId(getActivityTab());
if (packageName == null) return; // No associated package
boolean isConnected =
packageName.equals(mConnection.getClientPackageNameForSession(mSession));
int status = -1;
if (isConnected) {
if (mIsKeepAlive) {
status = ConnectionStatus.CONNECTED_KEEP_ALIVE;
} else {
status = ConnectionStatus.CONNECTED;
}
} else {
if (mIsKeepAlive) {
status = ConnectionStatus.DISCONNECTED_KEEP_ALIVE;
} else {
status = ConnectionStatus.DISCONNECTED;
}
}
assert status >= 0;
if (GSAState.isGsaPackageName(packageName)) {
RecordHistogram.recordEnumeratedHistogram("CustomTabs.ConnectionStatusOnReturn.GSA",
status, ConnectionStatus.NUM_ENTRIES);
} else {
RecordHistogram.recordEnumeratedHistogram("CustomTabs.ConnectionStatusOnReturn.NonGSA",
status, ConnectionStatus.NUM_ENTRIES);
}
}
@Override
public boolean canShowAppMenu() {
if (getActivityTab() == null || !mToolbarCoordinator.toolbarIsInitialized()) return false;
......@@ -642,9 +570,11 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
mTabFactory = component.resolveTabFactory();
mToolbarCoordinator = component.resolveToolbarCoordinator();
component.resolveUmaTracker();
CustomTabActivityClientConnectionKeeper connectionKeeper =
component.resolveConnectionKeeper();
mNavigationController = component.resolveNavigationController();
mNavigationController.setFinishHandler((reason) -> {
if (reason == USER_NAVIGATION) recordClientConnectionStatus();
if (reason == USER_NAVIGATION) connectionKeeper.recordClientConnectionStatus();
handleFinishAndClose();
});
mCustomTabIntentHandler = component.resolveIntentHandler();
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.customtabs;
import android.support.annotation.IntDef;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.gsa.GSAState;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.StartStopWithNativeObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabAssociatedApp;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;
import androidx.browser.customtabs.CustomTabsSessionToken;
/**
* Keeps the client app alive, when possible, while CustomTabActivity is in foreground (see
* {@link CustomTabsConnection#keepAliveForSession}).
*/
@ActivityScope
public class CustomTabActivityClientConnectionKeeper implements StartStopWithNativeObserver {
@IntDef({ConnectionStatus.DISCONNECTED, ConnectionStatus.DISCONNECTED_KEEP_ALIVE,
ConnectionStatus.CONNECTED, ConnectionStatus.CONNECTED_KEEP_ALIVE})
@Retention(RetentionPolicy.SOURCE)
private @interface ConnectionStatus {
int DISCONNECTED = 0;
int DISCONNECTED_KEEP_ALIVE = 1;
int CONNECTED = 2;
int CONNECTED_KEEP_ALIVE = 3;
int NUM_ENTRIES = 4;
}
private final CustomTabsConnection mConnection;
private final CustomTabIntentDataProvider mIntentDataProvider;
private final CustomTabActivityTabProvider mTabProvider;
private boolean mIsKeepingAlive;
@Inject
public CustomTabActivityClientConnectionKeeper(CustomTabsConnection connection,
CustomTabIntentDataProvider intentDataProvider,
ActivityLifecycleDispatcher lifecycleDispatcher,
CustomTabActivityTabProvider tabProvider) {
mConnection = connection;
mIntentDataProvider = intentDataProvider;
mTabProvider = tabProvider;
lifecycleDispatcher.register(this);
}
@Override
public void onStartWithNative() {
mIsKeepingAlive = mConnection.keepAliveForSession(
mIntentDataProvider.getSession(), mIntentDataProvider.getKeepAliveServiceIntent());
}
@Override
public void onStopWithNative() {
mConnection.dontKeepAliveForSession(mIntentDataProvider.getSession());
mIsKeepingAlive = false;
}
/**
* Records current client connection status.
*/
public void recordClientConnectionStatus() {
Tab tab = mTabProvider.getTab();
String packageName = tab == null ? null : TabAssociatedApp.getAppId(tab);
if (packageName == null) return; // No associated package
CustomTabsSessionToken session = mIntentDataProvider.getSession();
boolean isConnected =
packageName.equals(mConnection.getClientPackageNameForSession(session));
int status = -1;
if (isConnected) {
if (mIsKeepingAlive) {
status = ConnectionStatus.CONNECTED_KEEP_ALIVE;
} else {
status = ConnectionStatus.CONNECTED;
}
} else {
if (mIsKeepingAlive) {
status = ConnectionStatus.DISCONNECTED_KEEP_ALIVE;
} else {
status = ConnectionStatus.DISCONNECTED;
}
}
assert status >= 0;
if (GSAState.isGsaPackageName(packageName)) {
RecordHistogram.recordEnumeratedHistogram("CustomTabs.ConnectionStatusOnReturn.GSA",
status, ConnectionStatus.NUM_ENTRIES);
} else {
RecordHistogram.recordEnumeratedHistogram("CustomTabs.ConnectionStatusOnReturn.NonGSA",
status, ConnectionStatus.NUM_ENTRIES);
}
}
}
......@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.customtabs.dependency_injection;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityCoordinator;
import org.chromium.chrome.browser.customtabs.CustomTabActivityClientConnectionKeeper;
import org.chromium.chrome.browser.customtabs.CustomTabActivityLifecycleUmaTracker;
import org.chromium.chrome.browser.customtabs.CustomTabBottomBarDelegate;
import org.chromium.chrome.browser.customtabs.CustomTabCompositorContentInitializer;
......@@ -47,6 +48,7 @@ public interface CustomTabActivityComponent extends ChromeActivityComponent {
CustomTabToolbarCoordinator resolveToolbarCoordinator();
CustomTabCompositorContentInitializer resolveCompositorContentInitializer();
CustomTabSessionHandler resolveSessionHandler();
CustomTabActivityClientConnectionKeeper resolveConnectionKeeper();
CustomTabTabPersistencePolicy resolveTabPersistencePolicy(); // For testing
}
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