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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_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 | |
| 20 | // The Update Check action makes an update check request to Omaha and |
| 21 | // can output the response on the output ActionPipe. |
| 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 | |
| 29 | // This struct encapsulates the data Omaha gets for the update check. |
| 30 | // These strings in this struct should not be XML escaped. |
| 31 | struct UpdateCheckParams { |
| 32 | UpdateCheckParams() |
| 33 | : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {} |
| 34 | UpdateCheckParams(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_app_id, |
| 40 | const std::string& in_app_version, |
| 41 | const std::string& in_app_lang, |
| 42 | const std::string& in_app_track) |
| 43 | : machine_id(in_machine_id), |
| 44 | user_id(in_user_id), |
| 45 | os_platform(in_os_platform), |
| 46 | os_version(in_os_version), |
| 47 | os_sp(in_os_sp), |
| 48 | app_id(in_app_id), |
| 49 | app_version(in_app_version), |
| 50 | app_lang(in_app_lang), |
| 51 | app_track(in_app_track) {} |
| 52 | |
| 53 | std::string machine_id; |
| 54 | std::string user_id; |
| 55 | std::string os_platform; |
| 56 | std::string os_version; |
| 57 | std::string os_sp; |
| 58 | std::string app_id; |
| 59 | std::string app_version; |
| 60 | std::string app_lang; |
| 61 | std::string app_track; |
| 62 | |
| 63 | // Suggested defaults |
| 64 | static const char* const kAppId; |
| 65 | static const char* const kOsPlatform; |
| 66 | static const char* const kOsVersion; |
| 67 | }; |
| 68 | |
| 69 | // This struct encapsulates the data Omaha returns for the update check. |
| 70 | // These strings in this struct are not XML escaped. |
| 71 | struct UpdateCheckResponse { |
| 72 | UpdateCheckResponse() |
| 73 | : update_exists(false), size(0), needs_admin(false), prompt(false) {} |
| 74 | // True iff there is an update to be downloaded. |
| 75 | bool update_exists; |
| 76 | |
| 77 | // These are only valid if update_exists is true: |
| 78 | std::string display_version; |
| 79 | std::string codebase; |
| 80 | std::string more_info_url; |
| 81 | std::string hash; |
| 82 | off_t size; |
| 83 | bool needs_admin; |
| 84 | bool prompt; |
| 85 | }; |
| 86 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 87 | |
| 88 | class UpdateCheckAction; |
| 89 | class NoneType; |
| 90 | |
| 91 | template<> |
| 92 | class ActionTraits<UpdateCheckAction> { |
| 93 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 94 | // Takes parameters on the input pipe |
| 95 | typedef UpdateCheckParams InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | // On success, puts the output path on output |
| 97 | typedef UpdateCheckResponse OutputObjectType; |
| 98 | }; |
| 99 | |
| 100 | class UpdateCheckAction : public Action<UpdateCheckAction>, |
| 101 | public HttpFetcherDelegate { |
| 102 | public: |
| 103 | // The ctor takes in all the parameters that will be used for |
| 104 | // making the request to Omaha. For some of them we have constants |
| 105 | // that should be used. |
| 106 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 107 | // A good calling pattern is: |
| 108 | // UpdateCheckAction(..., new WhateverHttpFetcher); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 109 | UpdateCheckAction(HttpFetcher* http_fetcher); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | virtual ~UpdateCheckAction(); |
| 111 | typedef ActionTraits<UpdateCheckAction>::InputObjectType InputObjectType; |
| 112 | typedef ActionTraits<UpdateCheckAction>::OutputObjectType OutputObjectType; |
| 113 | void PerformAction(); |
| 114 | void TerminateProcessing(); |
| 115 | |
| 116 | // Debugging/logging |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 117 | static std::string StaticType() { return "UpdateCheckAction"; } |
| 118 | std::string Type() const { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 119 | |
| 120 | // Delegate methods (see http_fetcher.h) |
| 121 | virtual void ReceivedBytes(HttpFetcher *fetcher, |
| 122 | const char* bytes, int length); |
| 123 | virtual void TransferComplete(HttpFetcher *fetcher, bool successful); |
| 124 | |
| 125 | private: |
| 126 | // These are data that are passed in the request to the Omaha server |
| 127 | UpdateCheckParams params_; |
| 128 | |
| 129 | // pointer to the HttpFetcher that does the http work |
| 130 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 131 | |
| 132 | // Stores the response from the omaha server |
| 133 | std::vector<char> response_buffer_; |
| 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(UpdateCheckAction); |
| 136 | }; |
| 137 | |
| 138 | } // namespace chromeos_update_engine |
| 139 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 140 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__ |