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 | |
| 5 | #ifndef UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__ |
| 6 | #define UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__ |
| 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
| 11 | |
| 12 | #include <string> |
| 13 | |
| 14 | #include <curl/curl.h> |
| 15 | |
| 16 | #include "base/scoped_ptr.h" |
| 17 | #include "action.h" |
| 18 | #include "http_fetcher.h" |
| 19 | |
| 20 | // The Update Check action makes an update check request to Omaha and |
| 21 | // can output the response on the output ActionPipe. |
| 22 | |
| 23 | using std::string; |
| 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | // Encodes XML entities in a given string with libxml2. input must be |
| 28 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 29 | std::string XmlEncode(const std::string& input); |
| 30 | |
| 31 | // This struct encapsulates the data Omaha gets for the update check. |
| 32 | // These strings in this struct should not be XML escaped. |
| 33 | struct UpdateCheckParams { |
| 34 | UpdateCheckParams() |
| 35 | : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {} |
| 36 | UpdateCheckParams(const std::string& in_machine_id, |
| 37 | const std::string& in_user_id, |
| 38 | const std::string& in_os_platform, |
| 39 | const std::string& in_os_version, |
| 40 | const std::string& in_os_sp, |
| 41 | const std::string& in_app_id, |
| 42 | const std::string& in_app_version, |
| 43 | const std::string& in_app_lang, |
| 44 | const std::string& in_app_track) |
| 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), |
| 50 | app_id(in_app_id), |
| 51 | app_version(in_app_version), |
| 52 | app_lang(in_app_lang), |
| 53 | app_track(in_app_track) {} |
| 54 | |
| 55 | std::string machine_id; |
| 56 | std::string user_id; |
| 57 | std::string os_platform; |
| 58 | std::string os_version; |
| 59 | std::string os_sp; |
| 60 | std::string app_id; |
| 61 | std::string app_version; |
| 62 | std::string app_lang; |
| 63 | std::string app_track; |
| 64 | |
| 65 | // Suggested defaults |
| 66 | static const char* const kAppId; |
| 67 | static const char* const kOsPlatform; |
| 68 | static const char* const kOsVersion; |
| 69 | }; |
| 70 | |
| 71 | // This struct encapsulates the data Omaha returns for the update check. |
| 72 | // These strings in this struct are not XML escaped. |
| 73 | struct UpdateCheckResponse { |
| 74 | UpdateCheckResponse() |
| 75 | : update_exists(false), size(0), needs_admin(false), prompt(false) {} |
| 76 | // True iff there is an update to be downloaded. |
| 77 | bool update_exists; |
| 78 | |
| 79 | // These are only valid if update_exists is true: |
| 80 | std::string display_version; |
| 81 | std::string codebase; |
| 82 | std::string more_info_url; |
| 83 | std::string hash; |
| 84 | off_t size; |
| 85 | bool needs_admin; |
| 86 | bool prompt; |
| 87 | }; |
| 88 | COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit); |
| 89 | |
| 90 | class UpdateCheckAction; |
| 91 | class NoneType; |
| 92 | |
| 93 | template<> |
| 94 | class ActionTraits<UpdateCheckAction> { |
| 95 | public: |
| 96 | // Does not take an object for input |
| 97 | typedef NoneType InputObjectType; |
| 98 | // On success, puts the output path on output |
| 99 | typedef UpdateCheckResponse OutputObjectType; |
| 100 | }; |
| 101 | |
| 102 | class UpdateCheckAction : public Action<UpdateCheckAction>, |
| 103 | public HttpFetcherDelegate { |
| 104 | public: |
| 105 | // The ctor takes in all the parameters that will be used for |
| 106 | // making the request to Omaha. For some of them we have constants |
| 107 | // that should be used. |
| 108 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 109 | // A good calling pattern is: |
| 110 | // UpdateCheckAction(..., new WhateverHttpFetcher); |
| 111 | UpdateCheckAction(const UpdateCheckParams& params, |
| 112 | HttpFetcher* http_fetcher); |
| 113 | virtual ~UpdateCheckAction(); |
| 114 | typedef ActionTraits<UpdateCheckAction>::InputObjectType InputObjectType; |
| 115 | typedef ActionTraits<UpdateCheckAction>::OutputObjectType OutputObjectType; |
| 116 | void PerformAction(); |
| 117 | void TerminateProcessing(); |
| 118 | |
| 119 | // Debugging/logging |
| 120 | std::string Type() const { return "UpdateCheckAction"; } |
| 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 | |
| 127 | private: |
| 128 | // These are data that are passed in the request to the Omaha server |
| 129 | UpdateCheckParams params_; |
| 130 | |
| 131 | // pointer to the HttpFetcher that does the http work |
| 132 | scoped_ptr<HttpFetcher> http_fetcher_; |
| 133 | |
| 134 | // Stores the response from the omaha server |
| 135 | std::vector<char> response_buffer_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(UpdateCheckAction); |
| 138 | }; |
| 139 | |
| 140 | } // namespace chromeos_update_engine |
| 141 | |
| 142 | #endif // UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__ |