Commit 911f84fc authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

Move PopupBlockedInfoBarDelegate to //components/blocked_content

This infobar will be used in WebLayer when popups are blocked. This CL
componentizes the infobar delegate, and adds support to WebLayer for
showing the infobar.

Bug: 1019922
Change-Id: I21d0f0e40dac6af5f4c8e3d5a4067906f64a009a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2248075Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Reviewed-by: default avatarPavel Yatsuk <pavely@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779165}
parent e22dbf45
......@@ -191,6 +191,7 @@ android_resources("chrome_app_java_resources") {
"//chrome/browser/ui/android/strings:ui_strings_grd",
"//chrome/browser/ui/messages/android:java_resources",
"//components/autofill/android:autofill_java_resources",
"//components/blocked_content/android:java_resources",
"//components/browser_ui/http_auth/android:java_resources",
"//components/browser_ui/modaldialog/android:java_resources",
"//components/browser_ui/settings/android:java_resources",
......@@ -695,6 +696,7 @@ java_cpp_template("resource_id_javagen") {
package_path = "org/chromium/chrome/browser/resources"
inputs = [
"../browser/android/resource_id.h",
"//components/resources/android/blocked_content_resource_id.h",
"//components/resources/android/page_info_resource_id.h",
"//components/resources/android/permissions_resource_id.h",
]
......
......@@ -110,7 +110,6 @@ chrome_java_resources = [
"java/res/drawable-hdpi/incognito_small.png",
"java/res/drawable-hdpi/incognito_splash.png",
"java/res/drawable-hdpi/incognito_switch.png",
"java/res/drawable-hdpi/infobar_blocked_popups.png",
"java/res/drawable-hdpi/infobar_chrome.png",
"java/res/drawable-hdpi/infobar_mobile_friendly.png",
"java/res/drawable-hdpi/infobar_restore.png",
......@@ -271,7 +270,6 @@ chrome_java_resources = [
"java/res/drawable-mdpi/incognito_small.png",
"java/res/drawable-mdpi/incognito_splash.png",
"java/res/drawable-mdpi/incognito_switch.png",
"java/res/drawable-mdpi/infobar_blocked_popups.png",
"java/res/drawable-mdpi/infobar_chrome.png",
"java/res/drawable-mdpi/infobar_mobile_friendly.png",
"java/res/drawable-mdpi/infobar_restore.png",
......@@ -424,7 +422,6 @@ chrome_java_resources = [
"java/res/drawable-xhdpi/incognito_small.png",
"java/res/drawable-xhdpi/incognito_splash.png",
"java/res/drawable-xhdpi/incognito_switch.png",
"java/res/drawable-xhdpi/infobar_blocked_popups.png",
"java/res/drawable-xhdpi/infobar_chrome.png",
"java/res/drawable-xhdpi/infobar_mobile_friendly.png",
"java/res/drawable-xhdpi/infobar_restore.png",
......@@ -549,7 +546,6 @@ chrome_java_resources = [
"java/res/drawable-xxhdpi/incognito_small.png",
"java/res/drawable-xxhdpi/incognito_splash.png",
"java/res/drawable-xxhdpi/incognito_switch.png",
"java/res/drawable-xxhdpi/infobar_blocked_popups.png",
"java/res/drawable-xxhdpi/infobar_chrome.png",
"java/res/drawable-xxhdpi/infobar_mobile_friendly.png",
"java/res/drawable-xxhdpi/infobar_restore.png",
......@@ -671,7 +667,6 @@ chrome_java_resources = [
"java/res/drawable-xxxhdpi/incognito_small.png",
"java/res/drawable-xxxhdpi/incognito_splash.png",
"java/res/drawable-xxxhdpi/incognito_switch.png",
"java/res/drawable-xxxhdpi/infobar_blocked_popups.png",
"java/res/drawable-xxxhdpi/infobar_chrome.png",
"java/res/drawable-xxxhdpi/infobar_mobile_friendly.png",
"java/res/drawable-xxxhdpi/infobar_restore.png",
......
......@@ -12,6 +12,7 @@ class ResourceId {
#define LINK_RESOURCE_ID(c_id,java_id) java_id,
#define DECLARE_RESOURCE_ID(c_id,java_id) java_id,
#include "chrome/browser/android/resource_id.h"
#include "components/resources/android/blocked_content_resource_id.h"
#include "components/resources/android/page_info_resource_id.h"
#include "components/resources/android/permissions_resource_id.h"
};
......
......@@ -6206,14 +6206,6 @@ the Bookmarks menu.">
<message name="IDS_MEDIA_MENU_NO_DEVICE_TITLE" desc="The title of the media select menu if there is no microphone or camera available on the machine">
None available
</message>
<if expr="is_android">
<message name="IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW" desc="Pop-up Blocking Show Button [CHAR-LIMIT=32]">
Always show
</message>
<message name="IDS_POPUPS_BLOCKED_INFOBAR_TEXT" desc="Pop-up Blocking Title [CHAR-LIMIT=32] [ICU Syntax]">
{NUM_POPUPS,plural,=1{Pop-up blocked} other{# pop-ups blocked}}
</message>
</if>
<message name="IDS_ZOOMLEVELS_CHROME_ERROR_PAGES_LABEL" desc="Label for zoom level list entry for Chrome error pages.">
(Chrome error pages)
......
......@@ -29,8 +29,6 @@ LINK_RESOURCE_ID(0, 0)
LINK_RESOURCE_ID(IDR_INFOBAR_AUTOFILL_CC, R.drawable.infobar_autofill_cc)
// Android only infobars.
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_BLOCKED_POPUPS,
R.drawable.infobar_blocked_popups)
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_FROZEN_TAB, R.drawable.infobar_restore)
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_LITE_MODE, R.drawable.preview_pin_round)
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_MEDIA_STREAM_SCREEN,
......
......@@ -54,6 +54,7 @@ void ResourceMapper::ConstructMap() {
#define DECLARE_RESOURCE_ID(c_id, java_id) \
g_id_map.Get()[c_id] = resource_id_list[next_id++];
#include "chrome/browser/android/resource_id.h"
#include "components/resources/android/blocked_content_resource_id.h"
#include "components/resources/android/page_info_resource_id.h"
#include "components/resources/android/permissions_resource_id.h"
#undef LINK_RESOURCE_ID
......
......@@ -664,8 +664,6 @@ static_library("ui") {
"android/color_chooser_dialog_android.cc",
"android/content_settings/ads_blocked_infobar_delegate.cc",
"android/content_settings/ads_blocked_infobar_delegate.h",
"android/content_settings/popup_blocked_infobar_delegate.cc",
"android/content_settings/popup_blocked_infobar_delegate.h",
"android/context_menu_helper.cc",
"android/context_menu_helper.h",
"android/device_dialog/bluetooth_chooser_android.cc",
......
......@@ -5,6 +5,9 @@
#include "chrome/browser/ui/blocked_content/chrome_popup_navigation_delegate.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/chrome_content_settings_utils.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/common/chrome_render_frame.mojom.h"
#include "components/blocked_content/popup_navigation_delegate.h"
......@@ -14,8 +17,8 @@
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
#if defined(OS_ANDROID)
#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
#endif
ChromePopupNavigationDelegate::ChromePopupNavigationDelegate(
......@@ -66,7 +69,16 @@ void ChromePopupNavigationDelegate::OnPopupBlocked(
#if defined(OS_ANDROID)
// Should replace existing popup infobars, with an updated count of how many
// popups have been blocked.
PopupBlockedInfoBarDelegate::Create(web_contents,
total_popups_blocked_on_page);
if (blocked_content::PopupBlockedInfoBarDelegate::Create(
InfoBarService::FromWebContents(web_contents),
total_popups_blocked_on_page,
HostContentSettingsMapFactory::GetForProfile(
web_contents->GetBrowserContext()),
base::BindOnce(
&content_settings::RecordPopupsAction,
content_settings::POPUPS_ACTION_CLICKED_ALWAYS_SHOW_ON_MOBILE))) {
content_settings::RecordPopupsAction(
content_settings::POPUPS_ACTION_DISPLAYED_INFOBAR_ON_MOBILE);
}
#endif
}
......@@ -38,6 +38,31 @@ source_set("blocked_content") {
"//services/metrics/public/cpp:ukm_builders",
"//third_party/blink/public/common",
]
if (is_android) {
sources += [
"android/popup_blocked_infobar_delegate.cc",
"android/popup_blocked_infobar_delegate.h",
]
deps += [
"//components/infobars/content",
"//components/infobars/core",
"//components/resources:android_resources",
"//components/strings:components_strings_grit",
]
}
}
source_set("test_support") {
testonly = true
sources = [
"test/test_popup_navigation_delegate.cc",
"test/test_popup_navigation_delegate.h",
]
deps = [
":blocked_content",
"//third_party/blink/public/common",
"//url",
]
}
source_set("unit_tests") {
......@@ -48,6 +73,7 @@ source_set("unit_tests") {
]
deps = [
":blocked_content",
":test_support",
"//base",
"//base/test:test_support",
"//components/content_settings/browser",
......@@ -62,4 +88,8 @@ source_set("unit_tests") {
"//net:test_support",
"//testing/gtest",
]
if (is_android) {
sources += [ "android/popup_blocked_infobar_delegate_unittest.cc" ]
deps += [ "//components/infobars/content" ]
}
}
# 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.
import("//build/config/android/rules.gni")
android_resources("java_resources") {
sources = [
"res/drawable-hdpi/infobar_blocked_popups.png",
"res/drawable-mdpi/infobar_blocked_popups.png",
"res/drawable-xhdpi/infobar_blocked_popups.png",
"res/drawable-xxhdpi/infobar_blocked_popups.png",
"res/drawable-xxxhdpi/infobar_blocked_popups.png",
]
custom_package = "org.chromium.components.blocked_content"
}
include_rules = [
"+components/infobars/content",
"+components/infobars/core",
"+components/resources/android",
"+components/strings",
]
......@@ -2,61 +2,55 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
#include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
#include <stddef.h>
#include <utility>
#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/content_settings/chrome_content_settings_utils.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/resources/android/theme_resources.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace blocked_content {
// static
void PopupBlockedInfoBarDelegate::Create(content::WebContents* web_contents,
int num_popups) {
const GURL& url = web_contents->GetURL();
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents);
bool PopupBlockedInfoBarDelegate::Create(
infobars::ContentInfoBarManager* infobar_manager,
int num_popups,
HostContentSettingsMap* settings_map,
base::OnceClosure on_accept_callback) {
const GURL& url = infobar_manager->web_contents()->GetURL();
std::unique_ptr<infobars::InfoBar> infobar(
infobar_service->CreateConfirmInfoBar(
infobar_manager->CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate>(
new PopupBlockedInfoBarDelegate(
num_popups, url,
HostContentSettingsMapFactory::GetForProfile(profile)))));
new PopupBlockedInfoBarDelegate(num_popups, url, settings_map,
std::move(on_accept_callback)))));
// See if there is an existing popup infobar already.
// TODO(dfalcantara) When triggering more than one popup the infobar
// will be shown once, then hide then be shown again.
// This will be fixed once we have an in place replace infobar mechanism.
for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
infobars::InfoBar* existing_infobar = infobar_service->infobar_at(i);
for (size_t i = 0; i < infobar_manager->infobar_count(); ++i) {
infobars::InfoBar* existing_infobar = infobar_manager->infobar_at(i);
if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
infobar_service->ReplaceInfoBar(existing_infobar, std::move(infobar));
return;
infobar_manager->ReplaceInfoBar(existing_infobar, std::move(infobar));
return false;
}
}
infobar_service->AddInfoBar(std::move(infobar));
infobar_manager->AddInfoBar(std::move(infobar));
content_settings::RecordPopupsAction(
content_settings::POPUPS_ACTION_DISPLAYED_INFOBAR_ON_MOBILE);
return true;
}
PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() {
}
PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() = default;
infobars::InfoBarDelegate::InfoBarIdentifier
PopupBlockedInfoBarDelegate::GetIdentifier() const {
......@@ -68,15 +62,20 @@ int PopupBlockedInfoBarDelegate::GetIconId() const {
}
PopupBlockedInfoBarDelegate*
PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
return this;
}
PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
int num_popups,
const GURL& url,
HostContentSettingsMap* map)
: ConfirmInfoBarDelegate(), num_popups_(num_popups), url_(url), map_(map) {
HostContentSettingsMap* map,
base::OnceClosure on_accept_callback)
: ConfirmInfoBarDelegate(),
num_popups_(num_popups),
url_(url),
map_(map),
on_accept_callback_(std::move(on_accept_callback)) {
content_settings::SettingInfo setting_info;
std::unique_ptr<base::Value> setting = map->GetWebsiteSetting(
url, url, ContentSettingsType::POPUPS, std::string(), &setting_info);
......@@ -121,7 +120,7 @@ bool PopupBlockedInfoBarDelegate::Accept() {
// Launch popups.
content::WebContents* web_contents =
InfoBarService::WebContentsFromInfoBar(infobar());
infobars::ContentInfoBarManager::WebContentsFromInfoBar(infobar());
blocked_content::PopupBlockerTabHelper* popup_blocker_helper =
blocked_content::PopupBlockerTabHelper::FromWebContents(web_contents);
DCHECK(popup_blocker_helper);
......@@ -134,7 +133,9 @@ bool PopupBlockedInfoBarDelegate::Accept() {
WindowOpenDisposition::CURRENT_TAB);
}
content_settings::RecordPopupsAction(
content_settings::POPUPS_ACTION_CLICKED_ALWAYS_SHOW_ON_MOBILE);
if (on_accept_callback_)
std::move(on_accept_callback_).Run();
return true;
}
} // namespace blocked_content
......@@ -2,31 +2,43 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_ANDROID_CONTENT_SETTINGS_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
#define CHROME_BROWSER_UI_ANDROID_CONTENT_SETTINGS_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
#ifndef COMPONENTS_BLOCKED_CONTENT_ANDROID_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
#define COMPONENTS_BLOCKED_CONTENT_ANDROID_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
#include "base/macros.h"
#include "base/callback.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "url/gurl.h"
namespace content {
class WebContents;
} // namespace content
namespace infobars {
class ContentInfoBarManager;
}
class HostContentSettingsMap;
namespace blocked_content {
class PopupBlockedInfoBarDelegate : public ConfirmInfoBarDelegate {
public:
// Creates a popup blocked infobar and delegate and adds the infobar to
// |infobar_service|.
static void Create(content::WebContents* web_contents, int num_popups);
// |infobar_manager|. Returns true if the infobar was created, and false if it
// replaced an existing popup infobar. |on_accept_callback| will be run if the
// accept button is pressed on the infobar.
static bool Create(infobars::ContentInfoBarManager* infobar_manager,
int num_popups,
HostContentSettingsMap* settings_map,
base::OnceClosure on_accept_callback);
~PopupBlockedInfoBarDelegate() override;
PopupBlockedInfoBarDelegate(const PopupBlockedInfoBarDelegate&) = delete;
PopupBlockedInfoBarDelegate& operator=(const PopupBlockedInfoBarDelegate&) =
delete;
private:
PopupBlockedInfoBarDelegate(int num_popups,
const GURL& url,
HostContentSettingsMap* map);
HostContentSettingsMap* map,
base::OnceClosure on_accept_callback);
// ConfirmInfoBarDelegate:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
......@@ -37,12 +49,13 @@ class PopupBlockedInfoBarDelegate : public ConfirmInfoBarDelegate {
base::string16 GetButtonLabel(InfoBarButton button) const override;
bool Accept() override;
int num_popups_;
GURL url_;
const int num_popups_;
const GURL url_;
HostContentSettingsMap* map_;
bool can_show_popups_;
DISALLOW_COPY_AND_ASSIGN(PopupBlockedInfoBarDelegate);
base::OnceClosure on_accept_callback_;
};
#endif // CHROME_BROWSER_UI_ANDROID_CONTENT_SETTINGS_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
} // namespace blocked_content
#endif // COMPONENTS_BLOCKED_CONTENT_ANDROID_POPUP_BLOCKED_INFOBAR_DELEGATE_H_
// 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 "components/blocked_content/android/popup_blocked_infobar_delegate.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
#include "components/blocked_content/test/test_popup_navigation_delegate.h"
#include "components/content_settings/browser/tab_specific_content_settings.h"
#include "components/content_settings/browser/test_tab_specific_content_settings_delegate.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace blocked_content {
namespace {
constexpr char kPageUrl[] = "http://example_page.test";
constexpr char kPopupUrl[] = "http://example_popup.test";
class TestInfoBarManager : public infobars::ContentInfoBarManager {
public:
explicit TestInfoBarManager(content::WebContents* web_contents)
: ContentInfoBarManager(web_contents) {}
// infobars::InfoBarManager:
std::unique_ptr<infobars::InfoBar> CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate) override {
return std::make_unique<infobars::InfoBar>(std::move(delegate));
}
};
} // namespace
class PopupBlockedInfoBarDelegateTest
: public content::RenderViewHostTestHarness {
public:
~PopupBlockedInfoBarDelegateTest() override {
settings_map_->ShutdownOnUIThread();
}
// content::RenderViewHostTestHarness:
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
// Make sure the SafeBrowsingTriggeredPopupBlocker is not created.
feature_list_.InitAndDisableFeature(kAbusiveExperienceEnforce);
HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
&pref_service_, false, false, false, false);
content_settings::TabSpecificContentSettings::CreateForWebContents(
web_contents(),
std::make_unique<
content_settings::TestTabSpecificContentSettingsDelegate>(
/*prefs=*/nullptr, settings_map_.get()));
PopupBlockerTabHelper::CreateForWebContents(web_contents());
helper_ = PopupBlockerTabHelper::FromWebContents(web_contents());
infobar_manager_ = std::make_unique<TestInfoBarManager>(web_contents());
NavigateAndCommit(GURL(kPageUrl));
}
PopupBlockerTabHelper* helper() { return helper_; }
TestInfoBarManager* infobar_manager() { return infobar_manager_.get(); }
HostContentSettingsMap* settings_map() { return settings_map_.get(); }
private:
base::test::ScopedFeatureList feature_list_;
PopupBlockerTabHelper* helper_ = nullptr;
sync_preferences::TestingPrefServiceSyncable pref_service_;
scoped_refptr<HostContentSettingsMap> settings_map_;
std::unique_ptr<TestInfoBarManager> infobar_manager_;
};
TEST_F(PopupBlockedInfoBarDelegateTest, ReplacesInfobarOnSecondPopup) {
EXPECT_TRUE(PopupBlockedInfoBarDelegate::Create(
infobar_manager(), 1, settings_map(), base::NullCallback()));
EXPECT_EQ(infobar_manager()->infobar_count(), 1u);
// First message should not contain "2";
EXPECT_FALSE(base::Contains(infobar_manager()
->infobar_at(0)
->delegate()
->AsConfirmInfoBarDelegate()
->GetMessageText(),
base::ASCIIToUTF16("2")));
EXPECT_FALSE(PopupBlockedInfoBarDelegate::Create(
infobar_manager(), 2, settings_map(), base::NullCallback()));
EXPECT_EQ(infobar_manager()->infobar_count(), 1u);
// Second message blocks 2 popups, so should contain "2";
EXPECT_TRUE(base::Contains(infobar_manager()
->infobar_at(0)
->delegate()
->AsConfirmInfoBarDelegate()
->GetMessageText(),
base::ASCIIToUTF16("2")));
}
TEST_F(PopupBlockedInfoBarDelegateTest, ShowsBlockedPopups) {
TestPopupNavigationDelegate::ResultHolder result;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kPopupUrl), &result),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
bool on_accept_called = false;
EXPECT_TRUE(PopupBlockedInfoBarDelegate::Create(
infobar_manager(), 1, settings_map(),
base::BindLambdaForTesting(
[&on_accept_called] { on_accept_called = true; })));
EXPECT_FALSE(on_accept_called);
EXPECT_TRUE(infobar_manager()
->infobar_at(0)
->delegate()
->AsConfirmInfoBarDelegate()
->Accept());
EXPECT_TRUE(result.did_navigate);
EXPECT_TRUE(on_accept_called);
EXPECT_EQ(settings_map()->GetContentSetting(GURL(kPageUrl), GURL(kPageUrl),
ContentSettingsType::POPUPS,
std::string()),
CONTENT_SETTING_ALLOW);
}
} // namespace blocked_content
......@@ -7,6 +7,7 @@
#include "base/test/scoped_feature_list.h"
#include "components/blocked_content/popup_navigation_delegate.h"
#include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
#include "components/blocked_content/test/test_popup_navigation_delegate.h"
#include "components/blocked_content/url_list_manager.h"
#include "components/content_settings/browser/tab_specific_content_settings.h"
#include "components/content_settings/browser/test_tab_specific_content_settings_delegate.h"
......@@ -42,46 +43,6 @@ class BlockedUrlListObserver : public UrlListManager::Observer {
std::map<int32_t, GURL> blocked_urls_;
ScopedObserver<UrlListManager, UrlListManager::Observer> observer_{this};
};
// Struct used to hold results from calls on TestPopupNavigationDelegate since
// the delegate will be destroyed on calls to ShowBlockedPopup().
struct ResultHolder {
bool did_navigate = false;
blink::mojom::WindowFeatures navigation_window_features;
base::Optional<WindowOpenDisposition> navigation_disposition;
int total_popups_blocked_on_page = 0;
};
// Test delegate which stores results of calls in a ResultHolder.
class TestPopupNavigationDelegate : public PopupNavigationDelegate {
public:
explicit TestPopupNavigationDelegate(const GURL& url,
ResultHolder* result_holder)
: url_(url), result_holder_(result_holder) {}
// PopupNavigationDelegate:
content::RenderFrameHost* GetOpener() override { return nullptr; }
bool GetOriginalUserGesture() override { return false; }
const GURL& GetURL() override { return url_; }
NavigateResult NavigateWithGesture(
const blink::mojom::WindowFeatures& window_features,
base::Optional<WindowOpenDisposition> updated_disposition) override {
result_holder_->did_navigate = true;
result_holder_->navigation_window_features = window_features;
result_holder_->navigation_disposition = updated_disposition;
return NavigateResult();
}
void OnPopupBlocked(content::WebContents* web_contents,
int total_popups_blocked_on_page) override {
result_holder_->total_popups_blocked_on_page = total_popups_blocked_on_page;
}
private:
const GURL url_;
ResultHolder* result_holder_;
};
} // namespace
class PopupBlockerTabHelperTest : public content::RenderViewHostTestHarness {
......@@ -118,7 +79,7 @@ class PopupBlockerTabHelperTest : public content::RenderViewHostTestHarness {
TEST_F(PopupBlockerTabHelperTest, BlocksAndShowsPopup) {
BlockedUrlListObserver observer(helper());
ResultHolder result;
TestPopupNavigationDelegate::ResultHolder result;
blink::mojom::WindowFeatures window_features;
window_features.has_x = true;
helper()->AddBlockedPopup(
......@@ -138,7 +99,7 @@ TEST_F(PopupBlockerTabHelperTest, BlocksAndShowsPopup) {
TEST_F(PopupBlockerTabHelperTest, MultiplePopups) {
BlockedUrlListObserver observer(helper());
ResultHolder result1;
TestPopupNavigationDelegate::ResultHolder result1;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kUrl1), &result1),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
......@@ -147,7 +108,7 @@ TEST_F(PopupBlockerTabHelperTest, MultiplePopups) {
UnorderedElementsAre(Pair(0, GURL(kUrl1))));
EXPECT_EQ(helper()->GetBlockedPopupsCount(), 1u);
ResultHolder result2;
TestPopupNavigationDelegate::ResultHolder result2;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kUrl2), &result2),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
......@@ -170,7 +131,7 @@ TEST_F(PopupBlockerTabHelperTest, MultiplePopups) {
}
TEST_F(PopupBlockerTabHelperTest, DoesNotShowPopupWithInvalidID) {
ResultHolder result;
TestPopupNavigationDelegate::ResultHolder result;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kUrl1), &result),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
......@@ -192,7 +153,7 @@ TEST_F(PopupBlockerTabHelperTest, SetsContentSettingsPopupState) {
web_contents());
EXPECT_FALSE(content_settings->IsContentBlocked(ContentSettingsType::POPUPS));
ResultHolder result;
TestPopupNavigationDelegate::ResultHolder result;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kUrl1), &result),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
......@@ -211,7 +172,7 @@ TEST_F(PopupBlockerTabHelperTest, SetsContentSettingsPopupState) {
}
TEST_F(PopupBlockerTabHelperTest, ClearsContentSettingsPopupStateOnNavigation) {
ResultHolder result;
TestPopupNavigationDelegate::ResultHolder result;
helper()->AddBlockedPopup(
std::make_unique<TestPopupNavigationDelegate>(GURL(kUrl1), &result),
blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
......
......@@ -17,6 +17,7 @@
#include "components/blocked_content/popup_blocker.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "components/blocked_content/popup_navigation_delegate.h"
#include "components/blocked_content/test/test_popup_navigation_delegate.h"
#include "components/content_settings/browser/tab_specific_content_settings.h"
#include "components/content_settings/browser/test_tab_specific_content_settings_delegate.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
......@@ -46,24 +47,6 @@ namespace blocked_content {
const char kNumBlockedHistogram[] =
"ContentSettings.Popups.StrongBlocker.NumBlocked";
class TestPopupNavigationDelegate : public PopupNavigationDelegate {
public:
explicit TestPopupNavigationDelegate(const GURL& url) : url_(url) {}
content::RenderFrameHost* GetOpener() override { return nullptr; }
bool GetOriginalUserGesture() override { return true; }
const GURL& GetURL() override { return url_; }
NavigateResult NavigateWithGesture(
const blink::mojom::WindowFeatures& window_features,
base::Optional<WindowOpenDisposition> updated_disposition) override {
return NavigateResult{nullptr, WindowOpenDisposition::UNKNOWN};
}
void OnPopupBlocked(content::WebContents* web_contents,
int num_blocked) override {}
private:
const GURL url_;
};
class SafeBrowsingTriggeredPopupBlockerTest
: public content::RenderViewHostTestHarness,
public subresource_filter::SubresourceFilterClient {
......@@ -245,7 +228,8 @@ TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
blink::TriggeringEventInfo::kFromUntrustedEvent;
MaybeBlockPopup(web_contents(), nullptr,
std::make_unique<TestPopupNavigationDelegate>(popup_url),
std::make_unique<TestPopupNavigationDelegate>(
popup_url, nullptr /* result_holder */),
&params, blink::mojom::WindowFeatures(), settings_map());
EXPECT_EQ(1u, PopupBlockerTabHelper::FromWebContents(web_contents())
......@@ -269,7 +253,8 @@ TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
params.triggering_event_info = blink::TriggeringEventInfo::kFromTrustedEvent;
MaybeBlockPopup(web_contents(), nullptr,
std::make_unique<TestPopupNavigationDelegate>(popup_url),
std::make_unique<TestPopupNavigationDelegate>(
popup_url, nullptr /* result_holder */),
&params, blink::mojom::WindowFeatures(), settings_map());
EXPECT_EQ(0u, PopupBlockerTabHelper::FromWebContents(web_contents())
......
// 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 "components/blocked_content/test/test_popup_navigation_delegate.h"
namespace blocked_content {
TestPopupNavigationDelegate::ResultHolder::ResultHolder() = default;
TestPopupNavigationDelegate::ResultHolder::~ResultHolder() = default;
TestPopupNavigationDelegate::TestPopupNavigationDelegate(
const GURL& url,
ResultHolder* result_holder)
: url_(url), result_holder_(result_holder) {}
content::RenderFrameHost* TestPopupNavigationDelegate::GetOpener() {
return nullptr;
}
bool TestPopupNavigationDelegate::GetOriginalUserGesture() {
return true;
}
const GURL& TestPopupNavigationDelegate::GetURL() {
return url_;
}
PopupNavigationDelegate::NavigateResult
TestPopupNavigationDelegate::NavigateWithGesture(
const blink::mojom::WindowFeatures& window_features,
base::Optional<WindowOpenDisposition> updated_disposition) {
if (result_holder_) {
result_holder_->did_navigate = true;
result_holder_->navigation_window_features = window_features;
result_holder_->navigation_disposition = updated_disposition;
}
return NavigateResult();
}
void TestPopupNavigationDelegate::OnPopupBlocked(
content::WebContents* web_contents,
int total_popups_blocked_on_page) {
if (result_holder_)
result_holder_->total_popups_blocked_on_page = total_popups_blocked_on_page;
}
} // namespace blocked_content
// 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 COMPONENTS_BLOCKED_CONTENT_TEST_TEST_POPUP_NAVIGATION_DELEGATE_H_
#define COMPONENTS_BLOCKED_CONTENT_TEST_TEST_POPUP_NAVIGATION_DELEGATE_H_
#include "components/blocked_content/popup_navigation_delegate.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
#include "url/gurl.h"
namespace blocked_content {
// Test delegate which stores results of calls in a ResultHolder.
class TestPopupNavigationDelegate : public PopupNavigationDelegate {
public:
// Struct used to hold results from calls on TestPopupNavigationDelegate since
// the delegate will be destroyed on calls to ShowBlockedPopup().
struct ResultHolder {
ResultHolder();
~ResultHolder();
bool did_navigate = false;
blink::mojom::WindowFeatures navigation_window_features;
base::Optional<WindowOpenDisposition> navigation_disposition;
int total_popups_blocked_on_page = 0;
};
TestPopupNavigationDelegate(const GURL& url, ResultHolder* result_holder);
// PopupNavigationDelegate:
content::RenderFrameHost* GetOpener() override;
bool GetOriginalUserGesture() override;
const GURL& GetURL() override;
NavigateResult NavigateWithGesture(
const blink::mojom::WindowFeatures& window_features,
base::Optional<WindowOpenDisposition> updated_disposition) override;
void OnPopupBlocked(content::WebContents* web_contents,
int total_popups_blocked_on_page) override;
private:
const GURL url_;
ResultHolder* result_holder_;
};
} // namespace blocked_content
#endif // COMPONENTS_BLOCKED_CONTENT_TEST_TEST_POPUP_NAVIGATION_DELEGATE_H_
<?xml version="1.0" encoding="utf-8"?>
<grit-part>
<if expr="is_android">
<message name="IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW" desc="Pop-up Blocking Show Button [CHAR-LIMIT=32]">
Always show
</message>
<message name="IDS_POPUPS_BLOCKED_INFOBAR_TEXT" desc="Pop-up Blocking Title [CHAR-LIMIT=32] [ICU Syntax]">
{NUM_POPUPS,plural,=1{Pop-up blocked} other{# pop-ups blocked}}
</message>
</if>
</grit-part>
......@@ -282,6 +282,7 @@
<part file="autofill_strings.grdp" />
<part file="bookmark_bar_strings.grdp" />
<part file="bookmark_component_strings.grdp" />
<part file="blocked_content_strings.grdp" />
<part file="browsing_data_strings.grdp" />
<part file="components_settings_strings.grdp" />
<part file="crash_strings.grdp" />
......
......@@ -100,7 +100,8 @@ HungRendererInfoBarDelegate* InfoBarDelegate::AsHungRendererInfoBarDelegate() {
return nullptr;
}
PopupBlockedInfoBarDelegate* InfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
blocked_content::PopupBlockedInfoBarDelegate*
InfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
return nullptr;
}
......
......@@ -13,9 +13,12 @@
class ConfirmInfoBarDelegate;
class HungRendererInfoBarDelegate;
class PopupBlockedInfoBarDelegate;
class ThemeInstalledInfoBarDelegate;
namespace blocked_content {
class PopupBlockedInfoBarDelegate;
}
#if defined(OS_ANDROID)
namespace offline_pages {
class OfflinePageInfoBarDelegate;
......@@ -256,7 +259,8 @@ class InfoBarDelegate {
// Type-checking downcast routines:
virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate();
virtual HungRendererInfoBarDelegate* AsHungRendererInfoBarDelegate();
virtual PopupBlockedInfoBarDelegate* AsPopupBlockedInfoBarDelegate();
virtual blocked_content::PopupBlockedInfoBarDelegate*
AsPopupBlockedInfoBarDelegate();
virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate();
virtual translate::TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
#if defined(OS_ANDROID)
......
......@@ -108,6 +108,7 @@ action("about_credits") {
if (is_android) {
source_set("android_resources") {
sources = [
"android/blocked_content_resource_id.h",
"android/page_info_resource_id.h",
"android/permissions_resource_id.h",
"android/theme_resources.h",
......
// 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.
// This file maps resource IDs to Android resource IDs.
// Presence of regular include guards is checked by:
// 1. cpplint
// 2. a custom presubmit in src/PRESUBMIT.py
// 3. clang (but it only checks the guard is correct if present)
// Disable the first two with these magic comments:
// NOLINT(build/header_guard)
// no-include-guard-because-multiply-included
// LINK_RESOURCE_ID is used for IDs that come from a .grd file.
#ifndef LINK_RESOURCE_ID
#error "LINK_RESOURCE_ID should be defined before including this file"
#endif
// DECLARE_RESOURCE_ID is used for IDs that don't have .grd entries, and
// are only declared in this file.
#ifndef DECLARE_RESOURCE_ID
#error "DECLARE_RESOURCE_ID should be defined before including this file"
#endif
// InfoBar resources.
DECLARE_RESOURCE_ID(IDR_ANDROID_INFOBAR_BLOCKED_POPUPS,
R.drawable.infobar_blocked_popups)
......@@ -14,6 +14,7 @@ enum {
// Not used; just provides a starting value for the enum. These must
// not conflict with IDR_* values, which top out at 2^16 - 1.
ANDROID_COMPONENTS_RESOURCE_ID_NONE = 1 << 16,
#include "components/resources/android/blocked_content_resource_id.h"
#include "components/resources/android/page_info_resource_id.h"
#include "components/resources/android/permissions_resource_id.h"
ANDROID_COMPONENTS_RESOURCE_ID_MAX,
......
......@@ -38,6 +38,7 @@ void ConstructMap() {
(*GetIdMap())[c_id] = resource_id_list[next_id++];
#define DECLARE_RESOURCE_ID(c_id, java_id) \
(*GetIdMap())[c_id] = resource_id_list[next_id++];
#include "components/resources/android/blocked_content_resource_id.h"
#include "components/resources/android/page_info_resource_id.h"
#include "components/resources/android/permissions_resource_id.h"
#undef LINK_RESOURCE_ID
......
......@@ -26,6 +26,7 @@ android_resources("weblayer_resources") {
custom_package = "org.chromium.weblayer_private"
deps = [
":weblayer_strings_grd",
"//components/blocked_content/android:java_resources",
"//components/browser_ui/http_auth/android:java_resources",
"//components/browser_ui/settings/android:java_resources",
"//components/browser_ui/site_settings/android:java_resources",
......@@ -47,6 +48,7 @@ java_cpp_template("resource_id_javagen") {
sources = [ "ResourceId.template" ]
package_path = "org/chromium/weblayer_private/resources"
inputs = [
"//components/resources/android/blocked_content_resource_id.h",
"//components/resources/android/page_info_resource_id.h",
"//components/resources/android/permissions_resource_id.h",
]
......
......@@ -11,6 +11,7 @@ class ResourceId {
int[] resourceList = {
#define LINK_RESOURCE_ID(c_id,java_id) java_id,
#define DECLARE_RESOURCE_ID(c_id,java_id) java_id,
#include "components/resources/android/blocked_content_resource_id.h"
#include "components/resources/android/page_info_resource_id.h"
#include "components/resources/android/permissions_resource_id.h"
};
......
......@@ -4,7 +4,14 @@
#include "weblayer/browser/popup_navigation_delegate_impl.h"
#include "build/build_config.h"
#include "content/public/browser/web_contents.h"
#include "weblayer/browser/host_content_settings_map_factory.h"
#include "weblayer/browser/infobar_service.h"
#if defined(OS_ANDROID)
#include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
#endif
namespace weblayer {
......@@ -48,8 +55,15 @@ PopupNavigationDelegateImpl::NavigateWithGesture(
void PopupNavigationDelegateImpl::OnPopupBlocked(
content::WebContents* web_contents,
int num_blocked) {
// TODO(crbug.com/1019922): Add popup blocked infobar.
int total_popups_blocked_on_page) {
#if defined(OS_ANDROID)
blocked_content::PopupBlockedInfoBarDelegate::Create(
InfoBarService::FromWebContents(web_contents),
total_popups_blocked_on_page,
HostContentSettingsMapFactory::GetForBrowserContext(
web_contents->GetBrowserContext()),
base::NullCallback());
#endif
}
} // namespace weblayer
......@@ -207,6 +207,8 @@ IDS_PAGE_INFO_USB_DEVICE_ALLOWED_BY_POLICY_LABEL
IDS_PAGE_INFO_USB_DEVICE_SECONDARY_LABEL
IDS_PERMISSION_ALLOW
IDS_PERMISSION_DENY
IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW
IDS_POPUPS_BLOCKED_INFOBAR_TEXT
IDS_PROTECTED_MEDIA_IDENTIFIER_PERMISSION_FRAGMENT
IDS_PROTECTED_MEDIA_IDENTIFIER_PER_DEVICE_PROVISIONING_INFOBAR_TEXT
IDS_PROTECTED_MEDIA_IDENTIFIER_PER_ORIGIN_PROVISIONING_INFOBAR_TEXT
......
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