-
Notifications
You must be signed in to change notification settings - Fork 19
/
update_time_restrictions_monitor.cc
132 lines (110 loc) · 4.46 KB
/
update_time_restrictions_monitor.cc
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
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/update_manager/update_time_restrictions_monitor.h"
#include <base/bind.h>
#include <base/time/time.h>
#include "update_engine/common/system_state.h"
using base::TimeDelta;
using brillo::MessageLoop;
using chromeos_update_engine::SystemState;
namespace chromeos_update_manager {
namespace {
const WeeklyTimeInterval* FindNextNearestInterval(
const WeeklyTimeIntervalVector& intervals, const WeeklyTime& now) {
const WeeklyTimeInterval* result_interval = nullptr;
// As we are dealing with weekly time here, the maximum duration can be one
// week.
TimeDelta duration_till_next_interval = TimeDelta::FromDays(7);
for (const auto& interval : intervals) {
if (interval.InRange(now)) {
return &interval;
}
const TimeDelta current_duration = now.GetDurationTo(interval.start());
if (current_duration < duration_till_next_interval) {
result_interval = &interval;
duration_till_next_interval = current_duration;
}
}
return result_interval;
}
WeeklyTime Now() {
return WeeklyTime::FromTime(SystemState::Get()->clock()->GetWallclockTime());
}
} // namespace
UpdateTimeRestrictionsMonitor::UpdateTimeRestrictionsMonitor(
DevicePolicyProvider* device_policy_provider, Delegate* delegate)
: evaluation_context_(/* evaluation_timeout = */ TimeDelta::Max(),
/* expiration_timeout = */ TimeDelta::Max(),
/* unregister_cb = */ {}),
device_policy_provider_(device_policy_provider),
delegate_(delegate),
weak_ptr_factory_(this) {
if (device_policy_provider_ != nullptr && delegate_ != nullptr)
StartMonitoring();
}
UpdateTimeRestrictionsMonitor::~UpdateTimeRestrictionsMonitor() {
StopMonitoring();
}
void UpdateTimeRestrictionsMonitor::StartMonitoring() {
DCHECK(device_policy_provider_);
const WeeklyTimeIntervalVector* new_intervals = evaluation_context_.GetValue(
device_policy_provider_->var_disallowed_time_intervals());
if (new_intervals && !new_intervals->empty())
WaitForRestrictedIntervalStarts(*new_intervals);
const bool is_registered = evaluation_context_.RunOnValueChangeOrTimeout(
base::Bind(&UpdateTimeRestrictionsMonitor::OnIntervalsChanged,
base::Unretained(this)));
DCHECK(is_registered);
}
void UpdateTimeRestrictionsMonitor::WaitForRestrictedIntervalStarts(
const WeeklyTimeIntervalVector& restricted_time_intervals) {
DCHECK(!restricted_time_intervals.empty());
const WeeklyTimeInterval* current_interval =
FindNextNearestInterval(restricted_time_intervals, Now());
if (current_interval == nullptr) {
LOG(WARNING) << "Could not find next nearest restricted interval.";
return;
}
// If |current_interval| happens right now, set delay to zero.
const TimeDelta duration_till_start =
current_interval->InRange(Now())
? TimeDelta::FromMicroseconds(0)
: Now().GetDurationTo(current_interval->start());
LOG(INFO) << "Found restricted interval starting at "
<< (SystemState::Get()->clock()->GetWallclockTime() +
duration_till_start);
timeout_event_ = MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&UpdateTimeRestrictionsMonitor::HandleRestrictedIntervalStarts,
weak_ptr_factory_.GetWeakPtr()),
duration_till_start);
}
void UpdateTimeRestrictionsMonitor::HandleRestrictedIntervalStarts() {
timeout_event_ = MessageLoop::kTaskIdNull;
if (delegate_)
delegate_->OnRestrictedIntervalStarts();
}
void UpdateTimeRestrictionsMonitor::StopMonitoring() {
MessageLoop::current()->CancelTask(timeout_event_);
timeout_event_ = MessageLoop::kTaskIdNull;
}
void UpdateTimeRestrictionsMonitor::OnIntervalsChanged() {
DCHECK(!evaluation_context_.is_expired());
StopMonitoring();
evaluation_context_.ResetEvaluation();
StartMonitoring();
}
} // namespace chromeos_update_manager