[go: nahoru, domu]

blob: f2c0e3e8eb13222e510bf2ccf1ddc24c8e6d54a8 [file] [log] [blame]
Avi Drissman4e1b7bc2022-09-15 14:03:501// Copyright 2022 The Chromium Authors
Alex Turnered592b02022-07-12 20:23:272// 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 McArdle08ad6112023-11-21 20:39:479#include <optional>
Alex Turnered592b02022-07-12 20:23:2710
Avi Drissmanadac21992023-01-11 23:46:3911#include "base/functional/callback_forward.h"
Alex Turnered592b02022-07-12 20:23:2712#include "base/memory/weak_ptr.h"
Andrew Paseltinerc1053e42022-12-06 00:22:0813#include "base/scoped_observation.h"
14#include "base/sequence_checker.h"
15#include "base/thread_annotations.h"
Dan McArdle84d73742023-12-05 21:23:2116#include "base/time/time.h"
Alex Turnered592b02022-07-12 20:23:2717#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 Lin87feb4002023-03-28 18:51:5020#include "services/network/public/mojom/network_change_manager.mojom.h"
Alex Turnered592b02022-07-12 20:23:2721
22namespace base {
23class Time;
24} // namespace base
25
26namespace 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.
34class CONTENT_EXPORT ReportSchedulerTimer
35 : public network::NetworkConnectionTracker::NetworkConnectionObserver {
36 public:
37 class Delegate {
38 public:
39 virtual ~Delegate() = default;
40
Alex Turnera022ec62022-07-20 21:10:2741 // 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 McArdle08ad6112023-11-21 20:39:4743 // next report time, `std::nullopt` should be returned instead.
Alex Turnered592b02022-07-12 20:23:2744 virtual void GetNextReportTime(
Dan McArdle08ad6112023-11-21 20:39:4745 base::OnceCallback<void(std::optional<base::Time>)>,
Alex Turnera022ec62022-07-20 21:10:2746 base::Time now) = 0;
Alex Turnered592b02022-07-12 20:23:2747
Dan McArdle84d73742023-12-05 21:23:2148 // 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 Turnered592b02022-07-12 20:23:2754
Anthony Garant645545392023-03-10 16:20:4455 // 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 Paseltiner2249dd3e2023-03-16 15:50:5158 // `AdjustOfflineReportTimes` will be called.
59 virtual void OnReportingPaused() {}
Anthony Garant645545392023-03-10 16:20:4460
Alex Turnered592b02022-07-12 20:23:2761 // 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 McArdle08ad6112023-11-21 20:39:4767 base::OnceCallback<void(std::optional<base::Time>)>) = 0;
Alex Turnered592b02022-07-12 20:23:2768 };
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 Lin87feb4002023-03-28 18:51:5079 network::mojom::ConnectionType connection_type() const;
80
Alex Turnered592b02022-07-12 20:23:2781 // Schedules `reporting_time_reached_timer_` to fire at that time, unless the
82 // timer is already set to fire earlier.
Dan McArdle08ad6112023-11-21 20:39:4783 void MaybeSet(std::optional<base::Time> reporting_time);
Alex Turnered592b02022-07-12 20:23:2784
Alex Turnered592b02022-07-12 20:23:2785 private:
86 void OnTimerFired();
Andrew Paseltinerc1053e42022-12-06 00:22:0887 void Refresh(base::Time now) VALID_CONTEXT_REQUIRED(sequence_checker_);
Alex Turnered592b02022-07-12 20:23:2788
Dan McArdle78826be2024-02-01 19:02:1589 // 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 Turnered592b02022-07-12 20:23:2794 // network::NetworkConnectionTracker::NetworkConnectionObserver:
Dan McArdle78826be2024-02-01 19:02:1595 void OnConnectionChanged(network::mojom::ConnectionType) final;
Alex Turnered592b02022-07-12 20:23:2796
Nan Lin87feb4002023-03-28 18:51:5097 bool IsOffline() const VALID_CONTEXT_REQUIRED(sequence_checker_);
98
Alex Turnered592b02022-07-12 20:23:2799 // Fires whenever a reporting time is reached for a report. Must be updated
100 // whenever the next report time changes.
Andrew Paseltinerc1053e42022-12-06 00:22:08101 base::WallClockTimer reporting_time_reached_timer_
102 GUARDED_BY_CONTEXT(sequence_checker_);
Alex Turnered592b02022-07-12 20:23:27103
Andrew Paseltinerc1053e42022-12-06 00:22:08104 const std::unique_ptr<Delegate> delegate_
105 GUARDED_BY_CONTEXT(sequence_checker_);
106
Nan Lin87feb4002023-03-28 18:51:50107 network::mojom::ConnectionType connection_type_ GUARDED_BY_CONTEXT(
108 sequence_checker_) = network::mojom::ConnectionType::CONNECTION_NONE;
Andrew Paseltinerc1053e42022-12-06 00:22:08109
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 Turnered592b02022-07-12 20:23:27116
117 base::WeakPtrFactory<ReportSchedulerTimer> weak_ptr_factory_{this};
118};
119
120} // namespace content
121
122#endif // CONTENT_BROWSER_AGGREGATION_SERVICE_REPORT_SCHEDULER_TIMER_H_