File: security_interstitial_tab_helper.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 (333 lines) | stat: -rw-r--r-- 13,147 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2017 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_tab_helper.h"

#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "components/security_interstitials/content/security_interstitial_page.h"
#include "components/security_interstitials/core/controller_client.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "content/public/browser/navigation_handle.h"

namespace {

bool FrameTypeMayTriggerInterstitial(
    content::NavigationHandle* navigation_handle) {
  return (navigation_handle->GetNavigatingFrameType() ==
              content::FrameType::kPrimaryMainFrame ||
          navigation_handle->IsGuestViewMainFrame() ||
          (navigation_handle->GetNavigatingFrameType() ==
               content::FrameType::kSubframe &&
           navigation_handle->GetParentFrame()->IsActive()));
}

}  // namespace
namespace security_interstitials {

SecurityInterstitialTabHelper::~SecurityInterstitialTabHelper() = default;

void SecurityInterstitialTabHelper::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  if (navigation_handle->IsSameDocument() ||
      !FrameTypeMayTriggerInterstitial(navigation_handle)) {
    return;
  }

  int64_t navigation_id = navigation_handle->GetNavigationId();
  content::FrameTreeNodeId frame_tree_node_id =
      navigation_handle->GetFrameTreeNodeId();
  if (navigation_handle->HasCommitted()) {
    // Prepare any existing interstitial in this frame for removal.
    if (IsInterstitialCommittedForFrame(frame_tree_node_id)) {
      base::UmaHistogramEnumeration("interstitial.CloseReason",
                                    InterstitialCloseReason::NAVIGATE_AWAY);
      blocking_documents_for_committed_navigations_.find(frame_tree_node_id)
          ->second->OnInterstitialClosing();
    }

    if (IsInterstitialPendingForNavigation(navigation_id)) {
      blocking_documents_for_committed_navigations_.insert_or_assign(
          frame_tree_node_id,
          std::move(
              blocking_documents_for_pending_navigations_[navigation_id]));
      // The navigation has been committed, update interstitial state to
      // reflect the interstitial is being shown.
      base::UmaHistogramEnumeration(
          "interstitial.CloseReason",
          InterstitialCloseReason::INTERSTITIAL_SHOWN);
      blocking_documents_for_committed_navigations_.find(frame_tree_node_id)
          ->second->OnInterstitialShown();
      blocking_documents_for_pending_navigations_.erase(navigation_id);
    } else {
      // There is no new interstitial for navigation_id, so clear any previously
      // committed blocking document in the same frame.
      blocking_documents_for_committed_navigations_.erase(frame_tree_node_id);
    }
  } else if (IsInterstitialPendingForNavigation(navigation_id)) {
    // Stop tracking the associated navigation since it has not committed.
    blocking_documents_for_pending_navigations_.erase(navigation_id);
  }

  // Interstitials may change the visibility of the URL or other security state.
  web_contents()->DidChangeVisibleSecurityState();
}

void SecurityInterstitialTabHelper::WebContentsDestroyed() {
  content::FrameTreeNodeId main_frame_tree_node_id =
      web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId();
  // Record a tab closing event if the main frame is displaying an interstitial.
  if (IsInterstitialCommittedForFrame(main_frame_tree_node_id)) {
    base::UmaHistogramEnumeration("interstitial.CloseReason",
                                  InterstitialCloseReason::CLOSE_TAB);
    blocking_documents_for_committed_navigations_.find(main_frame_tree_node_id)
        ->second->OnInterstitialClosing();
  }
  blocking_documents_for_committed_navigations_.clear();
}

void SecurityInterstitialTabHelper::FrameDeleted(
    content::FrameTreeNodeId frame_tree_node_id) {
  content::FrameTreeNodeId main_frame_tree_node_id =
      web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId();
  // Primary main frame interstitial closing is processed on
  // WebContentsDestroyed.
  // TODO(crbug.com/370683964): Investigate assumption in safe browsing that
  // requires main frame interstitials to be cleared in WebContentsDestroyed.
  if (!IsInterstitialCommittedForFrame(frame_tree_node_id) ||
      main_frame_tree_node_id == frame_tree_node_id) {
    return;
  }
  blocking_documents_for_committed_navigations_.find(frame_tree_node_id)
      ->second->OnInterstitialClosing();
  blocking_documents_for_committed_navigations_.erase(frame_tree_node_id);
}

