blob: 697657417f6d37af7f7b72d8eb508c467c6fd8db [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 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_NAVIGATION_THROTTLE_RUNNER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_
7
8#include <stddef.h>
9
Takashi Toyoshima0e58b582024-07-22 05:22:4910#include <optional>
11#include <vector>
12
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
mikt5272967f2024-10-01 10:01:4314#include "base/memory/safety_checks.h"
danakjc492bf82020-09-09 20:02:4415#include "base/memory/weak_ptr.h"
Gabriel Charetted87f10f2022-03-31 00:44:2216#include "base/time/time.h"
Takashi Toyoshima96a07bb2025-06-06 06:45:5817#include "content/browser/renderer_host/navigation_throttle_registry_impl.h"
Lei Zhang7ab313752021-11-17 01:26:0018#include "content/common/content_export.h"
danakjc492bf82020-09-09 20:02:4419#include "content/public/browser/navigation_throttle.h"
danakjc492bf82020-09-09 20:02:4420
21namespace content {
22
Takashi Toyoshima96a07bb2025-06-06 06:45:5823// 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.
27class CONTENT_EXPORT NavigationThrottleRunner {
mikt5272967f2024-10-01 10:01:4328 // Do not remove this macro!
29 // The macro is maintained by the memory safety team.
30 ADVANCED_MEMORY_SAFETY_CHECKS();
31
danakjc492bf82020-09-09 20:02:4432 public:
Takashi Toyoshima96a07bb2025-06-06 06:45:5833 // `registry` should outlive this instance.
34 NavigationThrottleRunner(NavigationThrottleRegistryBase* registry,
Ryan Sturmf972f192022-08-12 21:22:0135 int64_t navigation_id,
36 bool is_primary_main_frame);
Peter Boström828b9022021-09-21 02:28:4337
38 NavigationThrottleRunner(const NavigationThrottleRunner&) = delete;
39 NavigationThrottleRunner& operator=(const NavigationThrottleRunner&) = delete;
40
Takashi Toyoshima96a07bb2025-06-06 06:45:5841 ~NavigationThrottleRunner();
danakjc492bf82020-09-09 20:02:4442
43 // Will call the appropriate NavigationThrottle function based on |event| on
44 // all NavigationThrottles owned by this NavigationThrottleRunner.
Takashi Toyoshima96a07bb2025-06-06 06:45:5845 void ProcessNavigationEvent(NavigationThrottleEvent event);
danakjc492bf82020-09-09 20:02:4446
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
danakjf26536bf2020-09-10 00:46:1354 // Simulates the navigation resuming. Most callers should just let the
55 // deferring NavigationThrottle do the resuming.
danakjc492bf82020-09-09 20:02:4456 void CallResumeForTesting();
57
danakjc492bf82020-09-09 20:02:4458 // 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 Amniaf55b5b62022-07-19 23:11:0363 void set_first_deferral_callback_for_testing(base::OnceClosure callback) {
64 first_deferral_callback_for_testing_ = std::move(callback);
65 }
66
danakjc492bf82020-09-09 20:02:4467 private:
68 void ProcessInternal();
Takashi Toyoshima96a07bb2025-06-06 06:45:5869 void InformRegistry(const NavigationThrottle::ThrottleCheckResult& result);
danakjc492bf82020-09-09 20:02:4470
Ryan Sturmf972f192022-08-12 21:22:0171 // Records UKM about the deferring throttle when the navigation is resumed.
72 void RecordDeferTimeUKM();
73
Takashi Toyoshima96a07bb2025-06-06 06:45:5874 const raw_ref<NavigationThrottleRegistryBase> registry_;
danakjc492bf82020-09-09 20:02:4475
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 DuVall841e262b2021-03-16 03:33:4983 // The time a throttle started deferring the navigation.
84 base::Time defer_start_time_;
85
Takashi Toyoshimab22bde752024-06-21 05:30:2786 // The total duration time that throttles deferred the navigation.
87 base::TimeDelta total_defer_duration_time_;
Takashi Toyoshimaeb3bd1f12024-07-01 08:52:4488 base::TimeDelta total_defer_duration_time_for_request_;
Takashi Toyoshimab22bde752024-06-21 05:30:2789
Takashi Toyoshima0e58b582024-07-22 05:22:4990 // 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 Toyoshimab22bde752024-06-21 05:30:2798 // The total count to know how many times a throttle defer the navigation.
99 size_t defer_count_ = 0;
Takashi Toyoshimaeb3bd1f12024-07-01 08:52:44100 size_t defer_count_for_request_ = 0;
Takashi Toyoshimab22bde752024-06-21 05:30:27101
Rakina Zata Amniaf55b5b62022-07-19 23:11:03102 // 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
danakjc492bf82020-09-09 20:02:44106 // The event currently being processed.
Takashi Toyoshima96a07bb2025-06-06 06:45:58107 NavigationThrottleEvent current_event_ =
108 NavigationThrottleEvent::kNoEvent;
Ryan Sturmf972f192022-08-12 21:22:01109
110 // Whether the navigation is in the primary main frame.
111 bool is_primary_main_frame_ = false;
112
danakjc492bf82020-09-09 20:02:44113 base::WeakPtrFactory<NavigationThrottleRunner> weak_factory_{this};
danakjc492bf82020-09-09 20:02:44114};
115
116} // namespace content
117
118#endif // CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_