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