blob: 482a8b75329aae8768279fdf17666ab34ad15599 [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 {
Jay Srinivasan0a708742012-03-20 11:26:12 -070021
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022 OmahaRequestParams()
Jay Srinivasan0a708742012-03-20 11:26:12 -070023 : os_platform(kOsPlatform),
24 os_version(kOsVersion),
25 app_id(kAppId),
26 delta_okay(true),
27 update_disabled(false) {}
28
Darin Petkov84c763c2010-07-29 16:27:58 -070029 OmahaRequestParams(const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070030 const std::string& in_os_version,
31 const std::string& in_os_sp,
32 const std::string& in_os_board,
33 const std::string& in_app_id,
34 const std::string& in_app_version,
35 const std::string& in_app_lang,
36 const std::string& in_app_track,
Darin Petkovfbb40092010-07-29 17:05:50 -070037 const std::string& in_hardware_class,
Jay Srinivasan0a708742012-03-20 11:26:12 -070038 bool in_delta_okay,
39 const std::string& in_update_url,
40 bool in_update_disabled,
41 const std::string& in_target_version_prefix)
Darin Petkov84c763c2010-07-29 16:27:58 -070042 : os_platform(in_os_platform),
Darin Petkova4a8a8c2010-07-15 22:21:12 -070043 os_version(in_os_version),
44 os_sp(in_os_sp),
45 os_board(in_os_board),
46 app_id(in_app_id),
47 app_version(in_app_version),
48 app_lang(in_app_lang),
49 app_track(in_app_track),
Darin Petkovfbb40092010-07-29 17:05:50 -070050 hardware_class(in_hardware_class),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070051 delta_okay(in_delta_okay),
Jay Srinivasan0a708742012-03-20 11:26:12 -070052 update_url(in_update_url),
53 update_disabled(in_update_disabled),
54 target_version_prefix(in_target_version_prefix) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056 std::string os_platform;
57 std::string os_version;
58 std::string os_sp;
59 std::string os_board;
60 std::string app_id;
61 std::string app_version;
62 std::string app_lang;
63 std::string app_track;
Darin Petkovfbb40092010-07-29 17:05:50 -070064 std::string hardware_class; // Hardware Qualification ID of the client
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070065 bool delta_okay; // If this client can accept a delta
Darin Petkova4a8a8c2010-07-15 22:21:12 -070066
67 std::string update_url;
68
Darin Petkov49d91322010-10-25 16:34:58 -070069 static const char kUpdateTrackKey[];
70
Jay Srinivasan0a708742012-03-20 11:26:12 -070071 bool update_disabled;
72 std::string target_version_prefix;
73
Darin Petkova4a8a8c2010-07-15 22:21:12 -070074 // Suggested defaults
75 static const char* const kAppId;
76 static const char* const kOsPlatform;
77 static const char* const kOsVersion;
78 static const char* const kUpdateUrl;
79};
80
81class OmahaRequestDeviceParams : public OmahaRequestParams {
82 public:
Darin Petkov49d91322010-10-25 16:34:58 -070083 OmahaRequestDeviceParams();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070084
Darin Petkov5a7f5652010-07-22 21:40:09 -070085 // Initializes all the data in the object. Non-empty
86 // |in_app_version| or |in_update_url| prevents automatic detection
87 // of the parameter. Returns true on success, false otherwise.
88 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020089 const std::string& in_update_url,
90 const std::string& in_release_track);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070091
Darin Petkov49d91322010-10-25 16:34:58 -070092 // Permanently changes the release track to |track|. Returns true on success,
93 // false otherwise.
94 bool SetTrack(const std::string& track);
95 static bool SetDeviceTrack(const std::string& track);
96
Satoru Takabayashi583667b2010-10-27 13:09:57 +090097 // Returns the release track. On error, returns an empty string.
98 static std::string GetDeviceTrack();
99
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700100 // For unit-tests.
101 void set_root(const std::string& root) { root_ = root; }
102
Darin Petkov10d02dd2011-01-10 14:57:39 -0800103 // Enforce security mode for testing purposes.
104 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700105
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700106 private:
Darin Petkov49d91322010-10-25 16:34:58 -0700107 FRIEND_TEST(OmahaRequestDeviceParamsTest, IsValidTrackTest);
Darin Petkov10d02dd2011-01-10 14:57:39 -0800108 FRIEND_TEST(OmahaRequestDeviceParamsTest, ShouldLockDownTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700109
110 // Use a validator that is a non-static member of this class so that its
111 // inputs can be mocked in unit tests (e.g., build type for IsValidTrack).
112 typedef bool(OmahaRequestDeviceParams::*ValueValidator)(
113 const std::string&) const;
114
Darin Petkov10d02dd2011-01-10 14:57:39 -0800115 // Returns true if parameter values should be locked down for security
116 // reasons. If this is an official build running in normal boot mode, all
117 // values except the release track are parsed only from the read-only rootfs
118 // partition and the track values are restricted to a pre-approved set.
119 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700120
121 // Returns true if |track| is a valid track, false otherwise. This method
122 // restricts the track value only if the image is official (see
123 // IsOfficialBuild).
124 bool IsValidTrack(const std::string& track) const;
125
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700126 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800127 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
128 // is true. Failing that, it looks for the key in /etc/lsb-release. If
129 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700130 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700131 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800132 ValueValidator validator,
133 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700134
135 // Gets the machine type (e.g. "i686").
136 std::string GetMachineType() const;
137
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700138 // When reading files, prepend root_ to the paths. Useful for testing.
139 std::string root_;
140
Darin Petkov10d02dd2011-01-10 14:57:39 -0800141 // Force security lock down for testing purposes.
142 bool force_lock_down_;
143 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700144
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700145 DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
146};
147
148} // namespace chromeos_update_engine
149
150#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__