// static
void SecurityInterstitialTabHelper::AssociateBlockingPage(
    content::NavigationHandle* navigation_handle,
    std::unique_ptr<security_interstitials::SecurityInterstitialPage>
        blocking_page) {
  // An interstitial should not be shown in a prerendered page or in a fenced
  // frame. The prerender should just be canceled.
  CHECK(FrameTypeMayTriggerInterstitial(navigation_handle));

  // CreateForWebContents() creates a tab helper if it doesn't yet exist for the
  // WebContents provided by |navigation_handle|.
  auto* web_contents = navigation_handle->GetWebContents();
  SecurityInterstitialTabHelper::CreateForWebContents(web_contents);

  SecurityInterstitialTabHelper* helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents);
  helper->SetBlockingPage(navigation_handle->GetNavigationId(),
                          std::move(blocking_page));
}

// static
void SecurityInterstitialTabHelper::BindInterstitialCommands(
    mojo::PendingAssociatedReceiver<
        security_interstitials::mojom::InterstitialCommands> receiver,
    content::RenderFrameHost* rfh) {
  auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
  if (!web_contents) {
    return;
  }
  auto* tab_helper =
      SecurityInterstitialTabHelper::FromWebContents(web_contents);
  if (!tab_helper) {
    return;
  }
  tab_helper->receivers_.Bind(rfh, std::move(receiver));
}

bool SecurityInterstitialTabHelper::IsInterstitialPendingForNavigation(
    int64_t navigation_id) const {
  return base::Contains(blocking_documents_for_pending_navigations_,
                        navigation_id);
}

bool SecurityInterstitialTabHelper::ShouldDisplayURL() const {
  SecurityInterstitialPage* blocking_page = GetBlockingPageForMainFrame();
  // If the main frame does not display a blocking interstitial then default to
  // being able to display the URL.
  return blocking_page ? blocking_page->ShouldDisplayURL() : true;
}

bool SecurityInterstitialTabHelper::IsDisplayingInterstitial() const {
  return !blocking_documents_for_committed_navigations_.empty();
}

bool SecurityInterstitialTabHelper::HasPendingOrActiveInterstitial() const {
  return !blocking_documents_for_pending_navigations_.empty() ||
         IsDisplayingInterstitial();
}

bool SecurityInterstitialTabHelper::IsInterstitialCommittedForFrame(
    content::FrameTreeNodeId frame_tree_node_id) const {
  return base::Contains(blocking_documents_for_committed_navigations_,
                        frame_tree_node_id);
}

security_interstitials::SecurityInterstitialPage*
SecurityInterstitialTabHelper::GetBlockingPageForFrame(
    content::FrameTreeNodeId frame_tree_node_id) {
  if (IsInterstitialCommittedForFrame(frame_tree_node_id)) {
    return blocking_documents_for_committed_navigations_
        .find(frame_tree_node_id)
        ->second.get();
  }
  return nullptr;
}

security_interstitials::SecurityInterstitialPage*
SecurityInterstitialTabHelper::
    GetBlockingPageForCurrentlyCommittedNavigationForTesting() {
  // TODO(crbug.com/369759355): Support retrieving blocking page by frame ID.
  content::FrameTreeNodeId frame_tree_node_id =
      web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId();
  if (IsInterstitialCommittedForFrame(frame_tree_node_id)) {
    return blocking_documents_for_committed_navigations_
        .find(frame_tree_node_id)
        ->second.get();
  }
  return nullptr;
}

SecurityInterstitialTabHelper::SecurityInterstitialTabHelper(
    content::WebContents* web_contents)
    : WebContentsObserver(web_contents),
      content::WebContentsUserData<SecurityInterstitialTabHelper>(
          *web_contents),
      receivers_(web_contents, this) {}

