blob: da091c07a8fd3fafd55cedfdd8d5e0389c4618c4 [file] [log] [blame]
Darin Petkovf2065b42011-05-17 16:36:27 -07001// Copyright (c) 2011 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>
11#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070012
13// This gathers local system information and prepares info used by the
14// Omaha request action.
15
16namespace chromeos_update_engine {
17
18// This struct encapsulates the data Omaha gets for the request.
19// These strings in this struct should not be XML escaped.
20struct OmahaRequestParams {
21 OmahaRequestParams()
22 : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {}
Darin Petkov84c763c2010-07-29 16:27:58 -070023 OmahaRequestParams(const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070024 const std::string& in_os_version,
25 const std::string& in_os_sp,
26 const std::string& in_os_board,
27 const std::string& in_app_id,
28 const std::string& in_app_version,
29 const std::string& in_app_lang,
30 const std::string& in_app_track,
Darin Petkovfbb40092010-07-29 17:05:50 -070031 const std::string& in_hardware_class,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070032 const bool in_delta_okay,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070033 const std::string& in_update_url)
Darin Petkov84c763c2010-07-29 16:27:58 -070034 : os_platform(in_os_platform),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070035 os_version(in_os_version),
36 os_sp(in_os_sp),
37 os_board(in_os_board),
38 app_id(in_app_id),
39 app_version(in_app_version),
40 app_lang(in_app_lang),
41 app_track(in_app_track),
Darin Petkovfbb40092010-07-29 17:05:50 -070042 hardware_class(in_hardware_class),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070043 delta_okay(in_delta_okay),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070044 update_url(in_update_url) {}
45
Darin Petkova4a8a8c2010-07-15 22:21:12 -070046 std::string os_platform;
47 std::string os_version;
48 std::string os_sp;
49 std::string os_board;
50 std::string app_id;
51 std::string app_version;
52 std::string app_lang;
53 std::string app_track;
Darin Petkovfbb40092010-07-29 17:05:50 -070054 std::string hardware_class; // Hardware Qualification ID of the client
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070055 bool delta_okay; // If this client can accept a delta
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056
57 std::string update_url;
58
Darin Petkov49d91322010-10-25 16:34:58 -070059 static const char kUpdateTrackKey[];
60
Darin Petkova4a8a8c2010-07-15 22:21:12 -070061 // Suggested defaults
62 static const char* const kAppId;
63 static const char* const kOsPlatform;
64 static const char* const kOsVersion;
65 static const char* const kUpdateUrl;
66};
67
68class OmahaRequestDeviceParams : public OmahaRequestParams {
69 public:
Darin Petkov49d91322010-10-25 16:34:58 -070070 OmahaRequestDeviceParams();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070071
Darin Petkov5a7f5652010-07-22 21:40:09 -070072 // Initializes all the data in the object. Non-empty
73 // |in_app_version| or |in_update_url| prevents automatic detection
74 // of the parameter. Returns true on success, false otherwise.
75 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020076 const std::string& in_update_url,
77 const std::string& in_release_track);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070078
Darin Petkov49d91322010-10-25 16:34:58 -070079 // Permanently changes the release track to |track|. Returns true on success,
80 // false otherwise.
81 bool SetTrack(const std::string& track);
82 static bool SetDeviceTrack(const std::string& track);
83
Satoru Takabayashi583667b2010-10-27 13:09:57 +090084 // Returns the release track. On error, returns an empty string.
85 static std::string GetDeviceTrack();
86
Darin Petkova4a8a8c2010-07-15 22:21:12 -070087 // For unit-tests.
88 void set_root(const std::string& root) { root_ = root; }
89
Darin Petkov10d02dd2011-01-10 14:57:39 -080090 // Enforce security mode for testing purposes.
91 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -070092
Darin Petkova4a8a8c2010-07-15 22:21:12 -070093 private:
Darin Petkov49d91322010-10-25 16:34:58 -070094 FRIEND_TEST(OmahaRequestDeviceParamsTest, IsValidTrackTest);
Darin Petkov10d02dd2011-01-10 14:57:39 -080095 FRIEND_TEST(OmahaRequestDeviceParamsTest, ShouldLockDownTest);
Darin Petkov49d91322010-10-25 16:34:58 -070096
97 // Use a validator that is a non-static member of this class so that its
98 // inputs can be mocked in unit tests (e.g., build type for IsValidTrack).
99 typedef bool(OmahaRequestDeviceParams::*ValueValidator)(
100 const std::string&) const;
101
Darin Petkov10d02dd2011-01-10 14:57:39 -0800102 // Returns true if parameter values should be locked down for security
103 // reasons. If this is an official build running in normal boot mode, all
104 // values except the release track are parsed only from the read-only rootfs
105 // partition and the track values are restricted to a pre-approved set.
106 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700107
108 // Returns true if |track| is a valid track, false otherwise. This method
109 // restricts the track value only if the image is official (see
110 // IsOfficialBuild).
111 bool IsValidTrack(const std::string& track) const;
112
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700113 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800114 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
115 // is true. Failing that, it looks for the key in /etc/lsb-release. If
116 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700117 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700118 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800119 ValueValidator validator,
120 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700121
122 // Gets the machine type (e.g. "i686").
123 std::string GetMachineType() const;
124
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700125 // When reading files, prepend root_ to the paths. Useful for testing.
126 std::string root_;
127
Darin Petkov10d02dd2011-01-10 14:57:39 -0800128 // Force security lock down for testing purposes.
129 bool force_lock_down_;
130 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700131
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700132 DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
133};
134
135} // namespace chromeos_update_engine
136
137#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__