Commit ad52a7e0 authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

[WebLayer] Bring in fork of Chrome ConfirmInfoBar for WebLayer

The popup blocked infobar uses the ConfirmInfoBar, so we will need the
equivalent functionality in WebLayer. Since ConfirmInfoBar inherits from
InfoBar which was forked in http://crrev.com/c/2224869, we also need to
fork ConfirmInfoBar. The layout for non-compact infobars was
componentized in http://crrev.com/c/2243579 and can be used between both
Chrome and WebLayer.

The initial patchset here has a clean copy of the Chrome files, and the
subsequent patchsets have changes for WebLayer.

Bug: 1019922
Change-Id: I76e093e6113c26990dd1041ac4c901690489bad3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2248538Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#778903}
parent 22c74b9f
......@@ -149,6 +149,8 @@ source_set("weblayer_lib_base") {
"browser/browsing_data_remover_delegate_factory.h",
"browser/client_hints_factory.cc",
"browser/client_hints_factory.h",
"browser/confirm_infobar_android.cc",
"browser/confirm_infobar_android.h",
"browser/content_browser_client_impl.cc",
"browser/content_browser_client_impl.h",
"browser/content_browser_client_impl_receiver_bindings.cc",
......
// Copyright 2020 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.
#include "weblayer/browser/confirm_infobar_android.h"
#include <memory>
#include <utility>
#include "base/android/jni_string.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/window_android.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
#include "weblayer/browser/infobar_service.h"
#include "weblayer/browser/java/jni/ConfirmInfoBar_jni.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
namespace weblayer {
// InfoBarService -------------------------------------------------------------
std::unique_ptr<infobars::InfoBar> InfoBarService::CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate) {
return std::make_unique<ConfirmInfoBar>(std::move(delegate));
}
// ConfirmInfoBar -------------------------------------------------------------
ConfirmInfoBar::ConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate> delegate)
: InfoBarAndroid(std::move(delegate)) {}
ConfirmInfoBar::~ConfirmInfoBar() = default;
base::string16 ConfirmInfoBar::GetTextFor(
ConfirmInfoBarDelegate::InfoBarButton button) {
ConfirmInfoBarDelegate* delegate = GetDelegate();
return (delegate->GetButtons() & button) ? delegate->GetButtonLabel(button)
: base::string16();
}
ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() {
return delegate()->AsConfirmInfoBarDelegate();
}
ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar(JNIEnv* env) {
ScopedJavaLocalRef<jstring> ok_button_text =
base::android::ConvertUTF16ToJavaString(
env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK));
ScopedJavaLocalRef<jstring> cancel_button_text =
base::android::ConvertUTF16ToJavaString(
env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL));
ConfirmInfoBarDelegate* delegate = GetDelegate();
ScopedJavaLocalRef<jstring> message_text =
base::android::ConvertUTF16ToJavaString(env, delegate->GetMessageText());
ScopedJavaLocalRef<jstring> link_text =
base::android::ConvertUTF16ToJavaString(env, delegate->GetLinkText());
ScopedJavaLocalRef<jobject> java_bitmap;
if (delegate->GetIconId() == infobars::InfoBarDelegate::kNoIconID &&
!delegate->GetIcon().IsEmpty()) {
java_bitmap = gfx::ConvertToJavaBitmap(delegate->GetIcon().ToSkBitmap());
}
return Java_ConfirmInfoBar_create(env, GetJavaIconId(), java_bitmap,
message_text, link_text, ok_button_text,
cancel_button_text);
}
void ConfirmInfoBar::OnLinkClicked(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
if (!owner())
return; // We're closing; don't call anything, it might access the owner.
if (GetDelegate()->LinkClicked(WindowOpenDisposition::NEW_FOREGROUND_TAB))
RemoveSelf();
}
void ConfirmInfoBar::ProcessButton(int action) {
if (!owner())
return; // We're closing; don't call anything, it might access the owner.
DCHECK((action == InfoBarAndroid::ACTION_OK) ||
(action == InfoBarAndroid::ACTION_CANCEL));
ConfirmInfoBarDelegate* delegate = GetDelegate();
if ((action == InfoBarAndroid::ACTION_OK) ? delegate->Accept()
: delegate->Cancel()) {
RemoveSelf();
}
}
} // namespace weblayer
// Copyright 2020 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.
#ifndef WEBLAYER_BROWSER_CONFIRM_INFOBAR_ANDROID_H_
#define WEBLAYER_BROWSER_CONFIRM_INFOBAR_ANDROID_H_
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "weblayer/browser/infobar_android.h"
namespace weblayer {
class ConfirmInfoBar : public InfoBarAndroid {
public:
explicit ConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate> delegate);
~ConfirmInfoBar() override;
protected:
ConfirmInfoBarDelegate* GetDelegate();
base::string16 GetTextFor(ConfirmInfoBarDelegate::InfoBarButton button);
// InfoBarAndroid overrides.
base::android::ScopedJavaLocalRef<jobject> CreateRenderInfoBar(
JNIEnv* env) override;
void OnLinkClicked(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) override;
void ProcessButton(int action) override;
private:
DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBar);
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_CONFIRM_INFOBAR_ANDROID_H_
......@@ -29,6 +29,10 @@ class InfoBarService : public infobars::ContentInfoBarManager,
InfoBarService(const InfoBarService&) = delete;
InfoBarService& operator=(const InfoBarService&) = delete;
// InfoBarManager:
std::unique_ptr<infobars::InfoBar> CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate) override;
protected:
explicit InfoBarService(content::WebContents* web_contents);
......
......@@ -83,6 +83,7 @@ android_library("java") {
"org/chromium/weblayer_private/BrowserImpl.java",
"org/chromium/weblayer_private/BrowserViewController.java",
"org/chromium/weblayer_private/ChildProcessServiceImpl.java",
"org/chromium/weblayer_private/ConfirmInfoBar.java",
"org/chromium/weblayer_private/ContentViewRenderView.java",
"org/chromium/weblayer_private/CookieManagerImpl.java",
"org/chromium/weblayer_private/CrashReporterControllerImpl.java",
......@@ -261,6 +262,7 @@ generate_jni("jni") {
"org/chromium/weblayer_private/AutocompleteSchemeClassifierImpl.java",
"org/chromium/weblayer_private/BrowserControlsContainerView.java",
"org/chromium/weblayer_private/BrowserImpl.java",
"org/chromium/weblayer_private/ConfirmInfoBar.java",
"org/chromium/weblayer_private/ContentViewRenderView.java",
"org/chromium/weblayer_private/CookieManagerImpl.java",
"org/chromium/weblayer_private/DownloadCallbackProxy.java",
......
// Copyright 2020 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.weblayer_private;
import android.graphics.Bitmap;
import androidx.annotation.ColorRes;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.components.infobars.InfoBarLayout;
/**
* An infobar that presents the user with several buttons.
*
* TODO(newt): merge this into InfoBar.java.
*/
public class ConfirmInfoBar extends InfoBar {
/** Text shown on the primary button, e.g. "OK". */
private final String mPrimaryButtonText;
/** Text shown on the secondary button, e.g. "Cancel".*/
private final String mSecondaryButtonText;
/** Text shown on the link, e.g. "Learn more". */
private final String mLinkText;
protected ConfirmInfoBar(int iconDrawableId, @ColorRes int iconTintId, Bitmap iconBitmap,
String message, String linkText, String primaryButtonText, String secondaryButtonText) {
super(iconDrawableId, iconTintId, message, iconBitmap);
mPrimaryButtonText = primaryButtonText;
mSecondaryButtonText = secondaryButtonText;
mLinkText = linkText;
}
@Override
public void createContent(InfoBarLayout layout) {
setButtons(layout, mPrimaryButtonText, mSecondaryButtonText);
if (mLinkText != null && !mLinkText.isEmpty()) layout.appendMessageLinkText(mLinkText);
}
/**
* If your custom infobar overrides this function, YOU'RE PROBABLY DOING SOMETHING WRONG.
*
* Adds buttons to the infobar. This should only be overridden in cases where an infobar
* requires adding something other than a button for its secondary View on the bottom row
* (almost never).
*
* @param primaryText Text to display on the primary button.
* @param secondaryText Text to display on the secondary button. May be null.
*/
protected void setButtons(InfoBarLayout layout, String primaryText, String secondaryText) {
layout.setButtons(primaryText, secondaryText);
}
@Override
public void onButtonClicked(final boolean isPrimaryButton) {
int action = isPrimaryButton ? ActionType.OK : ActionType.CANCEL;
onButtonClicked(action);
}
/**
* Creates and begins the process for showing a ConfirmInfoBar.
* @param iconId ID corresponding to the icon that will be shown for the infobar.
* @param iconBitmap Bitmap to use if there is no equivalent Java resource for
* iconId.
* @param message Message to display to the user indicating what the infobar is for.
* @param linkText Link text to display in addition to the message.
* @param buttonOk String to display on the OK button.
* @param buttonCancel String to display on the Cancel button.
*/
@CalledByNative
private static ConfirmInfoBar create(int iconId, Bitmap iconBitmap, String message,
String linkText, String buttonOk, String buttonCancel) {
ConfirmInfoBar infoBar = new ConfirmInfoBar(
iconId, 0, iconBitmap, message, linkText, buttonOk, buttonCancel);
return infoBar;
}
}
......@@ -17,6 +17,7 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.infobar.InfoBarIdentifier;
import org.chromium.components.infobars.InfoBarInteractionHandler;
import org.chromium.components.infobars.InfoBarLayout;
import org.chromium.ui.modelutil.PropertyModel;
/**
......@@ -129,8 +130,11 @@ public abstract class InfoBar implements InfoBarInteractionHandler, InfoBarUiIte
createCompactLayoutContent(layout);
mView = layout;
} else {
// WebLayer supports only compact infobars.
assert false;
InfoBarLayout layout = new InfoBarLayout(
mContext, this, mIconDrawableId, mIconTintId, mIconBitmap, mMessage);
createContent(layout);
layout.onContentCreated();
mView = layout;
}
return mView;
......@@ -152,6 +156,12 @@ public abstract class InfoBar implements InfoBarInteractionHandler, InfoBarUiIte
return false;
}
/**
* Prepares the InfoBar for display and adds InfoBar-specific controls to the layout.
* @param layout Layout containing all of the controls.
*/
protected void createContent(InfoBarLayout layout) {}
/**
* Prepares and inserts views into an {@link InfoBarCompactLayout}.
* {@link #usesCompactLayout} must return 'true' for this function to be called.
......
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