blob: 8329c569ebe1c233eac5b305a834e3e798612fc8 [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) {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700103 // not passing update_disabled to Omaha because we want to
104 // get the update and report with UpdateDeferred result so that
105 // borgmon charts show up updates that are deferred. This is also
106 // the expected behavior when we move to Omaha v3.0 protocol, so it'll
107 // be consistent.
Jay Srinivasan0a708742012-03-20 11:26:12 -0700108 body += StringPrintf(
109 " <o:updatecheck"
Jay Srinivasan0a708742012-03-20 11:26:12 -0700110 " targetversionprefix=\"%s\""
111 "></o:updatecheck>\n",
Jay Srinivasan0a708742012-03-20 11:26:12 -0700112 XmlEncode(params.target_version_prefix).c_str());
113
Darin Petkov265f2902011-05-09 15:17:40 -0700114 // If this is the first update check after a reboot following a previous
115 // update, generate an event containing the previous version number. If
116 // the previous version preference file doesn't exist the event is still
117 // generated with a previous version of 0.0.0.0 -- this is relevant for
118 // older clients or new installs. The previous version event is not sent
119 // for ping-only requests because they come before the client has
120 // rebooted.
121 string prev_version;
122 if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) {
123 prev_version = "0.0.0.0";
124 }
125 if (!prev_version.empty()) {
126 body += StringPrintf(
127 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
128 "previousversion=\"%s\"></o:event>\n",
129 OmahaEvent::kTypeUpdateComplete,
130 OmahaEvent::kResultSuccessReboot,
131 XmlEncode(prev_version).c_str());
132 LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, ""))
133 << "Unable to reset the previous version.";
134 }
Darin Petkov95508da2011-01-05 12:42:29 -0800135 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700136 } else {
Darin Petkovc91dd6b2011-01-10 12:31:34 -0800137 // The error code is an optional attribute so append it only if the result
138 // is not success.
Darin Petkove17f86b2010-07-20 09:12:01 -0700139 string error_code;
140 if (event->result != OmahaEvent::kResultSuccess) {
Darin Petkov18c7bce2011-06-16 14:07:00 -0700141 error_code = StringPrintf(" errorcode=\"%d\"", event->error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -0700142 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700143 body = StringPrintf(
Darin Petkove17f86b2010-07-20 09:12:01 -0700144 " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n",
145 event->type, event->result, error_code.c_str());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700146 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700147 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
148 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
149 "version=\"" + XmlEncode(kGupdateVersion) + "\" "
150 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" "
151 "protocol=\"2.0\" ismachine=\"1\">\n"
rspangler@google.com49fdf182009-10-10 00:57:34 +0000152 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" +
153 XmlEncode(params.os_platform) + "\" sp=\"" +
154 XmlEncode(params.os_sp) + "\"></o:os>\n"
155 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" +
156 XmlEncode(params.app_version) + "\" "
157 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" +
Andrew de los Reyes37c20322010-06-30 13:27:19 -0700158 XmlEncode(params.app_track) + "\" board=\"" +
Darin Petkovfbb40092010-07-29 17:05:50 -0700159 XmlEncode(params.os_board) + "\" hardware_class=\"" +
160 XmlEncode(params.hardware_class) + "\" delta_okay=\"" +
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700161 (params.delta_okay ? "true" : "false") + "\">\n" + body +
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162 " </o:app>\n"
163 "</o:gupdate>\n";
164}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700165
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166} // namespace {}
167
168// Encodes XML entities in a given string with libxml2. input must be
169// UTF-8 formatted. Output will be UTF-8 formatted.
170string XmlEncode(const string& input) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700171 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
172 // // cpu, considering creating one and caching it.
173 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
174 // xmlNewDoc(ConstXMLStr("1.0")));
175 // if (!xml_doc.get()) {
176 // LOG(ERROR) << "Unable to create xmlDoc";
177 // return "";
178 // }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
180 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
181 return string(reinterpret_cast<const char *>(str.get()));
182}
183
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700184OmahaRequestAction::OmahaRequestAction(PrefsInterface* prefs,
185 const OmahaRequestParams& params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700186 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700187 HttpFetcher* http_fetcher,
188 bool ping_only)
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700189 : prefs_(prefs),
190 params_(params),
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700191 event_(event),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700192 http_fetcher_(http_fetcher),
Thieu Le116fda32011-04-19 11:01:54 -0700193 ping_only_(ping_only),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700194 ping_active_days_(0),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700195 ping_roll_call_days_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196
Darin Petkov6a5b3222010-07-13 14:55:28 -0700197OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700199// Calculates the value to use for the ping days parameter.
200int OmahaRequestAction::CalculatePingDays(const string& key) {
201 int days = kNeverPinged;
202 int64_t last_ping = 0;
203 if (prefs_->GetInt64(key, &last_ping) && last_ping >= 0) {
204 days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays();
205 if (days < 0) {
206 // If |days| is negative, then the system clock must have jumped
207 // back in time since the ping was sent. Mark the value so that
208 // it doesn't get sent to the server but we still update the
209 // last ping daystart preference. This way the next ping time
210 // will be correct, hopefully.
211 days = kPingTimeJump;
212 LOG(WARNING) <<
213 "System clock jumped back in time. Resetting ping daystarts.";
214 }
215 }
216 return days;
217}
218
219void OmahaRequestAction::InitPingDays() {
220 // We send pings only along with update checks, not with events.
221 if (IsEvent()) {
222 return;
223 }
224 // TODO(petkov): Figure a way to distinguish active use pings
225 // vs. roll call pings. Currently, the two pings are identical. A
226 // fix needs to change this code as well as UpdateLastPingDays.
227 ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay);
228 ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay);
229}
230
Darin Petkov6a5b3222010-07-13 14:55:28 -0700231void OmahaRequestAction::PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232 http_fetcher_->set_delegate(this);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700233 InitPingDays();
Thieu Leb44e9e82011-06-06 14:34:04 -0700234 if (ping_only_ &&
235 !ShouldPing(ping_active_days_) &&
236 !ShouldPing(ping_roll_call_days_)) {
237 processor_->ActionComplete(this, kActionCodeSuccess);
238 return;
239 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700240 string request_post(FormatRequest(event_.get(),
241 params_,
Thieu Le116fda32011-04-19 11:01:54 -0700242 ping_only_,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700243 ping_active_days_,
Darin Petkov95508da2011-01-05 12:42:29 -0800244 ping_roll_call_days_,
245 prefs_));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700246
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800247 http_fetcher_->SetPostData(request_post.data(), request_post.size(),
248 kHttpContentTypeTextXml);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700249 LOG(INFO) << "Posting an Omaha request to " << params_.update_url;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700250 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700251 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252}
253
Darin Petkov6a5b3222010-07-13 14:55:28 -0700254void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255 http_fetcher_->TerminateTransfer();
256}
257
258// We just store the response in the buffer. Once we've received all bytes,
259// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
261 const char* bytes,
262 int length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000263 response_buffer_.reserve(response_buffer_.size() + length);
264 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
265}
266
267namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000268// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
269// on the returned object.
270// This code is roughly based on the libxml tutorial at:
271// http://xmlsoft.org/tutorial/apd.html
272xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
273 const xmlChar* ns, const xmlChar* ns_url) {
274 xmlXPathObject* result = NULL;
275
276 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
277 xmlXPathNewContext(doc));
278 if (!context.get()) {
279 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
280 return NULL;
281 }
282 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
283 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
284 return NULL;
285 }
286
287 result = xmlXPathEvalExpression(xpath, context.get());
288
289 if (result == NULL) {
290 LOG(ERROR) << "xmlXPathEvalExpression returned error";
291 return NULL;
292 }
293 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
294 LOG(INFO) << "xpath not found in doc";
295 xmlXPathFreeObject(result);
296 return NULL;
297 }
298 return result;
299}
300
301// Returns the string value of a named attribute on a node, or empty string
302// if no such node exists. If the attribute exists and has a value of
303// empty string, there's no way to distinguish that from the attribute
304// not existing.
305string XmlGetProperty(xmlNode* node, const char* name) {
306 if (!xmlHasProp(node, ConstXMLStr(name)))
307 return "";
308 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
309 xmlGetProp(node, ConstXMLStr(name)));
310 string ret(reinterpret_cast<const char *>(str.get()));
311 return ret;
312}
313
314// Parses a 64 bit base-10 int from a string and returns it. Returns 0
315// on error. If the string contains "0", that's indistinguishable from
316// error.
317off_t ParseInt(const string& str) {
318 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700319 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000320 if (rc < 1) {
321 // failure
322 return 0;
323 }
324 return ret;
325}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700326
327// Update the last ping day preferences based on the server daystart
328// response. Returns true on success, false otherwise.
329bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) {
330 static const char kNamespace[] = "x";
331 static const char kDaystartNodeXpath[] = "/x:gupdate/x:daystart";
332 static const char kNsUrl[] = "http://www.google.com/update2/response";
333
334 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
335 xpath_nodeset(GetNodeSet(doc,
336 ConstXMLStr(kDaystartNodeXpath),
337 ConstXMLStr(kNamespace),
338 ConstXMLStr(kNsUrl)));
339 TEST_AND_RETURN_FALSE(xpath_nodeset.get());
340 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
341 TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1);
342 xmlNode* daystart_node = nodeset->nodeTab[0];
343 TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node,
344 ConstXMLStr("elapsed_seconds")));
345
346 int64_t elapsed_seconds = 0;
Chris Masone790e62e2010-08-12 10:41:18 -0700347 TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node,
348 "elapsed_seconds"),
349 &elapsed_seconds));
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700350 TEST_AND_RETURN_FALSE(elapsed_seconds >= 0);
351
352 // Remember the local time that matches the server's last midnight
353 // time.
354 Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds);
355 prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue());
356 prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue());
357 return true;
358}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000359} // namespace {}
360
361// If the transfer was successful, this uses libxml2 to parse the response
362// and fill in the appropriate fields of the output object. Also, notifies
363// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700364void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
365 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000366 ScopedActionCompleter completer(processor_, this);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700367 LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(),
368 response_buffer_.end());
369
370 // Events are best effort transactions -- assume they always succeed.
371 if (IsEvent()) {
372 CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests.";
Andrew de los Reyes2008e4c2011-01-12 10:17:52 -0800373 if (event_->result == OmahaEvent::kResultError && successful &&
374 utils::IsOfficialBuild()) {
375 LOG(INFO) << "Signalling Crash Reporter.";
376 utils::ScheduleCrashReporterUpload();
377 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700378 completer.set_code(kActionCodeSuccess);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700379 return;
380 }
381
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700382 if (!successful) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700383 LOG(ERROR) << "Omaha request network transfer failed.";
Darin Petkovedc522e2010-11-05 09:35:17 -0700384 int code = GetHTTPResponseCode();
385 // Makes sure we send sane error values.
386 if (code < 0 || code >= 1000) {
387 code = 999;
388 }
389 completer.set_code(static_cast<ActionExitCode>(
390 kActionCodeOmahaRequestHTTPResponseBase + code));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000391 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700392 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000393
394 // parse our response and fill the fields in the output object
395 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
396 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
397 if (!doc.get()) {
398 LOG(ERROR) << "Omaha response not valid XML";
Darin Petkovedc522e2010-11-05 09:35:17 -0700399 completer.set_code(response_buffer_.empty() ?
400 kActionCodeOmahaRequestEmptyResponseError :
401 kActionCodeOmahaRequestXMLParseError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000402 return;
403 }
404
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700405 // If a ping was sent, update the last ping day preferences based on
406 // the server daystart response.
407 if (ShouldPing(ping_active_days_) ||
408 ShouldPing(ping_roll_call_days_) ||
409 ping_active_days_ == kPingTimeJump ||
410 ping_roll_call_days_ == kPingTimeJump) {
411 LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), prefs_))
412 << "Failed to update the last ping day preferences!";
413 }
414
Thieu Le116fda32011-04-19 11:01:54 -0700415 if (!HasOutputPipe()) {
416 // Just set success to whether or not the http transfer succeeded,
417 // which must be true at this point in the code.
418 completer.set_code(kActionCodeSuccess);
419 return;
420 }
421
rspangler@google.com49fdf182009-10-10 00:57:34 +0000422 static const char* kNamespace("x");
423 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
424 static const char* kNsUrl("http://www.google.com/update2/response");
425
426 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
427 xpath_nodeset(GetNodeSet(doc.get(),
428 ConstXMLStr(kUpdatecheckNodeXpath),
429 ConstXMLStr(kNamespace),
430 ConstXMLStr(kNsUrl)));
431 if (!xpath_nodeset.get()) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700432 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckNode);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000433 return;
434 }
435 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
436 CHECK(nodeset) << "XPath missing NodeSet";
437 CHECK_GE(nodeset->nodeNr, 1);
438
439 xmlNode* updatecheck_node = nodeset->nodeTab[0];
440 // get status
441 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
442 LOG(ERROR) << "Response missing status";
Darin Petkovedc522e2010-11-05 09:35:17 -0700443 completer.set_code(kActionCodeOmahaRequestNoUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000444 return;
445 }
446
Darin Petkov6a5b3222010-07-13 14:55:28 -0700447 OmahaResponse output_object;
Darin Petkov85ced132010-09-01 10:20:56 -0700448 base::StringToInt(XmlGetProperty(updatecheck_node, "PollInterval"),
449 &output_object.poll_interval);
450 const string status(XmlGetProperty(updatecheck_node, "status"));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000451 if (status == "noupdate") {
452 LOG(INFO) << "No update.";
453 output_object.update_exists = false;
454 SetOutputObject(output_object);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700455 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000456 return;
457 }
458
459 if (status != "ok") {
460 LOG(ERROR) << "Unknown status: " << status;
Darin Petkovedc522e2010-11-05 09:35:17 -0700461 completer.set_code(kActionCodeOmahaRequestBadUpdateCheckStatus);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000462 return;
463 }
464
Jay Srinivasan0a708742012-03-20 11:26:12 -0700465 if (params_.update_disabled) {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700466 LOG(INFO) << "Ignoring Omaha updates as updates are disabled by policy.";
Jay Srinivasan0a708742012-03-20 11:26:12 -0700467 completer.set_code(kActionCodeOmahaUpdateIgnoredPerPolicy);
468 return;
469 }
470
rspangler@google.com49fdf182009-10-10 00:57:34 +0000471 // In best-effort fashion, fetch the rest of the expected attributes
472 // from the updatecheck node, then return the object
473 output_object.update_exists = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700474 completer.set_code(kActionCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000475
476 output_object.display_version =
477 XmlGetProperty(updatecheck_node, "DisplayVersion");
478 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
479 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
Darin Petkovd22cb292010-09-29 10:02:29 -0700480 output_object.hash = XmlGetProperty(updatecheck_node, "sha256");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000481 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
482 output_object.needs_admin =
483 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
484 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
Darin Petkov6c118642010-10-21 12:06:30 -0700485 output_object.deadline = XmlGetProperty(updatecheck_node, "deadline");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000486 SetOutputObject(output_object);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000487}
488
489}; // namespace chromeos_update_engine