blob: 772205c5c67f0f8210b8706572fbce731e6594e1 [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#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SYSTEM_STATE_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SYSTEM_STATE_H_
7
Jay Srinivasanf0572052012-10-23 18:12:56 -07008#include "metrics/metrics_library.h"
Jay Srinivasan43488792012-06-19 00:25:31 -07009#include <policy/device_policy.h>
10#include <policy/libpolicy.h>
11
12#include <update_engine/connection_manager.h>
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080013#include <update_engine/gpio_handler.h>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080014#include <update_engine/payload_state.h>
15#include <update_engine/prefs.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070016
17namespace chromeos_update_engine {
18
19// An interface to global system context, including platform resources,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080020// the current state of the system, high-level objects whose lifetime is same
21// as main, system interfaces, etc.
Jay Srinivasan43488792012-06-19 00:25:31 -070022// Carved out separately so it can be mocked for unit tests.
23// Currently it has only one method, but we should start migrating other
24// methods to use this as and when needed to unit test them.
25// TODO (jaysri): Consider renaming this to something like GlobalContext.
26class SystemState {
27 public:
28 // Destructs this object.
29 virtual ~SystemState() {}
30
31 // Returns true if the OOBE process has been completed and EULA accepted.
32 // False otherwise.
33 virtual bool IsOOBEComplete() = 0;
34
35 // Sets or gets the latest device policy.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036 virtual void set_device_policy(const policy::DevicePolicy* device_policy) = 0;
37 virtual const policy::DevicePolicy* device_policy() const = 0;
Jay Srinivasan43488792012-06-19 00:25:31 -070038
39 // Gets the connection manager object.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080040 virtual ConnectionManager* connection_manager() = 0;
Jay Srinivasanf0572052012-10-23 18:12:56 -070041
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042 // Gets the Metrics Library interface for reporting UMA stats.
Jay Srinivasanf0572052012-10-23 18:12:56 -070043 virtual MetricsLibraryInterface* metrics_lib() = 0;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044
45 // Gets the interface object for persisted store.
46 virtual PrefsInterface* prefs() = 0;
47
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080048 // Gets the interface for the payload state object.
49 virtual PayloadStateInterface* payload_state() = 0;
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080050
51 // Returns a pointer to the GPIO handler.
52 virtual GpioHandler* gpio_handler() const = 0;
Jay Srinivasan43488792012-06-19 00:25:31 -070053};
54
55// A real implementation of the SystemStateInterface which is
56// used by the actual product code.
57class RealSystemState : public SystemState {
58public:
59 // Constructors and destructors.
60 RealSystemState();
61 virtual ~RealSystemState() {}
62
63 virtual bool IsOOBEComplete();
64
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080065 virtual inline void set_device_policy(
66 const policy::DevicePolicy* device_policy) {
67 device_policy_ = device_policy;
68 }
Jay Srinivasan43488792012-06-19 00:25:31 -070069
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080070 virtual inline const policy::DevicePolicy* device_policy() const {
71 return device_policy_;
72 }
Jay Srinivasan43488792012-06-19 00:25:31 -070073
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080074 virtual inline ConnectionManager* connection_manager() {
75 return &connection_manager_;
76 }
Jay Srinivasanf0572052012-10-23 18:12:56 -070077
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080078 virtual inline MetricsLibraryInterface* metrics_lib() {
79 return &metrics_lib_;
80 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080081
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080082 virtual inline PrefsInterface* prefs() {
83 return &prefs_;
84 }
85
86 virtual inline PayloadStateInterface* payload_state() {
87 return &payload_state_;
88 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080089
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080090 // Returns a pointer to the GPIO handler.
91 virtual inline GpioHandler* gpio_handler() const {
92 return gpio_handler_.get();
93 }
94
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080095 // Initializs this concrete object. Other methods should be invoked only
96 // if the object has been initialized successfully.
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080097 bool Initialize(bool enable_gpio);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080098
Jay Srinivasan43488792012-06-19 00:25:31 -070099private:
100 // The latest device policy object from the policy provider.
101 const policy::DevicePolicy* device_policy_;
102
103 // The connection manager object that makes download
104 // decisions depending on the current type of connection.
105 ConnectionManager connection_manager_;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700106
107 // The Metrics Library interface for reporting UMA stats.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800108 MetricsLibrary metrics_lib_;
109
110 // Interface for persisted store.
111 Prefs prefs_;
112
113 // All state pertaining to payload state such as
Jay Srinivasan08262882012-12-28 19:29:43 -0800114 // response, URL, backoff states.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800115 PayloadState payload_state_;
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800116
117 // Pointer to a GPIO handler and other needed modules (note that the order of
118 // declaration significant for destruction, as the latter depends on the
119 // former).
120 scoped_ptr<UdevInterface> udev_iface_;
121 scoped_ptr<FileDescriptor> file_descriptor_;
122 scoped_ptr<GpioHandler> gpio_handler_;
Jay Srinivasan43488792012-06-19 00:25:31 -0700123};
124
125} // namespace chromeos_update_engine
126
127#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UTILS_H_