blob: faac5cdde2382e8c06a7344bdf0a95ea342b49d2 [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 Vakulenko88b591f2014-08-28 16:48:57 -070044 p2p_manager_.reset(P2PManager::Construct(nullptr, &prefs_, "cros_au",
David Zeuthen526cb582013-08-06 12:26:18 -070045 kMaxP2PFilesToKeep));
46
Alex Deymo63784a52014-05-28 10:46:14 -070047 // Initialize the Update Manager using the default state factory.
48 chromeos_update_manager::State* um_state =
49 chromeos_update_manager::DefaultStateFactory(
Alex Deymo680d0222014-04-24 21:00:08 -070050 &policy_provider_, &dbus_, this);
Alex Deymo63784a52014-05-28 10:46:14 -070051 if (!um_state) {
52 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070054 }
Alex Deymo63784a52014-05-28 10:46:14 -070055 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -070056 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -070057 &clock_, base::TimeDelta::FromSeconds(5),
58 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070059
60 if (!payload_state_.Initialize(this)) {
61 LOG(ERROR) << "Failed to initialize the payload state object.";
62 return false;
63 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080064
Gilad Arnold1f847232014-04-07 12:07:49 -070065 // Initialize the update attempter.
66 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080067
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080068 // All is well. Initialization successful.
69 return true;
70}
Jay Srinivasan43488792012-06-19 00:25:31 -070071
Jay Srinivasan43488792012-06-19 00:25:31 -070072} // namespace chromeos_update_engine