1 //
2 // Copyright (C) 2017 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_
18 #define UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_
19 
20 #include <string>
21 
22 #include <base/time/time.h>
23 
24 #include "update_engine/update_manager/policy_utils.h"
25 #include "update_engine/update_manager/prng.h"
26 
27 namespace chromeos_update_manager {
28 
29 // Constants that are provided to the policy implementation.
30 struct NextUpdateCheckPolicyConstants {
31   // Default update check timeout interval/fuzz values used to compute the
32   // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
33   // indicated value.
34   int timeout_initial_interval;
35   int timeout_periodic_interval;
36   int timeout_max_backoff_interval;
37   int timeout_regular_fuzz;
38 
39   // Maximum update attempt backoff interval and fuzz.
40   int attempt_backoff_max_interval_in_days;
41   int attempt_backoff_fuzz_in_hours;
42 };
43 
44 // Ensure that periodic update checks are timed properly.
45 class NextUpdateCheckTimePolicyImpl : public PolicyImplBase {
46  public:
47   explicit NextUpdateCheckTimePolicyImpl(
48       const NextUpdateCheckPolicyConstants& constants);
49 
50   // Policy overrides.
51   EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
52                                 State* state,
53                                 std::string* error,
54                                 UpdateCheckParams* result) const override;
55 
56   // A private policy implementation returning the wallclock timestamp when
57   // the next update check should happen.
58   // TODO(garnold) We should probably change that to infer a monotonic
59   // timestamp, which will make the update check intervals more resilient to
60   // clock skews. Might require switching some of the variables exported by the
61   // UpdaterProvider to report monotonic time, as well.
62   //
63   // NOTE:
64   // Exposed as a public static so that it's logic can be used to test
65   // Policy implementations that utilize this fragment for their
66   // timing, without needing to list them all with FRIEND_TEST (so that
67   // those Policy implementations can exist without modifying this
68   // class's definition.
69   //
70   // The output value from this method (|next_update_check|), isn't
71   // available via the UpdateCheckParams |result| of the Policy
72   // method, and so this timing logic needs to be otherwise exposed.
73   static EvalStatus NextUpdateCheckTime(
74       EvaluationContext* ec,
75       State* state,
76       std::string* error,
77       base::Time* next_update_check,
78       const NextUpdateCheckPolicyConstants& constants);
79 
80   // Returns a TimeDelta based on the provided |interval| seconds +/- half
81   // |fuzz| seconds. The return value is guaranteed to be a non-negative
82   // TimeDelta.
83   static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
84 
85  protected:
86   std::string PolicyName() const override {
87     return "NextUpdateCheckTimePolicyImpl";
88   }
89 
90  private:
91   const NextUpdateCheckPolicyConstants policy_constants_;
92 
93   DISALLOW_COPY_AND_ASSIGN(NextUpdateCheckTimePolicyImpl);
94 };
95 
96 }  // namespace chromeos_update_manager
97 
98 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_NEXT_UPDATE_CHECK_POLICY_IMPL_H_
99