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//
16c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo
1763784a578dd26880454d70797519358a2326291bAlex Deymo#include "update_engine/update_manager/update_manager.h"
187b948f060281eb1f9ab198bce67da5aadf03df60Alex Deymo
1963784a578dd26880454d70797519358a2326291bAlex Deymo#include "update_engine/update_manager/chromeos_policy.h"
2063784a578dd26880454d70797519358a2326291bAlex Deymo#include "update_engine/update_manager/state.h"
21c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo
2263784a578dd26880454d70797519358a2326291bAlex Deymonamespace chromeos_update_manager {
237b948f060281eb1f9ab198bce67da5aadf03df60Alex Deymo
2463784a578dd26880454d70797519358a2326291bAlex DeymoUpdateManager::UpdateManager(chromeos_update_engine::ClockInterface* clock,
25fd45a731d9f9176ce134b34e2a84acc0cf403d1dGilad Arnold                             base::TimeDelta evaluation_timeout,
26fd45a731d9f9176ce134b34e2a84acc0cf403d1dGilad Arnold                             base::TimeDelta expiration_timeout, State* state)
27a23e408368ad34e21ee90ebd0dcb55cd03417d22Gilad Arnold      : default_policy_(clock), state_(state), clock_(clock),
28fd45a731d9f9176ce134b34e2a84acc0cf403d1dGilad Arnold        evaluation_timeout_(evaluation_timeout),
2983ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold        expiration_timeout_(expiration_timeout),
3083ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold        weak_ptr_factory_(this) {
31c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo  // TODO(deymo): Make it possible to replace this policy with a different
32c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo  // implementation with a build-time flag.
33c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo  policy_.reset(new ChromeOSPolicy());
34c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo}
35c705cc86983dd46df4261ce692cb7558a515e282Alex Deymo
3683ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad ArnoldUpdateManager::~UpdateManager() {
3783ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  // Remove pending main loop events associated with any of the outstanding
3883ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  // evaluation contexts. This will prevent dangling pending events, causing
3983ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  // these contexts to be destructed once the repo itself is destructed.
4083ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  for (auto& ec : ec_repo_)
4183ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold    ec->RemoveObserversAndTimeout();
4283ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold}
4383ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold
4483ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnoldvoid UpdateManager::UnregisterEvalContext(EvaluationContext* ec) {
4583ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  if (!ec_repo_.erase(ec)) {
4683ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold    LOG(ERROR) << "Unregistering an unknown evaluation context, this is a bug.";
4783ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold  }
4883ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold}
4983ffddaa3b09ceb4361e5c01ba300d57ab697a7eGilad Arnold
5063784a578dd26880454d70797519358a2326291bAlex Deymo}  // namespace chromeos_update_manager
51