blob: 831039dfd1d37218a40cdfd95bf47ced61270905 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07009#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include <fcntl.h>
11
12#include <string>
13
14#include <curl/curl.h>
15
16#include "base/scoped_ptr.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017#include "update_engine/action.h"
18#include "update_engine/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
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.com49fdf182009-10-10 00:57:34 +000023namespace 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.
27std::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.
31struct 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,
Andrew de los Reyesf9714432010-05-04 10:21:23 -070042 const std::string& in_app_track,
43 const std::string& in_update_url)
rspangler@google.com49fdf182009-10-10 00:57:34 +000044 : machine_id(in_machine_id),
45 user_id(in_user_id),
46 os_platform(in_os_platform),
47 os_version(in_os_version),
48 os_sp(in_os_sp),
49 app_id(in_app_id),
50 app_version(in_app_version),
51 app_lang(in_app_lang),
Andrew de los Reyesf9714432010-05-04 10:21:23 -070052 app_track(in_app_track),
53 update_url(in_update_url) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
Andrew de los Reyesf9714432010-05-04 10:21:23 -070055 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 std::string update_url;
rspangler@google.com49fdf182009-10-10 00:57:34 +000066
67 // Suggested defaults
68 static const char* const kAppId;
69 static const char* const kOsPlatform;
70 static const char* const kOsVersion;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070071 static const char* const kUpdateUrl;
rspangler@google.com49fdf182009-10-10 00:57:34 +000072};
73
74// This struct encapsulates the data Omaha returns for the update check.
75// These strings in this struct are not XML escaped.
76struct UpdateCheckResponse {
77 UpdateCheckResponse()
78 : update_exists(false), size(0), needs_admin(false), prompt(false) {}
79 // True iff there is an update to be downloaded.
80 bool update_exists;
81
82 // These are only valid if update_exists is true:
83 std::string display_version;
84 std::string codebase;
85 std::string more_info_url;
86 std::string hash;
87 off_t size;
88 bool needs_admin;
89 bool prompt;
90};
91COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
92
93class UpdateCheckAction;
94class NoneType;
95
96template<>
97class ActionTraits<UpdateCheckAction> {
98 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000099 // Takes parameters on the input pipe
100 typedef UpdateCheckParams InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 // On success, puts the output path on output
102 typedef UpdateCheckResponse OutputObjectType;
103};
104
105class UpdateCheckAction : public Action<UpdateCheckAction>,
106 public HttpFetcherDelegate {
107 public:
108 // The ctor takes in all the parameters that will be used for
109 // making the request to Omaha. For some of them we have constants
110 // that should be used.
111 // Takes ownership of the passed in HttpFetcher. Useful for testing.
112 // A good calling pattern is:
113 // UpdateCheckAction(..., new WhateverHttpFetcher);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000114 UpdateCheckAction(HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 virtual ~UpdateCheckAction();
116 typedef ActionTraits<UpdateCheckAction>::InputObjectType InputObjectType;
117 typedef ActionTraits<UpdateCheckAction>::OutputObjectType OutputObjectType;
118 void PerformAction();
119 void TerminateProcessing();
120
121 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000122 static std::string StaticType() { return "UpdateCheckAction"; }
123 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124
125 // Delegate methods (see http_fetcher.h)
126 virtual void ReceivedBytes(HttpFetcher *fetcher,
127 const char* bytes, int length);
128 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
129
130 private:
131 // These are data that are passed in the request to the Omaha server
132 UpdateCheckParams params_;
133
134 // pointer to the HttpFetcher that does the http work
135 scoped_ptr<HttpFetcher> http_fetcher_;
136
137 // Stores the response from the omaha server
138 std::vector<char> response_buffer_;
139
140 DISALLOW_COPY_AND_ASSIGN(UpdateCheckAction);
141};
142
143} // namespace chromeos_update_engine
144
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000145#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_ACTION_H__