blob: 7533d14b717582e6cf711ec03cfd59a8e6078ae0 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// 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's response for the request.
rspangler@google.com49fdf182009-10-10 00:57:34 +000030// These strings in this struct are not XML escaped.
Darin Petkov6a5b3222010-07-13 14:55:28 -070031struct OmahaResponse {
32 OmahaResponse()
rspangler@google.com49fdf182009-10-10 00:57:34 +000033 : 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;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070045 bool is_delta;
rspangler@google.com49fdf182009-10-10 00:57:34 +000046};
47COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64bit);
48
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070049// This struct encapsulates the Omaha event information. For a
50// complete list of defined event types and results, see
51// http://code.google.com/p/omaha/wiki/ServerProtocol#event
52struct OmahaEvent {
53 enum Type {
54 kTypeUnknown = 0,
55 kTypeDownloadComplete = 1,
56 kTypeInstallComplete = 2,
57 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070058 kTypeUpdateDownloadStarted = 13,
59 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070060 };
61
62 enum Result {
63 kResultError = 0,
64 kResultSuccess = 1,
65 };
66
67 OmahaEvent()
68 : type(kTypeUnknown),
69 result(kResultError),
Darin Petkove17f86b2010-07-20 09:12:01 -070070 error_code(kActionCodeError) {}
71 explicit OmahaEvent(Type in_type)
72 : type(in_type),
73 result(kResultSuccess),
74 error_code(kActionCodeSuccess) {}
75 OmahaEvent(Type in_type, Result in_result, ActionExitCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070076 : type(in_type),
77 result(in_result),
78 error_code(in_error_code) {}
79
80 Type type;
81 Result result;
Darin Petkove17f86b2010-07-20 09:12:01 -070082 ActionExitCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070083};
84
rspangler@google.com49fdf182009-10-10 00:57:34 +000085class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070086class OmahaRequestAction;
87struct OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070088class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000089
90template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070091class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070093 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070094 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070095 // On UpdateCheck success, puts the Omaha response on output. Event
96 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070097 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000098};
99
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100class OmahaRequestAction : public Action<OmahaRequestAction>,
101 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700103 static const int kNeverPinged = -1;
104 static const int kPingTimeJump = -2;
105
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700106 // The ctor takes in all the parameters that will be used for making
107 // the request to Omaha. For some of them we have constants that
108 // should be used.
109 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700111 //
112 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
113 // this is an UpdateCheck request, otherwise it's an Event request.
114 // Event requests always succeed.
115 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700117 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700118 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700119 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700120 OmahaRequestAction(PrefsInterface* prefs,
121 const OmahaRequestParams& params,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700122 OmahaEvent* event,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700123 HttpFetcher* http_fetcher);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700124 virtual ~OmahaRequestAction();
125 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
126 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127 void PerformAction();
128 void TerminateProcessing();
129
Darin Petkov1023a602010-08-30 13:47:51 -0700130 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
131
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700133 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000134 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135
136 // Delegate methods (see http_fetcher.h)
137 virtual void ReceivedBytes(HttpFetcher *fetcher,
138 const char* bytes, int length);
139 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
140
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700141 // Returns true if this is an Event request, false if it's an UpdateCheck.
142 bool IsEvent() const { return event_.get() != NULL; }
143
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144 private:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700145 // If this is an update check request, initializes
146 // |ping_active_days_| and |ping_roll_call_days_| to values that may
147 // be sent as pings to Omaha.
148 void InitPingDays();
149
Darin Petkov84c763c2010-07-29 16:27:58 -0700150 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700151 // number of days since the last ping sent for |key|.
152 int CalculatePingDays(const std::string& key);
153
154 // Access to the preferences store.
155 PrefsInterface* prefs_;
156
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700157 // These are data that are passed in the request to the Omaha server.
158 const OmahaRequestParams& params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000159
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700160 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
161 scoped_ptr<OmahaEvent> event_;
162
rspangler@google.com49fdf182009-10-10 00:57:34 +0000163 // pointer to the HttpFetcher that does the http work
164 scoped_ptr<HttpFetcher> http_fetcher_;
165
166 // Stores the response from the omaha server
167 std::vector<char> response_buffer_;
168
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700169 // Initialized by InitPingDays to values that may be sent to Omaha
170 // as part of a ping message. Note that only positive values and -1
171 // are sent to Omaha.
172 int ping_active_days_;
173 int ping_roll_call_days_;
174
Darin Petkov6a5b3222010-07-13 14:55:28 -0700175 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176};
177
178} // namespace chromeos_update_engine
179
Darin Petkov6a5b3222010-07-13 14:55:28 -0700180#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__