blob: 743546887b1fa082aa42f6809cd5ed86755d4b8d [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkova4a8a8c2010-07-15 22:21:12 -07002// 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_OMAHA_REQUEST_PARAMS_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
7
8#include <string>
9
Darin Petkov49d91322010-10-25 16:34:58 -070010#include <base/basictypes.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070011#include <base/time.h>
Darin Petkov49d91322010-10-25 16:34:58 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070013
14// This gathers local system information and prepares info used by the
15// Omaha request action.
16
17namespace chromeos_update_engine {
18
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070019// This struct encapsulates the data Omaha gets for the request, along with
20// essential state needed for the processing of the request/response.
21// The strings in this struct should not be XML escaped.
22// TODO (jaysri): Consider renaming this to reflect its lifetime more
23// appropriately.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070024struct OmahaRequestParams {
Jay Srinivasan0a708742012-03-20 11:26:12 -070025
Darin Petkova4a8a8c2010-07-15 22:21:12 -070026 OmahaRequestParams()
Jay Srinivasan0a708742012-03-20 11:26:12 -070027 : os_platform(kOsPlatform),
28 os_version(kOsVersion),
29 app_id(kAppId),
30 delta_okay(true),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070031 update_disabled(false),
32 wall_clock_based_wait_enabled(false),
33 update_check_count_wait_enabled(false),
34 min_update_checks_needed(kDefaultMinUpdateChecks),
35 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Jay Srinivasan0a708742012-03-20 11:26:12 -070036
Darin Petkov84c763c2010-07-29 16:27:58 -070037 OmahaRequestParams(const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070038 const std::string& in_os_version,
39 const std::string& in_os_sp,
40 const std::string& in_os_board,
41 const std::string& in_app_id,
42 const std::string& in_app_version,
43 const std::string& in_app_lang,
44 const std::string& in_app_track,
Darin Petkovfbb40092010-07-29 17:05:50 -070045 const std::string& in_hardware_class,
Jay Srinivasan0a708742012-03-20 11:26:12 -070046 bool in_delta_okay,
47 const std::string& in_update_url,
48 bool in_update_disabled,
49 const std::string& in_target_version_prefix)
Darin Petkov84c763c2010-07-29 16:27:58 -070050 : os_platform(in_os_platform),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070051 os_version(in_os_version),
52 os_sp(in_os_sp),
53 os_board(in_os_board),
54 app_id(in_app_id),
55 app_version(in_app_version),
56 app_lang(in_app_lang),
57 app_track(in_app_track),
Darin Petkovfbb40092010-07-29 17:05:50 -070058 hardware_class(in_hardware_class),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070059 delta_okay(in_delta_okay),
Jay Srinivasan0a708742012-03-20 11:26:12 -070060 update_url(in_update_url),
61 update_disabled(in_update_disabled),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070062 target_version_prefix(in_target_version_prefix),
63 wall_clock_based_wait_enabled(false),
64 update_check_count_wait_enabled(false),
65 min_update_checks_needed(kDefaultMinUpdateChecks),
66 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070067
Darin Petkova4a8a8c2010-07-15 22:21:12 -070068 std::string os_platform;
69 std::string os_version;
70 std::string os_sp;
71 std::string os_board;
72 std::string app_id;
73 std::string app_version;
74 std::string app_lang;
75 std::string app_track;
Darin Petkovfbb40092010-07-29 17:05:50 -070076 std::string hardware_class; // Hardware Qualification ID of the client
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070077 bool delta_okay; // If this client can accept a delta
Darin Petkova4a8a8c2010-07-15 22:21:12 -070078
79 std::string update_url;
80
Darin Petkov49d91322010-10-25 16:34:58 -070081 static const char kUpdateTrackKey[];
82
Jay Srinivasan0a708742012-03-20 11:26:12 -070083 bool update_disabled;
84 std::string target_version_prefix;
85
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070086 bool wall_clock_based_wait_enabled;
87 base::TimeDelta waiting_period;
88
89 bool update_check_count_wait_enabled;
90 int64 min_update_checks_needed;
91 int64 max_update_checks_allowed;
92
Darin Petkova4a8a8c2010-07-15 22:21:12 -070093 // Suggested defaults
94 static const char* const kAppId;
95 static const char* const kOsPlatform;
96 static const char* const kOsVersion;
97 static const char* const kUpdateUrl;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070098 static const int64 kDefaultMinUpdateChecks = 0;
99 static const int64 kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700100};
101
102class OmahaRequestDeviceParams : public OmahaRequestParams {
103 public:
Darin Petkov49d91322010-10-25 16:34:58 -0700104 OmahaRequestDeviceParams();
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700105
Darin Petkov5a7f5652010-07-22 21:40:09 -0700106 // Initializes all the data in the object. Non-empty
107 // |in_app_version| or |in_update_url| prevents automatic detection
108 // of the parameter. Returns true on success, false otherwise.
109 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200110 const std::string& in_update_url,
111 const std::string& in_release_track);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700112
Darin Petkov49d91322010-10-25 16:34:58 -0700113 // Permanently changes the release track to |track|. Returns true on success,
114 // false otherwise.
115 bool SetTrack(const std::string& track);
116 static bool SetDeviceTrack(const std::string& track);
117
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900118 // Returns the release track. On error, returns an empty string.
119 static std::string GetDeviceTrack();
120
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700121 // For unit-tests.
122 void set_root(const std::string& root) { root_ = root; }
123
Darin Petkov10d02dd2011-01-10 14:57:39 -0800124 // Enforce security mode for testing purposes.
125 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700126
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700127 private:
Darin Petkov49d91322010-10-25 16:34:58 -0700128 FRIEND_TEST(OmahaRequestDeviceParamsTest, IsValidTrackTest);
Darin Petkov10d02dd2011-01-10 14:57:39 -0800129 FRIEND_TEST(OmahaRequestDeviceParamsTest, ShouldLockDownTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700130
131 // Use a validator that is a non-static member of this class so that its
132 // inputs can be mocked in unit tests (e.g., build type for IsValidTrack).
133 typedef bool(OmahaRequestDeviceParams::*ValueValidator)(
134 const std::string&) const;
135
Darin Petkov10d02dd2011-01-10 14:57:39 -0800136 // Returns true if parameter values should be locked down for security
137 // reasons. If this is an official build running in normal boot mode, all
138 // values except the release track are parsed only from the read-only rootfs
139 // partition and the track values are restricted to a pre-approved set.
140 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700141
142 // Returns true if |track| is a valid track, false otherwise. This method
143 // restricts the track value only if the image is official (see
144 // IsOfficialBuild).
145 bool IsValidTrack(const std::string& track) const;
146
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700147 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800148 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
149 // is true. Failing that, it looks for the key in /etc/lsb-release. If
150 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700151 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700152 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800153 ValueValidator validator,
154 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700155
156 // Gets the machine type (e.g. "i686").
157 std::string GetMachineType() const;
158
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700159 // When reading files, prepend root_ to the paths. Useful for testing.
160 std::string root_;
161
Darin Petkov10d02dd2011-01-10 14:57:39 -0800162 // Force security lock down for testing purposes.
163 bool force_lock_down_;
164 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700165
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700166 DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
167};
168
169} // namespace chromeos_update_engine
170
171#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__