Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 7 | #include <inttypes.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 8 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <sstream> |
| 10 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 11 | #include <base/string_number_conversions.h> |
| 12 | #include <base/string_util.h> |
| 13 | #include <base/time.h> |
| 14 | #include <base/logging.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include <libxml/parser.h> |
| 16 | #include <libxml/xpath.h> |
| 17 | #include <libxml/xpathInternals.h> |
| 18 | |
| 19 | #include "update_engine/action_pipe.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 20 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 21 | #include "update_engine/prefs_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 22 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 24 | using base::Time; |
| 25 | using base::TimeDelta; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | using std::string; |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
| 33 | |
| 34 | // This is handy for passing strings into libxml2 |
| 35 | #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
| 36 | |
| 37 | // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows |
| 38 | // a custom free() function to be specified. |
| 39 | class ScopedPtrXmlDocFree { |
| 40 | public: |
| 41 | inline void operator()(void* x) const { |
| 42 | xmlFreeDoc(reinterpret_cast<xmlDoc*>(x)); |
| 43 | } |
| 44 | }; |
| 45 | class ScopedPtrXmlFree { |
| 46 | public: |
| 47 | inline void operator()(void* x) const { |
| 48 | xmlFree(x); |
| 49 | } |
| 50 | }; |
| 51 | class ScopedPtrXmlXPathObjectFree { |
| 52 | public: |
| 53 | inline void operator()(void* x) const { |
| 54 | xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
| 55 | } |
| 56 | }; |
| 57 | class ScopedPtrXmlXPathContextFree { |
| 58 | public: |
| 59 | inline void operator()(void* x) const { |
| 60 | xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
| 61 | } |
| 62 | }; |
| 63 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 64 | // Returns true if |ping_days| has a value that needs to be sent, |
| 65 | // false otherwise. |
| 66 | bool ShouldPing(int ping_days) { |
| 67 | return ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged; |
| 68 | } |
| 69 | |
| 70 | // Returns an XML ping element attribute assignment with attribute |
| 71 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 72 | // to be sent, or an empty string otherwise. |
| 73 | string GetPingAttribute(const string& name, int ping_days) { |
| 74 | if (ShouldPing(ping_days)) { |
| 75 | return StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); |
| 76 | } |
| 77 | return ""; |
| 78 | } |
| 79 | |
| 80 | // Returns an XML ping element if any of the elapsed days need to be |
| 81 | // sent, or an empty string otherwise. |
| 82 | string GetPingBody(int ping_active_days, int ping_roll_call_days) { |
| 83 | string ping_active = GetPingAttribute("a", ping_active_days); |
| 84 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); |
| 85 | if (!ping_active.empty() || !ping_roll_call.empty()) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 86 | return StringPrintf(" <o:ping active=\"1\"%s%s></o:ping>\n", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 87 | ping_active.c_str(), |
| 88 | ping_roll_call.c_str()); |
| 89 | } |
| 90 | return ""; |
| 91 | } |
| 92 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 93 | string FormatRequest(const OmahaEvent* event, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 94 | const OmahaRequestParams& params, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 95 | bool ping_only, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 96 | int ping_active_days, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 97 | int ping_roll_call_days, |
| 98 | PrefsInterface* prefs) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 99 | string body; |
| 100 | if (event == NULL) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 101 | body = GetPingBody(ping_active_days, ping_roll_call_days); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 102 | if (!ping_only) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 103 | body += " <o:updatecheck></o:updatecheck>\n"; |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 104 | // If this is the first update check after a reboot following a previous |
| 105 | // update, generate an event containing the previous version number. If |
| 106 | // the previous version preference file doesn't exist the event is still |
| 107 | // generated with a previous version of 0.0.0.0 -- this is relevant for |
| 108 | // older clients or new installs. The previous version event is not sent |
| 109 | // for ping-only requests because they come before the client has |
| 110 | // rebooted. |
| 111 | string prev_version; |
| 112 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { |
| 113 | prev_version = "0.0.0.0"; |
| 114 | } |
| 115 | if (!prev_version.empty()) { |
| 116 | body += StringPrintf( |
| 117 | " <o:event eventtype=\"%d\" eventresult=\"%d\" " |
| 118 | "previousversion=\"%s\"></o:event>\n", |
| 119 | OmahaEvent::kTypeUpdateComplete, |
| 120 | OmahaEvent::kResultSuccessReboot, |
| 121 | XmlEncode(prev_version).c_str()); |
| 122 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) |
| 123 | << "Unable to reset the previous version."; |
| 124 | } |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 125 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 126 | } else { |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 127 | // The error code is an optional attribute so append it only if the result |
| 128 | // is not success. |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 129 | string error_code; |
| 130 | if (event->result != OmahaEvent::kResultSuccess) { |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame^] | 131 | error_code = StringPrintf(" errorcode=\"%d\"", event->error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 132 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 133 | body = StringPrintf( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 134 | " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n", |
| 135 | event->type, event->result, error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 136 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 137 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 138 | "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
| 139 | "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 140 | "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 141 | "protocol=\"2.0\" ismachine=\"1\">\n" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 142 | " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" + |
| 143 | XmlEncode(params.os_platform) + "\" sp=\"" + |
| 144 | XmlEncode(params.os_sp) + "\"></o:os>\n" |
| 145 | " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" + |
| 146 | XmlEncode(params.app_version) + "\" " |
| 147 | "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" + |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 148 | XmlEncode(params.app_track) + "\" board=\"" + |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 149 | XmlEncode(params.os_board) + "\" hardware_class=\"" + |
| 150 | XmlEncode(params.hardware_class) + "\" delta_okay=\"" + |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 151 | (params.delta_okay ? "true" : "false") + "\">\n" + body + |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 152 | " </o:app>\n" |
| 153 | "</o:gupdate>\n"; |
| 154 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 155 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 156 | } // namespace {} |
| 157 | |
| 158 | // Encodes XML entities in a given string with libxml2. input must be |
| 159 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 160 | string XmlEncode(const string& input) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 161 | // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
| 162 | // // cpu, considering creating one and caching it. |
| 163 | // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
| 164 | // xmlNewDoc(ConstXMLStr("1.0"))); |
| 165 | // if (!xml_doc.get()) { |
| 166 | // LOG(ERROR) << "Unable to create xmlDoc"; |
| 167 | // return ""; |
| 168 | // } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 170 | xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
| 171 | return string(reinterpret_cast<const char *>(str.get())); |
| 172 | } |
| 173 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 174 | OmahaRequestAction::OmahaRequestAction(PrefsInterface* prefs, |
| 175 | const OmahaRequestParams& params, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 176 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 177 | HttpFetcher* http_fetcher, |
| 178 | bool ping_only) |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 179 | : prefs_(prefs), |
| 180 | params_(params), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 181 | event_(event), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 182 | http_fetcher_(http_fetcher), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 183 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 184 | ping_active_days_(0), |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 185 | ping_roll_call_days_(0), |
| 186 | should_skip_(false) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 187 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 188 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 189 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 190 | // Calculates the value to use for the ping days parameter. |
| 191 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 192 | int days = kNeverPinged; |
| 193 | int64_t last_ping = 0; |
| 194 | if (prefs_->GetInt64(key, &last_ping) && last_ping >= 0) { |
| 195 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 196 | if (days < 0) { |
| 197 | // If |days| is negative, then the system clock must have jumped |
| 198 | // back in time since the ping was sent. Mark the value so that |
| 199 | // it doesn't get sent to the server but we still update the |
| 200 | // last ping daystart preference. This way the next ping time |
| 201 | // will be correct, hopefully. |
| 202 | days = kPingTimeJump; |
| 203 | LOG(WARNING) << |
| 204 | "System clock jumped back in time. Resetting ping daystarts."; |
| 205 | } |
| 206 | } |
| 207 | return days; |
| 208 | } |
| 209 | |
| 210 | void OmahaRequestAction::InitPingDays() { |
| 211 | // We send pings only along with update checks, not with events. |
| 212 | if (IsEvent()) { |
| 213 | return; |
| 214 | } |
| 215 | // TODO(petkov): Figure a way to distinguish active use pings |
| 216 | // vs. roll call pings. Currently, the two pings are identical. A |
| 217 | // fix needs to change this code as well as UpdateLastPingDays. |
| 218 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 219 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 220 | } |
| 221 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 222 | void OmahaRequestAction::PerformAction() { |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 223 | if (should_skip_) { |
| 224 | processor_->ActionComplete(this, kActionCodeSuccess); |
| 225 | return; |
| 226 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 227 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 228 | InitPingDays(); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 229 | if (ping_only_ && |
| 230 | !ShouldPing(ping_active_days_) && |
| 231 | !ShouldPing(ping_roll_call_days_)) { |
| 232 | processor_->ActionComplete(this, kActionCodeSuccess); |
| 233 | return; |
| 234 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 235 | string request_post(FormatRequest(event_.get(), |
| 236 | params_, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 237 | ping_only_, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 238 | ping_active_days_, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 239 | ping_roll_call_days_, |
| 240 | prefs_)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 241 | http_fetcher_->SetPostData(request_post.data(), request_post.size()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 242 | LOG(INFO) << "Posting an Omaha request to " << params_.update_url; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 243 | LOG(INFO) << "Request: " << request_post; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 244 | http_fetcher_->BeginTransfer(params_.update_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 247 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 248 | http_fetcher_->TerminateTransfer(); |
| 249 | } |
| 250 | |
| 251 | // We just store the response in the buffer. Once we've received all bytes, |
| 252 | // we'll look in the buffer and decide what to do. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 253 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
| 254 | const char* bytes, |
| 255 | int length) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 256 | response_buffer_.reserve(response_buffer_.size() + length); |
| 257 | response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
| 258 | } |
| 259 | |
| 260 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 261 | // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
| 262 | // on the returned object. |
| 263 | // This code is roughly based on the libxml tutorial at: |
| 264 | // http://xmlsoft.org/tutorial/apd.html |
| 265 | xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath, |
| 266 | const xmlChar* ns, const xmlChar* ns_url) { |
| 267 | xmlXPathObject* result = NULL; |
| 268 | |
| 269 | scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context( |
| 270 | xmlXPathNewContext(doc)); |
| 271 | if (!context.get()) { |
| 272 | LOG(ERROR) << "xmlXPathNewContext() returned NULL"; |
| 273 | return NULL; |
| 274 | } |
| 275 | if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) { |
| 276 | LOG(ERROR) << "xmlXPathRegisterNs() returned error"; |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | result = xmlXPathEvalExpression(xpath, context.get()); |
| 281 | |
| 282 | if (result == NULL) { |
| 283 | LOG(ERROR) << "xmlXPathEvalExpression returned error"; |
| 284 | return NULL; |
| 285 | } |
| 286 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
| 287 | LOG(INFO) << "xpath not found in doc"; |
| 288 | xmlXPathFreeObject(result); |
| 289 | return NULL; |
| 290 | } |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | // Returns the string value of a named attribute on a node, or empty string |
| 295 | // if no such node exists. If the attribute exists and has a value of |
| 296 | // empty string, there's no way to distinguish that from the attribute |
| 297 | // not existing. |
| 298 | string XmlGetProperty(xmlNode* node, const char* name) { |
| 299 | if (!xmlHasProp(node, ConstXMLStr(name))) |
| 300 | return ""; |
| 301 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 302 | xmlGetProp(node, ConstXMLStr(name))); |
| 303 | string ret(reinterpret_cast<const char *>(str.get())); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 308 | // on error. If the string contains "0", that's indistinguishable from |
| 309 | // error. |
| 310 | off_t ParseInt(const string& str) { |
| 311 | off_t ret = 0; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 312 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 313 | if (rc < 1) { |
| 314 | // failure |
| 315 | return 0; |
| 316 | } |
| 317 | return ret; |
| 318 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 319 | |
| 320 | // Update the last ping day preferences based on the server daystart |
| 321 | // response. Returns true on success, false otherwise. |
| 322 | bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) { |
| 323 | static const char kNamespace[] = "x"; |
| 324 | static const char kDaystartNodeXpath[] = "/x:gupdate/x:daystart"; |
| 325 | static const char kNsUrl[] = "http://www.google.com/update2/response"; |
| 326 | |
| 327 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 328 | xpath_nodeset(GetNodeSet(doc, |
| 329 | ConstXMLStr(kDaystartNodeXpath), |
| 330 | ConstXMLStr(kNamespace), |
| 331 | ConstXMLStr(kNsUrl))); |
| 332 | TEST_AND_RETURN_FALSE(xpath_nodeset.get()); |
| 333 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 334 | TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1); |
| 335 | xmlNode* daystart_node = nodeset->nodeTab[0]; |
| 336 | TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node, |
| 337 | ConstXMLStr("elapsed_seconds"))); |
| 338 | |
| 339 | int64_t elapsed_seconds = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 340 | TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node, |
| 341 | "elapsed_seconds"), |
| 342 | &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 343 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 344 | |
| 345 | // Remember the local time that matches the server's last midnight |
| 346 | // time. |
| 347 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 348 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 349 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 350 | return true; |
| 351 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 352 | } // namespace {} |
| 353 | |
| 354 | // If the transfer was successful, this uses libxml2 to parse the response |
| 355 | // and fill in the appropriate fields of the output object. Also, notifies |
| 356 | // the processor that we're done. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 357 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
| 358 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 359 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 360 | LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(), |
| 361 | response_buffer_.end()); |
| 362 | |
| 363 | // Events are best effort transactions -- assume they always succeed. |
| 364 | if (IsEvent()) { |
| 365 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 366 | if (event_->result == OmahaEvent::kResultError && successful && |
| 367 | utils::IsOfficialBuild()) { |
| 368 | LOG(INFO) << "Signalling Crash Reporter."; |
| 369 | utils::ScheduleCrashReporterUpload(); |
| 370 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 371 | completer.set_code(kActionCodeSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 375 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 376 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 377 | int code = GetHTTPResponseCode(); |
| 378 | // Makes sure we send sane error values. |
| 379 | if (code < 0 || code >= 1000) { |
| 380 | code = 999; |
| 381 | } |
| 382 | completer.set_code(static_cast<ActionExitCode>( |
| 383 | kActionCodeOmahaRequestHTTPResponseBase + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 384 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 385 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 386 | |
| 387 | // parse our response and fill the fields in the output object |
| 388 | scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc( |
| 389 | xmlParseMemory(&response_buffer_[0], response_buffer_.size())); |
| 390 | if (!doc.get()) { |
| 391 | LOG(ERROR) << "Omaha response not valid XML"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 392 | completer.set_code(response_buffer_.empty() ? |
| 393 | kActionCodeOmahaRequestEmptyResponseError : |
| 394 | kActionCodeOmahaRequestXMLParseError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 398 | // If a ping was sent, update the last ping day preferences based on |
| 399 | // the server daystart response. |
| 400 | if (ShouldPing(ping_active_days_) || |
| 401 | ShouldPing(ping_roll_call_days_) || |
| 402 | ping_active_days_ == kPingTimeJump || |
| 403 | ping_roll_call_days_ == kPingTimeJump) { |
| 404 | LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), prefs_)) |
| 405 | << "Failed to update the last ping day preferences!"; |
| 406 | } |
| 407 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 408 | if (!HasOutputPipe()) { |
| 409 | // Just set success to whether or not the http transfer succeeded, |
| 410 | // which must be true at this point in the code. |
| 411 | completer.set_code(kActionCodeSuccess); |
| 412 | return; |
| 413 | } |
| 414 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 415 | static const char* kNamespace("x"); |
| 416 | static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck"); |
| 417 | static const char* kNsUrl("http://www.google.com/update2/response"); |
| 418 | |
| 419 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 420 | xpath_nodeset(GetNodeSet(doc.get(), |
| 421 | ConstXMLStr(kUpdatecheckNodeXpath), |
| 422 | ConstXMLStr(kNamespace), |
| 423 | ConstXMLStr(kNsUrl))); |
| 424 | if (!xpath_nodeset.get()) { |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 425 | completer.set_code(kActionCodeOmahaRequestNoUpdateCheckNode); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 429 | CHECK(nodeset) << "XPath missing NodeSet"; |
| 430 | CHECK_GE(nodeset->nodeNr, 1); |
| 431 | |
| 432 | xmlNode* updatecheck_node = nodeset->nodeTab[0]; |
| 433 | // get status |
| 434 | if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) { |
| 435 | LOG(ERROR) << "Response missing status"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 436 | completer.set_code(kActionCodeOmahaRequestNoUpdateCheckStatus); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 440 | OmahaResponse output_object; |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 441 | base::StringToInt(XmlGetProperty(updatecheck_node, "PollInterval"), |
| 442 | &output_object.poll_interval); |
| 443 | const string status(XmlGetProperty(updatecheck_node, "status")); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 444 | if (status == "noupdate") { |
| 445 | LOG(INFO) << "No update."; |
| 446 | output_object.update_exists = false; |
| 447 | SetOutputObject(output_object); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 448 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | |
| 452 | if (status != "ok") { |
| 453 | LOG(ERROR) << "Unknown status: " << status; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 454 | completer.set_code(kActionCodeOmahaRequestBadUpdateCheckStatus); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | // In best-effort fashion, fetch the rest of the expected attributes |
| 459 | // from the updatecheck node, then return the object |
| 460 | output_object.update_exists = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 461 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 462 | |
| 463 | output_object.display_version = |
| 464 | XmlGetProperty(updatecheck_node, "DisplayVersion"); |
| 465 | output_object.codebase = XmlGetProperty(updatecheck_node, "codebase"); |
| 466 | output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo"); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 467 | output_object.hash = XmlGetProperty(updatecheck_node, "sha256"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 468 | output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size")); |
| 469 | output_object.needs_admin = |
| 470 | XmlGetProperty(updatecheck_node, "needsadmin") == "true"; |
| 471 | output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true"; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 472 | output_object.is_delta = |
| 473 | XmlGetProperty(updatecheck_node, "IsDelta") == "true"; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 474 | output_object.deadline = XmlGetProperty(updatecheck_node, "deadline"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 475 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | }; // namespace chromeos_update_engine |