Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <sys/stat.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 9 | #include <sys/types.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | |
| 12 | #include <string> |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 13 | #include <vector> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | #include <curl/curl.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 17 | #include <libxml/parser.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include "update_engine/action.h" |
| 20 | #include "update_engine/http_fetcher.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 21 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 22 | // The Omaha Request action makes a request to Omaha and can output |
| 23 | // the response on the output ActionPipe. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | // Encodes XML entities in a given string with libxml2. input must be |
| 28 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 29 | std::string XmlEncode(const std::string& input); |
| 30 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 31 | // This struct encapsulates the data Omaha's response for the request. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | // These strings in this struct are not XML escaped. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 33 | struct OmahaResponse { |
| 34 | OmahaResponse() |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 35 | : update_exists(false), |
| 36 | poll_interval(0), |
| 37 | size(0), |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 38 | metadata_size(0), |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 39 | max_days_to_scatter(0), |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 40 | needs_admin(false), |
| 41 | prompt(false) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 42 | // True iff there is an update to be downloaded. |
| 43 | bool update_exists; |
| 44 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 45 | // If non-zero, server-dictated poll frequency in seconds. |
| 46 | int poll_interval; |
| 47 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | // These are only valid if update_exists is true: |
| 49 | std::string display_version; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 50 | |
| 51 | // The ordered list of URLs in the Omaha response. Each item is a complete |
| 52 | // URL (i.e. in terms of Omaha XML, each value is a urlBase + packageName) |
| 53 | std::vector<std::string> payload_urls; |
| 54 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | std::string more_info_url; |
| 56 | std::string hash; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 57 | std::string metadata_signature; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 58 | std::string deadline; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | off_t size; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 60 | off_t metadata_size; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 61 | int max_days_to_scatter; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | bool needs_admin; |
| 63 | bool prompt; |
| 64 | }; |
| 65 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 66 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 67 | // This struct encapsulates the Omaha event information. For a |
| 68 | // complete list of defined event types and results, see |
| 69 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 70 | struct OmahaEvent { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 71 | // The Type values correspond to EVENT_TYPE values of Omaha. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 72 | enum Type { |
| 73 | kTypeUnknown = 0, |
| 74 | kTypeDownloadComplete = 1, |
| 75 | kTypeInstallComplete = 2, |
| 76 | kTypeUpdateComplete = 3, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 77 | kTypeUpdateDownloadStarted = 13, |
| 78 | kTypeUpdateDownloadFinished = 14, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 81 | // The Result values correspond to EVENT_RESULT values of Omaha. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 82 | enum Result { |
| 83 | kResultError = 0, |
| 84 | kResultSuccess = 1, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 85 | kResultSuccessReboot = 2, |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 86 | kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | OmahaEvent() |
| 90 | : type(kTypeUnknown), |
| 91 | result(kResultError), |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 92 | error_code(kActionCodeError) {} |
| 93 | explicit OmahaEvent(Type in_type) |
| 94 | : type(in_type), |
| 95 | result(kResultSuccess), |
| 96 | error_code(kActionCodeSuccess) {} |
| 97 | OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code) |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 98 | : type(in_type), |
| 99 | result(in_result), |
| 100 | error_code(in_error_code) {} |
| 101 | |
| 102 | Type type; |
| 103 | Result result; |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 104 | ActionExitCode error_code; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 107 | class NoneType; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 108 | class OmahaRequestAction; |
| 109 | struct OmahaRequestParams; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 110 | class PrefsInterface; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | |
| 112 | template<> |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 113 | class ActionTraits<OmahaRequestAction> { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 115 | // Takes parameters on the input pipe. |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 116 | typedef NoneType InputObjectType; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 117 | // On UpdateCheck success, puts the Omaha response on output. Event |
| 118 | // requests do not have an output pipe. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 119 | typedef OmahaResponse OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 122 | class OmahaRequestAction : public Action<OmahaRequestAction>, |
| 123 | public HttpFetcherDelegate { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 124 | public: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 125 | static const int kNeverPinged = -1; |
| 126 | static const int kPingTimeJump = -2; |
| 127 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 128 | // These are the possible outcome upon checking whether we satisfied |
| 129 | // the wall-clock-based-wait. |
| 130 | enum WallClockWaitResult { |
| 131 | kWallClockWaitNotSatisfied, |
| 132 | kWallClockWaitDoneButUpdateCheckWaitRequired, |
| 133 | kWallClockWaitDoneAndUpdateCheckWaitNotRequired, |
| 134 | }; |
| 135 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 136 | // The ctor takes in all the parameters that will be used for making |
| 137 | // the request to Omaha. For some of them we have constants that |
| 138 | // should be used. |
| 139 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 140 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 141 | // |
| 142 | // Takes ownership of the passed in OmahaEvent. If |event| is NULL, |
| 143 | // this is an UpdateCheck request, otherwise it's an Event request. |
| 144 | // Event requests always succeed. |
| 145 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | // A good calling pattern is: |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 147 | // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 148 | // or |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 149 | // OmahaRequestAction(..., NULL, new WhateverHttpFetcher); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 150 | OmahaRequestAction(SystemState* system_state, |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 151 | OmahaRequestParams* params, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 152 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 153 | HttpFetcher* http_fetcher, |
| 154 | bool ping_only); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 155 | virtual ~OmahaRequestAction(); |
| 156 | typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType; |
| 157 | typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 158 | void PerformAction(); |
| 159 | void TerminateProcessing(); |
| 160 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 161 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } |
| 162 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 163 | // Debugging/logging |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 164 | static std::string StaticType() { return "OmahaRequestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 165 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 166 | |
| 167 | // Delegate methods (see http_fetcher.h) |
| 168 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 169 | const char* bytes, int length); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 170 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 171 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 172 | // Returns true if this is an Event request, false if it's an UpdateCheck. |
| 173 | bool IsEvent() const { return event_.get() != NULL; } |
| 174 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 175 | private: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 176 | // If this is an update check request, initializes |
| 177 | // |ping_active_days_| and |ping_roll_call_days_| to values that may |
| 178 | // be sent as pings to Omaha. |
| 179 | void InitPingDays(); |
| 180 | |
Darin Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 181 | // Based on the persistent preference store values, calculates the |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 182 | // number of days since the last ping sent for |key|. |
| 183 | int CalculatePingDays(const std::string& key); |
| 184 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 185 | // Returns true if the download of a new update should be deferred. |
| 186 | // False if the update can be downloaded. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 187 | bool ShouldDeferDownload(OmahaResponse* output_object); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 188 | |
| 189 | // Returns true if the basic wall-clock-based waiting period has been |
| 190 | // satisfied based on the scattering policy setting. False otherwise. |
| 191 | // If true, it also indicates whether the additional update-check-count-based |
| 192 | // waiting period also needs to be satisfied before the download can begin. |
| 193 | WallClockWaitResult IsWallClockBasedWaitingSatisfied( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 194 | OmahaResponse* output_object); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 195 | |
| 196 | // Returns true if the update-check-count-based waiting period has been |
| 197 | // satisfied. False otherwise. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 198 | bool IsUpdateCheckCountBasedWaitingSatisfied(); |
| 199 | |
| 200 | // Parses the response from Omaha that's available in |doc| using the other |
| 201 | // helper methods below and populates the |output_object| with the relevant |
| 202 | // values. Returns true if we should continue the parsing. False otherwise, |
| 203 | // in which case it sets any error code using |completer|. |
| 204 | bool ParseResponse(xmlDoc* doc, |
| 205 | OmahaResponse* output_object, |
| 206 | ScopedActionCompleter* completer); |
| 207 | |
| 208 | // Parses the status property in the given update_check_node and populates |
| 209 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 210 | // False otherwise, in which case it sets any error code using |completer|. |
| 211 | bool ParseStatus(xmlNode* update_check_node, |
| 212 | OmahaResponse* output_object, |
| 213 | ScopedActionCompleter* completer); |
| 214 | |
| 215 | // Parses the URL nodes in the given XML document and populates |
| 216 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 217 | // False otherwise, in which case it sets any error code using |completer|. |
| 218 | bool ParseUrls(xmlDoc* doc, |
| 219 | OmahaResponse* output_object, |
| 220 | ScopedActionCompleter* completer); |
| 221 | |
| 222 | // Parses the package node in the given XML document and populates |
| 223 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 224 | // False otherwise, in which case it sets any error code using |completer|. |
| 225 | bool ParsePackage(xmlDoc* doc, |
| 226 | OmahaResponse* output_object, |
| 227 | ScopedActionCompleter* completer); |
| 228 | |
| 229 | // Parses the other parameters in the given XML document and populates |
| 230 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 231 | // False otherwise, in which case it sets any error code using |completer|. |
| 232 | bool ParseParams(xmlDoc* doc, |
| 233 | OmahaResponse* output_object, |
| 234 | ScopedActionCompleter* completer); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 235 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame^] | 236 | // Global system context. |
| 237 | SystemState* system_state_; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 238 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 239 | // Contains state that is relevant in the processing of the Omaha request. |
| 240 | OmahaRequestParams* params_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 241 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 242 | // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL. |
| 243 | scoped_ptr<OmahaEvent> event_; |
| 244 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 245 | // pointer to the HttpFetcher that does the http work |
| 246 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 247 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 248 | // If true, only include the <ping> element in the request. |
| 249 | bool ping_only_; |
| 250 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 251 | // Stores the response from the omaha server |
| 252 | std::vector<char> response_buffer_; |
| 253 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 254 | // Initialized by InitPingDays to values that may be sent to Omaha |
| 255 | // as part of a ping message. Note that only positive values and -1 |
| 256 | // are sent to Omaha. |
| 257 | int ping_active_days_; |
| 258 | int ping_roll_call_days_; |
| 259 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 260 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | } // namespace chromeos_update_engine |
| 264 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 265 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |