blob: 5465196697454d7f5fb52d4295bfd5dea874798a [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
5#include <base/file_util.h>
6
Jay Srinivasan55f50c22013-01-10 19:24:35 -08007#include "update_engine/real_system_state.h"
Chris Sosabe45bef2013-04-09 18:25:12 -07008#include "update_engine/utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -07009
10namespace chromeos_update_engine {
11
12static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
13
Jay Srinivasan34b5d862012-07-23 11:43:22 -070014RealSystemState::RealSystemState()
15 : device_policy_(NULL),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070016 connection_manager_(this),
Chris Sosabe45bef2013-04-09 18:25:12 -070017 request_params_(this),
David Zeuthen526cb582013-08-06 12:26:18 -070018 p2p_manager_(NULL),
Chris Sosabe45bef2013-04-09 18:25:12 -070019 system_rebooted_(false) {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080020
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080021bool RealSystemState::Initialize(bool enable_gpio) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080022 metrics_lib_.Init();
23
24 if (!prefs_.Init(FilePath(kPrefsDirectory))) {
25 LOG(ERROR) << "Failed to initialize preferences.";
26 return false;
27 }
28
Chris Sosaaa18e162013-06-20 13:20:30 -070029 if (!powerwash_safe_prefs_.Init(FilePath(kPowerwashSafePrefsDir))) {
30 LOG(ERROR) << "Failed to initialize powerwash preferences.";
31 return false;
32 }
33
Chris Sosabe45bef2013-04-09 18:25:12 -070034 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
35 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
36 LOG(ERROR) << "Could not create reboot marker file";
37 return false;
38 }
39 system_rebooted_ = true;
40 }
41
David Zeuthen526cb582013-08-06 12:26:18 -070042 p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
43 kMaxP2PFilesToKeep));
44
Jay Srinivasan19409b72013-04-12 19:23:36 -070045 if (!payload_state_.Initialize(this))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046 return false;
47
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080048 // Initialize the GPIO handler as instructed.
49 if (enable_gpio) {
50 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
51 // time to export the devices. Also require that test mode is physically
52 // queried at most once and the result cached, for a more consistent update
53 // behavior.
54 udev_iface_.reset(new StandardUdevInterface());
55 file_descriptor_.reset(new EintrSafeFileDescriptor());
56 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
57 file_descriptor_.get(),
58 true, true));
59 } else {
60 // A no-op GPIO handler, always indicating a non-test mode.
61 gpio_handler_.reset(new NoopGpioHandler(false));
62 }
63
Jay Srinivasan55f50c22013-01-10 19:24:35 -080064 // Create the update attempter.
65 update_attempter_.reset(new UpdateAttempter(this, &dbus_));
66
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080067 // All is well. Initialization successful.
68 return true;
69}
Jay Srinivasan43488792012-06-19 00:25:31 -070070
71bool RealSystemState::IsOOBEComplete() {
72 return file_util::PathExists(FilePath(kOOBECompletedMarker));
73}
74
Jay Srinivasan53173b92013-05-17 17:13:01 -070075bool RealSystemState::IsOfficialBuild() {
76 return utils::IsOfficialBuild();
77}
78
Jay Srinivasan43488792012-06-19 00:25:31 -070079} // namespace chromeos_update_engine