blob: fd7f548decd45959762c37b017c5c15752a6847d [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 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
Darin Petkov85ced132010-09-01 10:20:56 -070014#include <base/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <curl/curl.h>
16
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017#include "update_engine/action.h"
18#include "update_engine/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Darin Petkov6a5b3222010-07-13 14:55:28 -070020// The Omaha Request action makes a request to Omaha and can output
21// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
rspangler@google.com49fdf182009-10-10 00:57:34 +000023namespace chromeos_update_engine {
24
25// Encodes XML entities in a given string with libxml2. input must be
26// UTF-8 formatted. Output will be UTF-8 formatted.
27std::string XmlEncode(const std::string& input);
28
Darin Petkov6a5b3222010-07-13 14:55:28 -070029// This struct encapsulates the data Omaha's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000030// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070031struct OmahaResponse {
32 OmahaResponse()
Darin Petkov85ced132010-09-01 10:20:56 -070033 : update_exists(false),
34 poll_interval(0),
35 size(0),
36 needs_admin(false),
37 prompt(false) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000038 // True iff there is an update to be downloaded.
39 bool update_exists;
40
Darin Petkov85ced132010-09-01 10:20:56 -070041 // If non-zero, server-dictated poll frequency in seconds.
42 int poll_interval;
43
rspangler@google.com49fdf182009-10-10 00:57:34 +000044 // These are only valid if update_exists is true:
45 std::string display_version;
46 std::string codebase;
47 std::string more_info_url;
48 std::string hash;
Darin Petkov6c118642010-10-21 12:06:30 -070049 std::string deadline;
rspangler@google.com49fdf182009-10-10 00:57:34 +000050 off_t size;
51 bool needs_admin;
52 bool prompt;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070053 bool is_delta;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054};
55COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
56
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070057// This struct encapsulates the Omaha event information. For a
58// complete list of defined event types and results, see
59// http://code.google.com/p/omaha/wiki/ServerProtocol#event
60struct OmahaEvent {
61 enum Type {
62 kTypeUnknown = 0,
63 kTypeDownloadComplete = 1,
64 kTypeInstallComplete = 2,
65 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070066 kTypeUpdateDownloadStarted = 13,
67 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070068 };
69
70 enum Result {
71 kResultError = 0,
72 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080073 kResultSuccessReboot = 2,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070074 };
75
76 OmahaEvent()
77 : type(kTypeUnknown),
78 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070079 error_code(kActionCodeError) {}
80 explicit OmahaEvent(Type in_type)
81 : type(in_type),
82 result(kResultSuccess),
83 error_code(kActionCodeSuccess) {}
84 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070085 : type(in_type),
86 result(in_result),
87 error_code(in_error_code) {}
88
89 Type type;
90 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -070091 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070092};
93
rspangler@google.com49fdf182009-10-10 00:57:34 +000094class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070095class OmahaRequestAction;
96struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070097class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000098
99template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700102 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700103 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700104 // On UpdateCheck success, puts the Omaha response on output. Event
105 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107};
108
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109class OmahaRequestAction : public Action<OmahaRequestAction>,
110 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700112 static const int kNeverPinged = -1;
113 static const int kPingTimeJump = -2;
114
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700115 // The ctor takes in all the parameters that will be used for making
116 // the request to Omaha. For some of them we have constants that
117 // should be used.
118 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700120 //
121 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
122 // this is an UpdateCheck request, otherwise it's an Event request.
123 // Event requests always succeed.
124 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700126 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700127 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700128 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700129 OmahaRequestAction(PrefsInterface* prefs,
130 const OmahaRequestParams& params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700131 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700132 HttpFetcher* http_fetcher,
133 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 virtual ~OmahaRequestAction();
135 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
136 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137 void PerformAction();
138 void TerminateProcessing();
139
Darin Petkov1023a602010-08-30 13:47:51 -0700140 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
141
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700143 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000144 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145
146 // Delegate methods (see http_fetcher.h)
147 virtual void ReceivedBytes(HttpFetcher *fetcher,
148 const char* bytes, int length);
149 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
150
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700151 // Returns true if this is an Event request, false if it's an UpdateCheck.
152 bool IsEvent() const { return event_.get() != NULL; }
153
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700154 void set_should_skip(bool should_skip) { should_skip_ = should_skip; }
155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700157 // If this is an update check request, initializes
158 // |ping_active_days_| and |ping_roll_call_days_| to values that may
159 // be sent as pings to Omaha.
160 void InitPingDays();
161
Darin Petkov84c763c2010-07-29 16:27:58 -0700162 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700163 // number of days since the last ping sent for |key|.
164 int CalculatePingDays(const std::string& key);
165
166 // Access to the preferences store.
167 PrefsInterface* prefs_;
168
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700169 // These are data that are passed in the request to the Omaha server.
170 const OmahaRequestParams& params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000171
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700172 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
173 scoped_ptr<OmahaEvent> event_;
174
rspangler@google.com49fdf182009-10-10 00:57:34 +0000175 // pointer to the HttpFetcher that does the http work
176 scoped_ptr<HttpFetcher> http_fetcher_;
177
Thieu Le116fda32011-04-19 11:01:54 -0700178 // If true, only include the <ping> element in the request.
179 bool ping_only_;
180
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181 // Stores the response from the omaha server
182 std::vector<char> response_buffer_;
183
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700184 // Initialized by InitPingDays to values that may be sent to Omaha
185 // as part of a ping message. Note that only positive values and -1
186 // are sent to Omaha.
187 int ping_active_days_;
188 int ping_roll_call_days_;
189
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700190 // If true, this action should be a noop.
191 bool should_skip_;
192
Darin Petkov6a5b3222010-07-13 14:55:28 -0700193 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194};
195
196} // namespace chromeos_update_engine
197
Darin Petkov6a5b3222010-07-13 14:55:28 -0700198#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__