File: navigation_throttle_registry_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (204 lines) | stat: -rw-r--r-- 9,304 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/navigation_throttle_registry_impl.h"

#include <algorithm>

#include "base/check_deref.h"
#include "base/metrics/histogram_functions.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/preloading/prefetch/contamination_delay_navigation_throttle.h"
#include "content/browser/preloading/prerender/prerender_navigation_throttle.h"
#include "content/browser/preloading/prerender/prerender_subframe_navigation_throttle.h"
#include "content/browser/renderer_host/ancestor_throttle.h"
#include "content/browser/renderer_host/back_forward_cache_subframe_navigation_throttle.h"
#include "content/browser/renderer_host/blocked_scheme_navigation_throttle.h"
#include "content/browser/renderer_host/http_error_navigation_throttle.h"
#include "content/browser/renderer_host/isolated_web_app_throttle.h"
#include "content/browser/renderer_host/mixed_content_navigation_throttle.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/renderer_host/navigator_delegate.h"
#include "content/browser/renderer_host/partitioned_popins/partitioned_popins_navigation_throttle.h"
#include "content/browser/renderer_host/renderer_cancellation_throttle.h"
#include "content/browser/renderer_host/subframe_history_navigation_throttle.h"
#include "content/public/browser/navigation_handle.h"

#if !BUILDFLAG(IS_ANDROID)
#include "content/browser/picture_in_picture/document_picture_in_picture_navigation_throttle.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace content {

NavigationThrottleRegistryBase::~NavigationThrottleRegistryBase() = default;

NavigationThrottleRegistryImpl::NavigationThrottleRegistryImpl(
    NavigationRequest* navigation_request)
    : navigation_request_(CHECK_DEREF(navigation_request)) {}

NavigationThrottleRegistryImpl::~NavigationThrottleRegistryImpl() = default;

void NavigationThrottleRegistryImpl::RegisterNavigationThrottles() {
  TRACE_EVENT0("navigation",
               "NavigationThrottleRegistryImpl::RegisterNavigationThrottles");
  // Note: `throttles_` might not be empty. Some NavigationThrottles might have
  // been registered with RegisterThrottleForTesting. These must reside at the
  // end of `throttles_`. TestNavigationManagerThrottle expects that the
  // NavigationThrottles added for test are the last NavigationThrottles to
  // execute. Take them out while appending the rest of the
  // NavigationThrottles.
  std::vector<std::unique_ptr<NavigationThrottle>> testing_throttles =
      std::move(throttles_);

  // The NavigationRequest associated with the NavigationThrottles this
  // NavigationThrottleRunner manages.
  navigation_request_->GetDelegate()->CreateThrottlesForNavigation(*this);

  // Check for renderer-inititated main frame navigations to blocked URL schemes
  // (data, filesystem). This is done early as it may block the main frame
  // navigation altogether.
  BlockedSchemeNavigationThrottle::MaybeCreateAndAdd(*this);

#if !BUILDFLAG(IS_ANDROID)
  // Prevent cross-document navigations from document picture-in-picture
  // windows.
  DocumentPictureInPictureNavigationThrottle::MaybeCreateAndAdd(*this);
#endif  // !BUILDFLAG(IS_ANDROID)

  AncestorThrottle::CreateAndAdd(*this);

  // Check for mixed content. This is done after the AncestorThrottle and the
  // FormSubmissionThrottle so that when folks block mixed content with a CSP
  // policy, they don't get a warning. They'll still get a warning in the
  // console about CSP blocking the load.
  MixedContentNavigationThrottle::CreateAndAdd(*this);

  // Delay response processing for certain prefetch responses where it might
  // otherwise reveal information about cross-site state.
  ContaminationDelayNavigationThrottle::MaybeCreateAndAdd(*this);

  // Block certain requests that are not permitted for prerendering.
  PrerenderNavigationThrottle::MaybeCreateAndAdd(*this);

  // Defer cross-origin subframe loading during prerendering state.
  PrerenderSubframeNavigationThrottle::MaybeCreateAndAdd(*this);

  // Prevent navigations to/from Isolated Web Apps.
  IsolatedWebAppThrottle::MaybeCreateAndAdd(*this);

  devtools_instrumentation::CreateAndAddNavigationThrottles(*this);

  // Make main frame navigations with error HTTP status code and an empty body
  // commit an error page instead. Note that this should take lower priority
  // than other throttles that might care about those navigations, e.g.
  // throttles handling pages with 407 errors that require extra authentication.
  HttpErrorNavigationThrottle::MaybeCreateAndAdd(*this);

  // Wait for renderer-initiated navigation cancelation window to end. This will
  // wait for the JS task that starts the navigation to finish, so add it close
  // to the end to not delay running other throttles.
  RendererCancellationThrottle::MaybeCreateAndAdd(*this);

  // Defer any cross-document subframe history navigations if there is an
  // associated main-frame same-document history navigation in progress, until
  // the main frame has had an opportunity to fire a navigate event in the
  // renderer. If the navigate event cancels the history navigation, the
  // subframe navigations should not proceed.
  SubframeHistoryNavigationThrottle::MaybeCreateAndAdd(*this);

  // Defer subframe navigation in bfcached page if it hasn't sent a network
  // request.
  // This must be the last throttle to run. See https://crrev.com/c/5316738.
  BackForwardCacheSubframeNavigationThrottle::MaybeCreateAndAdd(*this);

  // Add a throttle to manage top-frame navigations from a partitioned popin.
  // See https://explainers-by-googlers.github.io/partitioned-popins/
  PartitionedPopinsNavigationThrottle::MaybeCreateAndAdd(*this);
  // DO NOT ADD any throttles after this line.

  // Insert all testing NavigationThrottles last.
  throttles_.insert(throttles_.end(),
                    std::make_move_iterator(testing_throttles.begin()),
                    std::make_move_iterator(testing_throttles.end()));

  base::UmaHistogramCounts100("Navigation.ThrottleCount", throttles_.size());
}

