blob: 8a5a5d55b71422bd2a4010d33e78a7b90d6cac35 [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>
13
Chris Masoned903c3b2011-05-12 15:35:46 -070014#include <base/memory/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <curl/curl.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070016#include <libxml/parser.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/action.h"
19#include "update_engine/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000020
Darin Petkov6a5b3222010-07-13 14:55:28 -070021// The Omaha Request action makes a request to Omaha and can output
22// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
rspangler@google.com49fdf182009-10-10 00:57:34 +000024namespace chromeos_update_engine {
25
26// Encodes XML entities in a given string with libxml2. input must be
27// UTF-8 formatted. Output will be UTF-8 formatted.
28std::string XmlEncode(const std::string& input);
29
Darin Petkov6a5b3222010-07-13 14:55:28 -070030// This struct encapsulates the data Omaha's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000031// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070032struct OmahaResponse {
33 OmahaResponse()
Darin Petkov85ced132010-09-01 10:20:56 -070034 : update_exists(false),
35 poll_interval(0),
36 size(0),
Jay Srinivasanf4318702012-09-24 11:56:24 -070037 metadata_size(0),
Jay Srinivasan23b92a52012-10-27 02:00:21 -070038 max_days_to_scatter(0),
Darin Petkov85ced132010-09-01 10:20:56 -070039 needs_admin(false),
40 prompt(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000041 // True iff there is an update to be downloaded.
42 bool update_exists;
43
Darin Petkov85ced132010-09-01 10:20:56 -070044 // If non-zero, server-dictated poll frequency in seconds.
45 int poll_interval;
46
rspangler@google.com49fdf182009-10-10 00:57:34 +000047 // These are only valid if update_exists is true:
48 std::string display_version;
49 std::string codebase;
50 std::string more_info_url;
51 std::string hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070052 std::string metadata_signature;
Darin Petkov6c118642010-10-21 12:06:30 -070053 std::string deadline;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 off_t size;
Jay Srinivasanf4318702012-09-24 11:56:24 -070055 off_t metadata_size;
Jay Srinivasan23b92a52012-10-27 02:00:21 -070056 int max_days_to_scatter;
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 bool needs_admin;
58 bool prompt;
59};
60COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
61
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070062// This struct encapsulates the Omaha event information. For a
63// complete list of defined event types and results, see
64// http://code.google.com/p/omaha/wiki/ServerProtocol#event
65struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070066 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070067 enum Type {
68 kTypeUnknown = 0,
69 kTypeDownloadComplete = 1,
70 kTypeInstallComplete = 2,
71 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070072 kTypeUpdateDownloadStarted = 13,
73 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070074 };
75
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070076 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070077 enum Result {
78 kResultError = 0,
79 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080080 kResultSuccessReboot = 2,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070081 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 };
83
84 OmahaEvent()
85 : type(kTypeUnknown),
86 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070087 error_code(kActionCodeError) {}
88 explicit OmahaEvent(Type in_type)
89 : type(in_type),
90 result(kResultSuccess),
91 error_code(kActionCodeSuccess) {}
92 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070093 : type(in_type),
94 result(in_result),
95 error_code(in_error_code) {}
96
97 Type type;
98 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -070099 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700100};
101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700103class OmahaRequestAction;
104struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700105class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106
107template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700108class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700110 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700111 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700112 // On UpdateCheck success, puts the Omaha response on output. Event
113 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700114 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115};
116
Darin Petkov6a5b3222010-07-13 14:55:28 -0700117class OmahaRequestAction : public Action<OmahaRequestAction>,
118 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700120 static const int kNeverPinged = -1;
121 static const int kPingTimeJump = -2;
122
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700123 // These are the possible outcome upon checking whether we satisfied
124 // the wall-clock-based-wait.
125 enum WallClockWaitResult {
126 kWallClockWaitNotSatisfied,
127 kWallClockWaitDoneButUpdateCheckWaitRequired,
128 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
129 };
130
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700131 // The ctor takes in all the parameters that will be used for making
132 // the request to Omaha. For some of them we have constants that
133 // should be used.
134 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700136 //
137 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
138 // this is an UpdateCheck request, otherwise it's an Event request.
139 // Event requests always succeed.
140 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700142 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700143 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700144 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700145 OmahaRequestAction(PrefsInterface* prefs,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700146 OmahaRequestParams* params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700147 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700148 HttpFetcher* http_fetcher,
149 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700150 virtual ~OmahaRequestAction();
151 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
152 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000153 void PerformAction();
154 void TerminateProcessing();
155
Darin Petkov1023a602010-08-30 13:47:51 -0700156 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
157
rspangler@google.com49fdf182009-10-10 00:57:34 +0000158 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700159 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000160 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000161
162 // Delegate methods (see http_fetcher.h)
163 virtual void ReceivedBytes(HttpFetcher *fetcher,
164 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000165
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700166 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700167 // Returns true if this is an Event request, false if it's an UpdateCheck.
168 bool IsEvent() const { return event_.get() != NULL; }
169
rspangler@google.com49fdf182009-10-10 00:57:34 +0000170 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700171 // If this is an update check request, initializes
172 // |ping_active_days_| and |ping_roll_call_days_| to values that may
173 // be sent as pings to Omaha.
174 void InitPingDays();
175
Darin Petkov84c763c2010-07-29 16:27:58 -0700176 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700177 // number of days since the last ping sent for |key|.
178 int CalculatePingDays(const std::string& key);
179
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700180 // Returns true if the download of a new update should be deferred.
181 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700182 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700183
184 // Returns true if the basic wall-clock-based waiting period has been
185 // satisfied based on the scattering policy setting. False otherwise.
186 // If true, it also indicates whether the additional update-check-count-based
187 // waiting period also needs to be satisfied before the download can begin.
188 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700189 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700190
191 // Returns true if the update-check-count-based waiting period has been
192 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700193 bool IsUpdateCheckCountBasedWaitingSatisfied();
194
195 // Parses the response from Omaha that's available in |doc| using the other
196 // helper methods below and populates the |output_object| with the relevant
197 // values. Returns true if we should continue the parsing. False otherwise,
198 // in which case it sets any error code using |completer|.
199 bool ParseResponse(xmlDoc* doc,
200 OmahaResponse* output_object,
201 ScopedActionCompleter* completer);
202
203 // Parses the status property in the given update_check_node and populates
204 // |output_object| if valid. Returns true if we should continue the parsing.
205 // False otherwise, in which case it sets any error code using |completer|.
206 bool ParseStatus(xmlNode* update_check_node,
207 OmahaResponse* output_object,
208 ScopedActionCompleter* completer);
209
210 // Parses the URL nodes in the given XML document and populates
211 // |output_object| if valid. Returns true if we should continue the parsing.
212 // False otherwise, in which case it sets any error code using |completer|.
213 bool ParseUrls(xmlDoc* doc,
214 OmahaResponse* output_object,
215 ScopedActionCompleter* completer);
216
217 // Parses the package node in the given XML document and populates
218 // |output_object| if valid. Returns true if we should continue the parsing.
219 // False otherwise, in which case it sets any error code using |completer|.
220 bool ParsePackage(xmlDoc* doc,
221 OmahaResponse* output_object,
222 ScopedActionCompleter* completer);
223
224 // Parses the other parameters in the given XML document and populates
225 // |output_object| if valid. Returns true if we should continue the parsing.
226 // False otherwise, in which case it sets any error code using |completer|.
227 bool ParseParams(xmlDoc* doc,
228 OmahaResponse* output_object,
229 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700230
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700231 // Access to the preferences store.
232 PrefsInterface* prefs_;
233
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700234 // Contains state that is relevant in the processing of the Omaha request.
235 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000236
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700237 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
238 scoped_ptr<OmahaEvent> event_;
239
rspangler@google.com49fdf182009-10-10 00:57:34 +0000240 // pointer to the HttpFetcher that does the http work
241 scoped_ptr<HttpFetcher> http_fetcher_;
242
Thieu Le116fda32011-04-19 11:01:54 -0700243 // If true, only include the <ping> element in the request.
244 bool ping_only_;
245
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246 // Stores the response from the omaha server
247 std::vector<char> response_buffer_;
248
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700249 // Initialized by InitPingDays to values that may be sent to Omaha
250 // as part of a ping message. Note that only positive values and -1
251 // are sent to Omaha.
252 int ping_active_days_;
253 int ping_roll_call_days_;
254
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256};
257
258} // namespace chromeos_update_engine
259
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__