blob: ea8bab09899d94c2f5cb8c4992f269220dd59557 [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 Srinivasan55f50c22013-01-10 19:24:35 -080019// The default "official" Omaha update URL.
20extern const char* const kProductionOmahaUrl;
21
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070022// This struct encapsulates the data Omaha gets for the request, along with
23// essential state needed for the processing of the request/response.
24// The strings in this struct should not be XML escaped.
25// TODO (jaysri): Consider renaming this to reflect its lifetime more
26// appropriately.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027struct OmahaRequestParams {
Jay Srinivasan0a708742012-03-20 11:26:12 -070028
Darin Petkova4a8a8c2010-07-15 22:21:12 -070029 OmahaRequestParams()
Jay Srinivasan0a708742012-03-20 11:26:12 -070030 : os_platform(kOsPlatform),
31 os_version(kOsVersion),
32 app_id(kAppId),
33 delta_okay(true),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070034 update_disabled(false),
35 wall_clock_based_wait_enabled(false),
36 update_check_count_wait_enabled(false),
37 min_update_checks_needed(kDefaultMinUpdateChecks),
38 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Jay Srinivasan0a708742012-03-20 11:26:12 -070039
Darin Petkov84c763c2010-07-29 16:27:58 -070040 OmahaRequestParams(const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070041 const std::string& in_os_version,
42 const std::string& in_os_sp,
43 const std::string& in_os_board,
44 const std::string& in_app_id,
45 const std::string& in_app_version,
46 const std::string& in_app_lang,
47 const std::string& in_app_track,
Darin Petkovfbb40092010-07-29 17:05:50 -070048 const std::string& in_hardware_class,
Jay Srinivasan0a708742012-03-20 11:26:12 -070049 bool in_delta_okay,
50 const std::string& in_update_url,
51 bool in_update_disabled,
52 const std::string& in_target_version_prefix)
Darin Petkov84c763c2010-07-29 16:27:58 -070053 : os_platform(in_os_platform),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070054 os_version(in_os_version),
55 os_sp(in_os_sp),
56 os_board(in_os_board),
57 app_id(in_app_id),
58 app_version(in_app_version),
59 app_lang(in_app_lang),
60 app_track(in_app_track),
Darin Petkovfbb40092010-07-29 17:05:50 -070061 hardware_class(in_hardware_class),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070062 delta_okay(in_delta_okay),
Jay Srinivasan0a708742012-03-20 11:26:12 -070063 update_url(in_update_url),
64 update_disabled(in_update_disabled),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070065 target_version_prefix(in_target_version_prefix),
66 wall_clock_based_wait_enabled(false),
67 update_check_count_wait_enabled(false),
68 min_update_checks_needed(kDefaultMinUpdateChecks),
69 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070070
Darin Petkova4a8a8c2010-07-15 22:21:12 -070071 std::string os_platform;
72 std::string os_version;
73 std::string os_sp;
74 std::string os_board;
75 std::string app_id;
76 std::string app_version;
77 std::string app_lang;
78 std::string app_track;
Darin Petkovfbb40092010-07-29 17:05:50 -070079 std::string hardware_class; // Hardware Qualification ID of the client
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070080 bool delta_okay; // If this client can accept a delta
Darin Petkova4a8a8c2010-07-15 22:21:12 -070081
82 std::string update_url;
83
Darin Petkov49d91322010-10-25 16:34:58 -070084 static const char kUpdateTrackKey[];
85
Jay Srinivasan0a708742012-03-20 11:26:12 -070086 bool update_disabled;
87 std::string target_version_prefix;
88
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070089 bool wall_clock_based_wait_enabled;
90 base::TimeDelta waiting_period;
91
92 bool update_check_count_wait_enabled;
93 int64 min_update_checks_needed;
94 int64 max_update_checks_allowed;
95
Darin Petkova4a8a8c2010-07-15 22:21:12 -070096 // Suggested defaults
97 static const char* const kAppId;
98 static const char* const kOsPlatform;
99 static const char* const kOsVersion;
100 static const char* const kUpdateUrl;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700101 static const int64 kDefaultMinUpdateChecks = 0;
102 static const int64 kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700103};
104
105class OmahaRequestDeviceParams : public OmahaRequestParams {
106 public:
Darin Petkov49d91322010-10-25 16:34:58 -0700107 OmahaRequestDeviceParams();
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700108
Darin Petkov5a7f5652010-07-22 21:40:09 -0700109 // Initializes all the data in the object. Non-empty
110 // |in_app_version| or |in_update_url| prevents automatic detection
111 // of the parameter. Returns true on success, false otherwise.
112 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200113 const std::string& in_update_url,
114 const std::string& in_release_track);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700115
Darin Petkov49d91322010-10-25 16:34:58 -0700116 // Permanently changes the release track to |track|. Returns true on success,
117 // false otherwise.
118 bool SetTrack(const std::string& track);
119 static bool SetDeviceTrack(const std::string& track);
120
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900121 // Returns the release track. On error, returns an empty string.
122 static std::string GetDeviceTrack();
123
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700124 // For unit-tests.
125 void set_root(const std::string& root) { root_ = root; }
126
Darin Petkov10d02dd2011-01-10 14:57:39 -0800127 // Enforce security mode for testing purposes.
128 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700129
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700130 private:
Darin Petkov49d91322010-10-25 16:34:58 -0700131 FRIEND_TEST(OmahaRequestDeviceParamsTest, IsValidTrackTest);
Darin Petkov10d02dd2011-01-10 14:57:39 -0800132 FRIEND_TEST(OmahaRequestDeviceParamsTest, ShouldLockDownTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700133
134 // Use a validator that is a non-static member of this class so that its
135 // inputs can be mocked in unit tests (e.g., build type for IsValidTrack).
136 typedef bool(OmahaRequestDeviceParams::*ValueValidator)(
137 const std::string&) const;
138
Darin Petkov10d02dd2011-01-10 14:57:39 -0800139 // Returns true if parameter values should be locked down for security
140 // reasons. If this is an official build running in normal boot mode, all
141 // values except the release track are parsed only from the read-only rootfs
142 // partition and the track values are restricted to a pre-approved set.
143 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700144
145 // Returns true if |track| is a valid track, false otherwise. This method
146 // restricts the track value only if the image is official (see
147 // IsOfficialBuild).
148 bool IsValidTrack(const std::string& track) const;
149
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700150 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800151 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
152 // is true. Failing that, it looks for the key in /etc/lsb-release. If
153 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700154 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700155 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800156 ValueValidator validator,
157 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700158
159 // Gets the machine type (e.g. "i686").
160 std::string GetMachineType() const;
161
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700162 // When reading files, prepend root_ to the paths. Useful for testing.
163 std::string root_;
164
Darin Petkov10d02dd2011-01-10 14:57:39 -0800165 // Force security lock down for testing purposes.
166 bool force_lock_down_;
167 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700168
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700169 DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
170};
171
172} // namespace chromeos_update_engine
173
174#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__