File: scheduling.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 5,764 bytes parent folder | download | duplicates (9)
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
// Copyright 2020 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/feed/core/v2/scheduling.h"

#include "base/json/values_util.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/values.h"
#include "components/feed/core/v2/config.h"
#include "components/feed/core/v2/feedstore_util.h"
#include "components/feed/feed_feature_list.h"

namespace feed {
namespace {

base::Value::List VectorToList(const std::vector<base::TimeDelta>& values) {
  base::Value::List result;
  for (base::TimeDelta delta : values) {
    result.Append(base::TimeDeltaToValue(delta));
  }
  return result;
}

bool ListToVector(const base::Value::List& value,
                  std::vector<base::TimeDelta>* result) {
  for (const base::Value& entry : value) {
    std::optional<base::TimeDelta> delta = base::ValueToTimeDelta(entry);
    if (!delta)
      return false;
    result->push_back(*delta);
  }
  return true;
}

base::TimeDelta GetThresholdTime(base::TimeDelta default_threshold,
                                 base::TimeDelta server_threshold) {
  if (server_threshold <= base::TimeDelta() ||
      server_threshold > default_threshold) {
    return default_threshold;
  }
  return server_threshold;
}

RequestSchedule::Type GetScheduleType(const base::Value* value) {
  if (value && value->is_int()) {
    int int_value = value->GetInt();
    if (int_value >= 0 &&
        int_value <= base::to_underlying(RequestSchedule::Type::kMaxValue)) {
      return static_cast<RequestSchedule::Type>(int_value);
    }
  }
  return RequestSchedule::Type::kScheduledRefresh;
}

}  // namespace

RequestSchedule::RequestSchedule() = default;
RequestSchedule::~RequestSchedule() = default;
RequestSchedule::RequestSchedule(const RequestSchedule&) = default;
RequestSchedule& RequestSchedule::operator=(const RequestSchedule&) = default;
RequestSchedule::RequestSchedule(RequestSchedule&&) = default;
RequestSchedule& RequestSchedule::operator=(RequestSchedule&&) = default;

base::Value::Dict RequestScheduleToDict(const RequestSchedule& schedule) {
  base::Value::Dict result;
  result.Set("anchor", base::TimeToValue(schedule.anchor_time));
  result.Set("offsets", VectorToList(schedule.refresh_offsets));
  result.Set("type", base::to_underlying(schedule.type));
  return result;
}

RequestSchedule RequestScheduleFromDict(const base::Value::Dict& value) {
  RequestSchedule result;
  std::optional<base::Time> anchor = base::ValueToTime(value.Find("anchor"));
  const base::Value::List* offsets = value.FindList("offsets");
  result.type = GetScheduleType(value.Find("type"));

  if (!anchor || !offsets || !ListToVector(*offsets, &result.refresh_offsets))
    return {};
  result.anchor_time = *anchor;
  return result;
}

base::Time NextScheduledRequestTime(base::Time now, RequestSchedule* schedule) {
  if (schedule->refresh_offsets.empty())
    return now + GetFeedConfig().default_background_refresh_interval;
  // Attempt to detect system clock changes. If |anchor_time| is in the future,
  // or too far in the past, we reset |anchor_time| to now.
  if (now < schedule->anchor_time ||
      schedule->anchor_time + base::Days(7) < now) {
    schedule->anchor_time = now;
  }
  while (!schedule->refresh_offsets.empty()) {
    base::Time request_time =
        schedule->anchor_time + schedule->refresh_offsets[0];
    if (request_time <= now) {
      // The schedule time is in the past. This can happen if the scheduled
      // request already ran, or if the scheduled task was missed. Just ignore
      // this fetch so that we don't risk multiple fetches at a time.
      schedule->refresh_offsets.erase(schedule->refresh_offsets.begin());
      continue;
    }
    return request_time;
  }
  return now + GetFeedConfig().default_background_refresh_interval;
}

bool ShouldWaitForNewContent(const feedstore::Metadata& metadata,
                             const StreamType& stream_type,
                             base::TimeDelta content_age,
                             bool is_web_feed_subscriber) {
  const feedstore::Metadata::StreamMetadata* stream_metadata =
      feedstore::FindMetadataForStream(metadata, stream_type);
  if (stream_metadata && stream_metadata->is_known_stale())
    return true;

  base::TimeDelta staleness_threshold = GetFeedConfig().GetStalenessThreshold(
      stream_type, is_web_feed_subscriber);
  if (stream_metadata && stream_metadata->has_content_lifetime()) {
    staleness_threshold = GetThresholdTime(
        staleness_threshold,
        base::Milliseconds(stream_metadata->content_lifetime().stale_age_ms()));
  }

  return content_age > staleness_threshold;
}

bool ContentInvalidFromAge(const feedstore::Metadata& metadata,
                           const StreamType& stream_type,
                           base::TimeDelta content_age,
                           bool is_web_feed_subscriber) {
  const feedstore::Metadata::StreamMetadata* stream_metadata =
      feedstore::FindMetadataForStream(metadata, stream_type);

  base::TimeDelta content_expiration_threshold =
      GetFeedConfig().content_expiration_threshold;
  if (base::FeatureList::IsEnabled(kWebFeedOnboarding) &&
      !is_web_feed_subscriber && stream_type.IsWebFeed()) {
    content_expiration_threshold =
        GetFeedConfig().subscriptionless_content_expiration_threshold;
  }
  if (stream_metadata && stream_metadata->has_content_lifetime()) {
    content_expiration_threshold = GetThresholdTime(
        content_expiration_threshold,
        base::Milliseconds(
            stream_metadata->content_lifetime().invalid_age_ms()));
  }

  return content_age > content_expiration_threshold;
}

}  // namespace feed