default_policy.h revision 610277efc6f7e5239158dfa4bb3b1021804326e0
1// 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
5#ifndef UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
7
8#include <memory>
9#include <string>
10
11#include <base/time/time.h>
12
13#include "update_engine/clock_interface.h"
14#include "update_engine/update_manager/policy.h"
15
16namespace chromeos_update_manager {
17
18// 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
46// 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
48// actual policy being used by the UpdateManager.
49class DefaultPolicy : public Policy {
50 public:
51  explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock);
52  DefaultPolicy() : DefaultPolicy(nullptr) {}
53  ~DefaultPolicy() override {}
54
55  // Policy overrides.
56  EvalStatus UpdateCheckAllowed(
57      EvaluationContext* ec, State* state, std::string* error,
58      UpdateCheckParams* result) const override;
59
60  EvalStatus UpdateCanStart(
61      EvaluationContext* ec, State* state, std::string* error,
62      UpdateDownloadParams* result,
63      UpdateState update_state) const override;
64
65  EvalStatus UpdateDownloadAllowed(
66      EvaluationContext* ec, State* state, std::string* error,
67      bool* result) const override;
68
69  EvalStatus P2PEnabled(
70      EvaluationContext* ec, State* state, std::string* error,
71      bool* result) const override;
72
73  EvalStatus P2PEnabledChanged(
74      EvaluationContext* ec, State* state, std::string* error,
75      bool* result, bool prev_result) const override;
76
77 protected:
78  // Policy override.
79  std::string PolicyName() const override { return "DefaultPolicy"; }
80
81 private:
82  // A clock interface.
83  chromeos_update_engine::ClockInterface* clock_;
84
85  // An auxiliary state object.
86  std::unique_ptr<DefaultPolicyState> aux_state_;
87
88  DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
89};
90
91}  // namespace chromeos_update_manager
92
93#endif  // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
94