Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 2 | // 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 | |
| 12 | namespace 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. |
| 41 | class CONTENT_EXPORT RendererCancellationThrottle : public NavigationThrottle { |
| 42 | public: |
Takashi Toyoshima | a6f3af181 | 2025-05-29 04:45:59 | [diff] [blame] | 43 | static void MaybeCreateAndAdd(NavigationThrottleRegistry& registry); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 44 | |
| 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 Toyoshima | a6f3af181 | 2025-05-29 04:45:59 | [diff] [blame] | 49 | explicit RendererCancellationThrottle(NavigationThrottleRegistry& registry); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 50 | ~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 Amni | df8d1b4 | 2024-07-26 09:21:25 | [diff] [blame] | 62 | NavigationThrottle::ThrottleCheckResult WillCommitWithoutUrlLoader() override; |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 63 | const char* GetNameForLogging() override; |
| 64 | |
Rakina Zata Amni | df8d1b4 | 2024-07-26 09:21:25 | [diff] [blame] | 65 | NavigationThrottle::ThrottleCheckResult WaitForRendererCancellationIfNeeded(); |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 66 | void OnTimeout(); |
| 67 | void RestartTimeout(); |
| 68 | |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 69 | 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_ |