blob: a4d9fdf65ae859d73c55d64bd5460a4686ea25cd [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
6#define UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Alex Vakulenko44cab302014-07-23 13:12:15 -07008#include <fcntl.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011
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>
17
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/action.h"
19#include "update_engine/http_fetcher.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080020#include "update_engine/omaha_response.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 Petkov0dc8e9a2010-07-14 14:51:57 -070031// This struct encapsulates the Omaha event information. For a
32// complete list of defined event types and results, see
33// http://code.google.com/p/omaha/wiki/ServerProtocol#event
34struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070035 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070036 enum Type {
37 kTypeUnknown = 0,
38 kTypeDownloadComplete = 1,
39 kTypeInstallComplete = 2,
40 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070041 kTypeUpdateDownloadStarted = 13,
42 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070043 };
44
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070045 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070046 enum Result {
47 kResultError = 0,
48 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080049 kResultSuccessReboot = 2,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070050 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070051 };
52
53 OmahaEvent()
54 : type(kTypeUnknown),
55 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070056 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070057 explicit OmahaEvent(Type in_type)
58 : type(in_type),
59 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070060 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070061 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070062 : type(in_type),
63 result(in_result),
64 error_code(in_error_code) {}
65
66 Type type;
67 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070068 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070069};
70
rspangler@google.com49fdf182009-10-10 00:57:34 +000071class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070072class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070073class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070074class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075
David Zeuthene8ed8632014-07-24 13:38:10 -040076// This struct is declared in the .cc file.
77struct OmahaParserData;
78
rspangler@google.com49fdf182009-10-10 00:57:34 +000079template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070080class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070083 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070084 // On UpdateCheck success, puts the Omaha response on output. Event
85 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070086 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000087};
88
Darin Petkov6a5b3222010-07-13 14:55:28 -070089class OmahaRequestAction : public Action<OmahaRequestAction>,
90 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070092 static const int kNeverPinged = -1;
93 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080094 // We choose this value of 10 as a heuristic for a work day in trying
95 // each URL, assuming we check roughly every 45 mins. This is a good time to
96 // wait - neither too long nor too little - so we don't give up the preferred
97 // URLs that appear earlier in list too quickly before moving on to the
98 // fallback ones.
99 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700100
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700101 // These are the possible outcome upon checking whether we satisfied
102 // the wall-clock-based-wait.
103 enum WallClockWaitResult {
104 kWallClockWaitNotSatisfied,
105 kWallClockWaitDoneButUpdateCheckWaitRequired,
106 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
107 };
108
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700109 // The ctor takes in all the parameters that will be used for making
110 // the request to Omaha. For some of them we have constants that
111 // should be used.
112 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700114 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700115 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700116 // this is an UpdateCheck request, otherwise it's an Event request.
117 // Event requests always succeed.
118 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700121 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700122 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800123 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700124 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700125 HttpFetcher* http_fetcher,
126 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700127 virtual ~OmahaRequestAction();
128 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
129 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 void PerformAction();
131 void TerminateProcessing();
David Zeuthen33bae492014-02-25 16:16:18 -0800132 void ActionCompleted(ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133
Darin Petkov1023a602010-08-30 13:47:51 -0700134 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
135
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000138 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139
140 // Delegate methods (see http_fetcher.h)
141 virtual void ReceivedBytes(HttpFetcher *fetcher,
142 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700144 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700145 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700146 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700147
rspangler@google.com49fdf182009-10-10 00:57:34 +0000148 private:
David Zeuthen639aa362014-02-03 16:23:44 -0800149 FRIEND_TEST(OmahaRequestActionTest, GetInstallDate);
150
151 // Enumeration used in PersistInstallDate().
152 enum InstallDateProvisioningSource {
153 kProvisionedFromOmahaResponse,
154 kProvisionedFromOOBEMarker,
155
156 // kProvisionedMax is the count of the number of enums above. Add
157 // any new enums above this line only.
158 kProvisionedMax
159 };
160
161 // Gets the install date, expressed as the number of PST8PDT
162 // calendar weeks since January 1st 2007, times seven. Returns -1 if
163 // unknown. See http://crbug.com/336838 for details about this value.
164 static int GetInstallDate(SystemState* system_state);
165
166 // Parses the Omaha Response in |doc| and sets the
167 // |install_date_days| field of |output_object| to the value of the
168 // elapsed_days attribute of the daystart element. Returns True if
169 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400170 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800171 OmahaResponse* output_object);
172
173 // Returns True if the kPrefsInstallDateDays state variable is set,
174 // False otherwise.
175 static bool HasInstallDate(SystemState *system_state);
176
177 // Writes |install_date_days| into the kPrefsInstallDateDays state
178 // variable and emits an UMA stat for the |source| used. Returns
179 // True if the value was written, False if an error occurred.
180 static bool PersistInstallDate(SystemState *system_state,
181 int install_date_days,
182 InstallDateProvisioningSource source);
183
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700184 // If this is an update check request, initializes
185 // |ping_active_days_| and |ping_roll_call_days_| to values that may
186 // be sent as pings to Omaha.
187 void InitPingDays();
188
Darin Petkov84c763c2010-07-29 16:27:58 -0700189 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700190 // number of days since the last ping sent for |key|.
191 int CalculatePingDays(const std::string& key);
192
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700193 // Returns true if the download of a new update should be deferred.
194 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700195 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700196
197 // Returns true if the basic wall-clock-based waiting period has been
198 // satisfied based on the scattering policy setting. False otherwise.
199 // If true, it also indicates whether the additional update-check-count-based
200 // waiting period also needs to be satisfied before the download can begin.
201 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700202 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700203
204 // Returns true if the update-check-count-based waiting period has been
205 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700206 bool IsUpdateCheckCountBasedWaitingSatisfied();
207
208 // Parses the response from Omaha that's available in |doc| using the other
209 // helper methods below and populates the |output_object| with the relevant
210 // values. Returns true if we should continue the parsing. False otherwise,
211 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400212 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700213 OmahaResponse* output_object,
214 ScopedActionCompleter* completer);
215
216 // Parses the status property in the given update_check_node and populates
217 // |output_object| if valid. Returns true if we should continue the parsing.
218 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400219 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700220 OmahaResponse* output_object,
221 ScopedActionCompleter* completer);
222
223 // Parses the URL nodes in the given XML document and populates
224 // |output_object| if valid. Returns true if we should continue the parsing.
225 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400226 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700227 OmahaResponse* output_object,
228 ScopedActionCompleter* completer);
229
230 // Parses the package node in the given XML document and populates
231 // |output_object| if valid. Returns true if we should continue the parsing.
232 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400233 bool ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700234 OmahaResponse* output_object,
235 ScopedActionCompleter* completer);
236
237 // Parses the other parameters in the given XML document and populates
238 // |output_object| if valid. Returns true if we should continue the parsing.
239 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400240 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700241 OmahaResponse* output_object,
242 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700243
David Zeuthen8f191b22013-08-06 12:27:50 -0700244 // Called by TransferComplete() to complete processing, either
245 // asynchronously after looking up resources via p2p or directly.
246 void CompleteProcessing();
247
248 // Helper to asynchronously look up payload on the LAN.
249 void LookupPayloadViaP2P(const OmahaResponse& response);
250
251 // Callback used by LookupPayloadViaP2P().
252 void OnLookupPayloadViaP2PCompleted(const std::string& url);
253
Chris Sosa77f79e82014-06-02 18:16:24 -0700254 // Returns true if the current update should be ignored.
255 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
256
257 // Returns true if updates are allowed over the current type of connection.
258 // False otherwise.
259 bool IsUpdateAllowedOverCurrentConnection() const;
260
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800261 // Global system context.
262 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700263
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700264 // Contains state that is relevant in the processing of the Omaha request.
265 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000266
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700267 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700268 scoped_ptr<OmahaEvent> event_;
269
rspangler@google.com49fdf182009-10-10 00:57:34 +0000270 // pointer to the HttpFetcher that does the http work
271 scoped_ptr<HttpFetcher> http_fetcher_;
272
Thieu Le116fda32011-04-19 11:01:54 -0700273 // If true, only include the <ping> element in the request.
274 bool ping_only_;
275
rspangler@google.com49fdf182009-10-10 00:57:34 +0000276 // Stores the response from the omaha server
277 std::vector<char> response_buffer_;
278
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700279 // Initialized by InitPingDays to values that may be sent to Omaha
280 // as part of a ping message. Note that only positive values and -1
281 // are sent to Omaha.
282 int ping_active_days_;
283 int ping_roll_call_days_;
284
Darin Petkov6a5b3222010-07-13 14:55:28 -0700285 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286};
287
288} // namespace chromeos_update_engine
289
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700290#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_