Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <sys/stat.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 9 | #include <sys/types.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | |
| 12 | #include <string> |
| 13 | |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include <curl/curl.h> |
| 16 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 17 | #include "update_engine/action.h" |
| 18 | #include "update_engine/http_fetcher.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 20 | // The Omaha Request action makes a request to Omaha and can output |
| 21 | // the response on the output ActionPipe. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | namespace 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. |
| 27 | std::string XmlEncode(const std::string& input); |
| 28 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 29 | // This struct encapsulates the data Omaha's response for the request. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | // These strings in this struct are not XML escaped. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 31 | struct OmahaResponse { |
| 32 | OmahaResponse() |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 33 | : update_exists(false), |
| 34 | poll_interval(0), |
| 35 | size(0), |
| 36 | needs_admin(false), |
| 37 | prompt(false) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | // True iff there is an update to be downloaded. |
| 39 | bool update_exists; |
| 40 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 41 | // If non-zero, server-dictated poll frequency in seconds. |
| 42 | int poll_interval; |
| 43 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | // 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 Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 49 | std::string deadline; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 | off_t size; |
| 51 | bool needs_admin; |
| 52 | bool prompt; |
| 53 | }; |
| 54 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 55 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 56 | // This struct encapsulates the Omaha event information. For a |
| 57 | // complete list of defined event types and results, see |
| 58 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 59 | struct OmahaEvent { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 60 | // The Type values correspond to EVENT_TYPE values of Omaha. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 61 | enum Type { |
| 62 | kTypeUnknown = 0, |
| 63 | kTypeDownloadComplete = 1, |
| 64 | kTypeInstallComplete = 2, |
| 65 | kTypeUpdateComplete = 3, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 66 | kTypeUpdateDownloadStarted = 13, |
| 67 | kTypeUpdateDownloadFinished = 14, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 70 | // The Result values correspond to EVENT_RESULT values of Omaha. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 71 | enum Result { |
| 72 | kResultError = 0, |
| 73 | kResultSuccess = 1, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 74 | kResultSuccessReboot = 2, |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 75 | kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | OmahaEvent() |
| 79 | : type(kTypeUnknown), |
| 80 | result(kResultError), |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 81 | error_code(kActionCodeError) {} |
| 82 | explicit OmahaEvent(Type in_type) |
| 83 | : type(in_type), |
| 84 | result(kResultSuccess), |
| 85 | error_code(kActionCodeSuccess) {} |
| 86 | OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code) |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 87 | : type(in_type), |
| 88 | result(in_result), |
| 89 | error_code(in_error_code) {} |
| 90 | |
| 91 | Type type; |
| 92 | Result result; |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 93 | ActionExitCode error_code; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | class NoneType; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 97 | class OmahaRequestAction; |
| 98 | struct OmahaRequestParams; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 99 | class PrefsInterface; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | |
| 101 | template<> |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 102 | class ActionTraits<OmahaRequestAction> { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 103 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 104 | // Takes parameters on the input pipe. |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 105 | typedef NoneType InputObjectType; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 106 | // On UpdateCheck success, puts the Omaha response on output. Event |
| 107 | // requests do not have an output pipe. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 108 | typedef OmahaResponse OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 111 | class OmahaRequestAction : public Action<OmahaRequestAction>, |
| 112 | public HttpFetcherDelegate { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 113 | public: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 114 | static const int kNeverPinged = -1; |
| 115 | static const int kPingTimeJump = -2; |
| 116 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 117 | // The ctor takes in all the parameters that will be used for making |
| 118 | // the request to Omaha. For some of them we have constants that |
| 119 | // should be used. |
| 120 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 121 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 122 | // |
| 123 | // Takes ownership of the passed in OmahaEvent. If |event| is NULL, |
| 124 | // this is an UpdateCheck request, otherwise it's an Event request. |
| 125 | // Event requests always succeed. |
| 126 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | // A good calling pattern is: |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 128 | // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 129 | // or |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 130 | // OmahaRequestAction(..., NULL, new WhateverHttpFetcher); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 131 | OmahaRequestAction(PrefsInterface* prefs, |
| 132 | const OmahaRequestParams& params, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 133 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 134 | HttpFetcher* http_fetcher, |
| 135 | bool ping_only); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 136 | virtual ~OmahaRequestAction(); |
| 137 | typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType; |
| 138 | typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 139 | void PerformAction(); |
| 140 | void TerminateProcessing(); |
| 141 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 142 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } |
| 143 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 | // Debugging/logging |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 145 | static std::string StaticType() { return "OmahaRequestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 146 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 147 | |
| 148 | // Delegate methods (see http_fetcher.h) |
| 149 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 150 | const char* bytes, int length); |
| 151 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 152 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 153 | // Returns true if this is an Event request, false if it's an UpdateCheck. |
| 154 | bool IsEvent() const { return event_.get() != NULL; } |
| 155 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 156 | private: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 157 | // 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 Petkov | 84c763c | 2010-07-29 16:27:58 -0700 | [diff] [blame] | 162 | // Based on the persistent preference store values, calculates the |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 163 | // 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 Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 169 | // These are data that are passed in the request to the Omaha server. |
| 170 | const OmahaRequestParams& params_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 171 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 172 | // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL. |
| 173 | scoped_ptr<OmahaEvent> event_; |
| 174 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 175 | // pointer to the HttpFetcher that does the http work |
| 176 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 177 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 178 | // If true, only include the <ping> element in the request. |
| 179 | bool ping_only_; |
| 180 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 181 | // Stores the response from the omaha server |
| 182 | std::vector<char> response_buffer_; |
| 183 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 184 | // 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 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 190 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace chromeos_update_engine |
| 194 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 195 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |