Avi Drissman | 4e1b7bc | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [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_AGGREGATION_SERVICE_REPORT_SCHEDULER_TIMER_H_ |
| 6 | #define CONTENT_BROWSER_AGGREGATION_SERVICE_REPORT_SCHEDULER_TIMER_H_ |
| 7 | |
| 8 | #include <memory> |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 9 | #include <optional> |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 10 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 11 | #include "base/functional/callback_forward.h" |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
Andrew Paseltiner | c1053e4 | 2022-12-06 00:22:08 | [diff] [blame] | 13 | #include "base/scoped_observation.h" |
| 14 | #include "base/sequence_checker.h" |
| 15 | #include "base/thread_annotations.h" |
Dan McArdle | 84d7374 | 2023-12-05 21:23:21 | [diff] [blame] | 16 | #include "base/time/time.h" |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 17 | #include "base/timer/wall_clock_timer.h" |
| 18 | #include "content/common/content_export.h" |
| 19 | #include "services/network/public/cpp/network_connection_tracker.h" |
Nan Lin | 87feb400 | 2023-03-28 18:51:50 | [diff] [blame] | 20 | #include "services/network/public/mojom/network_change_manager.mojom.h" |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | class Time; |
| 24 | } // namespace base |
| 25 | |
| 26 | namespace content { |
| 27 | |
| 28 | // This class consolidates logic regarding when to schedule the browser to send |
| 29 | // reports for APIs using the aggregation service and for event-level |
| 30 | // attribution reporting. This includes handling network changes and browser |
| 31 | // startup. It delegates certain operations to API-specific implementations. |
| 32 | // TODO(alexmt): Consider moving out of the aggregation_service directory to a |
| 33 | // separate shared directory. |
| 34 | class CONTENT_EXPORT ReportSchedulerTimer |
| 35 | : public network::NetworkConnectionTracker::NetworkConnectionObserver { |
| 36 | public: |
| 37 | class Delegate { |
| 38 | public: |
| 39 | virtual ~Delegate() = default; |
| 40 | |
Alex Turner | a022ec6 | 2022-07-20 21:10:27 | [diff] [blame] | 41 | // Should be overridden with a method that gets the next report time that |
| 42 | // the timer should fire at and returns it via the callback. If there is no |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 43 | // next report time, `std::nullopt` should be returned instead. |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 44 | virtual void GetNextReportTime( |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 45 | base::OnceCallback<void(std::optional<base::Time>)>, |
Alex Turner | a022ec6 | 2022-07-20 21:10:27 | [diff] [blame] | 46 | base::Time now) = 0; |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 47 | |
Dan McArdle | 84d7374 | 2023-12-05 21:23:21 | [diff] [blame] | 48 | // Called when the timer is fired, with the current time `now` and the time |
| 49 | // the timer was instructed to fire `timer_desired_run_time`. `Refresh()` is |
| 50 | // automatically called after. If this causes a `GetNextReportTime()` call, |
| 51 | // that will be passed the same `now`. |
| 52 | virtual void OnReportingTimeReached(base::Time now, |
| 53 | base::Time timer_desired_run_time) = 0; |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 54 | |
Anthony Garant | 64554539 | 2023-03-10 16:20:44 | [diff] [blame] | 55 | // Called when the connection changes from online to offline. When this |
| 56 | // happens the timer is paused which means `OnReportingTimeReached` will not |
| 57 | // be called until it gets resumed. Before resuming the timer, |
Andrew Paseltiner | 2249dd3e | 2023-03-16 15:50:51 | [diff] [blame] | 58 | // `AdjustOfflineReportTimes` will be called. |
| 59 | virtual void OnReportingPaused() {} |
Anthony Garant | 64554539 | 2023-03-10 16:20:44 | [diff] [blame] | 60 | |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 61 | // Called when the connection changes from offline to online. May also be |
| 62 | // called on a connection change if there are no stored reports, see |
| 63 | // `OnConnectionChanged()`. Running the callback will call `MaybeSet()` with |
| 64 | // the given argument; this may be necessary after the report times were |
| 65 | // adjusted. |
| 66 | virtual void AdjustOfflineReportTimes( |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 67 | base::OnceCallback<void(std::optional<base::Time>)>) = 0; |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | explicit ReportSchedulerTimer(std::unique_ptr<Delegate> delegate); |
| 71 | |
| 72 | ReportSchedulerTimer(const ReportSchedulerTimer&) = delete; |
| 73 | ReportSchedulerTimer& operator=(const ReportSchedulerTimer&) = delete; |
| 74 | ReportSchedulerTimer(ReportSchedulerTimer&&) = delete; |
| 75 | ReportSchedulerTimer& operator=(ReportSchedulerTimer&&) = delete; |
| 76 | |
| 77 | ~ReportSchedulerTimer() override; |
| 78 | |
Nan Lin | 87feb400 | 2023-03-28 18:51:50 | [diff] [blame] | 79 | network::mojom::ConnectionType connection_type() const; |
| 80 | |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 81 | // Schedules `reporting_time_reached_timer_` to fire at that time, unless the |
| 82 | // timer is already set to fire earlier. |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 83 | void MaybeSet(std::optional<base::Time> reporting_time); |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 84 | |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 85 | private: |
| 86 | void OnTimerFired(); |
Andrew Paseltiner | c1053e4 | 2022-12-06 00:22:08 | [diff] [blame] | 87 | void Refresh(base::Time now) VALID_CONTEXT_REQUIRED(sequence_checker_); |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 88 | |
Dan McArdle | 78826be | 2024-02-01 19:02:15 | [diff] [blame] | 89 | // This method is marked `final` to enable the constructor to call it while |
| 90 | // complying with the style guide, which forbids constructors from making |
| 91 | // virtual method calls. |
| 92 | // https://google.github.io/styleguide/cppguide.html#Doing_Work_in_Constructors |
| 93 | // |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 94 | // network::NetworkConnectionTracker::NetworkConnectionObserver: |
Dan McArdle | 78826be | 2024-02-01 19:02:15 | [diff] [blame] | 95 | void OnConnectionChanged(network::mojom::ConnectionType) final; |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 96 | |
Nan Lin | 87feb400 | 2023-03-28 18:51:50 | [diff] [blame] | 97 | bool IsOffline() const VALID_CONTEXT_REQUIRED(sequence_checker_); |
| 98 | |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 99 | // Fires whenever a reporting time is reached for a report. Must be updated |
| 100 | // whenever the next report time changes. |
Andrew Paseltiner | c1053e4 | 2022-12-06 00:22:08 | [diff] [blame] | 101 | base::WallClockTimer reporting_time_reached_timer_ |
| 102 | GUARDED_BY_CONTEXT(sequence_checker_); |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 103 | |
Andrew Paseltiner | c1053e4 | 2022-12-06 00:22:08 | [diff] [blame] | 104 | const std::unique_ptr<Delegate> delegate_ |
| 105 | GUARDED_BY_CONTEXT(sequence_checker_); |
| 106 | |
Nan Lin | 87feb400 | 2023-03-28 18:51:50 | [diff] [blame] | 107 | network::mojom::ConnectionType connection_type_ GUARDED_BY_CONTEXT( |
| 108 | sequence_checker_) = network::mojom::ConnectionType::CONNECTION_NONE; |
Andrew Paseltiner | c1053e4 | 2022-12-06 00:22:08 | [diff] [blame] | 109 | |
| 110 | base::ScopedObservation< |
| 111 | network::NetworkConnectionTracker, |
| 112 | network::NetworkConnectionTracker::NetworkConnectionObserver> |
| 113 | obs_ GUARDED_BY_CONTEXT(sequence_checker_){this}; |
| 114 | |
| 115 | SEQUENCE_CHECKER(sequence_checker_); |
Alex Turner | ed592b0 | 2022-07-12 20:23:27 | [diff] [blame] | 116 | |
| 117 | base::WeakPtrFactory<ReportSchedulerTimer> weak_ptr_factory_{this}; |
| 118 | }; |
| 119 | |
| 120 | } // namespace content |
| 121 | |
| 122 | #endif // CONTENT_BROWSER_AGGREGATION_SERVICE_REPORT_SCHEDULER_TIMER_H_ |