blob: ee874f06f4295cd466b2acb380984711ad63798e [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2022 The Chromium Authors
Rakina Zata Amniaf55b5b62022-07-19 23:11:032// 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_RENDERER_CANCELLATION_THROTTLE_H_
6#define CONTENT_BROWSER_RENDERER_HOST_RENDERER_CANCELLATION_THROTTLE_H_
7
8#include "base/timer/timer.h"
9#include "content/common/content_export.h"
10#include "content/public/browser/navigation_throttle.h"
11
12namespace content {
13
14// Renderer-initiated navigations can be canceled from the JS task it was
15// initiated from, e.g. if the script runs window.stop() after initiating the
16// navigation. See also https://github.com/whatwg/html/issues/3447 and
17// https://crbug.com/763106 for more background.
18//
19// The renderer cancels navigation by triggering the disconnection of the
20// NavigationClient interface used to start the navigation, eventually
21// calling `NavigationRequest::OnRendererAbortedNavigation()`.
22//
23// Same-SiteInstanceGroup navigations used to use the same NavigationClient for
24// starting and committing the navigation. This means even if a CommitNavigation
25// IPC is in-flight at the time of navigation cancellation, the navigation can
26// still get canceled. Also, since the same RenderFrame is reused, the
27// CommitNavigation IPC also implicitly waits for the JS task that triggers the
28// navigation to finish, as the commit can't be processed before then.
29//
30// However, with RenderDocument, the RenderFrame and NavigationClient won't be
31// reused, which means navigation cancellations might only affect navigations
32// that haven't entered READY_TO_COMMIT stage.
33//
34// RendererCancellationThrottle helps preserve the previous behavior by waiting
35// for the JS task to finish, through deferring the navigation before it gets
36// into the READY_TO_COMMIT stage, until the renderer that started the
37// navigation sends the `NavigationCancellationWindowEnded` IPC that corresponds
38// to the navigation, signifying that the JS task that initiated the navigation
39// had ended and no renderer-initiated navigation cancellations can happen after
40// that point.
41class CONTENT_EXPORT RendererCancellationThrottle : public NavigationThrottle {
42 public:
Takashi Toyoshimaa6f3af1812025-05-29 04:45:5943 static void MaybeCreateAndAdd(NavigationThrottleRegistry& registry);
Rakina Zata Amniaf55b5b62022-07-19 23:11:0344
45 // Sets the cancellation timeout. Resets the timeout to the default value if
46 // `timeout` is zero.
47 static void SetCancellationTimeoutForTesting(base::TimeDelta timeout);
48
Takashi Toyoshimaa6f3af1812025-05-29 04:45:5949 explicit RendererCancellationThrottle(NavigationThrottleRegistry& registry);
Rakina Zata Amniaf55b5b62022-07-19 23:11:0350 ~RendererCancellationThrottle() override;
51 RendererCancellationThrottle() = delete;
52 RendererCancellationThrottle(const RendererCancellationThrottle&) = delete;
53 RendererCancellationThrottle& operator=(const RendererCancellationThrottle&) =
54 delete;
55
56 // The renderer had indicated that the navigation cancellation window had
57 // ended, so the navigation can resume.
58 void NavigationCancellationWindowEnded();
59
60 private:
61 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override;
Rakina Zata Amnidf8d1b42024-07-26 09:21:2562 NavigationThrottle::ThrottleCheckResult WillCommitWithoutUrlLoader() override;
Rakina Zata Amniaf55b5b62022-07-19 23:11:0363 const char* GetNameForLogging() override;
64
Rakina Zata Amnidf8d1b42024-07-26 09:21:2565 NavigationThrottle::ThrottleCheckResult WaitForRendererCancellationIfNeeded();
Rakina Zata Amniaf55b5b62022-07-19 23:11:0366 void OnTimeout();
67 void RestartTimeout();
68
Rakina Zata Amniaf55b5b62022-07-19 23:11:0369 base::OneShotTimer renderer_cancellation_timeout_timer_;
70
71 base::WeakPtrFactory<RendererCancellationThrottle> weak_factory_{this};
72};
73
74} // namespace content
75
76#endif // CONTENT_BROWSER_RENDERER_HOST_RENDERER_CANCELLATION_THROTTLE_H_