Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame^] | 1 | // |
| 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 | |
| 17 | #ifndef UPDATE_ENGINE_OMAHA_REQUEST_BUILDER_XML_H_ |
| 18 | #define UPDATE_ENGINE_OMAHA_REQUEST_BUILDER_XML_H_ |
| 19 | |
| 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" |
| 36 | #include "update_engine/omaha_response.h" |
| 37 | #include "update_engine/system_state.h" |
| 38 | |
| 39 | // TODO(ahassani): Make the xml builder into a class of its own so we don't have |
| 40 | // to pass all these parameters around. |
| 41 | |
| 42 | namespace chromeos_update_engine { |
| 43 | |
| 44 | extern const int kNeverPinged; |
| 45 | |
| 46 | // This struct encapsulates the Omaha event information. For a |
| 47 | // complete list of defined event types and results, see |
| 48 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 49 | struct OmahaEvent { |
| 50 | // The Type values correspond to EVENT_TYPE values of Omaha. |
| 51 | enum Type { |
| 52 | kTypeUnknown = 0, |
| 53 | kTypeDownloadComplete = 1, |
| 54 | kTypeInstallComplete = 2, |
| 55 | kTypeUpdateComplete = 3, |
| 56 | kTypeUpdateDownloadStarted = 13, |
| 57 | kTypeUpdateDownloadFinished = 14, |
| 58 | // Chromium OS reserved type sent after the first reboot following an update |
| 59 | // completed. |
| 60 | kTypeRebootedAfterUpdate = 54, |
| 61 | }; |
| 62 | |
| 63 | // The Result values correspond to EVENT_RESULT values of Omaha. |
| 64 | enum Result { |
| 65 | kResultError = 0, |
| 66 | kResultSuccess = 1, |
| 67 | kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy. |
| 68 | }; |
| 69 | |
| 70 | OmahaEvent() |
| 71 | : type(kTypeUnknown), |
| 72 | result(kResultError), |
| 73 | error_code(ErrorCode::kError) {} |
| 74 | explicit OmahaEvent(Type in_type) |
| 75 | : type(in_type), |
| 76 | result(kResultSuccess), |
| 77 | error_code(ErrorCode::kSuccess) {} |
| 78 | OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code) |
| 79 | : type(in_type), result(in_result), error_code(in_error_code) {} |
| 80 | |
| 81 | Type type; |
| 82 | Result result; |
| 83 | ErrorCode error_code; |
| 84 | }; |
| 85 | |
| 86 | struct OmahaAppData { |
| 87 | std::string id; |
| 88 | std::string version; |
| 89 | std::string product_components; |
| 90 | }; |
| 91 | |
| 92 | // Encodes XML entities in a given string. Input must be ASCII-7 valid. If |
| 93 | // the input is invalid, the default value is used instead. |
| 94 | std::string XmlEncodeWithDefault(const std::string& input, |
| 95 | const std::string& default_value); |
| 96 | |
| 97 | // Escapes text so it can be included as character data and attribute |
| 98 | // values. The |input| string must be valid ASCII-7, no UTF-8 supported. |
| 99 | // Returns whether the |input| was valid and escaped properly in |output|. |
| 100 | bool XmlEncode(const std::string& input, std::string* output); |
| 101 | |
| 102 | // Returns an XML ping element attribute assignment with attribute |
| 103 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 104 | // to be sent, or an empty string otherwise. |
| 105 | std::string GetPingAttribute(const std::string& name, int ping_days); |
| 106 | |
| 107 | // Returns an XML ping element if any of the elapsed days need to be |
| 108 | // sent, or an empty string otherwise. |
| 109 | std::string GetPingXml(int ping_active_days, int ping_roll_call_days); |
| 110 | |
| 111 | // Returns an XML that goes into the body of the <app> element of the Omaha |
| 112 | // request based on the given parameters. |
| 113 | std::string GetAppBody(const OmahaEvent* event, |
| 114 | OmahaRequestParams* params, |
| 115 | bool ping_only, |
| 116 | bool include_ping, |
| 117 | bool skip_updatecheck, |
| 118 | int ping_active_days, |
| 119 | int ping_roll_call_days, |
| 120 | PrefsInterface* prefs); |
| 121 | |
| 122 | // Returns the cohort* argument to include in the <app> tag for the passed |
| 123 | // |arg_name| and |prefs_key|, if any. The return value is suitable to |
| 124 | // concatenate to the list of arguments and includes a space at the end. |
| 125 | std::string GetCohortArgXml(PrefsInterface* prefs, |
| 126 | const std::string arg_name, |
| 127 | const std::string prefs_key); |
| 128 | |
| 129 | bool IsValidComponentID(const std::string& id); |
| 130 | |
| 131 | // Returns an XML that corresponds to the entire <app> node of the Omaha |
| 132 | // request based on the given parameters. |
| 133 | std::string GetAppXml(const OmahaEvent* event, |
| 134 | OmahaRequestParams* params, |
| 135 | const OmahaAppData& app_data, |
| 136 | bool ping_only, |
| 137 | bool include_ping, |
| 138 | bool skip_updatecheck, |
| 139 | int ping_active_days, |
| 140 | int ping_roll_call_days, |
| 141 | int install_date_in_days, |
| 142 | SystemState* system_state); |
| 143 | |
| 144 | // Returns an XML that corresponds to the entire <os> node of the Omaha |
| 145 | // request based on the given parameters. |
| 146 | std::string GetOsXml(OmahaRequestParams* params); |
| 147 | |
| 148 | // Returns an XML that corresponds to the entire Omaha request based on the |
| 149 | // given parameters. |
| 150 | std::string GetRequestXml(const OmahaEvent* event, |
| 151 | OmahaRequestParams* params, |
| 152 | bool ping_only, |
| 153 | bool include_ping, |
| 154 | int ping_active_days, |
| 155 | int ping_roll_call_days, |
| 156 | int install_date_in_days, |
| 157 | SystemState* system_state); |
| 158 | |
| 159 | } // namespace chromeos_update_engine |
| 160 | |
| 161 | #endif // UPDATE_ENGINE_OMAHA_REQUEST_BUILDER_XML_H_ |