File: relaunch_required_timer_internal.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 (71 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/relaunch_notification/relaunch_required_timer_internal.h"

#include <cmath>

namespace relaunch_notification {

base::TimeDelta ComputeDeadlineDelta(base::TimeDelta deadline_offset) {
  // Round deadline_offset to the nearest second for the computations below.
  deadline_offset = base::Seconds(std::round(deadline_offset.InSecondsF()));

  // At/above 47.5 hours, round up to showing N days (min 2).
  // TODO(grt): Explore ways to make this more obvious by way of new methods in
  // base::TimeDelta (e.g., float variants of FromXXX and rounding variants of
  // InXXX).
  static constexpr base::TimeDelta kMinDaysDelta = base::Minutes(47 * 60 + 30);
  // At/above 59.5 minutes, round up to showing N hours (min 1).
  static constexpr base::TimeDelta kMinHoursDelta = base::Seconds(59 * 60 + 30);
  // At/above 59.5 seconds, round up to showing N minutes (min 1).
  static constexpr base::TimeDelta kMinMinutesDelta =
      base::Milliseconds(59 * 1000 + 500);

  // Round based on the time scale.
  if (deadline_offset >= kMinDaysDelta) {
    return base::Days((deadline_offset + base::Hours(12)).InDays());
  }

  if (deadline_offset >= kMinHoursDelta) {
    return base::Hours((deadline_offset + base::Minutes(30)).InHours());
  }

  if (deadline_offset >= kMinMinutesDelta) {
    return base::Minutes((deadline_offset + base::Seconds(30)).InMinutes());
  }

  return base::Seconds((deadline_offset + base::Milliseconds(500)).InSeconds());
}

base::TimeDelta ComputeNextRefreshDelta(base::TimeDelta deadline_offset) {
  // What would be in the title now?
  const base::TimeDelta rounded_offset = ComputeDeadlineDelta(deadline_offset);

  // Compute the refresh moment to bring |rounded_offset| down to the next value
  // to be displayed. This is the moment that the title must switch from N to
  // N-1 of the same units (e.g., # of days) or from one form of units to the
  // next granular form of units (e.g., 2 days to 47 hours).
  // TODO(grt): Find a way to reduce duplication with the constants in
  // ComputeDeadlineDelta once https://crbug.com/761570 is resolved.
  static constexpr base::TimeDelta kMinDays = base::Days(2);
  static constexpr base::TimeDelta kMinHours = base::Hours(1);
  static constexpr base::TimeDelta kMinMinutes = base::Minutes(1);
  static constexpr base::TimeDelta kMinSeconds = base::Seconds(1);

  base::TimeDelta delta;
  if (rounded_offset > kMinDays) {
    delta = base::Days(rounded_offset.InDays() - 1);
  } else if (rounded_offset > kMinHours) {
    delta = base::Hours(rounded_offset.InHours() - 1);
  } else if (rounded_offset > kMinMinutes) {
    delta = base::Minutes(rounded_offset.InMinutes() - 1);
  } else if (rounded_offset > kMinSeconds) {
    delta = base::Seconds(rounded_offset.InSeconds() - 1);
  }

  return deadline_offset - delta;
}

}  // namespace relaunch_notification