blob: f8dddc899bcbeb0b14a262477e7e402af68067b3 [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
Darin Petkov6a5b3222010-07-13 14:55:28 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_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
Darin Petkov6a5b3222010-07-13 14:55:28 -070020// The Omaha Request action makes a request to Omaha and can output
21// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
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
Darin Petkov6a5b3222010-07-13 14:55:28 -070029// This struct encapsulates the data Omaha gets for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000030// These strings in this struct should not be XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070031struct OmahaRequestParams {
32 OmahaRequestParams()
rspangler@google.com49fdf182009-10-10 00:57:34 +000033 : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070034 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.com49fdf182009-10-10 00:57:34 +000045 : 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 Reyes37c20322010-06-30 13:27:19 -070050 os_board(in_os_board),
rspangler@google.com49fdf182009-10-10 00:57:34 +000051 app_id(in_app_id),
52 app_version(in_app_version),
53 app_lang(in_app_lang),
Andrew de los Reyesf9714432010-05-04 10:21:23 -070054 app_track(in_app_track),
55 update_url(in_update_url) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
Andrew de los Reyesf9714432010-05-04 10:21:23 -070057 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 Reyes37c20322010-06-30 13:27:19 -070062 std::string os_board;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070063 std::string app_id;
64 std::string app_version;
65 std::string app_lang;
66 std::string app_track;
Darin Petkov6a5b3222010-07-13 14:55:28 -070067
Andrew de los Reyesf9714432010-05-04 10:21:23 -070068 std::string update_url;
rspangler@google.com49fdf182009-10-10 00:57:34 +000069
70 // Suggested defaults
71 static const char* const kAppId;
72 static const char* const kOsPlatform;
73 static const char* const kOsVersion;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070074 static const char* const kUpdateUrl;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075};
76
Darin Petkov6a5b3222010-07-13 14:55:28 -070077// This struct encapsulates the data Omaha's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000078// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070079struct OmahaResponse {
80 OmahaResponse()
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 : 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};
94COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
95
Darin Petkov6a5b3222010-07-13 14:55:28 -070096class OmahaRequestAction;
rspangler@google.com49fdf182009-10-10 00:57:34 +000097class NoneType;
98
99template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000102 // Takes parameters on the input pipe
Darin Petkov6a5b3222010-07-13 14:55:28 -0700103 typedef OmahaRequestParams InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 // On success, puts the output path on output
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106};
107
Darin Petkov6a5b3222010-07-13 14:55:28 -0700108class OmahaRequestAction : public Action<OmahaRequestAction>,
109 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 public:
111 // The ctor takes in all the parameters that will be used for
112 // making the request to Omaha. For some of them we have constants
113 // that should be used.
114 // Takes ownership of the passed in HttpFetcher. Useful for testing.
115 // A good calling pattern is:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116 // OmahaRequestAction(..., new WhateverHttpFetcher);
117 OmahaRequestAction(HttpFetcher* http_fetcher);
118 virtual ~OmahaRequestAction();
119 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
120 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121 void PerformAction();
122 void TerminateProcessing();
123
124 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700125 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000126 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127
128 // Delegate methods (see http_fetcher.h)
129 virtual void ReceivedBytes(HttpFetcher *fetcher,
130 const char* bytes, int length);
131 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
132
133 private:
134 // These are data that are passed in the request to the Omaha server
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 OmahaRequestParams params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
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 Petkov6a5b3222010-07-13 14:55:28 -0700143 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144};
145
146} // namespace chromeos_update_engine
147
Darin Petkov6a5b3222010-07-13 14:55:28 -0700148#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__