blob: 861a702ef1c50606344075844960b6fa6ae16aea [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>
Alex Deymo30534502015-07-20 15:06:33 -07009#include <chromeos/dbus/service_constants.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070010
David Zeuthen639aa362014-02-03 16:23:44 -080011#include "update_engine/constants.h"
Alex Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/state_factory.h"
Chris Sosabe45bef2013-04-09 18:25:12 -070013#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070014
15namespace chromeos_update_engine {
16
Alex Deymo30534502015-07-20 15:06:33 -070017RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
18 : debugd_proxy_(bus, debugd::kDebugdServiceName),
19 power_manager_proxy_(bus, power_manager::kPowerManagerServiceName),
20 session_manager_proxy_(bus, login_manager::kSessionManagerServiceName),
21 shill_proxy_(bus),
22 libcros_proxy_(bus) {
23}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -070025bool RealSystemState::Initialize() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026 metrics_lib_.Init();
27
Alex Deymo30534502015-07-20 15:06:33 -070028 if (!shill_proxy_.Init()) {
29 LOG(ERROR) << "Failed to initialize shill proxy.";
30 return false;
31 }
32
Alex Vakulenko75039d72014-03-25 12:36:28 -070033 if (!prefs_.Init(base::FilePath(kPrefsDirectory))) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034 LOG(ERROR) << "Failed to initialize preferences.";
35 return false;
36 }
37
Alex Vakulenko75039d72014-03-25 12:36:28 -070038 if (!powerwash_safe_prefs_.Init(base::FilePath(kPowerwashSafePrefsDir))) {
Chris Sosaaa18e162013-06-20 13:20:30 -070039 LOG(ERROR) << "Failed to initialize powerwash preferences.";
40 return false;
41 }
42
Chris Sosabe45bef2013-04-09 18:25:12 -070043 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
44 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
45 LOG(ERROR) << "Could not create reboot marker file";
46 return false;
47 }
48 system_rebooted_ = true;
49 }
50
Alex Deymo63784a52014-05-28 10:46:14 -070051 // Initialize the Update Manager using the default state factory.
52 chromeos_update_manager::State* um_state =
53 chromeos_update_manager::DefaultStateFactory(
Alex Deymo30534502015-07-20 15:06:33 -070054 &policy_provider_, &shill_proxy_, &session_manager_proxy_, this);
Alex Deymo63784a52014-05-28 10:46:14 -070055 if (!um_state) {
56 LOG(ERROR) << "Failed to initialize the Update Manager.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057 return false;
Gilad Arnold1f847232014-04-07 12:07:49 -070058 }
Alex Deymo63784a52014-05-28 10:46:14 -070059 update_manager_.reset(
Gilad Arnoldb2271992014-06-19 12:35:24 -070060 new chromeos_update_manager::UpdateManager(
Gilad Arnoldfd45a732014-08-07 15:53:46 -070061 &clock_, base::TimeDelta::FromSeconds(5),
62 base::TimeDelta::FromHours(12), um_state));
Gilad Arnold1f847232014-04-07 12:07:49 -070063
Gilad Arnold4a0321b2014-10-28 15:57:30 -070064 // The P2P Manager depends on the Update Manager for its initialization.
65 p2p_manager_.reset(P2PManager::Construct(
66 nullptr, &clock_, update_manager_.get(), "cros_au",
67 kMaxP2PFilesToKeep, base::TimeDelta::FromDays(kMaxP2PFileAgeDays)));
68
Gilad Arnold1f847232014-04-07 12:07:49 -070069 if (!payload_state_.Initialize(this)) {
70 LOG(ERROR) << "Failed to initialize the payload state object.";
71 return false;
72 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080073
Gilad Arnold1f847232014-04-07 12:07:49 -070074 // Initialize the update attempter.
75 update_attempter_.Init();
Jay Srinivasan55f50c22013-01-10 19:24:35 -080076
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080077 // All is well. Initialization successful.
78 return true;
79}
Jay Srinivasan43488792012-06-19 00:25:31 -070080
Jay Srinivasan43488792012-06-19 00:25:31 -070081} // namespace chromeos_update_engine