blob: 6bbc84e7d8c54967c43db3699d99f57c68a1afdb [file] [log] [blame]
Amin Hassani7fca2862019-03-28 16:09:22 -07001//
2// Copyright (C) 2019 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_
18#define UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_
Amin Hassani7fca2862019-03-28 16:09:22 -070019
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <map>
25#include <memory>
26#include <string>
27#include <vector>
28
29#include <gtest/gtest_prod.h> // for FRIEND_TEST
30
31#include <brillo/secure_blob.h>
32#include <curl/curl.h>
33
34#include "update_engine/common/action.h"
35#include "update_engine/common/http_fetcher.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070036#include "update_engine/cros/omaha_request_params.h"
37#include "update_engine/cros/omaha_response.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070038
Amin Hassani7fca2862019-03-28 16:09:22 -070039namespace chromeos_update_engine {
40
Jae Hoon Kim5fc00a22020-01-08 20:15:42 -080041extern const char kNoVersion[];
Andrewe045aef2020-01-08 16:29:22 -080042extern const int kPingNeverPinged;
43extern const int kPingUnknownValue;
44extern const int kPingActiveValue;
45extern const int kPingInactiveValue;
Amin Hassani7fca2862019-03-28 16:09:22 -070046
47// This struct encapsulates the Omaha event information. For a
48// complete list of defined event types and results, see
49// http://code.google.com/p/omaha/wiki/ServerProtocol#event
50struct OmahaEvent {
51 // The Type values correspond to EVENT_TYPE values of Omaha.
52 enum Type {
53 kTypeUnknown = 0,
54 kTypeDownloadComplete = 1,
55 kTypeInstallComplete = 2,
56 kTypeUpdateComplete = 3,
57 kTypeUpdateDownloadStarted = 13,
58 kTypeUpdateDownloadFinished = 14,
59 // Chromium OS reserved type sent after the first reboot following an update
60 // completed.
61 kTypeRebootedAfterUpdate = 54,
62 };
63
64 // The Result values correspond to EVENT_RESULT values of Omaha.
65 enum Result {
66 kResultError = 0,
67 kResultSuccess = 1,
68 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
69 };
70
71 OmahaEvent()
72 : type(kTypeUnknown),
73 result(kResultError),
74 error_code(ErrorCode::kError) {}
75 explicit OmahaEvent(Type in_type)
76 : type(in_type),
77 result(kResultSuccess),
78 error_code(ErrorCode::kSuccess) {}
79 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
80 : type(in_type), result(in_result), error_code(in_error_code) {}
81
82 Type type;
83 Result result;
84 ErrorCode error_code;
85};
86
87struct OmahaAppData {
88 std::string id;
89 std::string version;
90 std::string product_components;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070091 bool skip_update;
Jae Hoon Kim37d15372020-01-08 18:11:26 -080092 bool is_dlc;
Andrewe045aef2020-01-08 16:29:22 -080093 OmahaRequestParams::AppParams app_params;
Amin Hassani7fca2862019-03-28 16:09:22 -070094};
95
96// Encodes XML entities in a given string. Input must be ASCII-7 valid. If
97// the input is invalid, the default value is used instead.
98std::string XmlEncodeWithDefault(const std::string& input,
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070099 const std::string& default_value = "");
Amin Hassani7fca2862019-03-28 16:09:22 -0700100
101// Escapes text so it can be included as character data and attribute
102// values. The |input| string must be valid ASCII-7, no UTF-8 supported.
103// Returns whether the |input| was valid and escaped properly in |output|.
104bool XmlEncode(const std::string& input, std::string* output);
105
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700106// Returns a boolean based on examining each character on whether it's a valid
107// component (meaning all characters are an alphanum excluding '-', '_', '.').
Amin Hassani7fca2862019-03-28 16:09:22 -0700108bool IsValidComponentID(const std::string& id);
109
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700110class OmahaRequestBuilder {
111 public:
112 OmahaRequestBuilder() = default;
113 virtual ~OmahaRequestBuilder() = default;
Amin Hassani7fca2862019-03-28 16:09:22 -0700114
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700115 virtual std::string GetRequest() const = 0;
Amin Hassani7fca2862019-03-28 16:09:22 -0700116
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700117 private:
118 DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilder);
119};
120
121class OmahaRequestBuilderXml : OmahaRequestBuilder {
122 public:
123 OmahaRequestBuilderXml(const OmahaEvent* event,
124 OmahaRequestParams* params,
125 bool ping_only,
126 bool include_ping,
127 int ping_active_days,
128 int ping_roll_call_days,
129 int install_date_in_days,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700130 PrefsInterface* prefs,
131 const std::string& session_id)
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700132 : event_(event),
133 params_(params),
134 ping_only_(ping_only),
135 include_ping_(include_ping),
136 ping_active_days_(ping_active_days),
137 ping_roll_call_days_(ping_roll_call_days),
138 install_date_in_days_(install_date_in_days),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700139 prefs_(prefs),
140 session_id_(session_id) {}
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700141
142 ~OmahaRequestBuilderXml() override = default;
143
144 // Returns an XML that corresponds to the entire Omaha request.
145 std::string GetRequest() const override;
146
147 private:
Jae Hoon Kim37d15372020-01-08 18:11:26 -0800148 FRIEND_TEST(OmahaRequestBuilderXmlTest, PlatformGetAppTest);
149 FRIEND_TEST(OmahaRequestBuilderXmlTest, DlcGetAppTest);
150
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700151 // Returns an XML that corresponds to the entire <os> node of the Omaha
152 // request based on the member variables.
153 std::string GetOs() const;
154
155 // Returns an XML that corresponds to all <app> nodes of the Omaha
156 // request based on the given parameters.
157 std::string GetApps() const;
158
159 // Returns an XML that corresponds to the single <app> node of the Omaha
160 // request based on the given parameters.
161 std::string GetApp(const OmahaAppData& app_data) const;
162
163 // Returns an XML that goes into the body of the <app> element of the Omaha
164 // request based on the given parameters.
Andrewe045aef2020-01-08 16:29:22 -0800165 std::string GetAppBody(const OmahaAppData& app_data) const;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700166
167 // Returns the cohort* argument to include in the <app> tag for the passed
168 // |arg_name| and |prefs_key|, if any. The return value is suitable to
169 // concatenate to the list of arguments and includes a space at the end.
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800170 std::string GetCohortArg(const std::string& arg_name,
171 const std::string& prefs_key,
172 const std::string& override_value = "") const;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700173
174 // Returns an XML ping element if any of the elapsed days need to be
175 // sent, or an empty string otherwise.
176 std::string GetPing() const;
177
Andrewe045aef2020-01-08 16:29:22 -0800178 // Returns an XML ping element if any of the elapsed days need to be
179 // sent, or an empty string otherwise.
180 std::string GetPingDateBased(
181 const OmahaRequestParams::AppParams& app_params) const;
182
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700183 const OmahaEvent* event_;
184 OmahaRequestParams* params_;
185 bool ping_only_;
186 bool include_ping_;
187 int ping_active_days_;
188 int ping_roll_call_days_;
189 int install_date_in_days_;
190 PrefsInterface* prefs_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700191 std::string session_id_;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700192
193 DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilderXml);
194};
Amin Hassani7fca2862019-03-28 16:09:22 -0700195
196} // namespace chromeos_update_engine
197
Amin Hassaniec7bc112020-10-29 16:47:58 -0700198#endif // UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_