void SecurityInterstitialTabHelper::SetBlockingPage(
    int64_t navigation_id,
    std::unique_ptr<security_interstitials::SecurityInterstitialPage>
        blocking_page) {
  blocking_documents_for_pending_navigations_[navigation_id] =
      std::move(blocking_page);
}

SecurityInterstitialPage*
SecurityInterstitialTabHelper::GetBlockingPageForCurrentTargetFrame() {
  auto* render_frame_host = receivers_.GetCurrentTargetFrame();
  content::FrameTreeNodeId id = render_frame_host->GetFrameTreeNodeId();
  if (!IsInterstitialCommittedForFrame(id)) {
    // TODO(crbug.com/376688788): Remove this condition. This method should not
    // be invoked if there is no blocking page for the current target frame.
    return nullptr;
  }
  return blocking_documents_for_committed_navigations_.find(id)->second.get();
}

SecurityInterstitialPage*
SecurityInterstitialTabHelper::GetBlockingPageForMainFrame() const {
  content::FrameTreeNodeId frame_tree_node_id =
      web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId();
  if (IsInterstitialCommittedForFrame(frame_tree_node_id)) {
    return blocking_documents_for_committed_navigations_
        .find(frame_tree_node_id)
        ->second.get();
  }
  return nullptr;
}

void SecurityInterstitialTabHelper::HandleCommand(
    security_interstitials::SecurityInterstitialCommand cmd) {
  // TODO(crbug.com/369784619): Currently commands need to be converted to
  // strings before passing them to CommandReceived, which then turns them into
  // integers again, this redundant conversion should be removed.
  //
  // HandleCommand is only called in response to a Mojo message sent from frames
  // that have a committed interstitial. This ensures that the current target
  // frame is present and can process the corresponding command received.
  SecurityInterstitialPage* blocking_page =
      GetBlockingPageForCurrentTargetFrame();
  // TODO(crbug.com/376688788): Remove this check once the statement above is
  // guaranteed.
  if (blocking_page) {
    blocking_page->CommandReceived(base::NumberToString(cmd));
  }
}

void SecurityInterstitialTabHelper::DontProceed() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_DONT_PROCEED);
}

void SecurityInterstitialTabHelper::Proceed() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_PROCEED);
}

void SecurityInterstitialTabHelper::ShowMoreSection() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_SHOW_MORE_SECTION);
}

void SecurityInterstitialTabHelper::OpenHelpCenter() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_OPEN_HELP_CENTER);
}

void SecurityInterstitialTabHelper::OpenDiagnostic() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_OPEN_DIAGNOSTIC);
}

void SecurityInterstitialTabHelper::Reload() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_RELOAD);
}

void SecurityInterstitialTabHelper::OpenDateSettings() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_OPEN_DATE_SETTINGS);
}

void SecurityInterstitialTabHelper::OpenLogin() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_OPEN_LOGIN);
}

void SecurityInterstitialTabHelper::DoReport() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_DO_REPORT);
}

void SecurityInterstitialTabHelper::DontReport() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_DONT_REPORT);
}

void SecurityInterstitialTabHelper::OpenReportingPrivacy() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_OPEN_REPORTING_PRIVACY);
}

void SecurityInterstitialTabHelper::OpenWhitepaper() {
  HandleCommand(
      security_interstitials::SecurityInterstitialCommand::CMD_OPEN_WHITEPAPER);
}

void SecurityInterstitialTabHelper::ReportPhishingError() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_REPORT_PHISHING_ERROR);
}

void SecurityInterstitialTabHelper::OpenEnhancedProtectionSettings() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_OPEN_ENHANCED_PROTECTION_SETTINGS);
}

#if BUILDFLAG(IS_ANDROID)
void SecurityInterstitialTabHelper::OpenAndroidAdvancedProtectionSettings() {
  HandleCommand(security_interstitials::SecurityInterstitialCommand::
                    CMD_OPEN_ANDROID_ADVANCED_PROTECTION_SETTINGS);
}
#endif  // BUILDFLAG(IS_ANDROID)

WEB_CONTENTS_USER_DATA_KEY_IMPL(SecurityInterstitialTabHelper);

}  //  namespace security_interstitials