blob: f903d32fc3881dc5126afb247bc18856d660d904 [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"
Jay Srinivasan08262882012-12-28 19:29:43 -080021#include "update_engine/omaha_response.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
Darin Petkov6a5b3222010-07-13 14:55:28 -070023// The Omaha Request action makes a request to Omaha and can output
24// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
rspangler@google.com49fdf182009-10-10 00:57:34 +000026namespace chromeos_update_engine {
27
28// Encodes XML entities in a given string with libxml2. input must be
29// UTF-8 formatted. Output will be UTF-8 formatted.
30std::string XmlEncode(const std::string& input);
31
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070032// This struct encapsulates the Omaha event information. For a
33// complete list of defined event types and results, see
34// http://code.google.com/p/omaha/wiki/ServerProtocol#event
35struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070036 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070037 enum Type {
38 kTypeUnknown = 0,
39 kTypeDownloadComplete = 1,
40 kTypeInstallComplete = 2,
41 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070042 kTypeUpdateDownloadStarted = 13,
43 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070044 };
45
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070046 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070047 enum Result {
48 kResultError = 0,
49 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080050 kResultSuccessReboot = 2,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070051 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070052 };
53
54 OmahaEvent()
55 : type(kTypeUnknown),
56 result(kResultError),
David Zeuthena99981f2013-04-29 13:42:47 -070057 error_code(kErrorCodeError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070058 explicit OmahaEvent(Type in_type)
59 : type(in_type),
60 result(kResultSuccess),
David Zeuthena99981f2013-04-29 13:42:47 -070061 error_code(kErrorCodeSuccess) {}
62 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070063 : type(in_type),
64 result(in_result),
65 error_code(in_error_code) {}
66
67 Type type;
68 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070069 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070070};
71
rspangler@google.com49fdf182009-10-10 00:57:34 +000072class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070073class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070074class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070075class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
77template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070078class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070080 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070081 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 // On UpdateCheck success, puts the Omaha response on output. Event
83 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070084 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000085};
86
Darin Petkov6a5b3222010-07-13 14:55:28 -070087class OmahaRequestAction : public Action<OmahaRequestAction>,
88 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000089 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070090 static const int kNeverPinged = -1;
91 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080092 // We choose this value of 10 as a heuristic for a work day in trying
93 // each URL, assuming we check roughly every 45 mins. This is a good time to
94 // wait - neither too long nor too little - so we don't give up the preferred
95 // URLs that appear earlier in list too quickly before moving on to the
96 // fallback ones.
97 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070098
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070099 // These are the possible outcome upon checking whether we satisfied
100 // the wall-clock-based-wait.
101 enum WallClockWaitResult {
102 kWallClockWaitNotSatisfied,
103 kWallClockWaitDoneButUpdateCheckWaitRequired,
104 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
105 };
106
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700107 // The ctor takes in all the parameters that will be used for making
108 // the request to Omaha. For some of them we have constants that
109 // should be used.
110 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700112 //
113 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
114 // this is an UpdateCheck request, otherwise it's an Event request.
115 // Event requests always succeed.
116 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700118 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700119 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700122 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700123 HttpFetcher* http_fetcher,
124 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700125 virtual ~OmahaRequestAction();
126 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
127 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 void PerformAction();
129 void TerminateProcessing();
130
Darin Petkov1023a602010-08-30 13:47:51 -0700131 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
132
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000135 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
137 // Delegate methods (see http_fetcher.h)
138 virtual void ReceivedBytes(HttpFetcher *fetcher,
139 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700141 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700142 // Returns true if this is an Event request, false if it's an UpdateCheck.
143 bool IsEvent() const { return event_.get() != NULL; }
144
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145 private:
David Zeuthen639aa362014-02-03 16:23:44 -0800146 FRIEND_TEST(OmahaRequestActionTest, GetInstallDate);
147
148 // Enumeration used in PersistInstallDate().
149 enum InstallDateProvisioningSource {
150 kProvisionedFromOmahaResponse,
151 kProvisionedFromOOBEMarker,
152
153 // kProvisionedMax is the count of the number of enums above. Add
154 // any new enums above this line only.
155 kProvisionedMax
156 };
157
158 // Gets the install date, expressed as the number of PST8PDT
159 // calendar weeks since January 1st 2007, times seven. Returns -1 if
160 // unknown. See http://crbug.com/336838 for details about this value.
161 static int GetInstallDate(SystemState* system_state);
162
163 // Parses the Omaha Response in |doc| and sets the
164 // |install_date_days| field of |output_object| to the value of the
165 // elapsed_days attribute of the daystart element. Returns True if
166 // the value was set, False if it wasn't found.
167 static bool ParseInstallDate(xmlDoc* doc,
168 OmahaResponse* output_object);
169
170 // Returns True if the kPrefsInstallDateDays state variable is set,
171 // False otherwise.
172 static bool HasInstallDate(SystemState *system_state);
173
174 // Writes |install_date_days| into the kPrefsInstallDateDays state
175 // variable and emits an UMA stat for the |source| used. Returns
176 // True if the value was written, False if an error occurred.
177 static bool PersistInstallDate(SystemState *system_state,
178 int install_date_days,
179 InstallDateProvisioningSource source);
180
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700181 // If this is an update check request, initializes
182 // |ping_active_days_| and |ping_roll_call_days_| to values that may
183 // be sent as pings to Omaha.
184 void InitPingDays();
185
Darin Petkov84c763c2010-07-29 16:27:58 -0700186 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700187 // number of days since the last ping sent for |key|.
188 int CalculatePingDays(const std::string& key);
189
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700190 // Returns true if the download of a new update should be deferred.
191 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700192 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700193
194 // Returns true if the basic wall-clock-based waiting period has been
195 // satisfied based on the scattering policy setting. False otherwise.
196 // If true, it also indicates whether the additional update-check-count-based
197 // waiting period also needs to be satisfied before the download can begin.
198 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700199 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700200
201 // Returns true if the update-check-count-based waiting period has been
202 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700203 bool IsUpdateCheckCountBasedWaitingSatisfied();
204
205 // Parses the response from Omaha that's available in |doc| using the other
206 // helper methods below and populates the |output_object| with the relevant
207 // values. Returns true if we should continue the parsing. False otherwise,
208 // in which case it sets any error code using |completer|.
209 bool ParseResponse(xmlDoc* doc,
210 OmahaResponse* output_object,
211 ScopedActionCompleter* completer);
212
213 // Parses the status property in the given update_check_node and populates
214 // |output_object| if valid. Returns true if we should continue the parsing.
215 // False otherwise, in which case it sets any error code using |completer|.
216 bool ParseStatus(xmlNode* update_check_node,
217 OmahaResponse* output_object,
218 ScopedActionCompleter* completer);
219
220 // Parses the URL nodes in the given XML document and populates
221 // |output_object| if valid. Returns true if we should continue the parsing.
222 // False otherwise, in which case it sets any error code using |completer|.
223 bool ParseUrls(xmlDoc* doc,
224 OmahaResponse* output_object,
225 ScopedActionCompleter* completer);
226
227 // Parses the package node in the given XML document and populates
228 // |output_object| if valid. Returns true if we should continue the parsing.
229 // False otherwise, in which case it sets any error code using |completer|.
230 bool ParsePackage(xmlDoc* doc,
231 OmahaResponse* output_object,
232 ScopedActionCompleter* completer);
233
234 // Parses the other parameters in the given XML document and populates
235 // |output_object| if valid. Returns true if we should continue the parsing.
236 // False otherwise, in which case it sets any error code using |completer|.
237 bool ParseParams(xmlDoc* doc,
238 OmahaResponse* output_object,
239 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700240
David Zeuthen8f191b22013-08-06 12:27:50 -0700241 // Called by TransferComplete() to complete processing, either
242 // asynchronously after looking up resources via p2p or directly.
243 void CompleteProcessing();
244
245 // Helper to asynchronously look up payload on the LAN.
246 void LookupPayloadViaP2P(const OmahaResponse& response);
247
248 // Callback used by LookupPayloadViaP2P().
249 void OnLookupPayloadViaP2PCompleted(const std::string& url);
250
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800251 // Global system context.
252 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700253
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700254 // Contains state that is relevant in the processing of the Omaha request.
255 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700257 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
258 scoped_ptr<OmahaEvent> event_;
259
rspangler@google.com49fdf182009-10-10 00:57:34 +0000260 // pointer to the HttpFetcher that does the http work
261 scoped_ptr<HttpFetcher> http_fetcher_;
262
Thieu Le116fda32011-04-19 11:01:54 -0700263 // If true, only include the <ping> element in the request.
264 bool ping_only_;
265
rspangler@google.com49fdf182009-10-10 00:57:34 +0000266 // Stores the response from the omaha server
267 std::vector<char> response_buffer_;
268
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700269 // Initialized by InitPingDays to values that may be sent to Omaha
270 // as part of a ping message. Note that only positive values and -1
271 // are sent to Omaha.
272 int ping_active_days_;
273 int ping_roll_call_days_;
274
Darin Petkov6a5b3222010-07-13 14:55:28 -0700275 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000276};
277
278} // namespace chromeos_update_engine
279
Darin Petkov6a5b3222010-07-13 14:55:28 -0700280#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__