blob: 1673609b256b941965ca488065977bd67a2487c8 [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
Ben Chan02f7c1d2014-10-18 15:18:02 -07008#include <memory>
Gilad Arnold48415f12014-06-27 07:10:58 -07009#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -080010
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070011#include <base/time/time.h>
12
Gilad Arnolda23e4082014-07-17 11:40:43 -070013#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070014#include "update_engine/update_manager/policy.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080015
Alex Deymo63784a52014-05-28 10:46:14 -070016namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080017
Gilad Arnolda23e4082014-07-17 11:40:43 -070018// Auxiliary state class for DefaultPolicy evaluations.
19//
20// IMPORTANT: The use of a state object in policies is generally forbidden, as
21// it was a design decision to keep policy calls side-effect free. We make an
22// exception here to ensure that DefaultPolicy indeed serves as a safe (and
23// secure) fallback option. This practice should be avoided when imlpementing
24// other policies.
25class DefaultPolicyState {
26 public:
27 DefaultPolicyState() {}
28
29 bool IsLastCheckAllowedTimeSet() const {
30 return last_check_allowed_time_ != base::Time::Max();
31 }
32
33 // Sets/returns the point time on the monotonic time scale when the latest
34 // check allowed was recorded.
35 void set_last_check_allowed_time(base::Time timestamp) {
36 last_check_allowed_time_ = timestamp;
37 }
38 base::Time last_check_allowed_time() const {
39 return last_check_allowed_time_;
40 }
41
42 private:
43 base::Time last_check_allowed_time_ = base::Time::Max();
44};
45
Alex Deymoc705cc82014-02-19 11:15:00 -080046// The DefaultPolicy is a safe Policy implementation that doesn't fail. The
47// values returned by this policy are safe default in case of failure of the
Alex Deymo63784a52014-05-28 10:46:14 -070048// actual policy being used by the UpdateManager.
Alex Deymoc705cc82014-02-19 11:15:00 -080049class DefaultPolicy : public Policy {
50 public:
Gilad Arnolda23e4082014-07-17 11:40:43 -070051 explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock);
52 DefaultPolicy() : DefaultPolicy(nullptr) {}
Alex Deymoc705cc82014-02-19 11:15:00 -080053 virtual ~DefaultPolicy() {}
54
55 // Policy overrides.
Alex Vakulenko157fe302014-08-11 15:59:58 -070056 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070057 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnolda23e4082014-07-17 11:40:43 -070058 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070059
Alex Vakulenko157fe302014-08-11 15:59:58 -070060 EvalStatus UpdateCanStart(
Gilad Arnolddc4bb262014-07-23 10:45:19 -070061 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070062 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -070063 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080064
Alex Vakulenko157fe302014-08-11 15:59:58 -070065 EvalStatus UpdateDownloadAllowed(
Gilad Arnolddc4bb262014-07-23 10:45:19 -070066 EvaluationContext* ec, State* state, std::string* error,
67 bool* result) const override;
Gilad Arnold0adbc942014-05-12 10:35:43 -070068
Gilad Arnoldb3b05442014-05-30 14:25:05 -070069 protected:
70 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070071 std::string PolicyName() const override { return "DefaultPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070072
Alex Deymoc705cc82014-02-19 11:15:00 -080073 private:
Gilad Arnolda23e4082014-07-17 11:40:43 -070074 // A clock interface.
75 chromeos_update_engine::ClockInterface* clock_;
76
77 // An auxiliary state object.
Ben Chan02f7c1d2014-10-18 15:18:02 -070078 std::unique_ptr<DefaultPolicyState> aux_state_;
Gilad Arnolda23e4082014-07-17 11:40:43 -070079
Alex Deymoc705cc82014-02-19 11:15:00 -080080 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
81};
82
Alex Deymo63784a52014-05-28 10:46:14 -070083} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -080084
Gilad Arnold48415f12014-06-27 07:10:58 -070085#endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_