File: security_interstitial_controller_client.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (188 lines) | stat: -rw-r--r-- 7,702 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/security_interstitials/content/security_interstitial_controller_client.h"

#include <utility>

#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/common/safebrowsing_referral_methods.h"
#include "components/security_interstitials/content/security_interstitial_page.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/security_interstitials/content/settings_page_helper.h"
#include "components/security_interstitials/core/metrics_helper.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/referrer.h"

using content::Referrer;

namespace security_interstitials {

SecurityInterstitialControllerClient::SecurityInterstitialControllerClient(
    content::WebContents* web_contents,
    std::unique_ptr<MetricsHelper> metrics_helper,
    PrefService* prefs,
    const std::string& app_locale,
    const GURL& default_safe_page,
    std::unique_ptr<SettingsPageHelper> settings_page_helper)
    : ControllerClient(std::move(metrics_helper)),
      web_contents_(web_contents->GetWeakPtr()),
      prefs_(prefs),
      app_locale_(app_locale),
      default_safe_page_(default_safe_page),
      settings_page_helper_(std::move(settings_page_helper)) {}

SecurityInterstitialControllerClient::~SecurityInterstitialControllerClient() =
    default;

void SecurityInterstitialControllerClient::GoBack() {
  // TODO(crbug.com/40688528): This method is left so class can be non abstract
  // since it is still instantiated in tests. This can be cleaned up by having
  // tests use a subclass.
  NOTREACHED();
}

bool SecurityInterstitialControllerClient::CanGoBack() {
  return web_contents_->GetController().CanGoBack();
}

content::RenderFrameHost*
SecurityInterstitialControllerClient::InterstitialRenderFrameHost() const {
  content::RenderFrameHost* render_frame_host =
      web_contents_->GetPrimaryMainFrame();
  security_interstitials::SecurityInterstitialTabHelper* helper =
      security_interstitials::SecurityInterstitialTabHelper::FromWebContents(
          web_contents_.get());

  // Search to see if this SecurityInterstitialControllerClient is for a
  // <webview> main frame. If it is, and it has a SecurityInterstitial matching
  // this controller, use the NavigationController for the <webview>'s frame
  // tree. Since, when kGuestViewMPArch is enabled, the <webview> isn't the
  // primary main frame of a WebContents, we need to walk through the
  // RenderFrameHosts to see if any of them (i) are for guests, and (ii) have an
  // interstitial page showing whose controller matches this one. If no guest is
  // found, then default to the WebContents' NavigationController.
  web_contents_->ForEachRenderFrameHostWithAction(
      [&render_frame_host, helper, this](content::RenderFrameHost* rfh) {
        if (rfh->GetSiteInstance()->IsGuest()) {
          // Only consider `rfh` if it's for a guest.
          if (auto* blocking_page =
                  helper->GetBlockingPageForFrame(rfh->GetFrameTreeNodeId())) {
            if (blocking_page->controller() == this) {
              // Verify that `rfh` is a main frame for the guest.
              CHECK_EQ(nullptr, rfh->GetParent());
              // `this` corresponds to an interstitial page being shown in a
              // guest frame, so return `rfh`.
              render_frame_host = rfh;
              return content::RenderFrameHost::FrameIterationAction::kStop;
            }
          }
        }
        return content::RenderFrameHost::FrameIterationAction::kContinue;
      });
  return render_frame_host;
}

void SecurityInterstitialControllerClient::GoBackAfterNavigationCommitted() {
  // If the offending entry has committed, go back or to a safe page without
  // closing the error page. This error page will be closed when the new page
  // commits.
  content::RenderFrameHost* render_frame_host = InterstitialRenderFrameHost();
  auto& controller = render_frame_host->GetController();

  if (controller.CanGoBack()) {
    controller.GoBack();
  } else {
    // For <webview> tags (also known as guests), use about:blank as the
    // default safe page. This is because unlike a normal WebContents, guests
    // cannot load pages like WebUI, including the NTP, which is often used as
    // the default safe page here.
    GURL url_to_load = render_frame_host->GetSiteInstance()->IsGuest()
                           ? GURL(url::kAboutBlankURL)
                           : default_safe_page_;
    controller.LoadURL(url_to_load, content::Referrer(),
                       ui::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string());
  }
}

void SecurityInterstitialControllerClient::Proceed() {
  // TODO(crbug.com/40688528): This method is left so class can be non abstract
  // since it is still instantiated in tests. This can be cleaned up by having
  // tests use a subclass.
  NOTREACHED();
}

void SecurityInterstitialControllerClient::Reload() {
  InterstitialRenderFrameHost()->GetController().Reload(
      content::ReloadType::NORMAL, true);
}

void SecurityInterstitialControllerClient::OpenUrlInCurrentTab(
    const GURL& url) {
  content::OpenURLParams params(url, Referrer(),
                                WindowOpenDisposition::CURRENT_TAB,
                                ui::PAGE_TRANSITION_LINK, false);
  web_contents_->OpenURL(params, /*navigation_handle_callback=*/{});
}

void SecurityInterstitialControllerClient::OpenUrlInNewForegroundTab(
    const GURL& url) {
  content::OpenURLParams params(url, Referrer(),
                                WindowOpenDisposition::NEW_FOREGROUND_TAB,
                                ui::PAGE_TRANSITION_LINK, false);
  web_contents_->OpenURL(params, /*navigation_handle_callback=*/{});
}

void SecurityInterstitialControllerClient::OpenEnhancedProtectionSettings() {
#if BUILDFLAG(IS_ANDROID)
  settings_page_helper_->OpenEnhancedProtectionSettings(&*web_contents_);
#else
  settings_page_helper_->OpenEnhancedProtectionSettingsWithIph(
      &*web_contents_,
      safe_browsing::SafeBrowsingSettingReferralMethod::kSecurityInterstitial);
#endif
}

#if BUILDFLAG(IS_ANDROID)
void SecurityInterstitialControllerClient::OpenAdvancedProtectionSettings() {
  settings_page_helper_->OpenAdvancedProtectionSettings(*web_contents_);
}
#endif

const std::string& SecurityInterstitialControllerClient::GetApplicationLocale()
    const {
  return app_locale_;
}

PrefService* SecurityInterstitialControllerClient::GetPrefService() {
  return prefs_;
}

const std::string
SecurityInterstitialControllerClient::GetExtendedReportingPrefName() const {
  return prefs::kSafeBrowsingScoutReportingEnabled;
}

bool SecurityInterstitialControllerClient::CanLaunchDateAndTimeSettings() {
  NOTREACHED();
}

void SecurityInterstitialControllerClient::LaunchDateAndTimeSettings() {
  NOTREACHED();
}

bool SecurityInterstitialControllerClient::CanGoBackBeforeNavigation() {
  // If checking before navigating to the interstitial, back to safety is
  // possible if the current entry is not the initial NavigationEtry. This
  // preserves old behavior to when we return nullptr instead of the initial
  // entry when no navigation has committed.
  return !web_contents_->GetController()
              .GetLastCommittedEntry()
              ->IsInitialEntry();
}

}  // namespace security_interstitials