blob: d4b77a8a6162444371cd7cd248acea8c96fba489 [file] [log] [blame]
Darin Petkov265f2902011-05-09 15:17:40 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov6a5b3222010-07-13 14:55:28 -07005#include "update_engine/omaha_request_action.h"
Darin Petkov85ced132010-09-01 10:20:56 -07006
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07007#include <inttypes.h>
Darin Petkov85ced132010-09-01 10:20:56 -07008
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <sstream>
10
Darin Petkov85ced132010-09-01 10:20:56 -070011#include <base/string_number_conversions.h>
12#include <base/string_util.h>
13#include <base/time.h>
14#include <base/logging.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <libxml/parser.h>
16#include <libxml/xpath.h>
17#include <libxml/xpathInternals.h>
18
19#include "update_engine/action_pipe.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070020#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070021#include "update_engine/prefs_interface.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Darin Petkov1cbd78f2010-07-29 12:38:34 -070024using base::Time;
25using base::TimeDelta;
rspangler@google.com49fdf182009-10-10 00:57:34 +000026using std::string;
27
28namespace chromeos_update_engine {
29
rspangler@google.com49fdf182009-10-10 00:57:34 +000030namespace {
31
32const 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.
39class ScopedPtrXmlDocFree {
40 public:
41 inline void operator()(void* x) const {
42 xmlFreeDoc(reinterpret_cast<xmlDoc*>(x));
43 }
44};
45class ScopedPtrXmlFree {
46 public:
47 inline void operator()(void* x) const {
48 xmlFree(x);
49 }
50};
51class ScopedPtrXmlXPathObjectFree {
52 public:
53 inline void operator()(void* x) const {
54 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x));
55 }
56};
57class ScopedPtrXmlXPathContextFree {
58 public:
59 inline void operator()(void* x) const {
60 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x));
61 }
62};
63
Darin Petkov1cbd78f2010-07-29 12:38:34 -070064// Returns true if |ping_days| has a value that needs to be sent,
65// false otherwise.
66bool 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.
73string 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.
82string 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 Le116fda32011-04-19 11:01:54 -070086 return StringPrintf(" <o:ping active=\"1\"%s%s></o:ping>\n",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070087 ping_active.c_str(),
88 ping_roll_call.c_str());
89 }
90 return "";
91}
92
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070093string FormatRequest(const OmahaEvent* event,
Darin Petkov1cbd78f2010-07-29 12:38:34 -070094 const OmahaRequestParams& params,
Thieu Le116fda32011-04-19 11:01:54 -070095 bool ping_only,
Darin Petkov1cbd78f2010-07-29 12:38:34 -070096 int ping_active_days,
Darin Petkov95508da2011-01-05 12:42:29 -080097 int ping_roll_call_days,
98 PrefsInterface* prefs) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070099 string body;
100 if (event == NULL) {
Thieu Le116fda32011-04-19 11:01:54 -0700101 body = GetPingBody(ping_active_days, ping_roll_call_days);
Darin Petkov265f2902011-05-09 15:17:40 -0700102 if (!ping_only) {
Thieu Le116fda32011-04-19 11:01:54 -0700103 body += " <o:updatecheck></o:updatecheck>\n";
Darin Petkov265f2902011-05-09 15:17:40 -0700104 // 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 Petkov95508da2011-01-05 12:42:29 -0800125 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700126 } else {
Darin Petkovc91dd6b2011-01-10 12:31:34 -0800127 // The error code is an optional attribute so append it only if the result
128 // is not success.
Darin Petkove17f86b2010-07-20 09:12:01 -0700129 string error_code;
130 if (event->result != OmahaEvent::kResultSuccess) {
Darin Petkovc91dd6b2011-01-10 12:31:34 -0800131 int code = event->error_code;
132 if (!utils::IsNormalBootMode()) {
133 code |= kActionCodeBootModeFlag;
134 }
135 error_code = StringPrintf(" errorcode=\"%d\"", code);
Darin Petkove17f86b2010-07-20 09:12:01 -0700136 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700137 body = StringPrintf(
Darin Petkove17f86b2010-07-20 09:12:01 -0700138 " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n",
139 event->type, event->result, error_code.c_str());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700140 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700141 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
142 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
143 "version=\"" + XmlEncode(kGupdateVersion) + "\" "
144 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" "
145 "protocol=\"2.0\" ismachine=\"1\">\n"
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" +
147 XmlEncode(params.os_platform) + "\" sp=\"" +
148 XmlEncode(params.os_sp) + "\"></o:os>\n"
149 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" +
150 XmlEncode(params.app_version) + "\" "
151 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" +
Andrew de los Reyes37c20322010-06-30 13:27:19 -0700152 XmlEncode(params.app_track) + "\" board=\"" +
Darin Petkovfbb40092010-07-29 17:05:50 -0700153 XmlEncode(params.os_board) + "\" hardware_class=\"" +
154 XmlEncode(params.hardware_class) + "\" delta_okay=\"" +
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700155 (params.delta_okay ? "true" : "false") + "\">\n" + body +
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 " </o:app>\n"
157 "</o:gupdate>\n";
158}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700159
rspangler@google.com49fdf182009-10-10 00:57:34 +0000160} // namespace {}
161
162// Encodes XML entities in a given string with libxml2. input must be
163// UTF-8 formatted. Output will be UTF-8 formatted.
164string XmlEncode(const string& input) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700165 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
166 // // cpu, considering creating one and caching it.
167 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
168 // xmlNewDoc(ConstXMLStr("1.0")));
169 // if (!xml_doc.get()) {
170 // LOG(ERROR) << "Unable to create xmlDoc";
171 // return "";
172 // }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000173 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
174 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
175 return string(reinterpret_cast<const char *>(str.get()));
176}
177
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700178OmahaRequestAction::OmahaRequestAction(PrefsInterface* prefs,
179 const OmahaRequestParams& params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700180 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700181 HttpFetcher* http_fetcher,
182 bool ping_only)
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700183 : prefs_(prefs),
184 params_(params),
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700185 event_(event),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700186 http_fetcher_(http_fetcher),
Thieu Le116fda32011-04-19 11:01:54 -0700187 ping_only_(ping_only),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700188 ping_active_days_(0),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700189 ping_roll_call_days_(0),
190 should_skip_(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191
Darin Petkov6a5b3222010-07-13 14:55:28 -0700192OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700194// Calculates the value to use for the ping days parameter.
195int OmahaRequestAction::CalculatePingDays(const string& key) {
196 int days = kNeverPinged;
197 int64_t last_ping = 0;
198 if (prefs_->GetInt64(key, &last_ping) && last_ping >= 0) {
199 days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays();
200 if (days < 0) {
201 // If |days| is negative, then the system clock must have jumped
202 // back in time since the ping was sent. Mark the value so that
203 // it doesn't get sent to the server but we still update the
204 // last ping daystart preference. This way the next ping time
205 // will be correct, hopefully.
206 days = kPingTimeJump;
207 LOG(WARNING) <<
208 "System clock jumped back in time. Resetting ping daystarts.";
209 }
210 }
211 return days;
212}
213
214void OmahaRequestAction::InitPingDays() {
215 // We send pings only along with update checks, not with events.
216 if (IsEvent()) {
217 return;
218 }
219 // TODO(petkov): Figure a way to distinguish active use pings
220 // vs. roll call pings. Currently, the two pings are identical. A
221 // fix needs to change this code as well as UpdateLastPingDays.
222 ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay);
223 ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay);
224}
225
Darin Petkov6a5b3222010-07-13 14:55:28 -0700226void OmahaRequestAction::PerformAction() {
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700227 if (should_skip_) {
228 processor_->ActionComplete(this, kActionCodeSuccess);
229 return;
230 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000231 http_fetcher_->set_delegate(this);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700232 InitPingDays();
233 string request_post(FormatRequest(event_.get(),
234 params_,
Thieu Le116fda32011-04-19 11:01:54 -0700235 ping_only_,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700236 ping_active_days_,
Darin Petkov95508da2011-01-05 12:42:29 -0800237 ping_roll_call_days_,
238 prefs_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000239 http_fetcher_->SetPostData(request_post.data(), request_post.size());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700240 LOG(INFO) << "Posting an Omaha request to " << params_.update_url;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700241 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700242 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243}
244
Darin Petkov6a5b3222010-07-13 14:55:28 -0700245void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246 http_fetcher_->TerminateTransfer();
247}
248
249// We just store the response in the buffer. Once we've received all bytes,
250// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700251void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
252 const char* bytes,
253 int length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000254 response_buffer_.reserve(response_buffer_.size() + length);
255 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
256}
257
258namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000259// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
260// on the returned object.
261// This code is roughly based on the libxml tutorial at:
262// http://xmlsoft.org/tutorial/apd.html
263xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
264 const xmlChar* ns, const xmlChar* ns_url) {
265 xmlXPathObject* result = NULL;
266
267 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
268 xmlXPathNewContext(doc));
269 if (!context.get()) {
270 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
271 return NULL;
272 }
273 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
274 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
275 return NULL;
276 }
277
278 result = xmlXPathEvalExpression(xpath, context.get());
279
280 if (result == NULL) {
281 LOG(ERROR) << "xmlXPathEvalExpression returned error";
282 return NULL;
283 }
284 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
285 LOG(INFO) << "xpath not found in doc";
286 xmlXPathFreeObject(result);
287 return NULL;
288 }
289 return result;
290}
291
292// Returns the string value of a named attribute on a node, or empty string
293// if no such node exists. If the attribute exists and has a value of
294// empty string, there's no way to distinguish that from the attribute
295// not existing.
296string XmlGetProperty(xmlNode* node, const char* name) {
297 if (!xmlHasProp(node, ConstXMLStr(name)))
298 return "";
299 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
300 xmlGetProp(node, ConstXMLStr(name)));
301 string ret(reinterpret_cast<const char *>(str.get()));
302 return ret;
303}
304
305// Parses a 64 bit base-10 int from a string and returns it. Returns 0
306// on error. If the string contains "0", that's indistinguishable from
307// error.
308off_t ParseInt(const string& str) {
309 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700310 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000311 if (rc < 1) {
312 // failure
313 return 0;
314 }
315 return ret;
316}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700317
318// Update the last ping day preferences based on the server daystart
319// response. Returns true on success, false otherwise.
320bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) {
321 static const char kNamespace[] = "x";
322 static const char kDaystartNodeXpath[] = "/x:gupdate/x:daystart";
323 static const char kNsUrl[] = "http://www.google.com/update2/response";
324
325 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
326 xpath_nodeset(GetNodeSet(doc,
327 ConstXMLStr(kDaystartNodeXpath),
328 ConstXMLStr(kNamespace),
329 ConstXMLStr(kNsUrl)));
330 TEST_AND_RETURN_FALSE(xpath_nodeset.get());
331 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
332 TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1);
333 xmlNode* daystart_node = nodeset->nodeTab[0];
334 TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node,
335 ConstXMLStr("elapsed_seconds")));
336
337 int64_t elapsed_seconds = 0;
Chris Masone790e62e2010-08-12 10:41:18 -0700338 TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node,
339 "elapsed_seconds"),
340 &elapsed_seconds));
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700341 TEST_AND_RETURN_FALSE(elapsed_seconds >= 0);
342
343 // Remember the local time that matches the server's last midnight
344 // time.
345 Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds);
346 prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue());
347 prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue());
348 return true;
349}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000350} // namespace {}
351
352// If the transfer was successful, this uses libxml2 to parse the response
353// and fill in the appropriate fields of the output object. Also, notifies
354// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700355void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
356 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000357 ScopedActionCompleter completer(processor_, this);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700358 LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(),
359 response_buffer_.end());
360
361 // Events are best effort transactions -- assume they always succeed.
362 if (IsEvent()) {
363 CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests.";
Andrew de los Reyes2008e4c2011-01-12 10:17:52 -0800364 if (event_->result == OmahaEvent::kResultError && successful &&
365 utils::IsOfficialBuild()) {
366 LOG(INFO) << "Signalling Crash Reporter.";
367 utils::ScheduleCrashReporterUpload();
368 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700369 completer.set_code(kActionCodeSuccess);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700370 return;
371 }
372
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700373 if (!successful) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700374 LOG(ERROR) << "Omaha request network transfer failed.";
Darin Petkovedc522e2010-11-05 09:35:17 -0700375 int code = GetHTTPResponseCode();
376 // Makes sure we send sane error values.
377 if (code < 0 || code >= 1000) {
378 code = 999;
379 }
380 completer.set_code(static_cast<ActionExitCode>(
381 kActionCodeOmahaRequestHTTPResponseBase + code));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700383 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000384
385 // parse our response and fill the fields in the output object
386 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
387 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
388 if (!doc.get()) {
389 LOG(ERROR) << "Omaha response not valid XML";
Darin Petkovedc522e2010-11-05 09:35:17 -0700390 completer.set_code(response_buffer_.empty() ?
391 kActionCodeOmahaRequestEmptyResponseError :
392 kActionCodeOmahaRequestXMLParseError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000393 return;
394 }
395
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700396 // If a ping was sent, update the last ping day preferences based on
397 // the server daystart response.
398 if (ShouldPing(ping_active_days_) ||
399 ShouldPing(ping_roll_call_days_) ||
400 ping_active_days_ == kPingTimeJump ||
401 ping_roll_call_days_ == kPingTimeJump) {
402 LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), prefs_))
403 << "Failed to update the last ping day preferences!";
404 }
405
Thieu Le116fda32011-04-19 11:01:54 -0700406 if (!HasOutputPipe()) {
407 // Just set success to whether or not the http transfer succeeded,
408 // which must be true at this point in the code.
409 completer.set_code(kActionCodeSuccess);
410 return;
411 }
412
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413 static const char* kNamespace("x");
414 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
415 static const char* kNsUrl("http://www.google.com/update2/response");
416
417 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
418 xpath_nodeset(GetNodeSet(doc.get(),
419 ConstXMLStr(kUpdatecheckNodeXpath),
420 ConstXMLStr(kNamespace),
421 ConstXMLStr(kNsUrl)));
422 if (!xpath_nodeset.get()) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700423 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckNode);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000424 return;
425 }
426 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
427 CHECK(nodeset) << "XPath missing NodeSet";
428 CHECK_GE(nodeset->nodeNr, 1);
429
430 xmlNode* updatecheck_node = nodeset->nodeTab[0];
431 // get status
432 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
433 LOG(ERROR) << "Response missing status";
Darin Petkovedc522e2010-11-05 09:35:17 -0700434 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000435 return;
436 }
437
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438 OmahaResponse output_object;
Darin Petkov85ced132010-09-01 10:20:56 -0700439 base::StringToInt(XmlGetProperty(updatecheck_node, "PollInterval"),
440 &output_object.poll_interval);
441 const string status(XmlGetProperty(updatecheck_node, "status"));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000442 if (status == "noupdate") {
443 LOG(INFO) << "No update.";
444 output_object.update_exists = false;
445 SetOutputObject(output_object);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700446 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000447 return;
448 }
449
450 if (status != "ok") {
451 LOG(ERROR) << "Unknown status: " << status;
Darin Petkovedc522e2010-11-05 09:35:17 -0700452 completer.set_code(kActionCodeOmahaRequestBadUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000453 return;
454 }
455
456 // In best-effort fashion, fetch the rest of the expected attributes
457 // from the updatecheck node, then return the object
458 output_object.update_exists = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700459 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000460
461 output_object.display_version =
462 XmlGetProperty(updatecheck_node, "DisplayVersion");
463 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
464 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
Darin Petkovd22cb292010-09-29 10:02:29 -0700465 output_object.hash = XmlGetProperty(updatecheck_node, "sha256");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000466 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
467 output_object.needs_admin =
468 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
469 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700470 output_object.is_delta =
471 XmlGetProperty(updatecheck_node, "IsDelta") == "true";
Darin Petkov6c118642010-10-21 12:06:30 -0700472 output_object.deadline = XmlGetProperty(updatecheck_node, "deadline");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000473 SetOutputObject(output_object);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000474}
475
476}; // namespace chromeos_update_engine