blob: b6e31afec8016d1a5077205e1357f5ac2d56743b [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),
Darin Petkove17f86b2010-07-20 09:12:01 -070057 error_code(kActionCodeError) {}
58 explicit OmahaEvent(Type in_type)
59 : type(in_type),
60 result(kResultSuccess),
61 error_code(kActionCodeSuccess) {}
62 OmahaEvent(Type in_type, Result in_result, ActionExitCode 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;
Darin Petkove17f86b2010-07-20 09:12:01 -070069 ActionExitCode 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;
74struct 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,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700122 OmahaRequestParams* params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700123 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700124 HttpFetcher* http_fetcher,
125 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700126 virtual ~OmahaRequestAction();
127 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
128 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000129 void PerformAction();
130 void TerminateProcessing();
131
Darin Petkov1023a602010-08-30 13:47:51 -0700132 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
133
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000136 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137
138 // Delegate methods (see http_fetcher.h)
139 virtual void ReceivedBytes(HttpFetcher *fetcher,
140 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700142 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700143 // Returns true if this is an Event request, false if it's an UpdateCheck.
144 bool IsEvent() const { return event_.get() != NULL; }
145
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700147 // If this is an update check request, initializes
148 // |ping_active_days_| and |ping_roll_call_days_| to values that may
149 // be sent as pings to Omaha.
150 void InitPingDays();
151
Darin Petkov84c763c2010-07-29 16:27:58 -0700152 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700153 // number of days since the last ping sent for |key|.
154 int CalculatePingDays(const std::string& key);
155
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700156 // Returns true if the download of a new update should be deferred.
157 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700158 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700159
160 // Returns true if the basic wall-clock-based waiting period has been
161 // satisfied based on the scattering policy setting. False otherwise.
162 // If true, it also indicates whether the additional update-check-count-based
163 // waiting period also needs to be satisfied before the download can begin.
164 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700165 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700166
167 // Returns true if the update-check-count-based waiting period has been
168 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700169 bool IsUpdateCheckCountBasedWaitingSatisfied();
170
171 // Parses the response from Omaha that's available in |doc| using the other
172 // helper methods below and populates the |output_object| with the relevant
173 // values. Returns true if we should continue the parsing. False otherwise,
174 // in which case it sets any error code using |completer|.
175 bool ParseResponse(xmlDoc* doc,
176 OmahaResponse* output_object,
177 ScopedActionCompleter* completer);
178
179 // Parses the status property in the given update_check_node and populates
180 // |output_object| if valid. Returns true if we should continue the parsing.
181 // False otherwise, in which case it sets any error code using |completer|.
182 bool ParseStatus(xmlNode* update_check_node,
183 OmahaResponse* output_object,
184 ScopedActionCompleter* completer);
185
186 // Parses the URL nodes in the given XML document and populates
187 // |output_object| if valid. Returns true if we should continue the parsing.
188 // False otherwise, in which case it sets any error code using |completer|.
189 bool ParseUrls(xmlDoc* doc,
190 OmahaResponse* output_object,
191 ScopedActionCompleter* completer);
192
193 // Parses the package node in the given XML document and populates
194 // |output_object| if valid. Returns true if we should continue the parsing.
195 // False otherwise, in which case it sets any error code using |completer|.
196 bool ParsePackage(xmlDoc* doc,
197 OmahaResponse* output_object,
198 ScopedActionCompleter* completer);
199
200 // Parses the other parameters in the given XML document and populates
201 // |output_object| if valid. Returns true if we should continue the parsing.
202 // False otherwise, in which case it sets any error code using |completer|.
203 bool ParseParams(xmlDoc* doc,
204 OmahaResponse* output_object,
205 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700206
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800207 // Global system context.
208 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700209
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700210 // Contains state that is relevant in the processing of the Omaha request.
211 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700213 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
214 scoped_ptr<OmahaEvent> event_;
215
rspangler@google.com49fdf182009-10-10 00:57:34 +0000216 // pointer to the HttpFetcher that does the http work
217 scoped_ptr<HttpFetcher> http_fetcher_;
218
Thieu Le116fda32011-04-19 11:01:54 -0700219 // If true, only include the <ping> element in the request.
220 bool ping_only_;
221
rspangler@google.com49fdf182009-10-10 00:57:34 +0000222 // Stores the response from the omaha server
223 std::vector<char> response_buffer_;
224
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700225 // Initialized by InitPingDays to values that may be sent to Omaha
226 // as part of a ping message. Note that only positive values and -1
227 // are sent to Omaha.
228 int ping_active_days_;
229 int ping_roll_call_days_;
230
Darin Petkov6a5b3222010-07-13 14:55:28 -0700231 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232};
233
234} // namespace chromeos_update_engine
235
Darin Petkov6a5b3222010-07-13 14:55:28 -0700236#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__