blob: 6d107a2380f30aada08db007eac8ee75601dd739 [file] [log] [blame]
danakjc492bf82020-09-09 20:02:441// Copyright 2019 The Chromium Authors. All rights reserved.
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
Keishi Hattori0e45c022021-11-27 09:25:5210#include "base/memory/raw_ptr.h"
danakjc492bf82020-09-09 20:02:4411#include "base/memory/weak_ptr.h"
Lei Zhang7ab313752021-11-17 01:26:0012#include "content/common/content_export.h"
danakjc492bf82020-09-09 20:02:4413#include "content/public/browser/navigation_throttle.h"
14
15namespace content {
16
17// This class owns the set of NavigationThrottles added to a NavigationHandle.
18// It is responsible for calling the various sets of events on its
19// NavigationThrottle, and notifying its delegate of the results of said events.
20class CONTENT_EXPORT NavigationThrottleRunner {
21 public:
22 // The different event types that can be processed by NavigationThrottles.
23 enum class Event {
24 WillStartRequest,
25 WillRedirectRequest,
26 WillFailRequest,
27 WillProcessResponse,
28 NoEvent,
29 };
30
31 class Delegate {
32 public:
33 // Called when the NavigationThrottleRunner is done processing the
34 // navigation event of type |event|. |result| is the final
35 // NavigationThrottle::ThrottleCheckResult for this event.
36 virtual void OnNavigationEventProcessed(
37 Event event,
38 NavigationThrottle::ThrottleCheckResult result) = 0;
39 };
40
41 NavigationThrottleRunner(Delegate* delegate, int64_t navigation_id);
Peter Boström828b9022021-09-21 02:28:4342
43 NavigationThrottleRunner(const NavigationThrottleRunner&) = delete;
44 NavigationThrottleRunner& operator=(const NavigationThrottleRunner&) = delete;
45
danakjc492bf82020-09-09 20:02:4446 ~NavigationThrottleRunner();
47
48 // Will call the appropriate NavigationThrottle function based on |event| on
49 // all NavigationThrottles owned by this NavigationThrottleRunner.
50 void ProcessNavigationEvent(Event event);
51
52 // Resumes calling the appropriate NavigationThrottle functions for |event_|
53 // on all NavigationThrottles that have not yet been notified.
54 // |resuming_throttle| is the NavigationThrottle that asks for navigation
55 // event processing to be resumed; it should be the one currently deferring
56 // the navigation.
57 void ResumeProcessingNavigationEvent(NavigationThrottle* resuming_throttle);
58
danakjf26536bf2020-09-10 00:46:1359 // Simulates the navigation resuming. Most callers should just let the
60 // deferring NavigationThrottle do the resuming.
danakjc492bf82020-09-09 20:02:4461 void CallResumeForTesting();
62
63 void RegisterNavigationThrottles();
64
65 // Returns the throttle that is currently deferring the navigation (i.e. the
66 // throttle at index |next_index_ -1|). If the handle is not deferred, returns
67 // nullptr;
68 NavigationThrottle* GetDeferringThrottle() const;
69
70 // Takes ownership of |navigation_throttle|. Following this call, any event
71 // processed by the NavigationThrottleRunner will be called on
72 // |navigation_throttle|.
73 void AddThrottle(std::unique_ptr<NavigationThrottle> navigation_throttle);
74
75 private:
76 void ProcessInternal();
77 void InformDelegate(const NavigationThrottle::ThrottleCheckResult& result);
78
Keishi Hattori0e45c022021-11-27 09:25:5279 const raw_ptr<Delegate> delegate_;
danakjc492bf82020-09-09 20:02:4480
81 // A list of Throttles registered for this navigation.
82 std::vector<std::unique_ptr<NavigationThrottle>> throttles_;
83
84 // The index of the next throttle to check.
85 size_t next_index_;
86
87 // The unique id of the navigation which this throttle runner is associated
88 // with.
89 const int64_t navigation_id_;
90
Clark DuVall841e262b2021-03-16 03:33:4991 // The time a throttle started deferring the navigation.
92 base::Time defer_start_time_;
93
danakjc492bf82020-09-09 20:02:4494 // The event currently being processed.
95 Event current_event_ = Event::NoEvent;
96 base::WeakPtrFactory<NavigationThrottleRunner> weak_factory_{this};
danakjc492bf82020-09-09 20:02:4497};
98
99} // namespace content
100
101#endif // CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_