blob: f16f2f8f1f84946d8f0781b90a8e2cf663f822d4 [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
Ben Chan06c76a42014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -07008#include <base/time/time.h>
Jay Srinivasan43488792012-06-19 00:25:31 -07009
David Zeuthen639aa362014-02-03 16:23:44 -080010#include "update_engine/constants.h"
Alex Deymo63784a52014-05-28 10:46:14 -070011#include "update_engine/update_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070012#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070013
14namespace chromeos_update_engine {
15
Jay Srinivasan34b5d862012-07-23 11:43:22 -070016RealSystemState::RealSystemState()
Alex Vakulenko75039d72014-03-25 12:36:28 -070017 : device_policy_(nullptr),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070018 connection_manager_(this),
Gilad Arnold1f847232014-04-07 12:07:49 -070019 update_attempter_(this, &dbus_),
Chris Sosabe45bef2013-04-09 18:25:12 -070020 request_params_(this),
21 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080022
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070023bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024 metrics_lib_.Init();
25
Alex Vakulenko75039d72014-03-25 12:36:28 -070026 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027 LOG(ERROR) << "Failed to initialize preferences.";
28 return false;
29 }
30
Alex Vakulenko75039d72014-03-25 12:36:28 -070031 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070032 LOG(ERROR) << "Failed to initialize powerwash preferences.";
33 return false;
34 }
35
Chris Sosabe45bef2013-04-09 18:25:12 -070036 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
37 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
38 LOG(ERROR) << "Could not create reboot marker file";
39 return false;
40 }
41 system_rebooted_ = true;
42 }
43
Alex Deymo63784a52014-05-28 10:46:14 -070044 // Initialize the Update Manager using the default state factory.
45 chromeos_update_manager::State* um_state =
46 chromeos_update_manager::DefaultStateFactory(
Alex Deymo680d0222014-04-24 21:00:08 -070047 &policy_provider_, &dbus_, this);
Alex Deymo63784a52014-05-28 10:46:14 -070048 if (!um_state) {
49 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070051 }
Alex Deymo63784a52014-05-28 10:46:14 -070052 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -070053 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -070054 &clock_, base::TimeDelta::FromSeconds(5),
55 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070056
Gilad Arnold4a0321b2014-10-28 15:57:30 -070057 // The P2P Manager depends on the Update Manager for its initialization.
58 p2p_manager_.reset(P2PManager::Construct(
59 nullptr, &clock_, update_manager_.get(), "cros_au",
60 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
61
Gilad Arnold1f847232014-04-07 12:07:49 -070062 if (!payload_state_.Initialize(this)) {
63 LOG(ERROR) << "Failed to initialize the payload state object.";
64 return false;
65 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066
Gilad Arnold1f847232014-04-07 12:07:49 -070067 // Initialize the update attempter.
68 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080069
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080070 // All is well. Initialization successful.
71 return true;
72}
Jay Srinivasan43488792012-06-19 00:25:31 -070073
Jay Srinivasan43488792012-06-19 00:25:31 -070074} // namespace chromeos_update_engine