blob: c89f29731d8c002cdd3ee17c3a602a788e3e62a3 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan43488792012-06-19 00:25:31 -070016
Alex Deymo94c06162014-03-21 20:34:46 -070017#include "update_engine/real_system_state.h"
18
Ben Chan06c76a42014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070020#include <base/time/time.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070021
Alex Deymo763e7db2015-08-27 21:08:08 -070022#include "update_engine/boot_control_chromeos.h"
David Zeuthen639aa362014-02-03 16:23:44 -080023#include "update_engine/constants.h"
Alex Deymo63784a52014-05-28 10:46:14 -070024#include "update_engine/update_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070025#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070026
27namespace chromeos_update_engine {
28
Alex Deymo30534502015-07-20 15:06:33 -070029RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
Alex Deymod6deb1d2015-08-28 15:54:37 -070030 : debugd_proxy_(bus),
31 power_manager_proxy_(bus),
32 session_manager_proxy_(bus),
Alex Deymo30534502015-07-20 15:06:33 -070033 shill_proxy_(bus),
34 libcros_proxy_(bus) {
35}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070037bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038 metrics_lib_.Init();
39
Alex Deymo763e7db2015-08-27 21:08:08 -070040 // TODO(deymo): Initialize BootControl based on the build environment.
41 BootControlChromeOS* boot_control_cros = new BootControlChromeOS();
42 boot_control_.reset(boot_control_cros);
43 if (!boot_control_cros->Init()) {
44 LOG(ERROR) << "Ignoring BootControlChromeOS failure. We won't run updates.";
45 }
46
Alex Deymo30534502015-07-20 15:06:33 -070047 if (!shill_proxy_.Init()) {
48 LOG(ERROR) << "Failed to initialize shill proxy.";
49 return false;
50 }
51
Alex Vakulenko75039d72014-03-25 12:36:28 -070052 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 LOG(ERROR) << "Failed to initialize preferences.";
54 return false;
55 }
56
Alex Vakulenko75039d72014-03-25 12:36:28 -070057 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070058 LOG(ERROR) << "Failed to initialize powerwash preferences.";
59 return false;
60 }
61
Chris Sosabe45bef2013-04-09 18:25:12 -070062 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
63 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
64 LOG(ERROR) << "Could not create reboot marker file";
65 return false;
66 }
67 system_rebooted_ = true;
68 }
69
Alex Deymo63784a52014-05-28 10:46:14 -070070 // Initialize the Update Manager using the default state factory.
71 chromeos_update_manager::State* um_state =
72 chromeos_update_manager::DefaultStateFactory(
Alex Deymo30534502015-07-20 15:06:33 -070073 &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -070074 if (!um_state) {
75 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080076 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070077 }
Alex Deymo63784a52014-05-28 10:46:14 -070078 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -070079 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -070080 &clock_, base::TimeDelta::FromSeconds(5),
81 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070082
Gilad Arnold4a0321b2014-10-28 15:57:30 -070083 // The P2P Manager depends on the Update Manager for its initialization.
84 p2p_manager_.reset(P2PManager::Construct(
85 nullptr, &clock_, update_manager_.get(), "cros_au",
86 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
87
Gilad Arnold1f847232014-04-07 12:07:49 -070088 if (!payload_state_.Initialize(this)) {
89 LOG(ERROR) << "Failed to initialize the payload state object.";
90 return false;
91 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092
Gilad Arnold1f847232014-04-07 12:07:49 -070093 // Initialize the update attempter.
94 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080095
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080096 // All is well. Initialization successful.
97 return true;
98}
Jay Srinivasan43488792012-06-19 00:25:31 -070099
Jay Srinivasan43488792012-06-19 00:25:31 -0700100} // namespace chromeos_update_engine