Commit 3912764f authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Increase encapsulation of AppBannerInProductHelp java code

AppBannerInProductHelp is not slated for componentization.

This moves the jni code which invokes the IPH UI from
AppBannerManager.java, which is slated for componentization, to
AppBannerInProductHelpControllerProvider.java, and thus removes some
//chrome dependencies from the former class.

Bug: 1147268
Change-Id: If8eb505ec32081b6c85f2b901eac4887b2a49543
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2541062
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828278}
parent 8fa926d0
......@@ -2,6 +2,7 @@ include_rules = [
"-chrome/android/features/keyboard_accessory/internal",
"+chrome/browser/android/lifecycle",
"+chrome/browser/banners/android/java",
"+chrome/browser/device",
"+chrome/browser/privacy",
"+chrome/browser/profiles/android/java",
......@@ -69,5 +70,5 @@ specific_include_rules = {
"SplitMonochromeApplication\.java": [
"+android_webview/nonembedded/java/src/org/chromium/android_webview/nonembedded/WebViewApkApplication.java",
"+android_webview/glue/java/src/com/android/webview/chromium/MonochromeLibraryPreloader.java",
]
],
}
......@@ -37,6 +37,7 @@ import org.chromium.chrome.browser.DefaultBrowserInfo;
import org.chromium.chrome.browser.DeferredStartupHandler;
import org.chromium.chrome.browser.DevToolsServer;
import org.chromium.chrome.browser.app.video_tutorials.VideoTutorialShareHelper;
import org.chromium.chrome.browser.banners.AppBannerInProductHelpControllerProvider;
import org.chromium.chrome.browser.banners.AppBannerManager;
import org.chromium.chrome.browser.bookmarkswidget.BookmarkWidgetProvider;
import org.chromium.chrome.browser.contacts_picker.ChromePickerAdapter;
......@@ -216,7 +217,8 @@ public class ProcessInitializationHandler {
ChromeActivitySessionTracker.getInstance().initializeWithNative();
ProfileManagerUtils.removeSessionCookiesForAllProfiles();
AppBannerManager.setAppDetailsDelegate(AppHooks.get().createAppDetailsDelegate());
AppBannerManager.setTrackerFromProfileFactory(TrackerFactory::getTrackerForProfile);
AppBannerInProductHelpControllerProvider.setTrackerFromProfileFactory(
TrackerFactory::getTrackerForProfile);
ChromeLifetimeController.initialize();
Clipboard.getInstance().setImageFileProvider(new ClipboardImageFileProvider());
......
......@@ -41,8 +41,10 @@ android_resources("java_resources") {
}
generate_jni("jni_headers") {
sources =
[ "java/src/org/chromium/chrome/browser/banners/AppBannerManager.java" ]
sources = [
"java/src/org/chromium/chrome/browser/banners/AppBannerInProductHelpControllerProvider.java",
"java/src/org/chromium/chrome/browser/banners/AppBannerManager.java",
]
}
android_library("javatests") {
......
......@@ -4,7 +4,13 @@
package org.chromium.chrome.browser.banners;
import org.chromium.base.Function;
import org.chromium.base.UnownedUserDataKey;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.feature_engagement.FeatureConstants;
import org.chromium.components.feature_engagement.Tracker;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
......@@ -16,13 +22,24 @@ public class AppBannerInProductHelpControllerProvider {
private static final UnownedUserDataKey<AppBannerInProductHelpController> KEY =
new UnownedUserDataKey<>(AppBannerInProductHelpController.class);
private static Function<Profile, Tracker> sTrackerFromProfileFactory;
/**
* Sets the factory to obtain a Tracker from.
* @param trackerFromProfileFactory The factory to use.
*/
public static void setTrackerFromProfileFactory(
Function<Profile, Tracker> trackerFromProfileFactory) {
sTrackerFromProfileFactory = trackerFromProfileFactory;
}
/**
* Get the shared {@link AppBannerInProductHelpController} from the provided {@link
* WindowAndroid}.
* @param windowAndroid The window to pull the controller from.
* @return A shared instance of a {@link AppBannerInProductHelpController}.
*/
public static AppBannerInProductHelpController from(WindowAndroid windowAndroid) {
private static AppBannerInProductHelpController from(WindowAndroid windowAndroid) {
return KEY.retrieveDataFromHost(windowAndroid.getUnownedUserDataHost());
}
......@@ -33,4 +50,29 @@ public class AppBannerInProductHelpControllerProvider {
static void detach(AppBannerInProductHelpController controller) {
KEY.detachFromAllHosts(controller);
}
/**
* Request to show the in-product help for installing a PWA.
* @param webContents The current WebContents.
* @return An error message, if unsuccessful. Blank if the request was made.
*/
@CalledByNative
private static String showInProductHelp(WebContents webContents) {
// Consult the tracker to see if the IPH can be shown.
final Tracker tracker =
sTrackerFromProfileFactory.apply(Profile.fromWebContents(webContents));
if (!tracker.wouldTriggerHelpUI(FeatureConstants.PWA_INSTALL_AVAILABLE_FEATURE)) {
// Tracker replied that the request to show will not be honored. Return whether the
// limit of how often to show has been exceeded.
return "Trigger state: "
+ tracker.getTriggerState(FeatureConstants.PWA_INSTALL_AVAILABLE_FEATURE);
}
WindowAndroid window = webContents.getTopLevelNativeWindow();
if (window == null) return "No window";
AppBannerInProductHelpController controller = from(window);
if (controller == null) return "No controller";
controller.requestInProductHelp();
return "";
}
}
......@@ -11,16 +11,11 @@ import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
import org.chromium.base.Function;
import org.chromium.base.ThreadUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.feature_engagement.FeatureConstants;
import org.chromium.components.feature_engagement.Tracker;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* Manages an AppBannerInfoBar for a WebContents.
......@@ -58,8 +53,6 @@ public class AppBannerManager {
/** Retrieves information about a given package. */
private static AppDetailsDelegate sAppDetailsDelegate;
private static Function<Profile, Tracker> sTrackerFromProfileFactory;
/** Pointer to the native side AppBannerManager. */
private long mNativePointer;
......@@ -72,15 +65,6 @@ public class AppBannerManager {
sAppDetailsDelegate = delegate;
}
/**
* Sets the factory to obtain a Tracker from.
* @param trackerFromProfileFactory The factory to use.
*/
public static void setTrackerFromProfileFactory(
Function<Profile, Tracker> trackerFromProfileFactory) {
sTrackerFromProfileFactory = trackerFromProfileFactory;
}
/**
* Constructs an AppBannerManager.
* @param nativePointer the native-side object that owns this AppBannerManager.
......@@ -116,32 +100,6 @@ public class AppBannerManager {
createAppDetailsObserver(), url, packageName, referrer, iconSizeInPx);
}
/**
* Request to show the in-product help for installing a PWA.
* @param webContents The current WebContents.
* @return An error message, if unsuccessful. Blank if the request was made.
*/
@CalledByNative
private String showInProductHelp(WebContents webContents) {
// Consult the tracker to see if the IPH can be shown.
final Tracker tracker =
sTrackerFromProfileFactory.apply(Profile.fromWebContents(webContents));
if (!tracker.wouldTriggerHelpUI(FeatureConstants.PWA_INSTALL_AVAILABLE_FEATURE)) {
// Tracker replied that the request to show will not be honored. Return whether the
// limit of how often to show has been exceeded.
return "Trigger state: "
+ tracker.getTriggerState(FeatureConstants.PWA_INSTALL_AVAILABLE_FEATURE);
}
WindowAndroid window = webContents.getTopLevelNativeWindow();
if (window == null) return "No window";
AppBannerInProductHelpController controller =
AppBannerInProductHelpControllerProvider.from(window);
if (controller == null) return "No controller";
controller.requestInProductHelp();
return "";
}
private AppDetailsDelegate.Observer createAppDetailsObserver() {
return new AppDetailsDelegate.Observer() {
/**
......
......@@ -23,6 +23,7 @@
#include "chrome/browser/android/webapk/webapk_web_manifest_checker.h"
#include "chrome/browser/android/webapps/add_to_homescreen_coordinator.h"
#include "chrome/browser/android/webapps/add_to_homescreen_params.h"
#include "chrome/browser/banners/android/jni_headers/AppBannerInProductHelpControllerProvider_jni.h"
#include "chrome/browser/banners/android/jni_headers/AppBannerManager_jni.h"
#include "chrome/browser/banners/app_banner_metrics.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
......@@ -488,8 +489,8 @@ bool AppBannerManagerAndroid::MaybeShowInProductHelp() const {
JNIEnv* env = base::android::AttachCurrentThread();
std::string error_message = base::android::ConvertJavaStringToUTF8(
Java_AppBannerManager_showInProductHelp(
env, java_banner_manager_, web_contents()->GetJavaWebContents()));
Java_AppBannerInProductHelpControllerProvider_showInProductHelp(
env, web_contents()->GetJavaWebContents()));
if (!error_message.empty()) {
DVLOG(2) << "IPH for PWA showing aborted. " << error_message;
return false;
......
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