Commit 4322b3d8 authored by Livvie Lin's avatar Livvie Lin Committed by Commit Bot

Move common lookalike blocking page methods into a component

The methods that record to UKM and populate interstitial strings
will also be used by iOS lookalike interstitials.

Bug: 1058898
Change-Id: I8ecf1eff284181c27eb24942e4b92b726ba51d65
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2269553Reviewed-by: default avatarMustafa Emre Acer <meacer@chromium.org>
Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: Livvie Lin <livvielin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784153}
parent 86766988
...@@ -8,17 +8,14 @@ ...@@ -8,17 +8,14 @@
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
#include "components/grit/components_resources.h" #include "components/grit/components_resources.h"
#include "components/lookalikes/core/lookalike_url_ui_util.h"
#include "components/lookalikes/core/lookalike_url_util.h" #include "components/lookalikes/core/lookalike_url_util.h"
#include "components/security_interstitials/content/security_interstitial_controller_client.h" #include "components/security_interstitials/content/security_interstitial_controller_client.h"
#include "components/security_interstitials/core/common_string_util.h" #include "components/security_interstitials/core/common_string_util.h"
#include "components/security_interstitials/core/metrics_helper.h" #include "components/security_interstitials/core/metrics_helper.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "ui/base/l10n/l10n_util.h"
using security_interstitials::MetricsHelper; using security_interstitials::MetricsHelper;
...@@ -50,30 +47,6 @@ LookalikeUrlBlockingPage::LookalikeUrlBlockingPage( ...@@ -50,30 +47,6 @@ LookalikeUrlBlockingPage::LookalikeUrlBlockingPage(
LookalikeUrlBlockingPage::~LookalikeUrlBlockingPage() = default; LookalikeUrlBlockingPage::~LookalikeUrlBlockingPage() = default;
void LookalikeUrlBlockingPage::ReportUkmIfNeeded(
LookalikeUrlBlockingPageUserAction action) {
// We rely on the saved SourceId because deconstruction happens after the next
// navigation occurs, so web contents points to the new destination.
if (source_id_ != ukm::kInvalidSourceId) {
RecordUkmEvent(source_id_, match_type_, action);
source_id_ = ukm::kInvalidSourceId;
}
}
// static
void LookalikeUrlBlockingPage::RecordUkmEvent(
ukm::SourceId source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction user_action) {
ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
CHECK(ukm_recorder);
ukm::builders::LookalikeUrl_NavigationSuggestion(source_id)
.SetMatchType(static_cast<int>(match_type))
.SetUserAction(static_cast<int>(user_action))
.Record(ukm_recorder);
}
security_interstitials::SecurityInterstitialPage::TypeID security_interstitials::SecurityInterstitialPage::TypeID
LookalikeUrlBlockingPage::GetTypeForTesting() { LookalikeUrlBlockingPage::GetTypeForTesting() {
return LookalikeUrlBlockingPage::kTypeForTesting; return LookalikeUrlBlockingPage::kTypeForTesting;
...@@ -87,49 +60,13 @@ void LookalikeUrlBlockingPage::PopulateInterstitialStrings( ...@@ -87,49 +60,13 @@ void LookalikeUrlBlockingPage::PopulateInterstitialStrings(
base::DictionaryValue* load_time_data) { base::DictionaryValue* load_time_data) {
CHECK(load_time_data); CHECK(load_time_data);
PopulateStringsForSharedHTML(load_time_data); PopulateLookalikeUrlBlockingPageStrings(load_time_data, safe_url_);
load_time_data->SetString("tabTitle",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_TITLE));
load_time_data->SetString(
"optInLink",
l10n_util::GetStringUTF16(IDS_SAFE_BROWSING_SCOUT_REPORTING_AGREE));
if (safe_url_.is_valid()) {
const base::string16 hostname =
security_interstitials::common_string_util::GetFormattedHostName(
safe_url_);
load_time_data->SetString(
"heading",
l10n_util::GetStringFUTF16(IDS_LOOKALIKE_URL_HEADING, hostname));
load_time_data->SetString(
"primaryParagraph",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_PRIMARY_PARAGRAPH));
load_time_data->SetString(
"proceedButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_IGNORE));
load_time_data->SetString(
"primaryButtonText",
l10n_util::GetStringFUTF16(IDS_LOOKALIKE_URL_CONTINUE, hostname));
} else {
// No safe URL available to suggest. This can happen when the navigated
// domain fails IDN spoof checks but isn't a lookalike of a known domain.
// TODO: Change to actual strings.
load_time_data->SetString(
"heading", l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_TITLE));
load_time_data->SetString(
"primaryParagraph",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_PRIMARY_PARAGRAPH));
load_time_data->SetString(
"proceedButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_IGNORE));
load_time_data->SetString(
"primaryButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_BACK_TO_SAFETY));
}
} }
void LookalikeUrlBlockingPage::OnInterstitialClosing() { void LookalikeUrlBlockingPage::OnInterstitialClosing() {
ReportUkmIfNeeded(LookalikeUrlBlockingPageUserAction::kCloseOrBack); ReportUkmForLookalikeUrlBlockingPageIfNeeded(
source_id_, match_type_,
LookalikeUrlBlockingPageUserAction::kCloseOrBack);
} }
bool LookalikeUrlBlockingPage::ShouldDisplayURL() const { bool LookalikeUrlBlockingPage::ShouldDisplayURL() const {
...@@ -152,7 +89,9 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) { ...@@ -152,7 +89,9 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) {
case security_interstitials::CMD_DONT_PROCEED: case security_interstitials::CMD_DONT_PROCEED:
controller()->metrics_helper()->RecordUserDecision( controller()->metrics_helper()->RecordUserDecision(
MetricsHelper::DONT_PROCEED); MetricsHelper::DONT_PROCEED);
ReportUkmIfNeeded(LookalikeUrlBlockingPageUserAction::kAcceptSuggestion); ReportUkmForLookalikeUrlBlockingPageIfNeeded(
source_id_, match_type_,
LookalikeUrlBlockingPageUserAction::kAcceptSuggestion);
// If the interstitial doesn't have a suggested URL (e.g. punycode // If the interstitial doesn't have a suggested URL (e.g. punycode
// interstitial), simply open the new tab page. // interstitial), simply open the new tab page.
if (!safe_url_.is_valid()) { if (!safe_url_.is_valid()) {
...@@ -164,7 +103,9 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) { ...@@ -164,7 +103,9 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) {
case security_interstitials::CMD_PROCEED: case security_interstitials::CMD_PROCEED:
controller()->metrics_helper()->RecordUserDecision( controller()->metrics_helper()->RecordUserDecision(
MetricsHelper::PROCEED); MetricsHelper::PROCEED);
ReportUkmIfNeeded(LookalikeUrlBlockingPageUserAction::kClickThrough); ReportUkmForLookalikeUrlBlockingPageIfNeeded(
source_id_, match_type_,
LookalikeUrlBlockingPageUserAction::kClickThrough);
controller()->Proceed(); controller()->Proceed();
break; break;
case security_interstitials::CMD_DO_REPORT: case security_interstitials::CMD_DO_REPORT:
...@@ -192,18 +133,3 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) { ...@@ -192,18 +133,3 @@ void LookalikeUrlBlockingPage::CommandReceived(const std::string& command) {
int LookalikeUrlBlockingPage::GetHTMLTemplateId() { int LookalikeUrlBlockingPage::GetHTMLTemplateId() {
return IDR_SECURITY_INTERSTITIAL_HTML; return IDR_SECURITY_INTERSTITIAL_HTML;
} }
void LookalikeUrlBlockingPage::PopulateStringsForSharedHTML(
base::DictionaryValue* load_time_data) {
load_time_data->SetBoolean("lookalike_url", true);
load_time_data->SetBoolean("overridable", false);
load_time_data->SetBoolean("hide_primary_button", false);
load_time_data->SetBoolean("show_recurrent_error_paragraph", false);
load_time_data->SetString("recurrentErrorParagraph", "");
load_time_data->SetString("openDetails", "");
load_time_data->SetString("explanationParagraph", "");
load_time_data->SetString("finalParagraph", "");
load_time_data->SetString("type", "LOOKALIKE");
}
...@@ -42,11 +42,6 @@ class LookalikeUrlBlockingPage ...@@ -42,11 +42,6 @@ class LookalikeUrlBlockingPage
security_interstitials::SecurityInterstitialPage::TypeID GetTypeForTesting() security_interstitials::SecurityInterstitialPage::TypeID GetTypeForTesting()
override; override;
// Allow easier reporting of UKM when no interstitial is shown.
static void RecordUkmEvent(ukm::SourceId source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction user_action);
protected: protected:
// SecurityInterstitialPage implementation: // SecurityInterstitialPage implementation:
void CommandReceived(const std::string& command) override; void CommandReceived(const std::string& command) override;
...@@ -60,12 +55,6 @@ class LookalikeUrlBlockingPage ...@@ -60,12 +55,6 @@ class LookalikeUrlBlockingPage
private: private:
friend class LookalikeUrlNavigationThrottleBrowserTest; friend class LookalikeUrlNavigationThrottleBrowserTest;
// Values added to get our shared interstitial HTML to play nice.
void PopulateStringsForSharedHTML(base::DictionaryValue* load_time_data);
// Record UKM iff we haven't already reported for this page.
void ReportUkmIfNeeded(LookalikeUrlBlockingPageUserAction action);
// The URL suggested to the user as the safe URL. Can be empty, in which case // The URL suggested to the user as the safe URL. Can be empty, in which case
// the default action on the interstitial takes the user to the new tab page. // the default action on the interstitial takes the user to the new tab page.
const GURL safe_url_; const GURL safe_url_;
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "chrome/browser/reputation/safety_tips_config.h" #include "chrome/browser/reputation/safety_tips_config.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "components/lookalikes/core/features.h" #include "components/lookalikes/core/features.h"
#include "components/lookalikes/core/lookalike_url_ui_util.h"
#include "components/lookalikes/core/lookalike_url_util.h" #include "components/lookalikes/core/lookalike_url_util.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h" #include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/ukm/content/source_url_recorder.h" #include "components/ukm/content/source_url_recorder.h"
...@@ -415,7 +416,7 @@ ThrottleCheckResult LookalikeUrlNavigationThrottle::PerformChecks( ...@@ -415,7 +416,7 @@ ThrottleCheckResult LookalikeUrlNavigationThrottle::PerformChecks(
return ShowInterstitial(suggested_url, url, source_id, match_type); return ShowInterstitial(suggested_url, url, source_id, match_type);
} }
// Interstitial normally records UKM, but still record when it's not shown. // Interstitial normally records UKM, but still record when it's not shown.
LookalikeUrlBlockingPage::RecordUkmEvent( RecordUkmForLookalikeUrlBlockingPage(
source_id, match_type, source_id, match_type,
LookalikeUrlBlockingPageUserAction::kInterstitialNotShown); LookalikeUrlBlockingPageUserAction::kInterstitialNotShown);
return content::NavigationThrottle::PROCEED; return content::NavigationThrottle::PROCEED;
......
include_rules = [ include_rules = [
"+components/security_interstitials/core",
"+components/security_state", "+components/security_state",
"+components/strings/grit/components_strings.h",
"+components/url_formatter", "+components/url_formatter",
"+net/base", "+net/base",
"+services/metrics/public/cpp",
"+ui/base",
] ]
...@@ -6,18 +6,25 @@ import("//build/config/jumbo.gni") ...@@ -6,18 +6,25 @@ import("//build/config/jumbo.gni")
jumbo_static_library("core") { jumbo_static_library("core") {
sources = [ sources = [
"lookalike_url_ui_util.cc",
"lookalike_url_ui_util.h",
"lookalike_url_util.cc", "lookalike_url_util.cc",
"lookalike_url_util.h", "lookalike_url_util.h",
] ]
deps = [ deps = [
":features", ":features",
"//base", "//base",
"//components/security_interstitials/core",
"//components/security_state/core:features", "//components/security_state/core:features",
"//components/strings",
"//components/url_formatter", "//components/url_formatter",
"//components/url_formatter/spoof_checks/top_domains:common", "//components/url_formatter/spoof_checks/top_domains:common",
"//components/url_formatter/spoof_checks/top_domains:top500_domains", "//components/url_formatter/spoof_checks/top_domains:top500_domains",
"//components/url_formatter/spoof_checks/top_domains:top500_domains_header", "//components/url_formatter/spoof_checks/top_domains:top500_domains_header",
"//net", "//net",
"//services/metrics/public/cpp:metrics_cpp",
"//services/metrics/public/cpp:ukm_builders",
"//ui/base",
] ]
} }
......
// 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/lookalikes/core/lookalike_url_ui_util.h"
#include "components/lookalikes/core/lookalike_url_util.h"
#include "components/security_interstitials/core/common_string_util.h"
#include "components/strings/grit/components_strings.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "ui/base/l10n/l10n_util.h"
void RecordUkmForLookalikeUrlBlockingPage(
ukm::SourceId source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction user_action) {
ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
CHECK(ukm_recorder);
ukm::builders::LookalikeUrl_NavigationSuggestion(source_id)
.SetMatchType(static_cast<int>(match_type))
.SetUserAction(static_cast<int>(user_action))
.Record(ukm_recorder);
}
void ReportUkmForLookalikeUrlBlockingPageIfNeeded(
ukm::SourceId& source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction action) {
// Rely on the saved SourceId because deconstruction happens after the next
// navigation occurs, so web contents points to the new destination.
if (source_id != ukm::kInvalidSourceId) {
RecordUkmForLookalikeUrlBlockingPage(source_id, match_type, action);
source_id = ukm::kInvalidSourceId;
}
}
void PopulateLookalikeUrlBlockingPageStrings(
base::DictionaryValue* load_time_data,
const GURL& safe_url) {
CHECK(load_time_data);
PopulateStringsForSharedHTML(load_time_data);
load_time_data->SetString("tabTitle",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_TITLE));
load_time_data->SetString(
"optInLink",
l10n_util::GetStringUTF16(IDS_SAFE_BROWSING_SCOUT_REPORTING_AGREE));
if (safe_url.is_valid()) {
const base::string16 hostname =
security_interstitials::common_string_util::GetFormattedHostName(
safe_url);
load_time_data->SetString(
"heading",
l10n_util::GetStringFUTF16(IDS_LOOKALIKE_URL_HEADING, hostname));
load_time_data->SetString(
"primaryParagraph",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_PRIMARY_PARAGRAPH));
load_time_data->SetString(
"proceedButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_IGNORE));
load_time_data->SetString(
"primaryButtonText",
l10n_util::GetStringFUTF16(IDS_LOOKALIKE_URL_CONTINUE, hostname));
} else {
// No safe URL available to suggest. This can happen when the navigated
// domain fails IDN spoof checks but isn't a lookalike of a known domain.
// TODO: Change to actual strings.
load_time_data->SetString(
"heading", l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_TITLE));
load_time_data->SetString(
"primaryParagraph",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_PRIMARY_PARAGRAPH));
load_time_data->SetString(
"proceedButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_IGNORE));
load_time_data->SetString(
"primaryButtonText",
l10n_util::GetStringUTF16(IDS_LOOKALIKE_URL_BACK_TO_SAFETY));
}
}
void PopulateStringsForSharedHTML(base::DictionaryValue* load_time_data) {
load_time_data->SetBoolean("lookalike_url", true);
load_time_data->SetBoolean("overridable", false);
load_time_data->SetBoolean("hide_primary_button", false);
load_time_data->SetBoolean("show_recurrent_error_paragraph", false);
load_time_data->SetString("recurrentErrorParagraph", "");
load_time_data->SetString("openDetails", "");
load_time_data->SetString("explanationParagraph", "");
load_time_data->SetString("finalParagraph", "");
load_time_data->SetString("type", "LOOKALIKE");
}
// 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_LOOKALIKES_CORE_LOOKALIKE_URL_UI_UTIL_H_
#define COMPONENTS_LOOKALIKES_CORE_LOOKALIKE_URL_UI_UTIL_H_
#include "components/lookalikes/core/lookalike_url_util.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
namespace base {
class DictionaryValue;
} // namespace base
// Allow easier reporting of UKM when no interstitial is shown.
void RecordUkmForLookalikeUrlBlockingPage(
ukm::SourceId source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction user_action);
// Record UKM if not already reported for this page.
void ReportUkmForLookalikeUrlBlockingPageIfNeeded(
ukm::SourceId& source_id,
LookalikeUrlMatchType match_type,
LookalikeUrlBlockingPageUserAction action);
// Populates |load_time_data| for interstitial HTML.
void PopulateLookalikeUrlBlockingPageStrings(
base::DictionaryValue* load_time_data,
const GURL& safe_url);
// Values added to get shared interstitial HTML to play nice.
void PopulateStringsForSharedHTML(base::DictionaryValue* load_time_data);
#endif // COMPONENTS_LOOKALIKES_CORE_LOOKALIKE_URL_UI_UTIL_H_
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