blob: 526476ca8491ed1cfe9af4fd698566382b77749b [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
7
8#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -08009
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070010#include <base/time/time.h>
11
Gilad Arnolda23e4082014-07-17 11:40:43 -070012#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/policy.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080014
Alex Deymo63784a52014-05-28 10:46:14 -070015namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Gilad Arnolda23e4082014-07-17 11:40:43 -070017// Auxiliary state class for DefaultPolicy evaluations.
18//
19// IMPORTANT: The use of a state object in policies is generally forbidden, as
20// it was a design decision to keep policy calls side-effect free. We make an
21// exception here to ensure that DefaultPolicy indeed serves as a safe (and
22// secure) fallback option. This practice should be avoided when imlpementing
23// other policies.
24class DefaultPolicyState {
25 public:
26 DefaultPolicyState() {}
27
28 bool IsLastCheckAllowedTimeSet() const {
29 return last_check_allowed_time_ != base::Time::Max();
30 }
31
32 // Sets/returns the point time on the monotonic time scale when the latest
33 // check allowed was recorded.
34 void set_last_check_allowed_time(base::Time timestamp) {
35 last_check_allowed_time_ = timestamp;
36 }
37 base::Time last_check_allowed_time() const {
38 return last_check_allowed_time_;
39 }
40
41 private:
42 base::Time last_check_allowed_time_ = base::Time::Max();
43};
44
Alex Deymoc705cc82014-02-19 11:15:00 -080045// The DefaultPolicy is a safe Policy implementation that doesn't fail. The
46// values returned by this policy are safe default in case of failure of the
Alex Deymo63784a52014-05-28 10:46:14 -070047// actual policy being used by the UpdateManager.
Alex Deymoc705cc82014-02-19 11:15:00 -080048class DefaultPolicy : public Policy {
49 public:
Gilad Arnolda23e4082014-07-17 11:40:43 -070050 explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock);
51 DefaultPolicy() : DefaultPolicy(nullptr) {}
Alex Deymoc705cc82014-02-19 11:15:00 -080052 virtual ~DefaultPolicy() {}
53
54 // Policy overrides.
Alex Vakulenko157fe302014-08-11 15:59:58 -070055 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070056 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnolda23e4082014-07-17 11:40:43 -070057 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070058
Alex Vakulenko157fe302014-08-11 15:59:58 -070059 EvalStatus UpdateCanStart(
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070060 EvaluationContext* ec,
61 State* state,
62 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070063 UpdateDownloadParams* result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070064 const bool interactive,
65 const UpdateState& update_state) const override {
66 result->update_can_start = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070067 result->p2p_allowed = false;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070068 result->download_url_idx = 0;
69 result->download_url_num_failures = 0;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070070 result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
71 result->scatter_wait_period = base::TimeDelta();
72 result->scatter_check_threshold = 0;
Alex Deymoe636c3c2014-03-11 19:02:08 -070073 return EvalStatus::kSucceeded;
Alex Deymoc705cc82014-02-19 11:15:00 -080074 }
75
Alex Vakulenko157fe302014-08-11 15:59:58 -070076 EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -070077 EvaluationContext* ec,
78 State* state,
79 std::string* error,
80 bool* result) const override {
81 *result = true;
82 return EvalStatus::kSucceeded;
83 }
84
Gilad Arnoldb3b05442014-05-30 14:25:05 -070085 protected:
86 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070087 std::string PolicyName() const override { return "DefaultPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070088
Alex Deymoc705cc82014-02-19 11:15:00 -080089 private:
Gilad Arnolda23e4082014-07-17 11:40:43 -070090 // A clock interface.
91 chromeos_update_engine::ClockInterface* clock_;
92
93 // An auxiliary state object.
94 scoped_ptr<DefaultPolicyState> aux_state_;
95
Alex Deymoc705cc82014-02-19 11:15:00 -080096 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
97};
98
Alex Deymo63784a52014-05-28 10:46:14 -070099} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800100
Gilad Arnold48415f12014-06-27 07:10:58 -0700101#endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_