blob: 6c1848f5f8d719e6b0e4f352a9fd43b0ba055518 [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 Arnolddc4bb262014-07-23 10:45:19 -070060 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070061 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -070062 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080063
Alex Vakulenko157fe302014-08-11 15:59:58 -070064 EvalStatus UpdateDownloadAllowed(
Gilad Arnolddc4bb262014-07-23 10:45:19 -070065 EvaluationContext* ec, State* state, std::string* error,
66 bool* result) const override;
Gilad Arnold0adbc942014-05-12 10:35:43 -070067
Gilad Arnoldb3b05442014-05-30 14:25:05 -070068 protected:
69 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070070 std::string PolicyName() const override { return "DefaultPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070071
Alex Deymoc705cc82014-02-19 11:15:00 -080072 private:
Gilad Arnolda23e4082014-07-17 11:40:43 -070073 // A clock interface.
74 chromeos_update_engine::ClockInterface* clock_;
75
76 // An auxiliary state object.
77 scoped_ptr<DefaultPolicyState> aux_state_;
78
Alex Deymoc705cc82014-02-19 11:15:00 -080079 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
80};
81
Alex Deymo63784a52014-05-28 10:46:14 -070082} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -080083
Gilad Arnold48415f12014-06-27 07:10:58 -070084#endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_