blob: 307798bd0539b8414cfd5ebcd2f8b4fdf65f525f [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,
58 };
59
60 enum Result {
61 kResultError = 0,
62 kResultSuccess = 1,
63 };
64
65 OmahaEvent()
66 : type(kTypeUnknown),
67 result(kResultError),
68 error_code(0) {}
69 OmahaEvent(Type in_type, Result in_result, int in_error_code)
70 : type(in_type),
71 result(in_result),
72 error_code(in_error_code) {}
73
74 Type type;
75 Result result;
76 int error_code;
77};
78
rspangler@google.com49fdf182009-10-10 00:57:34 +000079class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070080class OmahaRequestAction;
81struct OmahaRequestParams;
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
83template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070084class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000085 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070086 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070087 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070088 // On UpdateCheck success, puts the Omaha response on output. Event
89 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000091};
92
Darin Petkov6a5b3222010-07-13 14:55:28 -070093class OmahaRequestAction : public Action<OmahaRequestAction>,
94 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070096 // The ctor takes in all the parameters that will be used for making
97 // the request to Omaha. For some of them we have constants that
98 // should be used.
99 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700101 //
102 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
103 // this is an UpdateCheck request, otherwise it's an Event request.
104 // Event requests always succeed.
105 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700108 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700109 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
110 OmahaRequestAction(const OmahaRequestParams& params,
111 OmahaEvent* event,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700112 HttpFetcher* http_fetcher);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700113 virtual ~OmahaRequestAction();
114 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
115 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116 void PerformAction();
117 void TerminateProcessing();
118
119 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700120 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000121 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122
123 // Delegate methods (see http_fetcher.h)
124 virtual void ReceivedBytes(HttpFetcher *fetcher,
125 const char* bytes, int length);
126 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
127
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700128 // Returns true if this is an Event request, false if it's an UpdateCheck.
129 bool IsEvent() const { return event_.get() != NULL; }
130
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131 private:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700132 // These are data that are passed in the request to the Omaha server.
133 const OmahaRequestParams& params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700135 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
136 scoped_ptr<OmahaEvent> event_;
137
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138 // pointer to the HttpFetcher that does the http work
139 scoped_ptr<HttpFetcher> http_fetcher_;
140
141 // Stores the response from the omaha server
142 std::vector<char> response_buffer_;
143
Darin Petkov6a5b3222010-07-13 14:55:28 -0700144 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145};
146
147} // namespace chromeos_update_engine
148
Darin Petkov6a5b3222010-07-13 14:55:28 -0700149#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H__