blob: 7a4d3f2e198646c4d61e86f186d3c0bc38893f7f [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:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700146 // If this is an update check request, initializes
147 // |ping_active_days_| and |ping_roll_call_days_| to values that may
148 // be sent as pings to Omaha.
149 void InitPingDays();
150
Darin Petkov84c763c2010-07-29 16:27:58 -0700151 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700152 // number of days since the last ping sent for |key|.
153 int CalculatePingDays(const std::string& key);
154
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700155 // Returns true if the download of a new update should be deferred.
156 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700157 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700158
159 // Returns true if the basic wall-clock-based waiting period has been
160 // satisfied based on the scattering policy setting. False otherwise.
161 // If true, it also indicates whether the additional update-check-count-based
162 // waiting period also needs to be satisfied before the download can begin.
163 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700164 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700165
166 // Returns true if the update-check-count-based waiting period has been
167 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700168 bool IsUpdateCheckCountBasedWaitingSatisfied();
169
170 // Parses the response from Omaha that's available in |doc| using the other
171 // helper methods below and populates the |output_object| with the relevant
172 // values. Returns true if we should continue the parsing. False otherwise,
173 // in which case it sets any error code using |completer|.
174 bool ParseResponse(xmlDoc* doc,
175 OmahaResponse* output_object,
176 ScopedActionCompleter* completer);
177
178 // Parses the status property in the given update_check_node and populates
179 // |output_object| if valid. Returns true if we should continue the parsing.
180 // False otherwise, in which case it sets any error code using |completer|.
181 bool ParseStatus(xmlNode* update_check_node,
182 OmahaResponse* output_object,
183 ScopedActionCompleter* completer);
184
185 // Parses the URL nodes in the given XML document and populates
186 // |output_object| if valid. Returns true if we should continue the parsing.
187 // False otherwise, in which case it sets any error code using |completer|.
188 bool ParseUrls(xmlDoc* doc,
189 OmahaResponse* output_object,
190 ScopedActionCompleter* completer);
191
192 // Parses the package node in the given XML document and populates
193 // |output_object| if valid. Returns true if we should continue the parsing.
194 // False otherwise, in which case it sets any error code using |completer|.
195 bool ParsePackage(xmlDoc* doc,
196 OmahaResponse* output_object,
197 ScopedActionCompleter* completer);
198
199 // Parses the other parameters in the given XML document and populates
200 // |output_object| if valid. Returns true if we should continue the parsing.
201 // False otherwise, in which case it sets any error code using |completer|.
202 bool ParseParams(xmlDoc* doc,
203 OmahaResponse* output_object,
204 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700205
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800206 // Global system context.
207 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700208
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700209 // Contains state that is relevant in the processing of the Omaha request.
210 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000211
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700212 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
213 scoped_ptr<OmahaEvent> event_;
214
rspangler@google.com49fdf182009-10-10 00:57:34 +0000215 // pointer to the HttpFetcher that does the http work
216 scoped_ptr<HttpFetcher> http_fetcher_;
217
Thieu Le116fda32011-04-19 11:01:54 -0700218 // If true, only include the <ping> element in the request.
219 bool ping_only_;
220
rspangler@google.com49fdf182009-10-10 00:57:34 +0000221 // Stores the response from the omaha server
222 std::vector<char> response_buffer_;
223
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700224 // Initialized by InitPingDays to values that may be sent to Omaha
225 // as part of a ping message. Note that only positive values and -1
226 // are sent to Omaha.
227 int ping_active_days_;
228 int ping_roll_call_days_;
229
Darin Petkov6a5b3222010-07-13 14:55:28 -0700230 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000231};
232
233} // namespace chromeos_update_engine
234
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__