1aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
2aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Copyright (C) 2012 The Android Open Source Project
3aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
4aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
5aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// you may not use this file except in compliance with the License.
6aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// You may obtain a copy of the License at
7aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
8aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
9aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
10aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Unless required by applicable law or agreed to in writing, software
11aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
12aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// See the License for the specific language governing permissions and
14aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// limitations under the License.
15aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo//
1649fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
17cf175a098081f3f0e9ca52d997a7ce1585c14c2dGilad Arnold#ifndef UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
18cf175a098081f3f0e9ca52d997a7ce1585c14c2dGilad Arnold#define UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
1949fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
2044cab30e0ee04b277e8463785ab069e9885a9f2dAlex Vakulenko#include <fcntl.h>
2149fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com#include <sys/stat.h>
2209e56d64202d2148b95008c5bd18cf719ec0f40cAndrew de los Reyes#include <sys/types.h>
2349fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
2402f7c1dee242f490143791dbb73fa23fa3007cfaBen Chan#include <memory>
2549fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com#include <string>
266f6ea00aa8c4cf54b6842be32ca1226854c24f78Jay Srinivasan#include <vector>
2749fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
28c1c17b4ed6a3896b6343e737fd89682fa0c8436bAlex Deymo#include <gtest/gtest_prod.h>  // for FRIEND_TEST
29c1c17b4ed6a3896b6343e737fd89682fa0c8436bAlex Deymo
303f39d5cc753905874d8d93bef94f857b8808f19eAlex Vakulenko#include <brillo/secure_blob.h>
3149fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com#include <curl/curl.h>
3249fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
3339910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/common/action.h"
3439910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/common/http_fetcher.h"
35082628869ed3ee3e173c08354d3fc40cdb7df2c0Jay Srinivasan#include "update_engine/omaha_response.h"
36c1c17b4ed6a3896b6343e737fd89682fa0c8436bAlex Deymo#include "update_engine/system_state.h"
3749fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
386a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov// The Omaha Request action makes a request to Omaha and can output
396a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov// the response on the output ActionPipe.
4049fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
4149fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.comnamespace chromeos_update_engine {
4249fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
43b0d74eb91183eef5955974c515e40c994098372fAlex Deymo// Encodes XML entities in a given string. Input must be ASCII-7 valid. If
44b0d74eb91183eef5955974c515e40c994098372fAlex Deymo// the input is invalid, the default value is used instead.
45b0d74eb91183eef5955974c515e40c994098372fAlex Deymostd::string XmlEncodeWithDefault(const std::string& input,
46b0d74eb91183eef5955974c515e40c994098372fAlex Deymo                                 const std::string& default_value);
47b0d74eb91183eef5955974c515e40c994098372fAlex Deymo
48b0d74eb91183eef5955974c515e40c994098372fAlex Deymo// Escapes text so it can be included as character data and attribute
49b0d74eb91183eef5955974c515e40c994098372fAlex Deymo// values. The |input| string must be valid ASCII-7, no UTF-8 supported.
50b0d74eb91183eef5955974c515e40c994098372fAlex Deymo// Returns whether the |input| was valid and escaped properly in |output|.
51b0d74eb91183eef5955974c515e40c994098372fAlex Deymobool XmlEncode(const std::string& input, std::string* output);
5249fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
530dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov// This struct encapsulates the Omaha event information. For a
540dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov// complete list of defined event types and results, see
550dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov// http://code.google.com/p/omaha/wiki/ServerProtocol#event
560dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkovstruct OmahaEvent {
5756d5aa471205bf2271219252ec94b5f0c986a68bJay Srinivasan  // The Type values correspond to EVENT_TYPE values of Omaha.
580dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  enum Type {
590dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kTypeUnknown = 0,
600dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kTypeDownloadComplete = 1,
610dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kTypeInstallComplete = 2,
620dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kTypeUpdateComplete = 3,
638c2980e8b973a21468c9fece0490cd440afc0660Darin Petkov    kTypeUpdateDownloadStarted = 13,
648c2980e8b973a21468c9fece0490cd440afc0660Darin Petkov    kTypeUpdateDownloadFinished = 14,
659fded1eee295295d33da477f00d9c9a240623e91Alex Deymo    // Chromium OS reserved type sent after the first reboot following an update
669fded1eee295295d33da477f00d9c9a240623e91Alex Deymo    // completed.
679fded1eee295295d33da477f00d9c9a240623e91Alex Deymo    kTypeRebootedAfterUpdate = 54,
680dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  };
690dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
7056d5aa471205bf2271219252ec94b5f0c986a68bJay Srinivasan  // The Result values correspond to EVENT_RESULT values of Omaha.
710dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  enum Result {
720dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kResultError = 0,
730dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov    kResultSuccess = 1,
74d2779df63aaad8b65fc5d4badee7dbc9bed7f2b6Alex Vakulenko    kResultUpdateDeferred = 9,  // When we ignore/defer updates due to policy.
750dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  };
760dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
770dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  OmahaEvent()
780dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov      : type(kTypeUnknown),
790dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov        result(kResultError),
80d1c4d2dd3daed1d507038046c0355fbafb85260cGilad Arnold        error_code(ErrorCode::kError) {}
81e17f86bae4299882232d3e6858ada68692e80501Darin Petkov  explicit OmahaEvent(Type in_type)
82e17f86bae4299882232d3e6858ada68692e80501Darin Petkov      : type(in_type),
83e17f86bae4299882232d3e6858ada68692e80501Darin Petkov        result(kResultSuccess),
84d1c4d2dd3daed1d507038046c0355fbafb85260cGilad Arnold        error_code(ErrorCode::kSuccess) {}
85a99981fda75fe0b17e96c700e3ddc93eca1cebe5David Zeuthen  OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
860dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov      : type(in_type),
870dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov        result(in_result),
880dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov        error_code(in_error_code) {}
890dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
900dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  Type type;
910dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  Result result;
92a99981fda75fe0b17e96c700e3ddc93eca1cebe5David Zeuthen  ErrorCode error_code;
930dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov};
940dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
9549fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.comclass NoneType;
96a4a8a8ccc2d9e0285728ed247b43f09433e63323Darin Petkovclass OmahaRequestAction;
97a178e5e6efc0849e286b9275d3b052ada6b1a43fYunlian Jiangclass OmahaRequestParams;
981cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkovclass PrefsInterface;
9949fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
100e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen// This struct is declared in the .cc file.
101e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthenstruct OmahaParserData;
102e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen
10349fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.comtemplate<>
1046a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkovclass ActionTraits<OmahaRequestAction> {
10549fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com public:
1060dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // Takes parameters on the input pipe.
107a4a8a8ccc2d9e0285728ed247b43f09433e63323Darin Petkov  typedef NoneType InputObjectType;
1080dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // On UpdateCheck success, puts the Omaha response on output. Event
1090dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // requests do not have an output pipe.
1106a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov  typedef OmahaResponse OutputObjectType;
11149fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com};
11249fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
1136a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkovclass OmahaRequestAction : public Action<OmahaRequestAction>,
1146a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov                           public HttpFetcherDelegate {
11549fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com public:
1161cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  static const int kNeverPinged = -1;
1171cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  static const int kPingTimeJump = -2;
1182b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  // We choose this value of 10 as a heuristic for a work day in trying
1192b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  // each URL, assuming we check roughly every 45 mins. This is a good time to
1202b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  // wait - neither too long nor too little - so we don't give up the preferred
1212b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  // URLs that appear earlier in list too quickly before moving on to the
1222b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  // fallback ones.
1232b5a0f065187fd19179e3809148dbfc376ada7a0Jay Srinivasan  static const int kDefaultMaxFailureCountPerUrl = 10;
1241cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov
125480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // These are the possible outcome upon checking whether we satisfied
126480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // the wall-clock-based-wait.
127480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  enum WallClockWaitResult {
128480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan    kWallClockWaitNotSatisfied,
129480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan    kWallClockWaitDoneButUpdateCheckWaitRequired,
130480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan    kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
131480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  };
132480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan
1330dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // The ctor takes in all the parameters that will be used for making
1340dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // the request to Omaha. For some of them we have constants that
1350dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // should be used.
1360dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  //
13749fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // Takes ownership of the passed in HttpFetcher. Useful for testing.
1380dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  //
13988b591f24cb3f94f982d7024c2e8ed25c2cc26a2Alex Vakulenko  // Takes ownership of the passed in OmahaEvent. If |event| is null,
1400dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // this is an UpdateCheck request, otherwise it's an Event request.
1410dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // Event requests always succeed.
1420dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  //
14349fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // A good calling pattern is:
144a4a8a8ccc2d9e0285728ed247b43f09433e63323Darin Petkov  // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
1450dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // or
14688b591f24cb3f94f982d7024c2e8ed25c2cc26a2Alex Vakulenko  // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
1476f6ea00aa8c4cf54b6842be32ca1226854c24f78Jay Srinivasan  OmahaRequestAction(SystemState* system_state,
148a4a8a8ccc2d9e0285728ed247b43f09433e63323Darin Petkov                     OmahaEvent* event,
149c1c17b4ed6a3896b6343e737fd89682fa0c8436bAlex Deymo                     std::unique_ptr<HttpFetcher> http_fetcher,
150116fda3221ff3df037ea1feb271883c87644c839Thieu Le                     bool ping_only);
151610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  ~OmahaRequestAction() override;
1526a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov  typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
1536a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov  typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
154610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  void PerformAction() override;
155610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  void TerminateProcessing() override;
156610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  void ActionCompleted(ErrorCode code) override;
15749fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
1581023a6029771fb8dea867e14193df8e58a59a662Darin Petkov  int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
1591023a6029771fb8dea867e14193df8e58a59a662Darin Petkov
16049fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // Debugging/logging
1616a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov  static std::string StaticType() { return "OmahaRequestAction"; }
162610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  std::string Type() const override { return StaticType(); }
16349fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
16449fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // Delegate methods (see http_fetcher.h)
165610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  void ReceivedBytes(HttpFetcher *fetcher,
166f68bbbc952aa9a71898e4939b5f36187fa564a50Alex Vakulenko                     const void* bytes, size_t length) override;
167610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo
168610277efc6f7e5239158dfa4bb3b1021804326e0Alex Deymo  void TransferComplete(HttpFetcher *fetcher, bool successful) override;
16949fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
1700dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov  // Returns true if this is an Event request, false if it's an UpdateCheck.
17188b591f24cb3f94f982d7024c2e8ed25c2cc26a2Alex Vakulenko  bool IsEvent() const { return event_.get() != nullptr; }
1720dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
17349fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com private:
174e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo  FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
175e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo  FRIEND_TEST(OmahaRequestActionTest,
176e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo              GetInstallDateWhenOOBECompletedWithInvalidDate);
177e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo  FRIEND_TEST(OmahaRequestActionTest,
178e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo              GetInstallDateWhenOOBECompletedWithValidDate);
179e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo  FRIEND_TEST(OmahaRequestActionTest,
180e1e3afe30a214b3661a36474c1448f520662f33cAlex Deymo              GetInstallDateWhenOOBECompletedDateChanges);
181639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
182639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // Enumeration used in PersistInstallDate().
183639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  enum InstallDateProvisioningSource {
184639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen    kProvisionedFromOmahaResponse,
185639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen    kProvisionedFromOOBEMarker,
186639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
187639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen    // kProvisionedMax is the count of the number of enums above. Add
188639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen    // any new enums above this line only.
189639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen    kProvisionedMax
190639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  };
191639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
192639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // Gets the install date, expressed as the number of PST8PDT
193639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // calendar weeks since January 1st 2007, times seven. Returns -1 if
194639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // unknown. See http://crbug.com/336838 for details about this value.
195639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  static int GetInstallDate(SystemState* system_state);
196639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
197639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // Parses the Omaha Response in |doc| and sets the
198639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // |install_date_days| field of |output_object| to the value of the
199639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // elapsed_days attribute of the daystart element. Returns True if
200639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // the value was set, False if it wasn't found.
201e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  static bool ParseInstallDate(OmahaParserData* parser_data,
202639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen                               OmahaResponse* output_object);
203639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
204639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // Returns True if the kPrefsInstallDateDays state variable is set,
205639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // False otherwise.
206639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  static bool HasInstallDate(SystemState *system_state);
207639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
208639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // Writes |install_date_days| into the kPrefsInstallDateDays state
209639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // variable and emits an UMA stat for the |source| used. Returns
210639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  // True if the value was written, False if an error occurred.
211639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen  static bool PersistInstallDate(SystemState *system_state,
212639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen                                 int install_date_days,
213639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen                                 InstallDateProvisioningSource source);
214639aa36fc7e27ba400402cd7a32b091f555783a6David Zeuthen
2158e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo  // Persist the new cohort* value received in the XML file in the |prefs_key|
2168e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo  // preference file. If the |new_value| is empty, the currently stored value
2178e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo  // will be deleted. Don't call this function with an empty |new_value| if the
2188e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo  // value was not set in the XML, since that would delete the stored value.
2198e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo  bool PersistCohortData(const std::string& prefs_key,
2208e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo                         const std::string& new_value);
2218e18f931f634d7c3b3a5fcbfc057e79fa9d6fb80Alex Deymo
2221cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // If this is an update check request, initializes
2231cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // |ping_active_days_| and |ping_roll_call_days_| to values that may
2241cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // be sent as pings to Omaha.
2251cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  void InitPingDays();
2261cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov
22784c763cffce6778711792944387fadb760c55c8dDarin Petkov  // Based on the persistent preference store values, calculates the
2281cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // number of days since the last ping sent for |key|.
2291cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  int CalculatePingDays(const std::string& key);
2301cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov
231ebbe7ef75279183ba3cf055158dbbb3b3b605e0eAlex Deymo  // Returns whether we have "active_days" or "roll_call_days" ping values to
232ebbe7ef75279183ba3cf055158dbbb3b3b605e0eAlex Deymo  // send to Omaha and thus we should include them in the response.
233ebbe7ef75279183ba3cf055158dbbb3b3b605e0eAlex Deymo  bool ShouldPing() const;
234ebbe7ef75279183ba3cf055158dbbb3b3b605e0eAlex Deymo
235480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // Returns true if the download of a new update should be deferred.
236480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // False if the update can be downloaded.
23723b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  bool ShouldDeferDownload(OmahaResponse* output_object);
238480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan
239480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // Returns true if the basic wall-clock-based waiting period has been
240480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // satisfied based on the scattering policy setting. False otherwise.
241480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // If true, it also indicates whether the additional update-check-count-based
242480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // waiting period also needs to be satisfied before the download can begin.
243480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  WallClockWaitResult IsWallClockBasedWaitingSatisfied(
24423b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan      OmahaResponse* output_object);
245480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan
246480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // Returns true if the update-check-count-based waiting period has been
247480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // satisfied. False otherwise.
24823b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  bool IsUpdateCheckCountBasedWaitingSatisfied();
24923b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan
25023b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // Parses the response from Omaha that's available in |doc| using the other
25123b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // helper methods below and populates the |output_object| with the relevant
25223b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // values. Returns true if we should continue the parsing.  False otherwise,
25323b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // in which case it sets any error code using |completer|.
254e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  bool ParseResponse(OmahaParserData* parser_data,
25523b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                     OmahaResponse* output_object,
25623b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                     ScopedActionCompleter* completer);
25723b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan
25823b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // Parses the status property in the given update_check_node and populates
25923b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // |output_object| if valid. Returns true if we should continue the parsing.
26023b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // False otherwise, in which case it sets any error code using |completer|.
261e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  bool ParseStatus(OmahaParserData* parser_data,
26223b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                   OmahaResponse* output_object,
26323b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                   ScopedActionCompleter* completer);
26423b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan
26523b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // Parses the URL nodes in the given XML document and populates
26623b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // |output_object| if valid. Returns true if we should continue the parsing.
26723b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // False otherwise, in which case it sets any error code using |completer|.
268e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  bool ParseUrls(OmahaParserData* parser_data,
26923b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                 OmahaResponse* output_object,
27023b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                 ScopedActionCompleter* completer);
27123b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan
27223b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // Parses the package node in the given XML document and populates
27323b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // |output_object| if valid. Returns true if we should continue the parsing.
27423b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // False otherwise, in which case it sets any error code using |completer|.
275e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  bool ParsePackage(OmahaParserData* parser_data,
27623b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                    OmahaResponse* output_object,
27723b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                    ScopedActionCompleter* completer);
27823b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan
27923b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // Parses the other parameters in the given XML document and populates
28023b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // |output_object| if valid. Returns true if we should continue the parsing.
28123b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan  // False otherwise, in which case it sets any error code using |completer|.
282e8ed86367d92d1f30f340ad28e841846ae58bc61David Zeuthen  bool ParseParams(OmahaParserData* parser_data,
28323b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                   OmahaResponse* output_object,
28423b92a52e8781d68c451d6cd5e67aab1e5f82264Jay Srinivasan                   ScopedActionCompleter* completer);
285480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan
2868f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  // Called by TransferComplete() to complete processing, either
2878f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  // asynchronously after looking up resources via p2p or directly.
2888f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  void CompleteProcessing();
2898f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen
2908f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  // Helper to asynchronously look up payload on the LAN.
2918f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  void LookupPayloadViaP2P(const OmahaResponse& response);
2928f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen
2938f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  // Callback used by LookupPayloadViaP2P().
2948f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen  void OnLookupPayloadViaP2PCompleted(const std::string& url);
2958f191b22a1a1ab2b803d65ee488729206e648695David Zeuthen
29677f79e876a77796fc248d099b6574f05bd23c954Chris Sosa  // Returns true if the current update should be ignored.
29777f79e876a77796fc248d099b6574f05bd23c954Chris Sosa  bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
29877f79e876a77796fc248d099b6574f05bd23c954Chris Sosa
29977f79e876a77796fc248d099b6574f05bd23c954Chris Sosa  // Returns true if updates are allowed over the current type of connection.
30077f79e876a77796fc248d099b6574f05bd23c954Chris Sosa  // False otherwise.
30177f79e876a77796fc248d099b6574f05bd23c954Chris Sosa  bool IsUpdateAllowedOverCurrentConnection() const;
30277f79e876a77796fc248d099b6574f05bd23c954Chris Sosa
3036f6ea00aa8c4cf54b6842be32ca1226854c24f78Jay Srinivasan  // Global system context.
3046f6ea00aa8c4cf54b6842be32ca1226854c24f78Jay Srinivasan  SystemState* system_state_;
3051cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov
306480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  // Contains state that is relevant in the processing of the Omaha request.
307480ddfa079ebd01ed87e495332dec121d9ae781fJay Srinivasan  OmahaRequestParams* params_;
30849fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
30988b591f24cb3f94f982d7024c2e8ed25c2cc26a2Alex Vakulenko  // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
31002f7c1dee242f490143791dbb73fa23fa3007cfaBen Chan  std::unique_ptr<OmahaEvent> event_;
3110dc8e9a73fc3179a67a72ab72ceb2bc6540949bfDarin Petkov
31249fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // pointer to the HttpFetcher that does the http work
31302f7c1dee242f490143791dbb73fa23fa3007cfaBen Chan  std::unique_ptr<HttpFetcher> http_fetcher_;
31449fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
315116fda3221ff3df037ea1feb271883c87644c839Thieu Le  // If true, only include the <ping> element in the request.
316116fda3221ff3df037ea1feb271883c87644c839Thieu Le  bool ping_only_;
317116fda3221ff3df037ea1feb271883c87644c839Thieu Le
31849fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com  // Stores the response from the omaha server
3193f39d5cc753905874d8d93bef94f857b8808f19eAlex Vakulenko  brillo::Blob response_buffer_;
32049fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
3211cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // Initialized by InitPingDays to values that may be sent to Omaha
3221cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // as part of a ping message. Note that only positive values and -1
3231cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  // are sent to Omaha.
3241cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  int ping_active_days_;
3251cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov  int ping_roll_call_days_;
3261cbd78ffe68039a5781c3434816e03e64033dc0bDarin Petkov
3276a5b3229b44c1f81ee153829e9b501e547f29926Darin Petkov  DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
32849fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com};
32949fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
33049fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com}  // namespace chromeos_update_engine
33149fdf1889b965be25f929eeebc5b60cd40b9043rspangler@google.com
332cf175a098081f3f0e9ca52d997a7ce1585c14c2dGilad Arnold#endif  // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
333