void NavigationThrottleRegistryImpl::
    RegisterNavigationThrottlesForCommitWithoutUrlLoader() {
  // Note: `throttles_` might not be empty. Some NavigationThrottles might have
  // been registered with RegisterThrottleForTesting. These must reside at the
  // end of `throttles_`. TestNavigationManagerThrottle expects that the
  // NavigationThrottles added for test are the last NavigationThrottles to
  // execute. Take them out while appending the rest of the
  // NavigationThrottles.
  std::vector<std::unique_ptr<NavigationThrottle>> testing_throttles =
      std::move(throttles_);

  // Defer any same-document subframe history navigations if there is an
  // associated main-frame same-document history navigation in progress, until
  // the main frame has had an opportunity to fire a navigate event in the
  // renderer. If the navigate event cancels the history navigation, the
  // subframe navigations should not proceed.
  SubframeHistoryNavigationThrottle::MaybeCreateAndAdd(*this);

  // Defer cross-origin about:srcdoc subframe loading during prerendering state.
  PrerenderSubframeNavigationThrottle::MaybeCreateAndAdd(*this);

  // Defer subframe navigation in bfcached page.
  BackForwardCacheSubframeNavigationThrottle::MaybeCreateAndAdd(*this);

  RendererCancellationThrottle::MaybeCreateAndAdd(*this);

  // Insert all testing NavigationThrottles last.
  throttles_.insert(throttles_.end(),
                    std::make_move_iterator(testing_throttles.begin()),
                    std::make_move_iterator(testing_throttles.end()));
}

NavigationHandle& NavigationThrottleRegistryImpl::GetNavigationHandle() {
  return *navigation_request_;
}

void NavigationThrottleRegistryImpl::AddThrottle(
    std::unique_ptr<NavigationThrottle> navigation_throttle) {
  CHECK(navigation_throttle);
  TRACE_EVENT1("navigation", "NavigationThrottleRegistryImpl::AddThrottle",
               "navigation_throttle", navigation_throttle->GetNameForLogging());
  throttles_.push_back(std::move(navigation_throttle));
}

bool NavigationThrottleRegistryImpl::HasThrottle(const std::string& name) {
  return std::ranges::find_if(throttles_, [name](const auto& throttle) {
    return throttle->GetNameForLogging() == name;
  }) != throttles_.end();
}

bool NavigationThrottleRegistryImpl::EraseThrottleForTesting(
    const std::string& name) {
  return std::erase_if(throttles_, [name](const auto& throttle) {
    return throttle->GetNameForLogging() == name;
  });
}

void NavigationThrottleRegistryImpl::OnEventProcessed(
    NavigationThrottleEvent event,
    NavigationThrottle::ThrottleCheckResult result) {
  navigation_request_->OnNavigationEventProcessed(event, result);
}

std::vector<std::unique_ptr<NavigationThrottle>>&
NavigationThrottleRegistryImpl::GetThrottles() {
  return throttles_;
}

NavigationThrottle& NavigationThrottleRegistryImpl::GetThrottleAtIndex(
    size_t index) {
  CHECK_LT(index, throttles_.size());
  return *throttles_[index];
}

}  // namespace content