blob: e7dfb0807c2d71acc6cb0cafb2e4a3a172b0312a [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>
13
Chris Masoned903c3b2011-05-12 15:35:46 -070014#include <base/memory/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <curl/curl.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070016#include <libxml/parser.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/action.h"
19#include "update_engine/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000020
Darin Petkov6a5b3222010-07-13 14:55:28 -070021// The Omaha Request action makes a request to Omaha and can output
22// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
rspangler@google.com49fdf182009-10-10 00:57:34 +000024namespace chromeos_update_engine {
25
26// Encodes XML entities in a given string with libxml2. input must be
27// UTF-8 formatted. Output will be UTF-8 formatted.
28std::string XmlEncode(const std::string& input);
29
Darin Petkov6a5b3222010-07-13 14:55:28 -070030// This struct encapsulates the data Omaha's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000031// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070032struct OmahaResponse {
33 OmahaResponse()
Darin Petkov85ced132010-09-01 10:20:56 -070034 : update_exists(false),
35 poll_interval(0),
36 size(0),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070037 manifest_size(0),
Darin Petkov85ced132010-09-01 10:20:56 -070038 needs_admin(false),
39 prompt(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 // True iff there is an update to be downloaded.
41 bool update_exists;
42
Darin Petkov85ced132010-09-01 10:20:56 -070043 // If non-zero, server-dictated poll frequency in seconds.
44 int poll_interval;
45
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 // These are only valid if update_exists is true:
47 std::string display_version;
48 std::string codebase;
49 std::string more_info_url;
50 std::string hash;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070051 std::string manifest_signature;
Darin Petkov6c118642010-10-21 12:06:30 -070052 std::string deadline;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 off_t size;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070054 off_t manifest_size;
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 bool needs_admin;
56 bool prompt;
57};
58COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
59
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070060// This struct encapsulates the Omaha event information. For a
61// complete list of defined event types and results, see
62// http://code.google.com/p/omaha/wiki/ServerProtocol#event
63struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070064 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070065 enum Type {
66 kTypeUnknown = 0,
67 kTypeDownloadComplete = 1,
68 kTypeInstallComplete = 2,
69 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070070 kTypeUpdateDownloadStarted = 13,
71 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070072 };
73
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070074 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070075 enum Result {
76 kResultError = 0,
77 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080078 kResultSuccessReboot = 2,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070079 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070080 };
81
82 OmahaEvent()
83 : type(kTypeUnknown),
84 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070085 error_code(kActionCodeError) {}
86 explicit OmahaEvent(Type in_type)
87 : type(in_type),
88 result(kResultSuccess),
89 error_code(kActionCodeSuccess) {}
90 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070091 : type(in_type),
92 result(in_result),
93 error_code(in_error_code) {}
94
95 Type type;
96 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -070097 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070098};
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700101class OmahaRequestAction;
102struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700103class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
105template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700108 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700109 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700110 // On UpdateCheck success, puts the Omaha response on output. Event
111 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700112 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113};
114
Darin Petkov6a5b3222010-07-13 14:55:28 -0700115class OmahaRequestAction : public Action<OmahaRequestAction>,
116 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700118 static const int kNeverPinged = -1;
119 static const int kPingTimeJump = -2;
120
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700121 // These are the possible outcome upon checking whether we satisfied
122 // the wall-clock-based-wait.
123 enum WallClockWaitResult {
124 kWallClockWaitNotSatisfied,
125 kWallClockWaitDoneButUpdateCheckWaitRequired,
126 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
127 };
128
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700129 // The ctor takes in all the parameters that will be used for making
130 // the request to Omaha. For some of them we have constants that
131 // should be used.
132 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700134 //
135 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
136 // this is an UpdateCheck request, otherwise it's an Event request.
137 // Event requests always succeed.
138 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700141 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700142 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700143 OmahaRequestAction(PrefsInterface* prefs,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700144 OmahaRequestParams* params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700145 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700146 HttpFetcher* http_fetcher,
147 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700148 virtual ~OmahaRequestAction();
149 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
150 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151 void PerformAction();
152 void TerminateProcessing();
153
Darin Petkov1023a602010-08-30 13:47:51 -0700154 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700157 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000159
160 // Delegate methods (see http_fetcher.h)
161 virtual void ReceivedBytes(HttpFetcher *fetcher,
162 const char* bytes, int length);
163 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
164
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700165 // Returns true if this is an Event request, false if it's an UpdateCheck.
166 bool IsEvent() const { return event_.get() != NULL; }
167
rspangler@google.com49fdf182009-10-10 00:57:34 +0000168 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700169 // If this is an update check request, initializes
170 // |ping_active_days_| and |ping_roll_call_days_| to values that may
171 // be sent as pings to Omaha.
172 void InitPingDays();
173
Darin Petkov84c763c2010-07-29 16:27:58 -0700174 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700175 // number of days since the last ping sent for |key|.
176 int CalculatePingDays(const std::string& key);
177
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700178 // Returns true if the download of a new update should be deferred.
179 // False if the update can be downloaded.
180 bool ShouldDeferDownload(xmlNode* updatecheck_node);
181
182 // Returns true if the basic wall-clock-based waiting period has been
183 // satisfied based on the scattering policy setting. False otherwise.
184 // If true, it also indicates whether the additional update-check-count-based
185 // waiting period also needs to be satisfied before the download can begin.
186 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
187 xmlNode* updatecheck_node);
188
189 // Returns true if the update-check-count-based waiting period has been
190 // satisfied. False otherwise.
191 bool IsUpdateCheckCountBasedWaitingSatisfied(xmlNode* updatecheck_node);
192
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700193 // Access to the preferences store.
194 PrefsInterface* prefs_;
195
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700196 // Contains state that is relevant in the processing of the Omaha request.
197 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700199 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
200 scoped_ptr<OmahaEvent> event_;
201
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202 // pointer to the HttpFetcher that does the http work
203 scoped_ptr<HttpFetcher> http_fetcher_;
204
Thieu Le116fda32011-04-19 11:01:54 -0700205 // If true, only include the <ping> element in the request.
206 bool ping_only_;
207
rspangler@google.com49fdf182009-10-10 00:57:34 +0000208 // Stores the response from the omaha server
209 std::vector<char> response_buffer_;
210
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700211 // Initialized by InitPingDays to values that may be sent to Omaha
212 // as part of a ping message. Note that only positive values and -1
213 // are sent to Omaha.
214 int ping_active_days_;
215 int ping_roll_call_days_;
216
Darin Petkov6a5b3222010-07-13 14:55:28 -0700217 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218};
219
220} // namespace chromeos_update_engine
221
Darin Petkov6a5b3222010-07-13 14:55:28 -0700222#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__