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