1// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/components/security_interstitials/lookalikes/lookalike_url_controller_client.h"
6
7#include "components/security_interstitials/core/metrics_helper.h"
8#include "ios/components/security_interstitials/ios_blocking_page_metrics_helper.h"
9#include "ios/components/security_interstitials/lookalikes/lookalike_url_tab_allow_list.h"
10#import "ios/web/public/web_state.h"
11
12#if !defined(__has_feature) || !__has_feature(objc_arc)
13#error "This file requires ARC support."
14#endif
15
16namespace {
17// Creates a metrics helper for |url|.
18std::unique_ptr<security_interstitials::IOSBlockingPageMetricsHelper>
19CreateMetricsHelper(web::WebState* web_state, const GURL& url) {
20  security_interstitials::MetricsHelper::ReportDetails reporting_info;
21  reporting_info.metric_prefix = "lookalike";
22  return std::make_unique<security_interstitials::IOSBlockingPageMetricsHelper>(
23      web_state, url, reporting_info);
24}
25}  // namespace
26
27LookalikeUrlControllerClient::LookalikeUrlControllerClient(
28    web::WebState* web_state,
29    const GURL& safe_url,
30    const GURL& request_url,
31    const std::string& app_locale)
32    : IOSBlockingPageControllerClient(
33          web_state,
34          CreateMetricsHelper(web_state, request_url),
35          app_locale),
36      safe_url_(safe_url),
37      request_url_(request_url) {}
38
39LookalikeUrlControllerClient::~LookalikeUrlControllerClient() {}
40
41void LookalikeUrlControllerClient::GoBack() {
42  // If the interstitial doesn't have a suggested URL (e.g. punycode
43  // interstitial), either go back or close the tab. If there is a
44  // suggested URL, instead of a 'go back' option, redirect to the
45  // legitimate site.
46  if (!safe_url_.is_valid()) {
47    IOSBlockingPageControllerClient::GoBack();
48  } else {
49    // For simplicity and because replacement doesn't always work, the
50    // navigation to the safe URL does not replace the navigation to
51    // the interstitial. However, this is acceptable since if a user
52    // navigates back to the lookalike, the interstitial will be shown.
53    OpenUrlInCurrentTab(safe_url_);
54  }
55}
56
57void LookalikeUrlControllerClient::Proceed() {
58  LookalikeUrlTabAllowList::FromWebState(web_state())
59      ->AllowDomain(request_url_.host());
60  Reload();
61}
62