1aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
2aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Copyright (C) 2014 The Android Open Source Project
3aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
4aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
5aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// you may not use this file except in compliance with the License.
6aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// You may obtain a copy of the License at
7aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
8aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
9aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
10aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Unless required by applicable law or agreed to in writing, software
11aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
12aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// See the License for the specific language governing permissions and
14aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// limitations under the License.
15aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
16a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
17a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold#include "update_engine/update_manager/default_policy.h"
18a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
19a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnoldnamespace {
20a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
21a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold// A fixed minimum interval between consecutive allowed update checks. This
22a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold// needs to be long enough to prevent busywork and/or DDoS attacks on Omaha, but
23a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold// at the same time short enough to allow the machine to update itself
24a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold// reasonably soon.
25a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnoldconst int kCheckIntervalInSeconds = 15 * 60;
26a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
27a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold}  // namespace
28a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
29a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnoldnamespace chromeos_update_manager {
30a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
31a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad ArnoldDefaultPolicy::DefaultPolicy(chromeos_update_engine::ClockInterface* clock)
32a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold    : clock_(clock), aux_state_(new DefaultPolicyState()) {}
33a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
34a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad ArnoldEvalStatus DefaultPolicy::UpdateCheckAllowed(
35a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold    EvaluationContext* ec, State* state, std::string* error,
36a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold    UpdateCheckParams* result) const {
37a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  result->updates_enabled = true;
38a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  result->target_channel.clear();
39d4b303246eae1fcd0b4820657f1d787e8238146fGilad Arnold  result->target_version_prefix.clear();
4044dc3bfd45eaddc1e7b586f8839babe4540418b5Gilad Arnold  result->is_interactive = false;
41a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
42a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  // Ensure that the minimum interval is set. If there's no clock, this defaults
43a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  // to always allowing the update.
44a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  if (!aux_state_->IsLastCheckAllowedTimeSet() ||
45a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold      ec->IsMonotonicTimeGreaterThan(
46a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold          aux_state_->last_check_allowed_time() +
47a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold          base::TimeDelta::FromSeconds(kCheckIntervalInSeconds))) {
48a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold    if (clock_)
49a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold      aux_state_->set_last_check_allowed_time(clock_->GetMonotonicTime());
50a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold    return EvalStatus::kSucceeded;
51a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  }
52a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
53a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold  return EvalStatus::kAskMeAgainLater;
54a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold}
55a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold
56dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad ArnoldEvalStatus DefaultPolicy::UpdateCanStart(
57dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    EvaluationContext* ec,
58dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    State* state,
59dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    std::string* error,
60dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    UpdateDownloadParams* result,
61d78caf9baf2026a356130f4754af2c504f423945Gilad Arnold    const UpdateState update_state) const {
62dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->update_can_start = true;
63dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
64dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->download_url_idx = 0;
6514a9e70709b4cda5afc97ac6219bc660810b2077Gilad Arnold  result->download_url_allowed = true;
66dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->download_url_num_errors = 0;
67b2f9919a0e4a77dd885625cb52ff7322974bd338Gilad Arnold  result->p2p_downloading_allowed = false;
68b2f9919a0e4a77dd885625cb52ff7322974bd338Gilad Arnold  result->p2p_sharing_allowed = false;
69dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->do_increment_failures = false;
70dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->backoff_expiry = base::Time();
71dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->scatter_wait_period = base::TimeDelta();
72dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  result->scatter_check_threshold = 0;
73dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  return EvalStatus::kSucceeded;
74dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold}
75dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold
76dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad ArnoldEvalStatus DefaultPolicy::UpdateDownloadAllowed(
77dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    EvaluationContext* ec,
78dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    State* state,
79dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    std::string* error,
80dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold    bool* result) const {
81dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  *result = true;
82dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold  return EvalStatus::kSucceeded;
83dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold}
84dc4bb268eb6e6ddcd087d5eccfd88c8e92252920Gilad Arnold
8578ecbfc254c574e52cfe63310a801381a0035c43Gilad ArnoldEvalStatus DefaultPolicy::P2PEnabled(
8678ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    EvaluationContext* ec,
8778ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    State* state,
8878ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    std::string* error,
8978ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    bool* result) const {
9078ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  *result = false;
9178ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  return EvalStatus::kSucceeded;
9278ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold}
9378ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold
9478ecbfc254c574e52cfe63310a801381a0035c43Gilad ArnoldEvalStatus DefaultPolicy::P2PEnabledChanged(
9578ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    EvaluationContext* ec,
9678ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    State* state,
9778ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    std::string* error,
9878ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    bool* result,
9978ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold    bool prev_result) const {
10078ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  // This policy will always prohibit P2P, so this is signaling to the caller
10178ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  // that the decision is final (because the current value is the same as the
10278ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  // previous one) and there's no need to issue another call.
10378ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  *result = false;
10478ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold  return EvalStatus::kSucceeded;
10578ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold}
10678ecbfc254c574e52cfe63310a801381a0035c43Gilad Arnold
107a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold}  // namespace chromeos_update_manager
108