File: prediction_model_fetch_timer.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/optimization_guide/core/prediction_model_fetch_timer.h"

#include "base/rand_util.h"
#include "base/time/default_clock.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/prefs/pref_service.h"

namespace optimization_guide {

namespace {

// Provide a random time delta before fetching models.
base::TimeDelta RandomFetchDelay() {
  return base::RandTimeDelta(features::PredictionModelFetchRandomMinDelay(),
                             features::PredictionModelFetchRandomMaxDelay());
}

}  // namespace

PredictionModelFetchTimer::PredictionModelFetchTimer(
    PrefService* pref_service,
    base::RepeatingCallback<void(void)> fetch_callback)
    : clock_(base::DefaultClock::GetInstance()),
      pref_service_(pref_service),
      fetch_callback_(fetch_callback) {}

PredictionModelFetchTimer::~PredictionModelFetchTimer() = default;

void PredictionModelFetchTimer::NotifyModelFetchAttempt() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  pref_service_->SetInt64(
      prefs::kModelAndFeaturesLastFetchAttempt,
      clock_->Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
}

void PredictionModelFetchTimer::NotifyModelFetchSuccess() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  pref_service_->SetInt64(
      prefs::kModelLastFetchSuccess,
      clock_->Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
}

base::Time PredictionModelFetchTimer::GetLastFetchAttemptTime() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(
      pref_service_->GetInt64(prefs::kModelAndFeaturesLastFetchAttempt)));
}

base::Time PredictionModelFetchTimer::GetLastFetchSuccessTime() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(
      pref_service_->GetInt64(prefs::kModelLastFetchSuccess)));
}

bool PredictionModelFetchTimer::IsFirstModelFetch() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return state_ == PredictionModelFetchTimerState::kFirstFetch;
}

void PredictionModelFetchTimer::OnFetchTimerFired() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_NE(PredictionModelFetchTimerState::kNone, state_);
  fetch_callback_.Run();
}

void PredictionModelFetchTimer::MaybeScheduleFirstModelFetch() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (state_ != PredictionModelFetchTimerState::kNone) {
    return;
  }

  // Add a slight delay to allow the rest of the browser startup process to
  // finish up.
  fetch_timer_.Start(FROM_HERE, features::PredictionModelFetchStartupDelay(),
                     this, &PredictionModelFetchTimer::OnFetchTimerFired);
  state_ = PredictionModelFetchTimerState::kFirstFetch;
}

void PredictionModelFetchTimer::ScheduleFetchOnModelRegistration() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  switch (state_) {
    case PredictionModelFetchTimerState::kNone:
      // If no fetch is scheduled, maybe schedule one.
      MaybeScheduleFirstModelFetch();
      break;
    case PredictionModelFetchTimerState::kPeriodicFetch:
      state_ = PredictionModelFetchTimerState::kNewRegistrationFetch;
      fetch_timer_.Start(
          FROM_HERE, features::PredictionModelNewRegistrationFetchRandomDelay(),
          this, &PredictionModelFetchTimer::OnFetchTimerFired);
      break;
    case PredictionModelFetchTimerState::kFirstFetch:
    case PredictionModelFetchTimerState::kNewRegistrationFetch:
      break;
  }
}

void PredictionModelFetchTimer::SchedulePeriodicModelsFetch() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!fetch_timer_.IsRunning());
  DCHECK_NE(PredictionModelFetchTimerState::kNone, state_);
  const base::TimeDelta time_until_update_time =
      GetLastFetchSuccessTime() + features::PredictionModelFetchInterval() -
      clock_->Now();
  const base::TimeDelta time_until_retry =
      GetLastFetchAttemptTime() + features::PredictionModelFetchRetryDelay() -
      clock_->Now();
  base::TimeDelta fetcher_delay =
      std::max(time_until_update_time, time_until_retry);
  state_ = PredictionModelFetchTimerState::kPeriodicFetch;
  if (fetcher_delay <= base::TimeDelta()) {
    fetch_timer_.Start(FROM_HERE, RandomFetchDelay(), this,
                       &PredictionModelFetchTimer::OnFetchTimerFired);
    return;
  }
  fetch_timer_.Start(FROM_HERE, fetcher_delay, this,
                     &PredictionModelFetchTimer::SchedulePeriodicModelsFetch);
}

void PredictionModelFetchTimer::Stop() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(fetch_timer_.IsRunning());
  fetch_timer_.Stop();
}

PredictionModelFetchTimer::PredictionModelFetchTimerState
PredictionModelFetchTimer::GetStateForTesting() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return state_;
}

const base::OneShotTimer* PredictionModelFetchTimer::GetFetchTimerForTesting()
    const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return &fetch_timer_;
}

void PredictionModelFetchTimer::SetClockForTesting(const base::Clock* clock) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  clock_ = clock;
}

void PredictionModelFetchTimer::ScheduleImmediateFetchForTesting() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  fetch_timer_.Stop();
  fetch_timer_.Start(FROM_HERE, base::Milliseconds(1), this,
                     &PredictionModelFetchTimer::OnFetchTimerFired);
}

}  // namespace optimization_guide