Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2010 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 | |
| 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'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() |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | : update_exists(false), size(0), needs_admin(false), prompt(false) {} |
| 34 | // True iff there is an update to be downloaded. |
| 35 | bool update_exists; |
| 36 | |
| 37 | // These are only valid if update_exists is true: |
| 38 | std::string display_version; |
| 39 | std::string codebase; |
| 40 | std::string more_info_url; |
| 41 | std::string hash; |
| 42 | off_t size; |
| 43 | bool needs_admin; |
| 44 | bool prompt; |
| 45 | }; |
| 46 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 47 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 48 | // This struct encapsulates the Omaha event information. For a |
| 49 | // complete list of defined event types and results, see |
| 50 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 51 | struct OmahaEvent { |
| 52 | enum Type { |
| 53 | kTypeUnknown = 0, |
| 54 | kTypeDownloadComplete = 1, |
| 55 | kTypeInstallComplete = 2, |
| 56 | kTypeUpdateComplete = 3, |
| 57 | }; |
| 58 | |
| 59 | enum Result { |
| 60 | kResultError = 0, |
| 61 | kResultSuccess = 1, |
| 62 | }; |
| 63 | |
| 64 | OmahaEvent() |
| 65 | : type(kTypeUnknown), |
| 66 | result(kResultError), |
| 67 | error_code(0) {} |
| 68 | OmahaEvent(Type in_type, Result in_result, int in_error_code) |
| 69 | : type(in_type), |
| 70 | result(in_result), |
| 71 | error_code(in_error_code) {} |
| 72 | |
| 73 | Type type; |
| 74 | Result result; |
| 75 | int error_code; |
| 76 | }; |
| 77 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | class NoneType; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 79 | class OmahaRequestAction; |
| 80 | struct OmahaRequestParams; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | |
| 82 | template<> |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 83 | class ActionTraits<OmahaRequestAction> { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 85 | // Takes parameters on the input pipe. |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 86 | typedef NoneType InputObjectType; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 87 | // On UpdateCheck success, puts the Omaha response on output. Event |
| 88 | // requests do not have an output pipe. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 89 | typedef OmahaResponse OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 92 | class OmahaRequestAction : public Action<OmahaRequestAction>, |
| 93 | public HttpFetcherDelegate { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 94 | public: |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 95 | // The ctor takes in all the parameters that will be used for making |
| 96 | // the request to Omaha. For some of them we have constants that |
| 97 | // should be used. |
| 98 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 99 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 100 | // |
| 101 | // Takes ownership of the passed in OmahaEvent. If |event| is NULL, |
| 102 | // this is an UpdateCheck request, otherwise it's an Event request. |
| 103 | // Event requests always succeed. |
| 104 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 | // A good calling pattern is: |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 106 | // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 107 | // or |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 108 | // OmahaRequestAction(..., NULL, new WhateverHttpFetcher); |
| 109 | OmahaRequestAction(const OmahaRequestParams& params, |
| 110 | OmahaEvent* event, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 111 | HttpFetcher* http_fetcher); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 112 | virtual ~OmahaRequestAction(); |
| 113 | typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType; |
| 114 | typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 115 | void PerformAction(); |
| 116 | void TerminateProcessing(); |
| 117 | |
| 118 | // Debugging/logging |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 119 | static std::string StaticType() { return "OmahaRequestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 120 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 121 | |
| 122 | // Delegate methods (see http_fetcher.h) |
| 123 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 124 | const char* bytes, int length); |
| 125 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 126 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 127 | // Returns true if this is an Event request, false if it's an UpdateCheck. |
| 128 | bool IsEvent() const { return event_.get() != NULL; } |
| 129 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 130 | private: |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 131 | // These are data that are passed in the request to the Omaha server. |
| 132 | const OmahaRequestParams& params_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 133 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 134 | // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL. |
| 135 | scoped_ptr<OmahaEvent> event_; |
| 136 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 137 | // pointer to the HttpFetcher that does the http work |
| 138 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 139 | |
| 140 | // Stores the response from the omaha server |
| 141 | std::vector<char> response_buffer_; |
| 142 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 143 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } // namespace chromeos_update_engine |
| 147 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 148 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__ |