blob: ebd3ed0be5cfdf3566d41211d66cf0f72e35e723 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov6a5b3222010-07-13 14:55:28 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07009#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include <fcntl.h>
11
12#include <string>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080013#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014
Chris Masoned903c3b2011-05-12 15:35:46 -070015#include <base/memory/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000016#include <curl/curl.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070017#include <libxml/parser.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include "update_engine/action.h"
20#include "update_engine/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000021
Darin Petkov6a5b3222010-07-13 14:55:28 -070022// The Omaha Request action makes a request to Omaha and can output
23// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
rspangler@google.com49fdf182009-10-10 00:57:34 +000025namespace chromeos_update_engine {
26
27// Encodes XML entities in a given string with libxml2. input must be
28// UTF-8 formatted. Output will be UTF-8 formatted.
29std::string XmlEncode(const std::string& input);
30
Darin Petkov6a5b3222010-07-13 14:55:28 -070031// This struct encapsulates the data Omaha's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000032// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070033struct OmahaResponse {
34 OmahaResponse()
Darin Petkov85ced132010-09-01 10:20:56 -070035 : update_exists(false),
36 poll_interval(0),
37 size(0),
Jay Srinivasanf4318702012-09-24 11:56:24 -070038 metadata_size(0),
Jay Srinivasan23b92a52012-10-27 02:00:21 -070039 max_days_to_scatter(0),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080040 max_failure_count_per_url(0),
Darin Petkov85ced132010-09-01 10:20:56 -070041 needs_admin(false),
42 prompt(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000043 // True iff there is an update to be downloaded.
44 bool update_exists;
45
Darin Petkov85ced132010-09-01 10:20:56 -070046 // If non-zero, server-dictated poll frequency in seconds.
47 int poll_interval;
48
rspangler@google.com49fdf182009-10-10 00:57:34 +000049 // These are only valid if update_exists is true:
50 std::string display_version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080051
52 // The ordered list of URLs in the Omaha response. Each item is a complete
53 // URL (i.e. in terms of Omaha XML, each value is a urlBase + packageName)
54 std::vector<std::string> payload_urls;
55
rspangler@google.com49fdf182009-10-10 00:57:34 +000056 std::string more_info_url;
57 std::string hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070058 std::string metadata_signature;
Darin Petkov6c118642010-10-21 12:06:30 -070059 std::string deadline;
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 off_t size;
Jay Srinivasanf4318702012-09-24 11:56:24 -070061 off_t metadata_size;
Jay Srinivasan23b92a52012-10-27 02:00:21 -070062 int max_days_to_scatter;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080063 // The number of URL-related failures to tolerate before moving on to the
64 // next URL in the current pass. This is a configurable value from the
65 // Omaha Response attribute, if ever we need to fine tune the behavior.
66 int max_failure_count_per_url;
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 bool needs_admin;
68 bool prompt;
69};
70COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
71
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070072// This struct encapsulates the Omaha event information. For a
73// complete list of defined event types and results, see
74// http://code.google.com/p/omaha/wiki/ServerProtocol#event
75struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070076 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070077 enum Type {
78 kTypeUnknown = 0,
79 kTypeDownloadComplete = 1,
80 kTypeInstallComplete = 2,
81 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070082 kTypeUpdateDownloadStarted = 13,
83 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070084 };
85
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070086 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070087 enum Result {
88 kResultError = 0,
89 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080090 kResultSuccessReboot = 2,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070091 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070092 };
93
94 OmahaEvent()
95 : type(kTypeUnknown),
96 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070097 error_code(kActionCodeError) {}
98 explicit OmahaEvent(Type in_type)
99 : type(in_type),
100 result(kResultSuccess),
101 error_code(kActionCodeSuccess) {}
102 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700103 : type(in_type),
104 result(in_result),
105 error_code(in_error_code) {}
106
107 Type type;
108 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -0700109 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700110};
111
rspangler@google.com49fdf182009-10-10 00:57:34 +0000112class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700113class OmahaRequestAction;
114struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700115class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116
117template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700118class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700120 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700121 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700122 // On UpdateCheck success, puts the Omaha response on output. Event
123 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700124 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125};
126
Darin Petkov6a5b3222010-07-13 14:55:28 -0700127class OmahaRequestAction : public Action<OmahaRequestAction>,
128 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000129 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700130 static const int kNeverPinged = -1;
131 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800132 // We choose this value of 10 as a heuristic for a work day in trying
133 // each URL, assuming we check roughly every 45 mins. This is a good time to
134 // wait - neither too long nor too little - so we don't give up the preferred
135 // URLs that appear earlier in list too quickly before moving on to the
136 // fallback ones.
137 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700138
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700139 // These are the possible outcome upon checking whether we satisfied
140 // the wall-clock-based-wait.
141 enum WallClockWaitResult {
142 kWallClockWaitNotSatisfied,
143 kWallClockWaitDoneButUpdateCheckWaitRequired,
144 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
145 };
146
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700147 // The ctor takes in all the parameters that will be used for making
148 // the request to Omaha. For some of them we have constants that
149 // should be used.
150 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700152 //
153 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
154 // this is an UpdateCheck request, otherwise it's an Event request.
155 // Event requests always succeed.
156 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700158 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700159 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700160 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800161 OmahaRequestAction(SystemState* system_state,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700162 OmahaRequestParams* params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700163 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700164 HttpFetcher* http_fetcher,
165 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700166 virtual ~OmahaRequestAction();
167 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
168 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 void PerformAction();
170 void TerminateProcessing();
171
Darin Petkov1023a602010-08-30 13:47:51 -0700172 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
173
rspangler@google.com49fdf182009-10-10 00:57:34 +0000174 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700175 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000176 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000177
178 // Delegate methods (see http_fetcher.h)
179 virtual void ReceivedBytes(HttpFetcher *fetcher,
180 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700182 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700183 // Returns true if this is an Event request, false if it's an UpdateCheck.
184 bool IsEvent() const { return event_.get() != NULL; }
185
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700187 // If this is an update check request, initializes
188 // |ping_active_days_| and |ping_roll_call_days_| to values that may
189 // be sent as pings to Omaha.
190 void InitPingDays();
191
Darin Petkov84c763c2010-07-29 16:27:58 -0700192 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700193 // number of days since the last ping sent for |key|.
194 int CalculatePingDays(const std::string& key);
195
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700196 // Returns true if the download of a new update should be deferred.
197 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700198 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700199
200 // Returns true if the basic wall-clock-based waiting period has been
201 // satisfied based on the scattering policy setting. False otherwise.
202 // If true, it also indicates whether the additional update-check-count-based
203 // waiting period also needs to be satisfied before the download can begin.
204 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700205 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700206
207 // Returns true if the update-check-count-based waiting period has been
208 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700209 bool IsUpdateCheckCountBasedWaitingSatisfied();
210
211 // Parses the response from Omaha that's available in |doc| using the other
212 // helper methods below and populates the |output_object| with the relevant
213 // values. Returns true if we should continue the parsing. False otherwise,
214 // in which case it sets any error code using |completer|.
215 bool ParseResponse(xmlDoc* doc,
216 OmahaResponse* output_object,
217 ScopedActionCompleter* completer);
218
219 // Parses the status property in the given update_check_node and populates
220 // |output_object| if valid. Returns true if we should continue the parsing.
221 // False otherwise, in which case it sets any error code using |completer|.
222 bool ParseStatus(xmlNode* update_check_node,
223 OmahaResponse* output_object,
224 ScopedActionCompleter* completer);
225
226 // Parses the URL nodes in the given XML document and populates
227 // |output_object| if valid. Returns true if we should continue the parsing.
228 // False otherwise, in which case it sets any error code using |completer|.
229 bool ParseUrls(xmlDoc* doc,
230 OmahaResponse* output_object,
231 ScopedActionCompleter* completer);
232
233 // Parses the package node in the given XML document and populates
234 // |output_object| if valid. Returns true if we should continue the parsing.
235 // False otherwise, in which case it sets any error code using |completer|.
236 bool ParsePackage(xmlDoc* doc,
237 OmahaResponse* output_object,
238 ScopedActionCompleter* completer);
239
240 // Parses the other parameters in the given XML document and populates
241 // |output_object| if valid. Returns true if we should continue the parsing.
242 // False otherwise, in which case it sets any error code using |completer|.
243 bool ParseParams(xmlDoc* doc,
244 OmahaResponse* output_object,
245 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700246
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247 // Global system context.
248 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700249
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700250 // Contains state that is relevant in the processing of the Omaha request.
251 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700253 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
254 scoped_ptr<OmahaEvent> event_;
255
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256 // pointer to the HttpFetcher that does the http work
257 scoped_ptr<HttpFetcher> http_fetcher_;
258
Thieu Le116fda32011-04-19 11:01:54 -0700259 // If true, only include the <ping> element in the request.
260 bool ping_only_;
261
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262 // Stores the response from the omaha server
263 std::vector<char> response_buffer_;
264
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700265 // Initialized by InitPingDays to values that may be sent to Omaha
266 // as part of a ping message. Note that only positive values and -1
267 // are sent to Omaha.
268 int ping_active_days_;
269 int ping_roll_call_days_;
270
Darin Petkov6a5b3222010-07-13 14:55:28 -0700271 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000272};
273
274} // namespace chromeos_update_engine
275
Darin Petkov6a5b3222010-07-13 14:55:28 -0700276#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__