rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 14 | #include <curl/curl.h> |
| 15 | |
| 16 | #include "base/scoped_ptr.h" |
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 gets for the request. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | // These strings in this struct should not be XML escaped. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 31 | struct OmahaRequestParams { |
| 32 | OmahaRequestParams() |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {} |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 34 | OmahaRequestParams(const std::string& in_machine_id, |
| 35 | const std::string& in_user_id, |
| 36 | const std::string& in_os_platform, |
| 37 | const std::string& in_os_version, |
| 38 | const std::string& in_os_sp, |
| 39 | const std::string& in_os_board, |
| 40 | const std::string& in_app_id, |
| 41 | const std::string& in_app_version, |
| 42 | const std::string& in_app_lang, |
| 43 | const std::string& in_app_track, |
| 44 | const std::string& in_update_url) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | : machine_id(in_machine_id), |
| 46 | user_id(in_user_id), |
| 47 | os_platform(in_os_platform), |
| 48 | os_version(in_os_version), |
| 49 | os_sp(in_os_sp), |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 50 | os_board(in_os_board), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 | app_id(in_app_id), |
| 52 | app_version(in_app_version), |
| 53 | app_lang(in_app_lang), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 54 | app_track(in_app_track), |
| 55 | update_url(in_update_url) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 57 | std::string machine_id; |
| 58 | std::string user_id; |
| 59 | std::string os_platform; |
| 60 | std::string os_version; |
| 61 | std::string os_sp; |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 62 | std::string os_board; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 63 | std::string app_id; |
| 64 | std::string app_version; |
| 65 | std::string app_lang; |
| 66 | std::string app_track; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 67 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 68 | std::string update_url; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | |
| 70 | // Suggested defaults |
| 71 | static const char* const kAppId; |
| 72 | static const char* const kOsPlatform; |
| 73 | static const char* const kOsVersion; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 74 | static const char* const kUpdateUrl; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 77 | // This struct encapsulates the data Omaha's response for the request. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | // These strings in this struct are not XML escaped. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 79 | struct OmahaResponse { |
| 80 | OmahaResponse() |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | : update_exists(false), size(0), needs_admin(false), prompt(false) {} |
| 82 | // True iff there is an update to be downloaded. |
| 83 | bool update_exists; |
| 84 | |
| 85 | // These are only valid if update_exists is true: |
| 86 | std::string display_version; |
| 87 | std::string codebase; |
| 88 | std::string more_info_url; |
| 89 | std::string hash; |
| 90 | off_t size; |
| 91 | bool needs_admin; |
| 92 | bool prompt; |
| 93 | }; |
| 94 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 95 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 96 | // This struct encapsulates the Omaha event information. For a |
| 97 | // complete list of defined event types and results, see |
| 98 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 99 | struct OmahaEvent { |
| 100 | enum Type { |
| 101 | kTypeUnknown = 0, |
| 102 | kTypeDownloadComplete = 1, |
| 103 | kTypeInstallComplete = 2, |
| 104 | kTypeUpdateComplete = 3, |
| 105 | }; |
| 106 | |
| 107 | enum Result { |
| 108 | kResultError = 0, |
| 109 | kResultSuccess = 1, |
| 110 | }; |
| 111 | |
| 112 | OmahaEvent() |
| 113 | : type(kTypeUnknown), |
| 114 | result(kResultError), |
| 115 | error_code(0) {} |
| 116 | OmahaEvent(Type in_type, Result in_result, int in_error_code) |
| 117 | : type(in_type), |
| 118 | result(in_result), |
| 119 | error_code(in_error_code) {} |
| 120 | |
| 121 | Type type; |
| 122 | Result result; |
| 123 | int error_code; |
| 124 | }; |
| 125 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 126 | class OmahaRequestAction; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | class NoneType; |
| 128 | |
| 129 | template<> |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 130 | class ActionTraits<OmahaRequestAction> { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 131 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 132 | // Takes parameters on the input pipe. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 133 | typedef OmahaRequestParams InputObjectType; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 134 | // On UpdateCheck success, puts the Omaha response on output. Event |
| 135 | // requests do not have an output pipe. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 136 | typedef OmahaResponse OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 139 | class OmahaRequestAction : public Action<OmahaRequestAction>, |
| 140 | public HttpFetcherDelegate { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 141 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 142 | // The ctor takes in all the parameters that will be used for making |
| 143 | // the request to Omaha. For some of them we have constants that |
| 144 | // should be used. |
| 145 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 147 | // |
| 148 | // Takes ownership of the passed in OmahaEvent. If |event| is NULL, |
| 149 | // this is an UpdateCheck request, otherwise it's an Event request. |
| 150 | // Event requests always succeed. |
| 151 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 152 | // A good calling pattern is: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 153 | // OmahaRequestAction(new OmahaEvent(...), new WhateverHttpFetcher); |
| 154 | // or |
| 155 | // OmahaRequestAction(NULL, new WhateverHttpFetcher); |
| 156 | OmahaRequestAction(OmahaEvent* event, |
| 157 | HttpFetcher* http_fetcher); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 158 | virtual ~OmahaRequestAction(); |
| 159 | typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType; |
| 160 | typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 161 | void PerformAction(); |
| 162 | void TerminateProcessing(); |
| 163 | |
| 164 | // Debugging/logging |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 165 | static std::string StaticType() { return "OmahaRequestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 166 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | |
| 168 | // Delegate methods (see http_fetcher.h) |
| 169 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 170 | const char* bytes, int length); |
| 171 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 172 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 173 | // Returns true if this is an Event request, false if it's an UpdateCheck. |
| 174 | bool IsEvent() const { return event_.get() != NULL; } |
| 175 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | private: |
| 177 | // These are data that are passed in the request to the Omaha server |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 178 | OmahaRequestParams params_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 179 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 180 | // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL. |
| 181 | scoped_ptr<OmahaEvent> event_; |
| 182 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 183 | // pointer to the HttpFetcher that does the http work |
| 184 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 185 | |
| 186 | // Stores the response from the omaha server |
| 187 | std::vector<char> response_buffer_; |
| 188 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 189 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | } // namespace chromeos_update_engine |
| 193 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 194 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |