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