blob: bbcc00b411f157ccb02ff07586619b103ed7b5b4 [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 Petkov18c7bce2011-06-16 14:07:00 -0700131 error_code = StringPrintf(" errorcode=\"%d\"", event->error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -0700132 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700133 body = StringPrintf(
Darin Petkove17f86b2010-07-20 09:12:01 -0700134 " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n",
135 event->type, event->result, error_code.c_str());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700136 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700137 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.com49fdf182009-10-10 00:57:34 +0000142 " <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 Reyes37c20322010-06-30 13:27:19 -0700148 XmlEncode(params.app_track) + "\" board=\"" +
Darin Petkovfbb40092010-07-29 17:05:50 -0700149 XmlEncode(params.os_board) + "\" hardware_class=\"" +
150 XmlEncode(params.hardware_class) + "\" delta_okay=\"" +
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700151 (params.delta_okay ? "true" : "false") + "\">\n" + body +
rspangler@google.com49fdf182009-10-10 00:57:34 +0000152 " </o:app>\n"
153 "</o:gupdate>\n";
154}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156} // 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.
160string XmlEncode(const string& input) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700161 // // 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.com49fdf182009-10-10 00:57:34 +0000169 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 Petkov1cbd78f2010-07-29 12:38:34 -0700174OmahaRequestAction::OmahaRequestAction(PrefsInterface* prefs,
175 const OmahaRequestParams& params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700176 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700177 HttpFetcher* http_fetcher,
178 bool ping_only)
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700179 : prefs_(prefs),
180 params_(params),
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700181 event_(event),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700182 http_fetcher_(http_fetcher),
Thieu Le116fda32011-04-19 11:01:54 -0700183 ping_only_(ping_only),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700184 ping_active_days_(0),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700185 ping_roll_call_days_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186
Darin Petkov6a5b3222010-07-13 14:55:28 -0700187OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700189// Calculates the value to use for the ping days parameter.
190int OmahaRequestAction::CalculatePingDays(const string& key) {
191 int days = kNeverPinged;
192 int64_t last_ping = 0;
193 if (prefs_->GetInt64(key, &last_ping) && last_ping >= 0) {
194 days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays();
195 if (days < 0) {
196 // If |days| is negative, then the system clock must have jumped
197 // back in time since the ping was sent. Mark the value so that
198 // it doesn't get sent to the server but we still update the
199 // last ping daystart preference. This way the next ping time
200 // will be correct, hopefully.
201 days = kPingTimeJump;
202 LOG(WARNING) <<
203 "System clock jumped back in time. Resetting ping daystarts.";
204 }
205 }
206 return days;
207}
208
209void OmahaRequestAction::InitPingDays() {
210 // We send pings only along with update checks, not with events.
211 if (IsEvent()) {
212 return;
213 }
214 // TODO(petkov): Figure a way to distinguish active use pings
215 // vs. roll call pings. Currently, the two pings are identical. A
216 // fix needs to change this code as well as UpdateLastPingDays.
217 ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay);
218 ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay);
219}
220
Darin Petkov6a5b3222010-07-13 14:55:28 -0700221void OmahaRequestAction::PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000222 http_fetcher_->set_delegate(this);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700223 InitPingDays();
Thieu Leb44e9e82011-06-06 14:34:04 -0700224 if (ping_only_ &&
225 !ShouldPing(ping_active_days_) &&
226 !ShouldPing(ping_roll_call_days_)) {
227 processor_->ActionComplete(this, kActionCodeSuccess);
228 return;
229 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700230 string request_post(FormatRequest(event_.get(),
231 params_,
Thieu Le116fda32011-04-19 11:01:54 -0700232 ping_only_,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700233 ping_active_days_,
Darin Petkov95508da2011-01-05 12:42:29 -0800234 ping_roll_call_days_,
235 prefs_));
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800236 http_fetcher_->SetPostData(request_post.data(), request_post.size(),
237 kHttpContentTypeTextXml);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700238 LOG(INFO) << "Posting an Omaha request to " << params_.update_url;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700239 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700240 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000241}
242
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000244 http_fetcher_->TerminateTransfer();
245}
246
247// We just store the response in the buffer. Once we've received all bytes,
248// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700249void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
250 const char* bytes,
251 int length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252 response_buffer_.reserve(response_buffer_.size() + length);
253 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
254}
255
256namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000257// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
258// on the returned object.
259// This code is roughly based on the libxml tutorial at:
260// http://xmlsoft.org/tutorial/apd.html
261xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
262 const xmlChar* ns, const xmlChar* ns_url) {
263 xmlXPathObject* result = NULL;
264
265 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
266 xmlXPathNewContext(doc));
267 if (!context.get()) {
268 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
269 return NULL;
270 }
271 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
272 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
273 return NULL;
274 }
275
276 result = xmlXPathEvalExpression(xpath, context.get());
277
278 if (result == NULL) {
279 LOG(ERROR) << "xmlXPathEvalExpression returned error";
280 return NULL;
281 }
282 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
283 LOG(INFO) << "xpath not found in doc";
284 xmlXPathFreeObject(result);
285 return NULL;
286 }
287 return result;
288}
289
290// Returns the string value of a named attribute on a node, or empty string
291// if no such node exists. If the attribute exists and has a value of
292// empty string, there's no way to distinguish that from the attribute
293// not existing.
294string XmlGetProperty(xmlNode* node, const char* name) {
295 if (!xmlHasProp(node, ConstXMLStr(name)))
296 return "";
297 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
298 xmlGetProp(node, ConstXMLStr(name)));
299 string ret(reinterpret_cast<const char *>(str.get()));
300 return ret;
301}
302
303// Parses a 64 bit base-10 int from a string and returns it. Returns 0
304// on error. If the string contains "0", that's indistinguishable from
305// error.
306off_t ParseInt(const string& str) {
307 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700308 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000309 if (rc < 1) {
310 // failure
311 return 0;
312 }
313 return ret;
314}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700315
316// Update the last ping day preferences based on the server daystart
317// response. Returns true on success, false otherwise.
318bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) {
319 static const char kNamespace[] = "x";
320 static const char kDaystartNodeXpath[] = "/x:gupdate/x:daystart";
321 static const char kNsUrl[] = "http://www.google.com/update2/response";
322
323 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
324 xpath_nodeset(GetNodeSet(doc,
325 ConstXMLStr(kDaystartNodeXpath),
326 ConstXMLStr(kNamespace),
327 ConstXMLStr(kNsUrl)));
328 TEST_AND_RETURN_FALSE(xpath_nodeset.get());
329 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
330 TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1);
331 xmlNode* daystart_node = nodeset->nodeTab[0];
332 TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node,
333 ConstXMLStr("elapsed_seconds")));
334
335 int64_t elapsed_seconds = 0;
Chris Masone790e62e2010-08-12 10:41:18 -0700336 TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node,
337 "elapsed_seconds"),
338 &elapsed_seconds));
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700339 TEST_AND_RETURN_FALSE(elapsed_seconds >= 0);
340
341 // Remember the local time that matches the server's last midnight
342 // time.
343 Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds);
344 prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue());
345 prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue());
346 return true;
347}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000348} // namespace {}
349
350// If the transfer was successful, this uses libxml2 to parse the response
351// and fill in the appropriate fields of the output object. Also, notifies
352// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700353void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
354 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000355 ScopedActionCompleter completer(processor_, this);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700356 LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(),
357 response_buffer_.end());
358
359 // Events are best effort transactions -- assume they always succeed.
360 if (IsEvent()) {
361 CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests.";
Andrew de los Reyes2008e4c2011-01-12 10:17:52 -0800362 if (event_->result == OmahaEvent::kResultError && successful &&
363 utils::IsOfficialBuild()) {
364 LOG(INFO) << "Signalling Crash Reporter.";
365 utils::ScheduleCrashReporterUpload();
366 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700367 completer.set_code(kActionCodeSuccess);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700368 return;
369 }
370
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700371 if (!successful) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700372 LOG(ERROR) << "Omaha request network transfer failed.";
Darin Petkovedc522e2010-11-05 09:35:17 -0700373 int code = GetHTTPResponseCode();
374 // Makes sure we send sane error values.
375 if (code < 0 || code >= 1000) {
376 code = 999;
377 }
378 completer.set_code(static_cast<ActionExitCode>(
379 kActionCodeOmahaRequestHTTPResponseBase + code));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000380 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700381 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382
383 // parse our response and fill the fields in the output object
384 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
385 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
386 if (!doc.get()) {
387 LOG(ERROR) << "Omaha response not valid XML";
Darin Petkovedc522e2010-11-05 09:35:17 -0700388 completer.set_code(response_buffer_.empty() ?
389 kActionCodeOmahaRequestEmptyResponseError :
390 kActionCodeOmahaRequestXMLParseError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000391 return;
392 }
393
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700394 // If a ping was sent, update the last ping day preferences based on
395 // the server daystart response.
396 if (ShouldPing(ping_active_days_) ||
397 ShouldPing(ping_roll_call_days_) ||
398 ping_active_days_ == kPingTimeJump ||
399 ping_roll_call_days_ == kPingTimeJump) {
400 LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), prefs_))
401 << "Failed to update the last ping day preferences!";
402 }
403
Thieu Le116fda32011-04-19 11:01:54 -0700404 if (!HasOutputPipe()) {
405 // Just set success to whether or not the http transfer succeeded,
406 // which must be true at this point in the code.
407 completer.set_code(kActionCodeSuccess);
408 return;
409 }
410
rspangler@google.com49fdf182009-10-10 00:57:34 +0000411 static const char* kNamespace("x");
412 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
413 static const char* kNsUrl("http://www.google.com/update2/response");
414
415 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
416 xpath_nodeset(GetNodeSet(doc.get(),
417 ConstXMLStr(kUpdatecheckNodeXpath),
418 ConstXMLStr(kNamespace),
419 ConstXMLStr(kNsUrl)));
420 if (!xpath_nodeset.get()) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700421 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckNode);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000422 return;
423 }
424 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
425 CHECK(nodeset) << "XPath missing NodeSet";
426 CHECK_GE(nodeset->nodeNr, 1);
427
428 xmlNode* updatecheck_node = nodeset->nodeTab[0];
429 // get status
430 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
431 LOG(ERROR) << "Response missing status";
Darin Petkovedc522e2010-11-05 09:35:17 -0700432 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000433 return;
434 }
435
Darin Petkov6a5b3222010-07-13 14:55:28 -0700436 OmahaResponse output_object;
Darin Petkov85ced132010-09-01 10:20:56 -0700437 base::StringToInt(XmlGetProperty(updatecheck_node, "PollInterval"),
438 &output_object.poll_interval);
439 const string status(XmlGetProperty(updatecheck_node, "status"));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000440 if (status == "noupdate") {
441 LOG(INFO) << "No update.";
442 output_object.update_exists = false;
443 SetOutputObject(output_object);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700444 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000445 return;
446 }
447
448 if (status != "ok") {
449 LOG(ERROR) << "Unknown status: " << status;
Darin Petkovedc522e2010-11-05 09:35:17 -0700450 completer.set_code(kActionCodeOmahaRequestBadUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000451 return;
452 }
453
454 // In best-effort fashion, fetch the rest of the expected attributes
455 // from the updatecheck node, then return the object
456 output_object.update_exists = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700457 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000458
459 output_object.display_version =
460 XmlGetProperty(updatecheck_node, "DisplayVersion");
461 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
462 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
Darin Petkovd22cb292010-09-29 10:02:29 -0700463 output_object.hash = XmlGetProperty(updatecheck_node, "sha256");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000464 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
465 output_object.needs_admin =
466 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
467 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
Darin Petkov6c118642010-10-21 12:06:30 -0700468 output_object.deadline = XmlGetProperty(updatecheck_node, "deadline");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000469 SetOutputObject(output_object);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000470}
471
472}; // namespace chromeos_update_engine