blob: bc9b4fefa6db30227b5e096523e745d489a585ee [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),
Darin Petkov85ced132010-09-01 10:20:56 -070040 needs_admin(false),
41 prompt(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000042 // True iff there is an update to be downloaded.
43 bool update_exists;
44
Darin Petkov85ced132010-09-01 10:20:56 -070045 // If non-zero, server-dictated poll frequency in seconds.
46 int poll_interval;
47
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 // These are only valid if update_exists is true:
49 std::string display_version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050
51 // The ordered list of URLs in the Omaha response. Each item is a complete
52 // URL (i.e. in terms of Omaha XML, each value is a urlBase + packageName)
53 std::vector<std::string> payload_urls;
54
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 std::string more_info_url;
56 std::string hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070057 std::string metadata_signature;
Darin Petkov6c118642010-10-21 12:06:30 -070058 std::string deadline;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 off_t size;
Jay Srinivasanf4318702012-09-24 11:56:24 -070060 off_t metadata_size;
Jay Srinivasan23b92a52012-10-27 02:00:21 -070061 int max_days_to_scatter;
rspangler@google.com49fdf182009-10-10 00:57:34 +000062 bool needs_admin;
63 bool prompt;
64};
65COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
66
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070067// This struct encapsulates the Omaha event information. For a
68// complete list of defined event types and results, see
69// http://code.google.com/p/omaha/wiki/ServerProtocol#event
70struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070071 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070072 enum Type {
73 kTypeUnknown = 0,
74 kTypeDownloadComplete = 1,
75 kTypeInstallComplete = 2,
76 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070077 kTypeUpdateDownloadStarted = 13,
78 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070079 };
80
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070081 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 enum Result {
83 kResultError = 0,
84 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080085 kResultSuccessReboot = 2,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070086 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070087 };
88
89 OmahaEvent()
90 : type(kTypeUnknown),
91 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070092 error_code(kActionCodeError) {}
93 explicit OmahaEvent(Type in_type)
94 : type(in_type),
95 result(kResultSuccess),
96 error_code(kActionCodeSuccess) {}
97 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070098 : type(in_type),
99 result(in_result),
100 error_code(in_error_code) {}
101
102 Type type;
103 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -0700104 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700105};
106
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700108class OmahaRequestAction;
109struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700110class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111
112template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700113class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700115 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700116 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700117 // On UpdateCheck success, puts the Omaha response on output. Event
118 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700119 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120};
121
Darin Petkov6a5b3222010-07-13 14:55:28 -0700122class OmahaRequestAction : public Action<OmahaRequestAction>,
123 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700125 static const int kNeverPinged = -1;
126 static const int kPingTimeJump = -2;
127
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700128 // These are the possible outcome upon checking whether we satisfied
129 // the wall-clock-based-wait.
130 enum WallClockWaitResult {
131 kWallClockWaitNotSatisfied,
132 kWallClockWaitDoneButUpdateCheckWaitRequired,
133 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
134 };
135
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700136 // The ctor takes in all the parameters that will be used for making
137 // the request to Omaha. For some of them we have constants that
138 // should be used.
139 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700141 //
142 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
143 // this is an UpdateCheck request, otherwise it's an Event request.
144 // Event requests always succeed.
145 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700147 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700148 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700149 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 OmahaRequestAction(SystemState* system_state,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700151 OmahaRequestParams* params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700152 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700153 HttpFetcher* http_fetcher,
154 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700155 virtual ~OmahaRequestAction();
156 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
157 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000158 void PerformAction();
159 void TerminateProcessing();
160
Darin Petkov1023a602010-08-30 13:47:51 -0700161 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
162
rspangler@google.com49fdf182009-10-10 00:57:34 +0000163 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700164 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000165 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166
167 // Delegate methods (see http_fetcher.h)
168 virtual void ReceivedBytes(HttpFetcher *fetcher,
169 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000170
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700171 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700172 // Returns true if this is an Event request, false if it's an UpdateCheck.
173 bool IsEvent() const { return event_.get() != NULL; }
174
rspangler@google.com49fdf182009-10-10 00:57:34 +0000175 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700176 // If this is an update check request, initializes
177 // |ping_active_days_| and |ping_roll_call_days_| to values that may
178 // be sent as pings to Omaha.
179 void InitPingDays();
180
Darin Petkov84c763c2010-07-29 16:27:58 -0700181 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700182 // number of days since the last ping sent for |key|.
183 int CalculatePingDays(const std::string& key);
184
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700185 // Returns true if the download of a new update should be deferred.
186 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700187 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700188
189 // Returns true if the basic wall-clock-based waiting period has been
190 // satisfied based on the scattering policy setting. False otherwise.
191 // If true, it also indicates whether the additional update-check-count-based
192 // waiting period also needs to be satisfied before the download can begin.
193 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700194 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700195
196 // Returns true if the update-check-count-based waiting period has been
197 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700198 bool IsUpdateCheckCountBasedWaitingSatisfied();
199
200 // Parses the response from Omaha that's available in |doc| using the other
201 // helper methods below and populates the |output_object| with the relevant
202 // values. Returns true if we should continue the parsing. False otherwise,
203 // in which case it sets any error code using |completer|.
204 bool ParseResponse(xmlDoc* doc,
205 OmahaResponse* output_object,
206 ScopedActionCompleter* completer);
207
208 // Parses the status property in the given update_check_node and populates
209 // |output_object| if valid. Returns true if we should continue the parsing.
210 // False otherwise, in which case it sets any error code using |completer|.
211 bool ParseStatus(xmlNode* update_check_node,
212 OmahaResponse* output_object,
213 ScopedActionCompleter* completer);
214
215 // Parses the URL nodes in the given XML document and populates
216 // |output_object| if valid. Returns true if we should continue the parsing.
217 // False otherwise, in which case it sets any error code using |completer|.
218 bool ParseUrls(xmlDoc* doc,
219 OmahaResponse* output_object,
220 ScopedActionCompleter* completer);
221
222 // Parses the package node in the given XML document and populates
223 // |output_object| if valid. Returns true if we should continue the parsing.
224 // False otherwise, in which case it sets any error code using |completer|.
225 bool ParsePackage(xmlDoc* doc,
226 OmahaResponse* output_object,
227 ScopedActionCompleter* completer);
228
229 // Parses the other parameters in the given XML document and populates
230 // |output_object| if valid. Returns true if we should continue the parsing.
231 // False otherwise, in which case it sets any error code using |completer|.
232 bool ParseParams(xmlDoc* doc,
233 OmahaResponse* output_object,
234 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700235
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800236 // Global system context.
237 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700238
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700239 // Contains state that is relevant in the processing of the Omaha request.
240 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000241
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700242 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
243 scoped_ptr<OmahaEvent> event_;
244
rspangler@google.com49fdf182009-10-10 00:57:34 +0000245 // pointer to the HttpFetcher that does the http work
246 scoped_ptr<HttpFetcher> http_fetcher_;
247
Thieu Le116fda32011-04-19 11:01:54 -0700248 // If true, only include the <ping> element in the request.
249 bool ping_only_;
250
rspangler@google.com49fdf182009-10-10 00:57:34 +0000251 // Stores the response from the omaha server
252 std::vector<char> response_buffer_;
253
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700254 // Initialized by InitPingDays to values that may be sent to Omaha
255 // as part of a ping message. Note that only positive values and -1
256 // are sent to Omaha.
257 int ping_active_days_;
258 int ping_roll_call_days_;
259
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000261};
262
263} // namespace chromeos_update_engine
264
Darin Petkov6a5b3222010-07-13 14:55:28 -0700265#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__