blob: 0708ad9c31c9661b55dd74883e20fbe8c2438912 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
danakjc492bf82020-09-09 20:02:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_NAVIGATOR_H_
6#define CONTENT_BROWSER_RENDERER_HOST_NAVIGATOR_H_
7
8#include <memory>
9
Keishi Hattori0e45c022021-11-27 09:25:5210#include "base/memory/raw_ptr.h"
danakjc492bf82020-09-09 20:02:4411#include "base/memory/ref_counted.h"
Carlos Caballero40b0efd2021-01-26 11:55:0012#include "content/browser/renderer_host/navigation_controller_impl.h"
Daniel Cheng390e2a72022-09-28 06:07:5313#include "content/browser/renderer_host/navigation_discard_reason.h"
danakjc492bf82020-09-09 20:02:4414#include "content/common/content_export.h"
15#include "content/common/navigation_client.mojom.h"
danakjc492bf82020-09-09 20:02:4416#include "content/public/browser/navigation_controller.h"
danakjc492bf82020-09-09 20:02:4417#include "mojo/public/cpp/bindings/pending_associated_remote.h"
18#include "mojo/public/cpp/bindings/pending_remote.h"
Lei Zhang698df03c2021-05-21 04:23:3419#include "third_party/abseil-cpp/absl/types/optional.h"
Gyuyoung Kim38e63632021-01-14 13:11:3920#include "third_party/blink/public/common/navigation/impression.h"
Gyuyoung Kimd85bd892021-04-13 02:11:5521#include "third_party/blink/public/mojom/frame/triggering_event_info.mojom-shared.h"
Minggang Wangb9f3fa92021-07-01 15:30:3122#include "third_party/blink/public/mojom/navigation/navigation_params.mojom-forward.h"
danakjc492bf82020-09-09 20:02:4423#include "ui/base/window_open_disposition.h"
24
25class GURL;
danakjc492bf82020-09-09 20:02:4426
27namespace base {
28class TimeTicks;
29}
30
31namespace network {
32class ResourceRequestBody;
33}
34
35namespace content {
36
Carlos Caballero40b0efd2021-01-26 11:55:0037class BrowserContext;
danakjc492bf82020-09-09 20:02:4438class FrameNavigationEntry;
Carlos Caballero40b0efd2021-01-26 11:55:0039class FrameTree;
danakjc492bf82020-09-09 20:02:4440class FrameTreeNode;
Carlos Caballero40b0efd2021-01-26 11:55:0041class NavigationControllerDelegate;
danakjc492bf82020-09-09 20:02:4442class NavigationEntryImpl;
43class NavigationRequest;
Carlos Caballero40b0efd2021-01-26 11:55:0044class NavigatorDelegate;
danakjc492bf82020-09-09 20:02:4445class PrefetchedSignedExchangeCache;
46class RenderFrameHostImpl;
47class WebBundleHandleTracker;
48struct LoadCommittedDetails;
W. James MacLean46cf26212020-10-01 16:43:3749struct UrlInfo;
danakjc492bf82020-09-09 20:02:4450
51// Navigator is responsible for performing navigations in nodes of the
52// FrameTree. Its lifetime is bound to the FrameTree.
53class CONTENT_EXPORT Navigator {
54 public:
Carlos Caballero40b0efd2021-01-26 11:55:0055 Navigator(BrowserContext* browser_context,
56 FrameTree& frame_tree,
57 NavigatorDelegate* delegate,
58 NavigationControllerDelegate* navigation_controller_delegate);
Peter Boström828b9022021-09-21 02:28:4359
60 Navigator(const Navigator&) = delete;
61 Navigator& operator=(const Navigator&) = delete;
62
danakjc492bf82020-09-09 20:02:4463 ~Navigator();
64
65 // This method verifies that a navigation to |url| doesn't commit into a WebUI
66 // process if it is not allowed to. Callers of this method should take one of
67 // two actions if the method returns false:
68 // * When called from browser process logic (e.g. NavigationRequest), this
69 // indicates issues with the navigation logic and the browser process must
70 // be terminated to avoid security issues.
71 // * If the codepath is processing an IPC message from a renderer process,
72 // then the renderer process is misbehaving and must be terminated.
73 // TODO(nasko): Remove the is_renderer_initiated_check parameter when callers
74 // of this method are migrated to use CHECK instead of DumpWithoutCrashing.
Daniel Cheng9c9fa1a2022-01-14 03:42:1175 [[nodiscard]] static bool CheckWebUIRendererDoesNotDisplayNormalURL(
danakjc492bf82020-09-09 20:02:4476 RenderFrameHostImpl* render_frame_host,
W. James MacLean46cf26212020-10-01 16:43:3777 const UrlInfo& url_info,
danakjc492bf82020-09-09 20:02:4478 bool is_renderer_initiated_check);
79
80 static bool ShouldIgnoreIncomingRendererRequest(
81 const NavigationRequest* ongoing_navigation_request,
82 bool has_user_gesture);
83
84 // Returns the delegate of this Navigator.
85 NavigatorDelegate* GetDelegate();
86
danakjc492bf82020-09-09 20:02:4487 // Notifications coming from the RenderFrameHosts ----------------------------
88
danakjc492bf82020-09-09 20:02:4489 // The RenderFrameHostImpl has committed a navigation. The Navigator is
90 // responsible for resetting |navigation_request| at the end of this method
91 // and should not attempt to keep it alive. Note: it is possible that
92 // |navigation_request| is not the NavigationRequest stored in the
93 // RenderFrameHost that just committed. This happens for example when a
94 // same-page navigation commits while another navigation is ongoing. The
95 // Navigator should use the NavigationRequest provided by this method and not
96 // attempt to access the RenderFrameHost's NavigationsRequests.
97 void DidNavigate(RenderFrameHostImpl* render_frame_host,
arthursonzogni73fe3212020-11-17 13:24:0798 const mojom::DidCommitProvisionalLoadParams& params,
danakjc492bf82020-09-09 20:02:4499 std::unique_ptr<NavigationRequest> navigation_request,
100 bool was_within_same_document);
101
102 // Called on a newly created subframe during a history navigation. The browser
103 // process looks up the corresponding FrameNavigationEntry for the new frame
104 // navigates it in the correct process. Returns false if the
105 // FrameNavigationEntry can't be found or the navigation fails.
106 bool StartHistoryNavigationInNewSubframe(
107 RenderFrameHostImpl* render_frame_host,
108 mojo::PendingAssociatedRemote<mojom::NavigationClient>*
109 navigation_client);
110
111 // Navigation requests -------------------------------------------------------
112
113 // Called by the NavigationController to cause the Navigator to navigate to
114 // |navigation_request|. The NavigationController should be called back with
115 // RendererDidNavigate on success or DiscardPendingEntry on failure. The
116 // callbacks should be called in a future iteration of the message loop.
117 void Navigate(std::unique_ptr<NavigationRequest> request,
Lukasz Anforowicz9ee83c272020-12-01 20:14:05118 ReloadType reload_type);
danakjc492bf82020-09-09 20:02:44119
120 // The RenderFrameHostImpl has received a request to open a URL with the
121 // specified |disposition|.
122 void RequestOpenURL(
123 RenderFrameHostImpl* render_frame_host,
124 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:02125 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:45126 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:07127 const absl::optional<url::Origin>& initiator_origin,
danakjc492bf82020-09-09 20:02:44128 const scoped_refptr<network::ResourceRequestBody>& post_body,
129 const std::string& extra_headers,
130 const Referrer& referrer,
131 WindowOpenDisposition disposition,
132 bool should_replace_current_entry,
133 bool user_gesture,
Gyuyoung Kim16a12f52020-12-19 04:24:26134 blink::mojom::TriggeringEventInfo triggering_event_info,
danakjc492bf82020-09-09 20:02:44135 const std::string& href_translate,
136 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:07137 const absl::optional<blink::Impression>& impression);
danakjc492bf82020-09-09 20:02:44138
139 // Called when a document requests a navigation in another document through a
Dave Tapuska2402595f2022-08-03 16:24:21140 // `blink::RemoteFrame`. If `method` is "POST", then `post_body` needs to
141 // specify the request body, otherwise `post_body` should be null.
danakjc492bf82020-09-09 20:02:44142 void NavigateFromFrameProxy(
143 RenderFrameHostImpl* render_frame_host,
144 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:02145 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:45146 int initiator_process_id,
danakjc492bf82020-09-09 20:02:44147 const url::Origin& initiator_origin,
148 SiteInstance* source_site_instance,
149 const Referrer& referrer,
150 ui::PageTransition page_transition,
151 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:25152 blink::NavigationDownloadPolicy download_policy,
danakjc492bf82020-09-09 20:02:44153 const std::string& method,
154 scoped_refptr<network::ResourceRequestBody> post_body,
155 const std::string& extra_headers,
156 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Antonio Sartori2f763d9d2021-04-21 10:04:14157 network::mojom::SourceLocationPtr source_location,
danakjc492bf82020-09-09 20:02:44158 bool has_user_gesture,
jongdeok.kim5de823b32022-06-14 04:37:50159 bool is_form_submission,
Tsuyoshi Horo167ca6432022-03-09 05:16:39160 const absl::optional<blink::Impression>& impression,
Nan Lin944e9b4e2022-04-12 13:51:22161 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:49162 bool is_embedder_initiated_fenced_frame_navigation = false,
Garrett Tanzerbb8db412022-09-27 21:59:46163 bool is_unfenced_top_navigation = false,
164 bool force_new_browsing_instance = false);
danakjc492bf82020-09-09 20:02:44165
166 // Called after BeforeUnloadCompleted callback is invoked from the renderer.
167 // If |frame_tree_node| has a NavigationRequest waiting for the renderer
168 // response, then the request is either started or canceled, depending on the
169 // value of |proceed|.
170 void BeforeUnloadCompleted(FrameTreeNode* frame_tree_node,
171 bool proceed,
172 const base::TimeTicks& proceed_time);
173
174 // Used to start a new renderer-initiated navigation, following a
175 // BeginNavigation IPC from the renderer.
176 void OnBeginNavigation(
177 FrameTreeNode* frame_tree_node,
Minggang Wangb9f3fa92021-07-01 15:30:31178 blink::mojom::CommonNavigationParamsPtr common_params,
179 blink::mojom::BeginNavigationParamsPtr begin_params,
danakjc492bf82020-09-09 20:02:44180 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
181 mojo::PendingAssociatedRemote<mojom::NavigationClient> navigation_client,
danakjc492bf82020-09-09 20:02:44182 scoped_refptr<PrefetchedSignedExchangeCache>
183 prefetched_signed_exchange_cache,
Rakina Zata Amniaf55b5b62022-07-19 23:11:03184 std::unique_ptr<WebBundleHandleTracker> web_bundle_handle_tracker,
185 mojo::PendingReceiver<mojom::NavigationRendererCancellationListener>
186 renderer_cancellation_listener);
danakjc492bf82020-09-09 20:02:44187
188 // Used to restart a navigation that was thought to be same-document in
189 // cross-document mode.
190 void RestartNavigationAsCrossDocument(
191 std::unique_ptr<NavigationRequest> navigation_request);
192
Rakina Zata Amnif8f2bb62022-11-23 05:54:32193 // Cancels the NavigationRequest owned by |frame_tree_node|. Note that this
194 // will only cancel NavigationRequests that haven't reached the "pending
195 // commit" stage yet, as after that the NavigationRequests will no longer be
196 // owned by the FrameTreeNode.
Daniel Cheng390e2a72022-09-28 06:07:53197 void CancelNavigation(FrameTreeNode* frame_tree_node,
198 NavigationDiscardReason reason);
danakjc492bf82020-09-09 20:02:44199
danakjc492bf82020-09-09 20:02:44200 // Called to record the time it took to execute the beforeunload hook for the
Scott Violetb1dd54592021-11-17 03:28:51201 // current navigation. See RenderFrameHostImpl::SendBeforeUnload() for details
202 // on `for_legacy`.
danakjc492bf82020-09-09 20:02:44203 void LogBeforeUnloadTime(base::TimeTicks renderer_before_unload_start_time,
204 base::TimeTicks renderer_before_unload_end_time,
Scott Violetb1dd54592021-11-17 03:28:51205 base::TimeTicks before_unload_sent_time,
206 bool for_legacy);
danakjc492bf82020-09-09 20:02:44207
Katie Dillone893d00f2021-01-22 23:06:43208 // Called to record the time that the RenderFrameHost told the renderer to
209 // commit the current navigation.
210 void LogCommitNavigationSent();
211
Carlos Caballero04aab362021-02-15 17:38:16212 // Returns the NavigationController associated with this Navigator.
Carlos Caballero40b0efd2021-01-26 11:55:00213 NavigationControllerImpl& controller() { return controller_; }
danakjc492bf82020-09-09 20:02:44214
215 private:
216 friend class NavigatorTestWithBrowserSideNavigation;
217
218 // Holds data used to track browser side navigation metrics.
219 struct NavigationMetricsData;
220
221 void RecordNavigationMetrics(
222 const LoadCommittedDetails& details,
arthursonzogni73fe3212020-11-17 13:24:07223 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni6345d2f2021-02-12 04:07:57224 SiteInstance* site_instance,
225 const GURL& original_request_url);
danakjc492bf82020-09-09 20:02:44226
227 // Called when a renderer initiated navigation has started. Returns the
228 // pending NavigationEntry to be used. Either null or a new one owned
229 // NavigationController.
230 NavigationEntryImpl* GetNavigationEntryForRendererInitiatedNavigation(
Minggang Wangb9f3fa92021-07-01 15:30:31231 const blink::mojom::CommonNavigationParams& common_params,
danakjc492bf82020-09-09 20:02:44232 FrameTreeNode* frame_tree_node);
233
234 // Called to record the time it took to execute beforeunload handlers for
235 // renderer-inititated navigations. It records the time it took to execute
236 // beforeunload handlers in the renderer process before sending the
237 // BeginNavigation IPC.
238 void LogRendererInitiatedBeforeUnloadTime(
239 base::TimeTicks renderer_before_unload_start_time,
240 base::TimeTicks renderer_before_unload_end_time);
241
242 // The NavigationController that will keep track of session history for all
243 // RenderFrameHost objects using this Navigator.
Carlos Caballero40b0efd2021-01-26 11:55:00244 NavigationControllerImpl controller_;
danakjc492bf82020-09-09 20:02:44245
246 // Used to notify the object embedding this Navigator about navigation
247 // events. Can be nullptr in tests.
Keishi Hattori0e45c022021-11-27 09:25:52248 raw_ptr<NavigatorDelegate> delegate_;
danakjc492bf82020-09-09 20:02:44249
250 std::unique_ptr<Navigator::NavigationMetricsData> navigation_data_;
danakjc492bf82020-09-09 20:02:44251};
252
253} // namespace content
254
255#endif // CONTENT_BROWSER_RENDERER_HOST_NAVIGATOR_H_