Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 5 | #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> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 10 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 15 | #include <base/strings/string_number_conversions.h> |
| 16 | #include <base/strings/string_util.h> |
| 17 | #include <base/strings/stringprintf.h> |
| 18 | #include <base/time/time.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | #include <libxml/xpath.h> |
| 20 | #include <libxml/xpathInternals.h> |
| 21 | |
| 22 | #include "update_engine/action_pipe.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 23 | #include "update_engine/constants.h" |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 24 | #include "update_engine/hardware_interface.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_hash_calculator.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 26 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 27 | #include "update_engine/p2p_manager.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 28 | #include "update_engine/payload_state_interface.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 29 | #include "update_engine/prefs_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 30 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 32 | using base::Time; |
| 33 | using base::TimeDelta; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | using std::string; |
| 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 38 | // List of custom pair tags that we interpret in the Omaha Response: |
| 39 | static const char* kTagDeadline = "deadline"; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 40 | static const char* kTagDisablePayloadBackoff = "DisablePayloadBackoff"; |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 41 | static const char* kTagVersion = "version"; |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 42 | // Deprecated: "IsDelta" |
| 43 | static const char* kTagIsDeltaPayload = "IsDeltaPayload"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 44 | static const char* kTagMaxFailureCountPerUrl = "MaxFailureCountPerUrl"; |
| 45 | static const char* kTagMaxDaysToScatter = "MaxDaysToScatter"; |
| 46 | // Deprecated: "ManifestSignatureRsa" |
| 47 | // Deprecated: "ManifestSize" |
| 48 | static const char* kTagMetadataSignatureRsa = "MetadataSignatureRsa"; |
| 49 | static const char* kTagMetadataSize = "MetadataSize"; |
| 50 | static const char* kTagMoreInfo = "MoreInfo"; |
Don Garrett | 42bd3aa | 2013-04-10 18:14:56 -0700 | [diff] [blame] | 51 | // Deprecated: "NeedsAdmin" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 52 | static const char* kTagPrompt = "Prompt"; |
| 53 | static const char* kTagSha256 = "sha256"; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 54 | static const char* kTagDisableP2PForDownloading = "DisableP2PForDownloading"; |
| 55 | static const char* kTagDisableP2PForSharing = "DisableP2PForSharing"; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 56 | static const char* kTagPublicKeyRsa = "PublicKeyRsa"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 57 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | |
| 60 | const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
| 61 | |
| 62 | // This is handy for passing strings into libxml2 |
| 63 | #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
| 64 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 65 | // These are for scoped_ptr with a custom free function to be specified. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | class ScopedPtrXmlDocFree { |
| 67 | public: |
| 68 | inline void operator()(void* x) const { |
| 69 | xmlFreeDoc(reinterpret_cast<xmlDoc*>(x)); |
| 70 | } |
| 71 | }; |
| 72 | class ScopedPtrXmlFree { |
| 73 | public: |
| 74 | inline void operator()(void* x) const { |
| 75 | xmlFree(x); |
| 76 | } |
| 77 | }; |
| 78 | class ScopedPtrXmlXPathObjectFree { |
| 79 | public: |
| 80 | inline void operator()(void* x) const { |
| 81 | xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
| 82 | } |
| 83 | }; |
| 84 | class ScopedPtrXmlXPathContextFree { |
| 85 | public: |
| 86 | inline void operator()(void* x) const { |
| 87 | xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
| 88 | } |
| 89 | }; |
| 90 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 91 | // Returns true if |ping_days| has a value that needs to be sent, |
| 92 | // false otherwise. |
| 93 | bool ShouldPing(int ping_days) { |
| 94 | return ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged; |
| 95 | } |
| 96 | |
| 97 | // Returns an XML ping element attribute assignment with attribute |
| 98 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 99 | // to be sent, or an empty string otherwise. |
| 100 | string GetPingAttribute(const string& name, int ping_days) { |
| 101 | if (ShouldPing(ping_days)) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 102 | return base::StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 103 | } |
| 104 | return ""; |
| 105 | } |
| 106 | |
| 107 | // Returns an XML ping element if any of the elapsed days need to be |
| 108 | // sent, or an empty string otherwise. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 109 | string GetPingXml(int ping_active_days, int ping_roll_call_days) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 110 | string ping_active = GetPingAttribute("a", ping_active_days); |
| 111 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); |
| 112 | if (!ping_active.empty() || !ping_roll_call.empty()) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 113 | return base::StringPrintf(" <ping active=\"1\"%s%s></ping>\n", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 114 | ping_active.c_str(), |
| 115 | ping_roll_call.c_str()); |
| 116 | } |
| 117 | return ""; |
| 118 | } |
| 119 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 120 | // Returns an XML that goes into the body of the <app> element of the Omaha |
| 121 | // request based on the given parameters. |
| 122 | string GetAppBody(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 123 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 124 | bool ping_only, |
| 125 | int ping_active_days, |
| 126 | int ping_roll_call_days, |
| 127 | PrefsInterface* prefs) { |
| 128 | string app_body; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 129 | if (event == NULL) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 130 | app_body = GetPingXml(ping_active_days, ping_roll_call_days); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 131 | if (!ping_only) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 132 | // not passing update_disabled to Omaha because we want to |
| 133 | // get the update and report with UpdateDeferred result so that |
| 134 | // borgmon charts show up updates that are deferred. This is also |
| 135 | // the expected behavior when we move to Omaha v3.0 protocol, so it'll |
| 136 | // be consistent. |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 137 | app_body += base::StringPrintf( |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 138 | " <updatecheck targetversionprefix=\"%s\"" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 139 | "></updatecheck>\n", |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 140 | XmlEncode(params->target_version_prefix()).c_str()); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 141 | |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 142 | // If this is the first update check after a reboot following a previous |
| 143 | // update, generate an event containing the previous version number. If |
| 144 | // the previous version preference file doesn't exist the event is still |
| 145 | // generated with a previous version of 0.0.0.0 -- this is relevant for |
| 146 | // older clients or new installs. The previous version event is not sent |
| 147 | // for ping-only requests because they come before the client has |
| 148 | // rebooted. |
| 149 | string prev_version; |
| 150 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { |
| 151 | prev_version = "0.0.0.0"; |
| 152 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 153 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 154 | app_body += base::StringPrintf( |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 155 | " <event eventtype=\"%d\" eventresult=\"%d\" " |
| 156 | "previousversion=\"%s\"></event>\n", |
| 157 | OmahaEvent::kTypeUpdateComplete, |
| 158 | OmahaEvent::kResultSuccessReboot, |
| 159 | XmlEncode(prev_version).c_str()); |
| 160 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) |
| 161 | << "Unable to reset the previous version."; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 162 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 163 | } else { |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 164 | // The error code is an optional attribute so append it only if the result |
| 165 | // is not success. |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 166 | string error_code; |
| 167 | if (event->result != OmahaEvent::kResultSuccess) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 168 | error_code = base::StringPrintf(" errorcode=\"%d\"", event->error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 169 | } |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 170 | app_body = base::StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 171 | " <event eventtype=\"%d\" eventresult=\"%d\"%s></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 172 | event->type, event->result, error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 173 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 174 | |
| 175 | return app_body; |
| 176 | } |
| 177 | |
| 178 | // Returns an XML that corresponds to the entire <app> node of the Omaha |
| 179 | // request based on the given parameters. |
| 180 | string GetAppXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 181 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 182 | bool ping_only, |
| 183 | int ping_active_days, |
| 184 | int ping_roll_call_days, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 185 | int install_date_in_days, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 186 | SystemState* system_state) { |
| 187 | string app_body = GetAppBody(event, params, ping_only, ping_active_days, |
| 188 | ping_roll_call_days, system_state->prefs()); |
| 189 | string app_versions; |
| 190 | |
| 191 | // If we are upgrading to a more stable channel and we are allowed to do |
| 192 | // powerwash, then pass 0.0.0.0 as the version. This is needed to get the |
| 193 | // highest-versioned payload on the destination channel. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 194 | if (params->to_more_stable_channel() && params->is_powerwash_allowed()) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 195 | LOG(INFO) << "Passing OS version as 0.0.0.0 as we are set to powerwash " |
| 196 | << "on downgrading to the version in the more stable channel"; |
| 197 | app_versions = "version=\"0.0.0.0\" from_version=\"" + |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 198 | XmlEncode(params->app_version()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 199 | } else { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 200 | app_versions = "version=\"" + XmlEncode(params->app_version()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 203 | string download_channel = params->download_channel(); |
| 204 | string app_channels = "track=\"" + XmlEncode(download_channel) + "\" "; |
| 205 | if (params->current_channel() != download_channel) |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 206 | app_channels += |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 207 | "from_track=\"" + XmlEncode(params->current_channel()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 208 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 209 | string delta_okay_str = params->delta_okay() ? "true" : "false"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 210 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 211 | // If install_date_days is not set (e.g. its value is -1 ), don't |
| 212 | // include the attribute. |
| 213 | string install_date_in_days_str = ""; |
| 214 | if (install_date_in_days >= 0) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 215 | install_date_in_days_str = base::StringPrintf("installdate=\"%d\" ", |
| 216 | install_date_in_days); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 219 | string app_xml = |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 220 | " <app appid=\"" + XmlEncode(params->GetAppId()) + "\" " + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 221 | app_versions + |
| 222 | app_channels + |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 223 | "lang=\"" + XmlEncode(params->app_lang()) + "\" " + |
| 224 | "board=\"" + XmlEncode(params->os_board()) + "\" " + |
| 225 | "hardware_class=\"" + XmlEncode(params->hwid()) + "\" " + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 226 | "delta_okay=\"" + delta_okay_str + "\" " |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 227 | "fw_version=\"" + XmlEncode(params->fw_version()) + "\" " + |
| 228 | "ec_version=\"" + XmlEncode(params->ec_version()) + "\" " + |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 229 | install_date_in_days_str + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 230 | ">\n" + |
| 231 | app_body + |
| 232 | " </app>\n"; |
| 233 | |
| 234 | return app_xml; |
| 235 | } |
| 236 | |
| 237 | // Returns an XML that corresponds to the entire <os> node of the Omaha |
| 238 | // request based on the given parameters. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 239 | string GetOsXml(OmahaRequestParams* params) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 240 | string os_xml = |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 241 | " <os version=\"" + XmlEncode(params->os_version()) + "\" " + |
| 242 | "platform=\"" + XmlEncode(params->os_platform()) + "\" " + |
| 243 | "sp=\"" + XmlEncode(params->os_sp()) + "\">" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 244 | "</os>\n"; |
| 245 | return os_xml; |
| 246 | } |
| 247 | |
| 248 | // Returns an XML that corresponds to the entire Omaha request based on the |
| 249 | // given parameters. |
| 250 | string GetRequestXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 251 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 252 | bool ping_only, |
| 253 | int ping_active_days, |
| 254 | int ping_roll_call_days, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 255 | int install_date_in_days, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 256 | SystemState* system_state) { |
| 257 | string os_xml = GetOsXml(params); |
| 258 | string app_xml = GetAppXml(event, params, ping_only, ping_active_days, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 259 | ping_roll_call_days, install_date_in_days, |
| 260 | system_state); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 261 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 262 | string install_source = base::StringPrintf("installsource=\"%s\" ", |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 263 | (params->interactive() ? "ondemandupdate" : "scheduler")); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 264 | |
| 265 | string request_xml = |
| 266 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 267 | "<request protocol=\"3.0\" " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 268 | "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 269 | "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " + |
| 270 | install_source + |
| 271 | "ismachine=\"1\">\n" + |
| 272 | os_xml + |
| 273 | app_xml + |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 274 | "</request>\n"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 275 | |
| 276 | return request_xml; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 277 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 278 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 279 | } // namespace {} |
| 280 | |
| 281 | // Encodes XML entities in a given string with libxml2. input must be |
| 282 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 283 | string XmlEncode(const string& input) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 284 | // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
| 285 | // // cpu, considering creating one and caching it. |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 286 | // scoped_ptr<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 287 | // xmlNewDoc(ConstXMLStr("1.0"))); |
| 288 | // if (!xml_doc.get()) { |
| 289 | // LOG(ERROR) << "Unable to create xmlDoc"; |
| 290 | // return ""; |
| 291 | // } |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 292 | scoped_ptr<xmlChar, ScopedPtrXmlFree> str( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 293 | xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
| 294 | return string(reinterpret_cast<const char *>(str.get())); |
| 295 | } |
| 296 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 297 | OmahaRequestAction::OmahaRequestAction(SystemState* system_state, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 298 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 299 | HttpFetcher* http_fetcher, |
| 300 | bool ping_only) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 301 | : system_state_(system_state), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 302 | event_(event), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 303 | http_fetcher_(http_fetcher), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 304 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 305 | ping_active_days_(0), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 306 | ping_roll_call_days_(0) { |
| 307 | params_ = system_state->request_params(); |
| 308 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 309 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 310 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 311 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 312 | // Calculates the value to use for the ping days parameter. |
| 313 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 314 | int days = kNeverPinged; |
| 315 | int64_t last_ping = 0; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 316 | if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 317 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 318 | if (days < 0) { |
| 319 | // If |days| is negative, then the system clock must have jumped |
| 320 | // back in time since the ping was sent. Mark the value so that |
| 321 | // it doesn't get sent to the server but we still update the |
| 322 | // last ping daystart preference. This way the next ping time |
| 323 | // will be correct, hopefully. |
| 324 | days = kPingTimeJump; |
| 325 | LOG(WARNING) << |
| 326 | "System clock jumped back in time. Resetting ping daystarts."; |
| 327 | } |
| 328 | } |
| 329 | return days; |
| 330 | } |
| 331 | |
| 332 | void OmahaRequestAction::InitPingDays() { |
| 333 | // We send pings only along with update checks, not with events. |
| 334 | if (IsEvent()) { |
| 335 | return; |
| 336 | } |
| 337 | // TODO(petkov): Figure a way to distinguish active use pings |
| 338 | // vs. roll call pings. Currently, the two pings are identical. A |
| 339 | // fix needs to change this code as well as UpdateLastPingDays. |
| 340 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 341 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 342 | } |
| 343 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 344 | // static |
| 345 | int OmahaRequestAction::GetInstallDate(SystemState* system_state) { |
| 346 | PrefsInterface* prefs = system_state->prefs(); |
| 347 | if (prefs == NULL) |
| 348 | return -1; |
| 349 | |
| 350 | // If we have the value stored on disk, just return it. |
| 351 | int64_t stored_value; |
| 352 | if (prefs->GetInt64(kPrefsInstallDateDays, &stored_value)) { |
| 353 | // Convert and sanity-check. |
| 354 | int install_date_days = static_cast<int>(stored_value); |
| 355 | if (install_date_days >= 0) |
| 356 | return install_date_days; |
| 357 | LOG(ERROR) << "Dropping stored Omaha InstallData since its value num_days=" |
| 358 | << install_date_days << " looks suspicious."; |
| 359 | prefs->Delete(kPrefsInstallDateDays); |
| 360 | } |
| 361 | |
| 362 | // Otherwise, if OOBE is not complete then do nothing and wait for |
| 363 | // ParseResponse() to call ParseInstallDate() and then |
| 364 | // PersistInstallDate() to set the kPrefsInstallDateDays state |
| 365 | // variable. Once that is done, we'll then report back in future |
| 366 | // Omaha requests. This works exactly because OOBE triggers an |
| 367 | // update check. |
| 368 | // |
| 369 | // However, if OOBE is complete and the kPrefsInstallDateDays state |
| 370 | // variable is not set, there are two possibilities |
| 371 | // |
| 372 | // 1. The update check in OOBE failed so we never got a response |
| 373 | // from Omaha (no network etc.); or |
| 374 | // |
| 375 | // 2. OOBE was done on an older version that didn't write to the |
| 376 | // kPrefsInstallDateDays state variable. |
| 377 | // |
| 378 | // In both cases, we approximate the install date by simply |
| 379 | // inspecting the timestamp of when OOBE happened. |
| 380 | |
| 381 | Time time_of_oobe; |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 382 | if (!system_state->hardware()->IsOOBEComplete(&time_of_oobe)) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 383 | LOG(INFO) << "Not generating Omaha InstallData as we have " |
| 384 | << "no prefs file and OOBE is not complete."; |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | int num_days; |
| 389 | if (!utils::ConvertToOmahaInstallDate(time_of_oobe, &num_days)) { |
| 390 | LOG(ERROR) << "Not generating Omaha InstallData from time of OOBE " |
| 391 | << "as its value '" << utils::ToString(time_of_oobe) |
| 392 | << "' looks suspicious."; |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | // Persist this to disk, for future use. |
| 397 | if (!OmahaRequestAction::PersistInstallDate(system_state, |
| 398 | num_days, |
| 399 | kProvisionedFromOOBEMarker)) |
| 400 | return -1; |
| 401 | |
| 402 | LOG(INFO) << "Set the Omaha InstallDate from OOBE time-stamp to " |
| 403 | << num_days << " days"; |
| 404 | |
| 405 | return num_days; |
| 406 | } |
| 407 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 408 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 409 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 410 | InitPingDays(); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 411 | if (ping_only_ && |
| 412 | !ShouldPing(ping_active_days_) && |
| 413 | !ShouldPing(ping_roll_call_days_)) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 414 | processor_->ActionComplete(this, kErrorCodeSuccess); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 417 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 418 | string request_post(GetRequestXml(event_.get(), |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 419 | params_, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 420 | ping_only_, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 421 | ping_active_days_, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 422 | ping_roll_call_days_, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 423 | GetInstallDate(system_state_), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 424 | system_state_)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 425 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 426 | http_fetcher_->SetPostData(request_post.data(), request_post.size(), |
| 427 | kHttpContentTypeTextXml); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 428 | LOG(INFO) << "Posting an Omaha request to " << params_->update_url(); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 429 | LOG(INFO) << "Request: " << request_post; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 430 | http_fetcher_->BeginTransfer(params_->update_url()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 433 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 434 | http_fetcher_->TerminateTransfer(); |
| 435 | } |
| 436 | |
| 437 | // We just store the response in the buffer. Once we've received all bytes, |
| 438 | // we'll look in the buffer and decide what to do. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 439 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
| 440 | const char* bytes, |
| 441 | int length) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 442 | response_buffer_.reserve(response_buffer_.size() + length); |
| 443 | response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
| 444 | } |
| 445 | |
| 446 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 447 | // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
| 448 | // on the returned object. |
| 449 | // This code is roughly based on the libxml tutorial at: |
| 450 | // http://xmlsoft.org/tutorial/apd.html |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 451 | xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 452 | xmlXPathObject* result = NULL; |
| 453 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 454 | scoped_ptr<xmlXPathContext, ScopedPtrXmlXPathContextFree> context( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 455 | xmlXPathNewContext(doc)); |
| 456 | if (!context.get()) { |
| 457 | LOG(ERROR) << "xmlXPathNewContext() returned NULL"; |
| 458 | return NULL; |
| 459 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 460 | |
| 461 | result = xmlXPathEvalExpression(xpath, context.get()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 462 | if (result == NULL) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 463 | LOG(ERROR) << "Unable to find " << xpath << " in XML document"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 464 | return NULL; |
| 465 | } |
| 466 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 467 | LOG(INFO) << "Nodeset is empty for " << xpath; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 468 | xmlXPathFreeObject(result); |
| 469 | return NULL; |
| 470 | } |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | // Returns the string value of a named attribute on a node, or empty string |
| 475 | // if no such node exists. If the attribute exists and has a value of |
| 476 | // empty string, there's no way to distinguish that from the attribute |
| 477 | // not existing. |
| 478 | string XmlGetProperty(xmlNode* node, const char* name) { |
| 479 | if (!xmlHasProp(node, ConstXMLStr(name))) |
| 480 | return ""; |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 481 | scoped_ptr<xmlChar, ScopedPtrXmlFree> str( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 482 | xmlGetProp(node, ConstXMLStr(name))); |
| 483 | string ret(reinterpret_cast<const char *>(str.get())); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 488 | // on error. If the string contains "0", that's indistinguishable from |
| 489 | // error. |
| 490 | off_t ParseInt(const string& str) { |
| 491 | off_t ret = 0; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 492 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 493 | if (rc < 1) { |
| 494 | // failure |
| 495 | return 0; |
| 496 | } |
| 497 | return ret; |
| 498 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 499 | |
| 500 | // Update the last ping day preferences based on the server daystart |
| 501 | // response. Returns true on success, false otherwise. |
| 502 | bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 503 | static const char kDaystartNodeXpath[] = "/response/daystart"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 504 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 505 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 506 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kDaystartNodeXpath))); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 507 | TEST_AND_RETURN_FALSE(xpath_nodeset.get()); |
| 508 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 509 | TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1); |
| 510 | xmlNode* daystart_node = nodeset->nodeTab[0]; |
| 511 | TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node, |
| 512 | ConstXMLStr("elapsed_seconds"))); |
| 513 | |
| 514 | int64_t elapsed_seconds = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 515 | TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node, |
| 516 | "elapsed_seconds"), |
| 517 | &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 518 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 519 | |
| 520 | // Remember the local time that matches the server's last midnight |
| 521 | // time. |
| 522 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 523 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 524 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 525 | return true; |
| 526 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 527 | } // namespace {} |
| 528 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 529 | bool OmahaRequestAction::ParseResponse(xmlDoc* doc, |
| 530 | OmahaResponse* output_object, |
| 531 | ScopedActionCompleter* completer) { |
| 532 | static const char* kUpdatecheckNodeXpath("/response/app/updatecheck"); |
| 533 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 534 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 535 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kUpdatecheckNodeXpath))); |
| 536 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 537 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
| 541 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 542 | CHECK(nodeset) << "XPath missing UpdateCheck NodeSet"; |
| 543 | CHECK_GE(nodeset->nodeNr, 1); |
| 544 | xmlNode* update_check_node = nodeset->nodeTab[0]; |
| 545 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 546 | // chromium-os:37289: The PollInterval is not supported by Omaha server |
| 547 | // currently. But still keeping this existing code in case we ever decide to |
| 548 | // slow down the request rate from the server-side. Note that the |
| 549 | // PollInterval is not persisted, so it has to be sent by the server on every |
| 550 | // response to guarantee that the UpdateCheckScheduler uses this value |
| 551 | // (otherwise, if the device got rebooted after the last server-indicated |
| 552 | // value, it'll revert to the default value). Also kDefaultMaxUpdateChecks |
| 553 | // value for the scattering logic is based on the assumption that we perform |
| 554 | // an update check every hour so that the max value of 8 will roughly be |
| 555 | // equivalent to one work day. If we decide to use PollInterval permanently, |
| 556 | // we should update the max_update_checks_allowed to take PollInterval into |
| 557 | // account. Note: The parsing for PollInterval happens even before parsing |
| 558 | // of the status because we may want to specify the PollInterval even when |
| 559 | // there's no update. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 560 | base::StringToInt(XmlGetProperty(update_check_node, "PollInterval"), |
| 561 | &output_object->poll_interval); |
| 562 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 563 | // Check for the "elapsed_days" attribute in the "daystart" |
| 564 | // element. This is the number of days since Jan 1 2007, 0:00 |
| 565 | // PST. If we don't have a persisted value of the Omaha InstallDate, |
| 566 | // we'll use it to calculate it and then persist it. |
| 567 | if (ParseInstallDate(doc, output_object) && !HasInstallDate(system_state_)) { |
| 568 | // Since output_object->install_date_days is never negative, the |
| 569 | // elapsed_days -> install-date calculation is reduced to simply |
| 570 | // rounding down to the nearest number divisible by 7. |
| 571 | int remainder = output_object->install_date_days % 7; |
| 572 | int install_date_days_rounded = |
| 573 | output_object->install_date_days - remainder; |
| 574 | if (PersistInstallDate(system_state_, |
| 575 | install_date_days_rounded, |
| 576 | kProvisionedFromOmahaResponse)) { |
| 577 | LOG(INFO) << "Set the Omaha InstallDate from Omaha Response to " |
| 578 | << install_date_days_rounded << " days"; |
| 579 | } |
| 580 | } |
| 581 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 582 | if (!ParseStatus(update_check_node, output_object, completer)) |
| 583 | return false; |
| 584 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 585 | // Note: ParseUrls MUST be called before ParsePackage as ParsePackage |
| 586 | // appends the package name to the URLs populated in this method. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 587 | if (!ParseUrls(doc, output_object, completer)) |
| 588 | return false; |
| 589 | |
| 590 | if (!ParsePackage(doc, output_object, completer)) |
| 591 | return false; |
| 592 | |
| 593 | if (!ParseParams(doc, output_object, completer)) |
| 594 | return false; |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | bool OmahaRequestAction::ParseStatus(xmlNode* update_check_node, |
| 600 | OmahaResponse* output_object, |
| 601 | ScopedActionCompleter* completer) { |
| 602 | // Get status. |
| 603 | if (!xmlHasProp(update_check_node, ConstXMLStr("status"))) { |
| 604 | LOG(ERROR) << "Omaha Response missing status"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 605 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 606 | return false; |
| 607 | } |
| 608 | |
| 609 | const string status(XmlGetProperty(update_check_node, "status")); |
| 610 | if (status == "noupdate") { |
| 611 | LOG(INFO) << "No update."; |
| 612 | output_object->update_exists = false; |
| 613 | SetOutputObject(*output_object); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 614 | completer->set_code(kErrorCodeSuccess); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | |
| 618 | if (status != "ok") { |
| 619 | LOG(ERROR) << "Unknown Omaha response status: " << status; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 620 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | bool OmahaRequestAction::ParseUrls(xmlDoc* doc, |
| 628 | OmahaResponse* output_object, |
| 629 | ScopedActionCompleter* completer) { |
| 630 | // Get the update URL. |
| 631 | static const char* kUpdateUrlNodeXPath("/response/app/updatecheck/urls/url"); |
| 632 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 633 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 634 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kUpdateUrlNodeXPath))); |
| 635 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 636 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 637 | return false; |
| 638 | } |
| 639 | |
| 640 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 641 | CHECK(nodeset) << "XPath missing " << kUpdateUrlNodeXPath; |
| 642 | CHECK_GE(nodeset->nodeNr, 1); |
| 643 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 644 | LOG(INFO) << "Found " << nodeset->nodeNr << " url(s)"; |
| 645 | output_object->payload_urls.clear(); |
| 646 | for (int i = 0; i < nodeset->nodeNr; i++) { |
| 647 | xmlNode* url_node = nodeset->nodeTab[i]; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 648 | const string codebase(XmlGetProperty(url_node, "codebase")); |
| 649 | if (codebase.empty()) { |
| 650 | LOG(ERROR) << "Omaha Response URL has empty codebase"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 651 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 652 | return false; |
| 653 | } |
| 654 | output_object->payload_urls.push_back(codebase); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 657 | return true; |
| 658 | } |
| 659 | |
| 660 | bool OmahaRequestAction::ParsePackage(xmlDoc* doc, |
| 661 | OmahaResponse* output_object, |
| 662 | ScopedActionCompleter* completer) { |
| 663 | // Get the package node. |
| 664 | static const char* kPackageNodeXPath( |
| 665 | "/response/app/updatecheck/manifest/packages/package"); |
| 666 | |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 667 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 668 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kPackageNodeXPath))); |
| 669 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 670 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
| 674 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 675 | CHECK(nodeset) << "XPath missing " << kPackageNodeXPath; |
| 676 | CHECK_GE(nodeset->nodeNr, 1); |
| 677 | |
| 678 | // We only care about the first package. |
| 679 | LOG(INFO) << "Processing first of " << nodeset->nodeNr << " package(s)"; |
| 680 | xmlNode* package_node = nodeset->nodeTab[0]; |
| 681 | |
| 682 | // Get package properties one by one. |
| 683 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 684 | // Parse the payload name to be appended to the base Url value. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 685 | const string package_name(XmlGetProperty(package_node, "name")); |
| 686 | LOG(INFO) << "Omaha Response package name = " << package_name; |
| 687 | if (package_name.empty()) { |
| 688 | LOG(ERROR) << "Omaha Response has empty package name"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 689 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 690 | return false; |
| 691 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 692 | |
| 693 | // Append the package name to each URL in our list so that we don't |
| 694 | // propagate the urlBase vs packageName distinctions beyond this point. |
| 695 | // From now on, we only need to use payload_urls. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 696 | for (size_t i = 0; i < output_object->payload_urls.size(); i++) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 697 | output_object->payload_urls[i] += package_name; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 698 | |
| 699 | // Parse the payload size. |
| 700 | off_t size = ParseInt(XmlGetProperty(package_node, "size")); |
| 701 | if (size <= 0) { |
| 702 | LOG(ERROR) << "Omaha Response has invalid payload size: " << size; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 703 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 704 | return false; |
| 705 | } |
| 706 | output_object->size = size; |
| 707 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 708 | LOG(INFO) << "Payload size = " << output_object->size << " bytes"; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 709 | |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | bool OmahaRequestAction::ParseParams(xmlDoc* doc, |
| 714 | OmahaResponse* output_object, |
| 715 | ScopedActionCompleter* completer) { |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 716 | // XPath location for response elements we care about. |
| 717 | static const char* kManifestNodeXPath("/response/app/updatecheck/manifest");\ |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 718 | static const char* kActionNodeXPath( |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 719 | "/response/app/updatecheck/manifest/actions/action"); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 720 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 721 | // Get the manifest node where version is present. |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 722 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 723 | xpath_manifest_nodeset(GetNodeSet(doc, ConstXMLStr(kManifestNodeXPath))); |
| 724 | if (!xpath_manifest_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 725 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 726 | return false; |
| 727 | } |
| 728 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 729 | // Grab the only matching node there should be from the xpath. |
| 730 | xmlNodeSet* nodeset = xpath_manifest_nodeset->nodesetval; |
| 731 | CHECK(nodeset) << "XPath missing " << kManifestNodeXPath; |
| 732 | CHECK_GE(nodeset->nodeNr, 1); |
| 733 | xmlNode* manifest_node = nodeset->nodeTab[0]; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 734 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 735 | // Set the version. |
| 736 | output_object->version = XmlGetProperty(manifest_node, kTagVersion); |
| 737 | if (output_object->version.empty()) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 738 | LOG(ERROR) << "Omaha Response does not have version in manifest!"; |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 739 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | LOG(INFO) << "Received omaha response to update to version " |
| 744 | << output_object->version; |
| 745 | |
| 746 | // Grab the action nodes. |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 747 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 748 | xpath_action_nodeset(GetNodeSet(doc, ConstXMLStr(kActionNodeXPath))); |
| 749 | if (!xpath_action_nodeset.get()) { |
| 750 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | // We only care about the action that has event "postinstall", because this is |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 755 | // where Omaha puts all the generic name/value pairs in the rule. |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 756 | nodeset = xpath_action_nodeset->nodesetval; |
| 757 | CHECK(nodeset) << "XPath missing " << kActionNodeXPath; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 758 | LOG(INFO) << "Found " << nodeset->nodeNr |
| 759 | << " action(s). Processing the postinstall action."; |
| 760 | |
| 761 | // pie_action_node holds the action node corresponding to the |
| 762 | // postinstall event action, if present. |
| 763 | xmlNode* pie_action_node = NULL; |
| 764 | for (int i = 0; i < nodeset->nodeNr; i++) { |
| 765 | xmlNode* action_node = nodeset->nodeTab[i]; |
| 766 | if (XmlGetProperty(action_node, "event") == "postinstall") { |
| 767 | pie_action_node = action_node; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | if (!pie_action_node) { |
| 773 | LOG(ERROR) << "Omaha Response has no postinstall event action"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 774 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 778 | output_object->hash = XmlGetProperty(pie_action_node, kTagSha256); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 779 | if (output_object->hash.empty()) { |
| 780 | LOG(ERROR) << "Omaha Response has empty sha256 value"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 781 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 785 | // Get the optional properties one by one. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 786 | output_object->more_info_url = XmlGetProperty(pie_action_node, kTagMoreInfo); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 787 | output_object->metadata_size = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 788 | ParseInt(XmlGetProperty(pie_action_node, kTagMetadataSize)); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 789 | output_object->metadata_signature = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 790 | XmlGetProperty(pie_action_node, kTagMetadataSignatureRsa); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 791 | output_object->prompt = XmlGetProperty(pie_action_node, kTagPrompt) == "true"; |
| 792 | output_object->deadline = XmlGetProperty(pie_action_node, kTagDeadline); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 793 | output_object->max_days_to_scatter = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 794 | ParseInt(XmlGetProperty(pie_action_node, kTagMaxDaysToScatter)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 795 | output_object->disable_p2p_for_downloading = |
| 796 | (XmlGetProperty(pie_action_node, kTagDisableP2PForDownloading) == "true"); |
| 797 | output_object->disable_p2p_for_sharing = |
| 798 | (XmlGetProperty(pie_action_node, kTagDisableP2PForSharing) == "true"); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 799 | output_object->public_key_rsa = |
| 800 | XmlGetProperty(pie_action_node, kTagPublicKeyRsa); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 801 | |
| 802 | string max = XmlGetProperty(pie_action_node, kTagMaxFailureCountPerUrl); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 803 | if (!base::StringToUint(max, &output_object->max_failure_count_per_url)) |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 804 | output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 805 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 806 | output_object->is_delta_payload = |
| 807 | XmlGetProperty(pie_action_node, kTagIsDeltaPayload) == "true"; |
| 808 | |
| 809 | output_object->disable_payload_backoff = |
| 810 | XmlGetProperty(pie_action_node, kTagDisablePayloadBackoff) == "true"; |
| 811 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 812 | return true; |
| 813 | } |
| 814 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 815 | // If the transfer was successful, this uses libxml2 to parse the response |
| 816 | // and fill in the appropriate fields of the output object. Also, notifies |
| 817 | // the processor that we're done. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 818 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
| 819 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 820 | ScopedActionCompleter completer(processor_, this); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 821 | string current_response(response_buffer_.begin(), response_buffer_.end()); |
| 822 | LOG(INFO) << "Omaha request response: " << current_response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 823 | |
| 824 | // Events are best effort transactions -- assume they always succeed. |
| 825 | if (IsEvent()) { |
| 826 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 827 | if (event_->result == OmahaEvent::kResultError && successful && |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 828 | system_state_->hardware()->IsOfficialBuild()) { |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 829 | LOG(INFO) << "Signalling Crash Reporter."; |
| 830 | utils::ScheduleCrashReporterUpload(); |
| 831 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 832 | completer.set_code(kErrorCodeSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 836 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 837 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 838 | int code = GetHTTPResponseCode(); |
| 839 | // Makes sure we send sane error values. |
| 840 | if (code < 0 || code >= 1000) { |
| 841 | code = 999; |
| 842 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 843 | completer.set_code(static_cast<ErrorCode>( |
| 844 | kErrorCodeOmahaRequestHTTPResponseBase + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 845 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 846 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 847 | |
| 848 | // parse our response and fill the fields in the output object |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 849 | scoped_ptr<xmlDoc, ScopedPtrXmlDocFree> doc( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 850 | xmlParseMemory(&response_buffer_[0], response_buffer_.size())); |
| 851 | if (!doc.get()) { |
| 852 | LOG(ERROR) << "Omaha response not valid XML"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 853 | completer.set_code(response_buffer_.empty() ? |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 854 | kErrorCodeOmahaRequestEmptyResponseError : |
| 855 | kErrorCodeOmahaRequestXMLParseError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 859 | // If a ping was sent, update the last ping day preferences based on |
| 860 | // the server daystart response. |
| 861 | if (ShouldPing(ping_active_days_) || |
| 862 | ShouldPing(ping_roll_call_days_) || |
| 863 | ping_active_days_ == kPingTimeJump || |
| 864 | ping_roll_call_days_ == kPingTimeJump) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 865 | LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), system_state_->prefs())) |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 866 | << "Failed to update the last ping day preferences!"; |
| 867 | } |
| 868 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 869 | if (!HasOutputPipe()) { |
| 870 | // Just set success to whether or not the http transfer succeeded, |
| 871 | // which must be true at this point in the code. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 872 | completer.set_code(kErrorCodeSuccess); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 876 | OmahaResponse output_object; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 877 | if (!ParseResponse(doc.get(), &output_object, &completer)) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 878 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 879 | output_object.update_exists = true; |
| 880 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 881 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 882 | if (params_->update_disabled()) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 883 | LOG(INFO) << "Ignoring Omaha updates as updates are disabled by policy."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 884 | output_object.update_exists = false; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 885 | completer.set_code(kErrorCodeOmahaUpdateIgnoredPerPolicy); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 886 | // Note: We could technically delete the UpdateFirstSeenAt state here. |
| 887 | // If we do, it'll mean a device has to restart the UpdateFirstSeenAt |
| 888 | // and thus help scattering take effect when the AU is turned on again. |
| 889 | // On the other hand, it also increases the chance of update starvation if |
| 890 | // an admin turns AU on/off more frequently. We choose to err on the side |
| 891 | // of preventing starvation at the cost of not applying scattering in |
| 892 | // those cases. |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 896 | // If Omaha says to disable p2p, respect that |
| 897 | if (output_object.disable_p2p_for_downloading) { |
| 898 | LOG(INFO) << "Forcibly disabling use of p2p for downloading as " |
| 899 | << "requested by Omaha."; |
| 900 | params_->set_use_p2p_for_downloading(false); |
| 901 | } |
| 902 | if (output_object.disable_p2p_for_sharing) { |
| 903 | LOG(INFO) << "Forcibly disabling use of p2p for sharing as " |
| 904 | << "requested by Omaha."; |
| 905 | params_->set_use_p2p_for_sharing(false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 906 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 907 | |
| 908 | // Update the payload state with the current response. The payload state |
| 909 | // will automatically reset all stale state if this response is different |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 910 | // from what's stored already. We are updating the payload state as late |
| 911 | // as possible in this method so that if a new release gets pushed and then |
| 912 | // got pulled back due to some issues, we don't want to clear our internal |
| 913 | // state unnecessarily. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 914 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 915 | payload_state->SetResponse(output_object); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 916 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 917 | // It could be we've already exceeded the deadline for when p2p is |
| 918 | // allowed or that we've tried too many times with p2p. Check that. |
| 919 | if (params_->use_p2p_for_downloading()) { |
| 920 | payload_state->P2PNewAttempt(); |
| 921 | if (!payload_state->P2PAttemptAllowed()) { |
| 922 | LOG(INFO) << "Forcibly disabling use of p2p for downloading because " |
| 923 | << "of previous failures when using p2p."; |
| 924 | params_->set_use_p2p_for_downloading(false); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // From here on, we'll complete stuff in CompleteProcessing() so |
| 929 | // disable |completer| since we'll create a new one in that |
| 930 | // function. |
| 931 | completer.set_should_complete(false); |
| 932 | |
| 933 | // If we're allowed to use p2p for downloading we do not pay |
| 934 | // attention to wall-clock-based waiting if the URL is indeed |
| 935 | // available via p2p. Therefore, check if the file is available via |
| 936 | // p2p before deferring... |
| 937 | if (params_->use_p2p_for_downloading()) { |
| 938 | LookupPayloadViaP2P(output_object); |
| 939 | } else { |
| 940 | CompleteProcessing(); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | void OmahaRequestAction::CompleteProcessing() { |
| 945 | ScopedActionCompleter completer(processor_, this); |
| 946 | OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject()); |
| 947 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 948 | |
| 949 | if (ShouldDeferDownload(&output_object)) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 950 | output_object.update_exists = false; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 951 | LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy."; |
| 952 | completer.set_code(kErrorCodeOmahaUpdateDeferredPerPolicy); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 953 | return; |
| 954 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 955 | |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 956 | if (payload_state->ShouldBackoffDownload()) { |
| 957 | output_object.update_exists = false; |
| 958 | LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry " |
| 959 | << "attempts"; |
| 960 | completer.set_code(kErrorCodeOmahaUpdateDeferredForBackoff); |
| 961 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 962 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 963 | completer.set_code(kErrorCodeSuccess); |
| 964 | } |
| 965 | |
| 966 | void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) { |
| 967 | LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'"; |
| 968 | if (!url.empty()) { |
| 969 | params_->set_p2p_url(url); |
| 970 | } else { |
| 971 | LOG(INFO) << "Forcibly disabling use of p2p for downloading " |
| 972 | << "because no suitable peer could be found."; |
| 973 | params_->set_use_p2p_for_downloading(false); |
| 974 | } |
| 975 | CompleteProcessing(); |
| 976 | } |
| 977 | |
| 978 | void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) { |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 979 | // If the device is in the middle of an update, the state variables |
| 980 | // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength |
| 981 | // tracks the offset and length of the operation currently in |
| 982 | // progress. The offset is based from the end of the manifest which |
| 983 | // is kPrefsManifestMetadataSize bytes long. |
| 984 | // |
| 985 | // To make forward progress and avoid deadlocks, we need to find a |
| 986 | // peer that has at least the entire operation we're currently |
| 987 | // working on. Otherwise we may end up in a situation where two |
| 988 | // devices bounce back and forth downloading from each other, |
| 989 | // neither making any forward progress until one of them decides to |
| 990 | // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds |
| 991 | // safe-guards). See http://crbug.com/297170 for an example) |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 992 | size_t minimum_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 993 | int64_t manifest_metadata_size = 0; |
| 994 | int64_t next_data_offset = 0; |
| 995 | int64_t next_data_length = 0; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 996 | if (system_state_ != NULL && |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 997 | system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize, |
| 998 | &manifest_metadata_size) && |
| 999 | manifest_metadata_size != -1 && |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1000 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset, |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1001 | &next_data_offset) && |
| 1002 | next_data_offset != -1 && |
| 1003 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength, |
| 1004 | &next_data_length)) { |
| 1005 | minimum_size = manifest_metadata_size + next_data_offset + next_data_length; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | string file_id = utils::CalculateP2PFileId(response.hash, response.size); |
| 1009 | if (system_state_->p2p_manager() != NULL) { |
| 1010 | LOG(INFO) << "Checking if payload is available via p2p, file_id=" |
| 1011 | << file_id << " minimum_size=" << minimum_size; |
| 1012 | system_state_->p2p_manager()->LookupUrlForFile( |
| 1013 | file_id, |
| 1014 | minimum_size, |
David Zeuthen | 4cc5ed2 | 2014-01-15 12:35:03 -0800 | [diff] [blame] | 1015 | TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds), |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1016 | base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted, |
| 1017 | base::Unretained(this))); |
| 1018 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1021 | bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1022 | if (params_->interactive()) { |
| 1023 | LOG(INFO) << "Not deferring download because update is interactive."; |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1027 | // If we're using p2p to download _and_ we have a p2p URL, we never |
| 1028 | // defer the download. This is because the download will always |
| 1029 | // happen from a peer on the LAN and we've been waiting in line for |
| 1030 | // our turn. |
| 1031 | if (params_->use_p2p_for_downloading() && !params_->p2p_url().empty()) { |
| 1032 | LOG(INFO) << "Download not deferred because download " |
| 1033 | << "will happen from a local peer (via p2p)."; |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1037 | // We should defer the downloads only if we've first satisfied the |
| 1038 | // wall-clock-based-waiting period and then the update-check-based waiting |
| 1039 | // period, if required. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1040 | if (!params_->wall_clock_based_wait_enabled()) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1041 | LOG(INFO) << "Wall-clock-based waiting period is not enabled," |
| 1042 | << " so no deferring needed."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
| 1045 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1046 | switch (IsWallClockBasedWaitingSatisfied(output_object)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1047 | case kWallClockWaitNotSatisfied: |
| 1048 | // We haven't even satisfied the first condition, passing the |
| 1049 | // wall-clock-based waiting period, so we should defer the downloads |
| 1050 | // until that happens. |
| 1051 | LOG(INFO) << "wall-clock-based-wait not satisfied."; |
| 1052 | return true; |
| 1053 | |
| 1054 | case kWallClockWaitDoneButUpdateCheckWaitRequired: |
| 1055 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1056 | << "update-check-based-wait required."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1057 | return !IsUpdateCheckCountBasedWaitingSatisfied(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1058 | |
| 1059 | case kWallClockWaitDoneAndUpdateCheckWaitNotRequired: |
| 1060 | // Wall-clock-based waiting period is satisfied, and it's determined |
| 1061 | // that we do not need the update-check-based wait. so no need to |
| 1062 | // defer downloads. |
| 1063 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1064 | << "update-check-based-wait is not required."; |
| 1065 | return false; |
| 1066 | |
| 1067 | default: |
| 1068 | // Returning false for this default case so we err on the |
| 1069 | // side of downloading updates than deferring in case of any bugs. |
| 1070 | NOTREACHED(); |
| 1071 | return false; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | OmahaRequestAction::WallClockWaitResult |
| 1076 | OmahaRequestAction::IsWallClockBasedWaitingSatisfied( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1077 | OmahaResponse* output_object) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1078 | Time update_first_seen_at; |
| 1079 | int64 update_first_seen_at_int; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1080 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1081 | if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) { |
| 1082 | if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt, |
| 1083 | &update_first_seen_at_int)) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1084 | // Note: This timestamp could be that of ANY update we saw in the past |
| 1085 | // (not necessarily this particular update we're considering to apply) |
| 1086 | // but never got to apply because of some reason (e.g. stop AU policy, |
| 1087 | // updates being pulled out from Omaha, changes in target version prefix, |
| 1088 | // new update being rolled out, etc.). But for the purposes of scattering |
| 1089 | // it doesn't matter which update the timestamp corresponds to. i.e. |
| 1090 | // the clock starts ticking the first time we see an update and we're |
| 1091 | // ready to apply when the random wait period is satisfied relative to |
| 1092 | // that first seen timestamp. |
| 1093 | update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int); |
| 1094 | LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: " |
| 1095 | << utils::ToString(update_first_seen_at); |
| 1096 | } else { |
| 1097 | // This seems like an unexpected error where the persisted value exists |
| 1098 | // but it's not readable for some reason. Just skip scattering in this |
| 1099 | // case to be safe. |
| 1100 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read"; |
| 1101 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1102 | } |
| 1103 | } else { |
| 1104 | update_first_seen_at = Time::Now(); |
| 1105 | update_first_seen_at_int = update_first_seen_at.ToInternalValue(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1106 | if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt, |
| 1107 | update_first_seen_at_int)) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1108 | LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: " |
| 1109 | << utils::ToString(update_first_seen_at); |
| 1110 | } |
| 1111 | else { |
| 1112 | // This seems like an unexpected error where the value cannot be |
| 1113 | // persisted for some reason. Just skip scattering in this |
| 1114 | // case to be safe. |
| 1115 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value " |
| 1116 | << utils::ToString(update_first_seen_at) |
| 1117 | << " cannot be persisted"; |
| 1118 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1119 | } |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1122 | TimeDelta elapsed_time = Time::Now() - update_first_seen_at; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1123 | TimeDelta max_scatter_period = TimeDelta::FromDays( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1124 | output_object->max_days_to_scatter); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1125 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1126 | LOG(INFO) << "Waiting Period = " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1127 | << utils::FormatSecs(params_->waiting_period().InSeconds()) |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1128 | << ", Time Elapsed = " |
| 1129 | << utils::FormatSecs(elapsed_time.InSeconds()) |
| 1130 | << ", MaxDaysToScatter = " |
| 1131 | << max_scatter_period.InDays(); |
| 1132 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1133 | if (!output_object->deadline.empty()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1134 | // The deadline is set for all rules which serve a delta update from a |
| 1135 | // previous FSI, which means this update will be applied mostly in OOBE |
| 1136 | // cases. For these cases, we shouldn't scatter so as to finish the OOBE |
| 1137 | // quickly. |
| 1138 | LOG(INFO) << "Not scattering as deadline flag is set"; |
| 1139 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1140 | } |
| 1141 | |
| 1142 | if (max_scatter_period.InDays() == 0) { |
| 1143 | // This means the Omaha rule creator decides that this rule |
| 1144 | // should not be scattered irrespective of the policy. |
| 1145 | LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0."; |
| 1146 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1147 | } |
| 1148 | |
| 1149 | if (elapsed_time > max_scatter_period) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1150 | // This means we've waited more than the upperbound wait in the rule |
| 1151 | // from the time we first saw a valid update available to us. |
| 1152 | // This will prevent update starvation. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1153 | LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit."; |
| 1154 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1155 | } |
| 1156 | |
| 1157 | // This means we are required to participate in scattering. |
| 1158 | // See if our turn has arrived now. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1159 | TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1160 | if (remaining_wait_time.InSeconds() <= 0) { |
| 1161 | // Yes, it's our turn now. |
| 1162 | LOG(INFO) << "Successfully passed the wall-clock-based-wait."; |
| 1163 | |
| 1164 | // But we can't download until the update-check-count-based wait is also |
| 1165 | // satisfied, so mark it as required now if update checks are enabled. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1166 | return params_->update_check_count_wait_enabled() ? |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1167 | kWallClockWaitDoneButUpdateCheckWaitRequired : |
| 1168 | kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1169 | } |
| 1170 | |
| 1171 | // Not our turn yet, so we have to wait until our turn to |
| 1172 | // help scatter the downloads across all clients of the enterprise. |
| 1173 | LOG(INFO) << "Update deferred for another " |
| 1174 | << utils::FormatSecs(remaining_wait_time.InSeconds()) |
| 1175 | << " per policy."; |
| 1176 | return kWallClockWaitNotSatisfied; |
| 1177 | } |
| 1178 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1179 | bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1180 | int64 update_check_count_value; |
| 1181 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1182 | if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) { |
| 1183 | if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount, |
| 1184 | &update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1185 | // We are unable to read the update check count from file for some reason. |
| 1186 | // So let's proceed anyway so as to not stall the update. |
| 1187 | LOG(ERROR) << "Unable to read update check count. " |
| 1188 | << "Skipping update-check-count-based-wait."; |
| 1189 | return true; |
| 1190 | } |
| 1191 | } else { |
| 1192 | // This file does not exist. This means we haven't started our update |
| 1193 | // check count down yet, so this is the right time to start the count down. |
| 1194 | update_check_count_value = base::RandInt( |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1195 | params_->min_update_checks_needed(), |
| 1196 | params_->max_update_checks_allowed()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1197 | |
| 1198 | LOG(INFO) << "Randomly picked update check count value = " |
| 1199 | << update_check_count_value; |
| 1200 | |
| 1201 | // Write out the initial value of update_check_count_value. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1202 | if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount, |
| 1203 | update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1204 | // We weren't able to write the update check count file for some reason. |
| 1205 | // So let's proceed anyway so as to not stall the update. |
| 1206 | LOG(ERROR) << "Unable to write update check count. " |
| 1207 | << "Skipping update-check-count-based-wait."; |
| 1208 | return true; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | if (update_check_count_value == 0) { |
| 1213 | LOG(INFO) << "Successfully passed the update-check-based-wait."; |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
| 1217 | if (update_check_count_value < 0 || |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1218 | update_check_count_value > params_->max_update_checks_allowed()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1219 | // We err on the side of skipping scattering logic instead of stalling |
| 1220 | // a machine from receiving any updates in case of any unexpected state. |
| 1221 | LOG(ERROR) << "Invalid value for update check count detected. " |
| 1222 | << "Skipping update-check-count-based-wait."; |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | // Legal value, we need to wait for more update checks to happen |
| 1227 | // until this becomes 0. |
| 1228 | LOG(INFO) << "Deferring Omaha updates for another " |
| 1229 | << update_check_count_value |
| 1230 | << " update checks per policy"; |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1234 | // static |
| 1235 | bool OmahaRequestAction::ParseInstallDate(xmlDoc* doc, |
| 1236 | OmahaResponse* output_object) { |
Ben Chan | 075248b | 2014-04-24 12:04:14 -0700 | [diff] [blame^] | 1237 | scoped_ptr<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1238 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr("/response/daystart"))); |
| 1239 | |
| 1240 | if (xpath_nodeset.get() == NULL) |
| 1241 | return false; |
| 1242 | |
| 1243 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 1244 | if (nodeset == NULL || nodeset->nodeNr < 1) |
| 1245 | return false; |
| 1246 | |
| 1247 | xmlNode* daystart_node = nodeset->nodeTab[0]; |
| 1248 | if (!xmlHasProp(daystart_node, ConstXMLStr("elapsed_days"))) |
| 1249 | return false; |
| 1250 | |
| 1251 | int64_t elapsed_days = 0; |
| 1252 | if (!base::StringToInt64(XmlGetProperty(daystart_node, "elapsed_days"), |
| 1253 | &elapsed_days)) |
| 1254 | return false; |
| 1255 | |
| 1256 | if (elapsed_days < 0) |
| 1257 | return false; |
| 1258 | |
| 1259 | output_object->install_date_days = elapsed_days; |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | // static |
| 1264 | bool OmahaRequestAction::HasInstallDate(SystemState *system_state) { |
| 1265 | PrefsInterface* prefs = system_state->prefs(); |
| 1266 | if (prefs == NULL) |
| 1267 | return false; |
| 1268 | |
| 1269 | return prefs->Exists(kPrefsInstallDateDays); |
| 1270 | } |
| 1271 | |
| 1272 | // static |
| 1273 | bool OmahaRequestAction::PersistInstallDate( |
| 1274 | SystemState *system_state, |
| 1275 | int install_date_days, |
| 1276 | InstallDateProvisioningSource source) { |
| 1277 | TEST_AND_RETURN_FALSE(install_date_days >= 0); |
| 1278 | |
| 1279 | PrefsInterface* prefs = system_state->prefs(); |
| 1280 | if (prefs == NULL) |
| 1281 | return false; |
| 1282 | |
| 1283 | if (!prefs->SetInt64(kPrefsInstallDateDays, install_date_days)) |
| 1284 | return false; |
| 1285 | |
| 1286 | string metric_name = "Installer.InstallDateProvisioningSource"; |
| 1287 | system_state->metrics_lib()->SendEnumToUMA( |
| 1288 | metric_name, |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1289 | static_cast<int>(source), // Sample. |
| 1290 | kProvisionedMax); // Maximum. |
| 1291 | |
| 1292 | metric_name = metrics::kMetricInstallDateProvisioningSource; |
| 1293 | system_state->metrics_lib()->SendEnumToUMA( |
| 1294 | metric_name, |
| 1295 | static_cast<int>(source), // Sample. |
| 1296 | kProvisionedMax); // Maximum. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1297 | |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1301 | void OmahaRequestAction::ActionCompleted(ErrorCode code) { |
| 1302 | // We only want to report this on "update check". |
| 1303 | if (ping_only_ || event_ != nullptr) |
| 1304 | return; |
| 1305 | |
| 1306 | metrics::CheckResult result = metrics::CheckResult::kUnset; |
| 1307 | metrics::CheckReaction reaction = metrics::CheckReaction::kUnset; |
| 1308 | metrics::DownloadErrorCode download_error_code = |
| 1309 | metrics::DownloadErrorCode::kUnset; |
| 1310 | |
| 1311 | // Regular update attempt. |
| 1312 | switch (code) { |
| 1313 | case kErrorCodeSuccess: |
| 1314 | // OK, we parsed the response successfully but that does |
| 1315 | // necessarily mean that an update is available. |
| 1316 | if (HasOutputPipe()) { |
| 1317 | const OmahaResponse& response = GetOutputObject(); |
| 1318 | if (response.update_exists) { |
| 1319 | result = metrics::CheckResult::kUpdateAvailable; |
| 1320 | reaction = metrics::CheckReaction::kUpdating; |
| 1321 | } else { |
| 1322 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1323 | } |
| 1324 | } else { |
| 1325 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1326 | } |
| 1327 | break; |
| 1328 | |
| 1329 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 1330 | result = metrics::CheckResult::kUpdateAvailable; |
| 1331 | reaction = metrics::CheckReaction::kIgnored; |
| 1332 | break; |
| 1333 | |
| 1334 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 1335 | result = metrics::CheckResult::kUpdateAvailable; |
| 1336 | reaction = metrics::CheckReaction::kDeferring; |
| 1337 | break; |
| 1338 | |
| 1339 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 1340 | result = metrics::CheckResult::kUpdateAvailable; |
| 1341 | reaction = metrics::CheckReaction::kBackingOff; |
| 1342 | break; |
| 1343 | |
| 1344 | default: |
| 1345 | // We report two flavors of errors, "Download errors" and "Parsing |
| 1346 | // error". Try to convert to the former and if that doesn't work |
| 1347 | // we know it's the latter. |
| 1348 | metrics::DownloadErrorCode tmp_error = utils::GetDownloadErrorCode(code); |
| 1349 | if (tmp_error != metrics::DownloadErrorCode::kInputMalformed) { |
| 1350 | result = metrics::CheckResult::kDownloadError; |
| 1351 | download_error_code = tmp_error; |
| 1352 | } else { |
| 1353 | result = metrics::CheckResult::kParsingError; |
| 1354 | } |
| 1355 | break; |
| 1356 | } |
| 1357 | |
| 1358 | metrics::ReportUpdateCheckMetrics(system_state_, |
| 1359 | result, reaction, download_error_code); |
| 1360 | } |
| 1361 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1362 | } // namespace chromeos_update_engine |