Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
danakj | c492bf8 | 2020-09-09 20:02:44 | [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_NAVIGATION_THROTTLE_RUNNER_H_ |
| 6 | #define CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
Takashi Toyoshima | 0e58b58 | 2024-07-22 05:22:49 | [diff] [blame] | 10 | #include <optional> |
| 11 | #include <vector> |
| 12 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
mikt | 5272967f | 2024-10-01 10:01:43 | [diff] [blame] | 14 | #include "base/memory/safety_checks.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 16 | #include "base/time/time.h" |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 17 | #include "content/browser/renderer_host/navigation_throttle_registry_impl.h" |
Lei Zhang | 7ab31375 | 2021-11-17 01:26:00 | [diff] [blame] | 18 | #include "content/common/content_export.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 19 | #include "content/public/browser/navigation_throttle.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 20 | |
| 21 | namespace content { |
| 22 | |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 23 | // This class collaborates with NavigationThrottleRegistry that owns the set of |
| 24 | // NavigationThrottles added to an underlying navigation, and is responsible for |
| 25 | // calling the various sets of events on its NavigationThrottles, and notifying |
| 26 | // its delegate of the results of said events. |
| 27 | class CONTENT_EXPORT NavigationThrottleRunner { |
mikt | 5272967f | 2024-10-01 10:01:43 | [diff] [blame] | 28 | // Do not remove this macro! |
| 29 | // The macro is maintained by the memory safety team. |
| 30 | ADVANCED_MEMORY_SAFETY_CHECKS(); |
| 31 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 32 | public: |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 33 | // `registry` should outlive this instance. |
| 34 | NavigationThrottleRunner(NavigationThrottleRegistryBase* registry, |
Ryan Sturm | f972f19 | 2022-08-12 21:22:01 | [diff] [blame] | 35 | int64_t navigation_id, |
| 36 | bool is_primary_main_frame); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 37 | |
| 38 | NavigationThrottleRunner(const NavigationThrottleRunner&) = delete; |
| 39 | NavigationThrottleRunner& operator=(const NavigationThrottleRunner&) = delete; |
| 40 | |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 41 | ~NavigationThrottleRunner(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 42 | |
| 43 | // Will call the appropriate NavigationThrottle function based on |event| on |
| 44 | // all NavigationThrottles owned by this NavigationThrottleRunner. |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 45 | void ProcessNavigationEvent(NavigationThrottleEvent event); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 46 | |
| 47 | // Resumes calling the appropriate NavigationThrottle functions for |event_| |
| 48 | // on all NavigationThrottles that have not yet been notified. |
| 49 | // |resuming_throttle| is the NavigationThrottle that asks for navigation |
| 50 | // event processing to be resumed; it should be the one currently deferring |
| 51 | // the navigation. |
| 52 | void ResumeProcessingNavigationEvent(NavigationThrottle* resuming_throttle); |
| 53 | |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 54 | // Simulates the navigation resuming. Most callers should just let the |
| 55 | // deferring NavigationThrottle do the resuming. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 56 | void CallResumeForTesting(); |
| 57 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 58 | // Returns the throttle that is currently deferring the navigation (i.e. the |
| 59 | // throttle at index |next_index_ -1|). If the handle is not deferred, returns |
| 60 | // nullptr; |
| 61 | NavigationThrottle* GetDeferringThrottle() const; |
| 62 | |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 63 | void set_first_deferral_callback_for_testing(base::OnceClosure callback) { |
| 64 | first_deferral_callback_for_testing_ = std::move(callback); |
| 65 | } |
| 66 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 67 | private: |
| 68 | void ProcessInternal(); |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 69 | void InformRegistry(const NavigationThrottle::ThrottleCheckResult& result); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 70 | |
Ryan Sturm | f972f19 | 2022-08-12 21:22:01 | [diff] [blame] | 71 | // Records UKM about the deferring throttle when the navigation is resumed. |
| 72 | void RecordDeferTimeUKM(); |
| 73 | |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 74 | const raw_ref<NavigationThrottleRegistryBase> registry_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 75 | |
| 76 | // The index of the next throttle to check. |
| 77 | size_t next_index_; |
| 78 | |
| 79 | // The unique id of the navigation which this throttle runner is associated |
| 80 | // with. |
| 81 | const int64_t navigation_id_; |
| 82 | |
Clark DuVall | 841e262b | 2021-03-16 03:33:49 | [diff] [blame] | 83 | // The time a throttle started deferring the navigation. |
| 84 | base::Time defer_start_time_; |
| 85 | |
Takashi Toyoshima | b22bde75 | 2024-06-21 05:30:27 | [diff] [blame] | 86 | // The total duration time that throttles deferred the navigation. |
| 87 | base::TimeDelta total_defer_duration_time_; |
Takashi Toyoshima | eb3bd1f1 | 2024-07-01 08:52:44 | [diff] [blame] | 88 | base::TimeDelta total_defer_duration_time_for_request_; |
Takashi Toyoshima | b22bde75 | 2024-06-21 05:30:27 | [diff] [blame] | 89 | |
Takashi Toyoshima | 0e58b58 | 2024-07-22 05:22:49 | [diff] [blame] | 90 | // The time this runner started ProcessInternal() for the current_event_. |
| 91 | // Should be reset when the processing is done. |
| 92 | std::optional<base::Time> event_process_start_time_; |
| 93 | |
| 94 | // The accumulated time duration this runner took to execute throttles for the |
| 95 | // current_event_. |
| 96 | base::TimeDelta event_process_execution_time_; |
| 97 | |
Takashi Toyoshima | b22bde75 | 2024-06-21 05:30:27 | [diff] [blame] | 98 | // The total count to know how many times a throttle defer the navigation. |
| 99 | size_t defer_count_ = 0; |
Takashi Toyoshima | eb3bd1f1 | 2024-07-01 08:52:44 | [diff] [blame] | 100 | size_t defer_count_for_request_ = 0; |
Takashi Toyoshima | b22bde75 | 2024-06-21 05:30:27 | [diff] [blame] | 101 | |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 102 | // This test-only callback will be run the first time a NavigationThrottle |
| 103 | // defers this navigation. |
| 104 | base::OnceClosure first_deferral_callback_for_testing_; |
| 105 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 106 | // The event currently being processed. |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 107 | NavigationThrottleEvent current_event_ = |
| 108 | NavigationThrottleEvent::kNoEvent; |
Ryan Sturm | f972f19 | 2022-08-12 21:22:01 | [diff] [blame] | 109 | |
| 110 | // Whether the navigation is in the primary main frame. |
| 111 | bool is_primary_main_frame_ = false; |
| 112 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 113 | base::WeakPtrFactory<NavigationThrottleRunner> weak_factory_{this}; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace content |
| 117 | |
| 118 | #endif // CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_ |