danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 1 | // 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 Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame^] | 10 | #include "base/memory/raw_ptr.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
Lei Zhang | 7ab31375 | 2021-11-17 01:26:00 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 13 | #include "content/public/browser/navigation_throttle.h" |
| 14 | |
| 15 | namespace 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. |
| 20 | class 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öm | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 42 | |
| 43 | NavigationThrottleRunner(const NavigationThrottleRunner&) = delete; |
| 44 | NavigationThrottleRunner& operator=(const NavigationThrottleRunner&) = delete; |
| 45 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 46 | ~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 | |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 59 | // Simulates the navigation resuming. Most callers should just let the |
| 60 | // deferring NavigationThrottle do the resuming. |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 61 | 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 Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame^] | 79 | const raw_ptr<Delegate> delegate_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 80 | |
| 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 DuVall | 841e262b | 2021-03-16 03:33:49 | [diff] [blame] | 91 | // The time a throttle started deferring the navigation. |
| 92 | base::Time defer_start_time_; |
| 93 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 94 | // The event currently being processed. |
| 95 | Event current_event_ = Event::NoEvent; |
| 96 | base::WeakPtrFactory<NavigationThrottleRunner> weak_factory_{this}; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace content |
| 100 | |
| 101 | #endif // CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_THROTTLE_RUNNER_H_ |