blob: f38978cfa219d88608893ae66a47eb52b3ebb03d [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),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080034 interactive(false),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070035 update_disabled(false),
36 wall_clock_based_wait_enabled(false),
37 update_check_count_wait_enabled(false),
38 min_update_checks_needed(kDefaultMinUpdateChecks),
39 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Jay Srinivasan0a708742012-03-20 11:26:12 -070040
Darin Petkov84c763c2010-07-29 16:27:58 -070041 OmahaRequestParams(const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070042 const std::string& in_os_version,
43 const std::string& in_os_sp,
44 const std::string& in_os_board,
45 const std::string& in_app_id,
46 const std::string& in_app_version,
47 const std::string& in_app_lang,
48 const std::string& in_app_track,
Darin Petkovfbb40092010-07-29 17:05:50 -070049 const std::string& in_hardware_class,
Jay Srinivasan0a708742012-03-20 11:26:12 -070050 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080051 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070052 const std::string& in_update_url,
53 bool in_update_disabled,
54 const std::string& in_target_version_prefix)
Darin Petkov84c763c2010-07-29 16:27:58 -070055 : os_platform(in_os_platform),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056 os_version(in_os_version),
57 os_sp(in_os_sp),
58 os_board(in_os_board),
59 app_id(in_app_id),
60 app_version(in_app_version),
61 app_lang(in_app_lang),
62 app_track(in_app_track),
Darin Petkovfbb40092010-07-29 17:05:50 -070063 hardware_class(in_hardware_class),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070064 delta_okay(in_delta_okay),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080065 interactive(in_interactive),
Jay Srinivasan0a708742012-03-20 11:26:12 -070066 update_url(in_update_url),
67 update_disabled(in_update_disabled),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070068 target_version_prefix(in_target_version_prefix),
69 wall_clock_based_wait_enabled(false),
70 update_check_count_wait_enabled(false),
71 min_update_checks_needed(kDefaultMinUpdateChecks),
72 max_update_checks_allowed(kDefaultMaxUpdateChecks) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070073
Darin Petkova4a8a8c2010-07-15 22:21:12 -070074 std::string os_platform;
75 std::string os_version;
76 std::string os_sp;
77 std::string os_board;
78 std::string app_id;
79 std::string app_version;
80 std::string app_lang;
81 std::string app_track;
Darin Petkovfbb40092010-07-29 17:05:50 -070082 std::string hardware_class; // Hardware Qualification ID of the client
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070083 bool delta_okay; // If this client can accept a delta
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080084 bool interactive; // Whether this is a user-initiated update check
Darin Petkova4a8a8c2010-07-15 22:21:12 -070085
86 std::string update_url;
87
Darin Petkov49d91322010-10-25 16:34:58 -070088 static const char kUpdateTrackKey[];
89
Jay Srinivasan0a708742012-03-20 11:26:12 -070090 bool update_disabled;
91 std::string target_version_prefix;
92
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070093 bool wall_clock_based_wait_enabled;
94 base::TimeDelta waiting_period;
95
96 bool update_check_count_wait_enabled;
97 int64 min_update_checks_needed;
98 int64 max_update_checks_allowed;
99
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700100 // Suggested defaults
101 static const char* const kAppId;
102 static const char* const kOsPlatform;
103 static const char* const kOsVersion;
104 static const char* const kUpdateUrl;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700105 static const int64 kDefaultMinUpdateChecks = 0;
106 static const int64 kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107};
108
109class OmahaRequestDeviceParams : public OmahaRequestParams {
110 public:
Darin Petkov49d91322010-10-25 16:34:58 -0700111 OmahaRequestDeviceParams();
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700112
Darin Petkov5a7f5652010-07-22 21:40:09 -0700113 // Initializes all the data in the object. Non-empty
114 // |in_app_version| or |in_update_url| prevents automatic detection
115 // of the parameter. Returns true on success, false otherwise.
116 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200117 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800118 const std::string& in_release_track,
119 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120
Darin Petkov49d91322010-10-25 16:34:58 -0700121 // Permanently changes the release track to |track|. Returns true on success,
122 // false otherwise.
123 bool SetTrack(const std::string& track);
124 static bool SetDeviceTrack(const std::string& track);
125
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900126 // Returns the release track. On error, returns an empty string.
127 static std::string GetDeviceTrack();
128
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700129 // For unit-tests.
130 void set_root(const std::string& root) { root_ = root; }
131
Darin Petkov10d02dd2011-01-10 14:57:39 -0800132 // Enforce security mode for testing purposes.
133 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700134
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700135 private:
Darin Petkov49d91322010-10-25 16:34:58 -0700136 FRIEND_TEST(OmahaRequestDeviceParamsTest, IsValidTrackTest);
Darin Petkov10d02dd2011-01-10 14:57:39 -0800137 FRIEND_TEST(OmahaRequestDeviceParamsTest, ShouldLockDownTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700138
139 // Use a validator that is a non-static member of this class so that its
140 // inputs can be mocked in unit tests (e.g., build type for IsValidTrack).
141 typedef bool(OmahaRequestDeviceParams::*ValueValidator)(
142 const std::string&) const;
143
Darin Petkov10d02dd2011-01-10 14:57:39 -0800144 // Returns true if parameter values should be locked down for security
145 // reasons. If this is an official build running in normal boot mode, all
146 // values except the release track are parsed only from the read-only rootfs
147 // partition and the track values are restricted to a pre-approved set.
148 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700149
150 // Returns true if |track| is a valid track, false otherwise. This method
151 // restricts the track value only if the image is official (see
152 // IsOfficialBuild).
153 bool IsValidTrack(const std::string& track) const;
154
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700155 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800156 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
157 // is true. Failing that, it looks for the key in /etc/lsb-release. If
158 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700159 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700160 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800161 ValueValidator validator,
162 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700163
164 // Gets the machine type (e.g. "i686").
165 std::string GetMachineType() const;
166
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700167 // When reading files, prepend root_ to the paths. Useful for testing.
168 std::string root_;
169
Darin Petkov10d02dd2011-01-10 14:57:39 -0800170 // Force security lock down for testing purposes.
171 bool force_lock_down_;
172 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700173
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700174 DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
175};
176
177} // namespace chromeos_update_engine
178
179#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__