blob: da18500722d13d11588ff35ba7e9b493acee55cf [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";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080013static const char kPrefsDirectory[] = "/var/lib/update_engine/prefs";
Jay Srinivasan43488792012-06-19 00:25:31 -070014
Jay Srinivasan34b5d862012-07-23 11:43:22 -070015RealSystemState::RealSystemState()
16 : device_policy_(NULL),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070017 connection_manager_(this),
Chris Sosabe45bef2013-04-09 18:25:12 -070018 request_params_(this),
19 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 Sosabe45bef2013-04-09 18:25:12 -070029 if (!utils::FileExists(kSystemRebootedMarkerFile)) {
30 if (!utils::WriteFile(kSystemRebootedMarkerFile, "", 0)) {
31 LOG(ERROR) << "Could not create reboot marker file";
32 return false;
33 }
34 system_rebooted_ = true;
35 }
36
Jay Srinivasan19409b72013-04-12 19:23:36 -070037 if (!payload_state_.Initialize(this))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038 return false;
39
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080040 // Initialize the GPIO handler as instructed.
41 if (enable_gpio) {
42 // A real GPIO handler. Defer GPIO discovery to ensure the udev has ample
43 // time to export the devices. Also require that test mode is physically
44 // queried at most once and the result cached, for a more consistent update
45 // behavior.
46 udev_iface_.reset(new StandardUdevInterface());
47 file_descriptor_.reset(new EintrSafeFileDescriptor());
48 gpio_handler_.reset(new StandardGpioHandler(udev_iface_.get(),
49 file_descriptor_.get(),
50 true, true));
51 } else {
52 // A no-op GPIO handler, always indicating a non-test mode.
53 gpio_handler_.reset(new NoopGpioHandler(false));
54 }
55
Jay Srinivasan55f50c22013-01-10 19:24:35 -080056 // Create the update attempter.
57 update_attempter_.reset(new UpdateAttempter(this, &dbus_));
58
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080059 // All is well. Initialization successful.
60 return true;
61}
Jay Srinivasan43488792012-06-19 00:25:31 -070062
63bool RealSystemState::IsOOBEComplete() {
64 return file_util::PathExists(FilePath(kOOBECompletedMarker));
65}
66
Jay Srinivasan43488792012-06-19 00:25:31 -070067} // namespace chromeos_update_engine