blob: 10b66f3a2a11716ad6aa6446c5554f1906f00159 [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 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
Alex Deymo94c06162014-03-21 20:34:46 -07005#include "update_engine/real_system_state.h"
6
Jay Srinivasan43488792012-06-19 00:25:31 -07007#include <base/file_util.h>
8
David Zeuthen639aa362014-02-03 16:23:44 -08009#include "update_engine/constants.h"
Alex Deymo94c06162014-03-21 20:34:46 -070010#include "update_engine/policy_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070011#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070012
13namespace chromeos_update_engine {
14
Jay Srinivasan34b5d862012-07-23 11:43:22 -070015RealSystemState::RealSystemState()
Alex Vakulenko75039d72014-03-25 12:36:28 -070016 : device_policy_(nullptr),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070017 connection_manager_(this),
Gilad Arnold1f847232014-04-07 12:07:49 -070018 update_attempter_(this, &dbus_),
Chris Sosabe45bef2013-04-09 18:25:12 -070019 request_params_(this),
20 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070022bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023 metrics_lib_.Init();
24
Alex Vakulenko75039d72014-03-25 12:36:28 -070025 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026 LOG(ERROR) << "Failed to initialize preferences.";
27 return false;
28 }
29
Alex Vakulenko75039d72014-03-25 12:36:28 -070030 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070031 LOG(ERROR) << "Failed to initialize powerwash preferences.";
32 return false;
33 }
34
Chris Sosabe45bef2013-04-09 18:25:12 -070035 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
36 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
37 LOG(ERROR) << "Could not create reboot marker file";
38 return false;
39 }
40 system_rebooted_ = true;
41 }
42
David Zeuthen526cb582013-08-06 12:26:18 -070043 p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
44 kMaxP2PFilesToKeep));
45
Alex Deymo94c06162014-03-21 20:34:46 -070046 // Initialize the PolicyManager using the default State Factory.
Alex Deymo680d0222014-04-24 21:00:08 -070047 chromeos_policy_manager::State* pm_state =
48 chromeos_policy_manager::DefaultStateFactory(
49 &policy_provider_, &dbus_, this);
50 if (!pm_state) {
Gilad Arnold1f847232014-04-07 12:07:49 -070051 LOG(ERROR) << "Failed to initialize the policy manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070053 }
Alex Deymo680d0222014-04-24 21:00:08 -070054 policy_manager_.reset(
55 new chromeos_policy_manager::PolicyManager(&clock_, pm_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070056
57 if (!payload_state_.Initialize(this)) {
58 LOG(ERROR) << "Failed to initialize the payload state object.";
59 return false;
60 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080061
Gilad Arnold1f847232014-04-07 12:07:49 -070062 // Initialize the update attempter.
63 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080064
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080065 // All is well. Initialization successful.
66 return true;
67}
Jay Srinivasan43488792012-06-19 00:25:31 -070068
Jay Srinivasan43488792012-06-19 00:25:31 -070069} // namespace chromeos_update_engine