Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // 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 Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 7 | #include "update_engine/real_system_state.h" |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 8 | #include "update_engine/utils.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 9 | |
| 10 | namespace chromeos_update_engine { |
| 11 | |
| 12 | static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 13 | static const char kPrefsDirectory[] = "/var/lib/update_engine/prefs"; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 14 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 15 | RealSystemState::RealSystemState() |
| 16 | : device_policy_(NULL), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 17 | connection_manager_(this), |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 18 | request_params_(this), |
| 19 | system_rebooted_(false) {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 20 | |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 21 | bool RealSystemState::Initialize(bool enable_gpio) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | metrics_lib_.Init(); |
| 23 | |
| 24 | if (!prefs_.Init(FilePath(kPrefsDirectory))) { |
| 25 | LOG(ERROR) << "Failed to initialize preferences."; |
| 26 | return false; |
| 27 | } |
| 28 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 29 | 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 Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 37 | if (!payload_state_.Initialize(this)) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | return false; |
| 39 | |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 40 | // 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 Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 56 | // Create the update attempter. |
| 57 | update_attempter_.reset(new UpdateAttempter(this, &dbus_)); |
| 58 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 59 | // All is well. Initialization successful. |
| 60 | return true; |
| 61 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 62 | |
| 63 | bool RealSystemState::IsOOBEComplete() { |
| 64 | return file_util::PathExists(FilePath(kOOBECompletedMarker)); |
| 65 | } |
| 66 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 67 | bool RealSystemState::IsOfficialBuild() { |
| 68 | return utils::IsOfficialBuild(); |
| 69 | } |
| 70 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 71 | } // namespace chromeos_update_engine |