blob: 7c246f7fbf5184f970264d4fcc0661a99afed99a [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,
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700124 bool ping_only,
125 bool include_ping,
126 int ping_active_days,
127 int ping_roll_call_days,
128 int install_date_in_days,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700129 const std::string& session_id)
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700130 : event_(event),
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700131 ping_only_(ping_only),
132 include_ping_(include_ping),
133 ping_active_days_(ping_active_days),
134 ping_roll_call_days_(ping_roll_call_days),
135 install_date_in_days_(install_date_in_days),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700136 session_id_(session_id) {}
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700137
138 ~OmahaRequestBuilderXml() override = default;
139
140 // Returns an XML that corresponds to the entire Omaha request.
141 std::string GetRequest() const override;
142
143 private:
Jae Hoon Kim37d15372020-01-08 18:11:26 -0800144 FRIEND_TEST(OmahaRequestBuilderXmlTest, PlatformGetAppTest);
145 FRIEND_TEST(OmahaRequestBuilderXmlTest, DlcGetAppTest);
146
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700147 // Returns an XML that corresponds to the entire <os> node of the Omaha
148 // request based on the member variables.
149 std::string GetOs() const;
150
151 // Returns an XML that corresponds to all <app> nodes of the Omaha
152 // request based on the given parameters.
153 std::string GetApps() const;
154
155 // Returns an XML that corresponds to the single <app> node of the Omaha
156 // request based on the given parameters.
157 std::string GetApp(const OmahaAppData& app_data) const;
158
159 // Returns an XML that goes into the body of the <app> element of the Omaha
160 // request based on the given parameters.
Andrewe045aef2020-01-08 16:29:22 -0800161 std::string GetAppBody(const OmahaAppData& app_data) const;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700162
163 // Returns the cohort* argument to include in the <app> tag for the passed
164 // |arg_name| and |prefs_key|, if any. The return value is suitable to
165 // concatenate to the list of arguments and includes a space at the end.
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800166 std::string GetCohortArg(const std::string& arg_name,
167 const std::string& prefs_key,
168 const std::string& override_value = "") const;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700169
170 // Returns an XML ping element if any of the elapsed days need to be
171 // sent, or an empty string otherwise.
172 std::string GetPing() const;
173
Andrewe045aef2020-01-08 16:29:22 -0800174 // Returns an XML ping element if any of the elapsed days need to be
175 // sent, or an empty string otherwise.
176 std::string GetPingDateBased(
177 const OmahaRequestParams::AppParams& app_params) const;
178
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700179 const OmahaEvent* event_;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700180 bool ping_only_;
181 bool include_ping_;
182 int ping_active_days_;
183 int ping_roll_call_days_;
184 int install_date_in_days_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700185 std::string session_id_;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -0700186
187 DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilderXml);
188};
Amin Hassani7fca2862019-03-28 16:09:22 -0700189
190} // namespace chromeos_update_engine
191
Amin Hassaniec7bc112020-10-29 16:47:58 -0700192#endif // UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_