1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_UPDATER_STATE_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_UPDATER_STATE_H_
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "base/containers/flat_map.h"
#include "base/gtest_prod_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version.h"
#include "build/build_config.h"
#include "components/update_client/update_client_errors.h"
namespace component_updater {
class UpdaterState {
public:
using Attributes = base::flat_map<std::string, std::string>;
// Returns a map of items representing the state of an updater.
// If |is_machine| is true, this indicates that the updater state corresponds
// to the machine instance of the updater. Returns nullptr on
// the platforms and builds where this feature is not supported.
static Attributes GetState(bool is_machine);
~UpdaterState();
private:
FRIEND_TEST_ALL_PREFIXES(UpdaterStateTest, SerializeChromePerUser);
FRIEND_TEST_ALL_PREFIXES(UpdaterStateTest, SerializeChromium);
FRIEND_TEST_ALL_PREFIXES(UpdaterStateTest, UpdaterNamePerUser);
struct State {
State();
State(const State&);
State& operator=(const State&);
~State();
std::string updater_name;
base::Version updater_version;
base::Time last_autoupdate_started;
base::Time last_checked;
bool is_autoupdate_check_enabled = false;
int update_policy = 0;
update_client::CategorizedError last_update_check_error = {};
};
class StateReader {
public:
static std::unique_ptr<StateReader> Create(bool is_machine);
// Returns the state of the Chrome updater.
State Read(bool is_machine) const;
virtual ~StateReader() = default;
private:
virtual std::string GetUpdaterName() const = 0;
virtual base::Version GetUpdaterVersion(bool is_machine) const = 0;
virtual bool IsAutoupdateCheckEnabled() const = 0;
virtual base::Time GetUpdaterLastStartedAU(bool is_machine) const = 0;
virtual base::Time GetUpdaterLastChecked(bool is_machine) const = 0;
virtual int GetUpdatePolicy() const = 0;
virtual update_client::CategorizedError GetLastUpdateCheckError() const = 0;
};
#if BUILDFLAG(IS_MAC)
class StateReaderKeystone final : public StateReader {
private:
// Overrides for StateReader.
std::string GetUpdaterName() const override;
base::Version GetUpdaterVersion(bool is_machine) const override;
bool IsAutoupdateCheckEnabled() const override;
base::Time GetUpdaterLastStartedAU(bool is_machine) const override;
base::Time GetUpdaterLastChecked(bool is_machine) const override;
int GetUpdatePolicy() const override;
update_client::CategorizedError GetLastUpdateCheckError() const override;
};
#elif BUILDFLAG(IS_WIN)
class StateReaderOmaha final : public StateReader {
private:
// Overrides for StateReader.
std::string GetUpdaterName() const override;
base::Version GetUpdaterVersion(bool is_machine) const override;
bool IsAutoupdateCheckEnabled() const override;
base::Time GetUpdaterLastStartedAU(bool is_machine) const override;
base::Time GetUpdaterLastChecked(bool is_machine) const override;
int GetUpdatePolicy() const override;
update_client::CategorizedError GetLastUpdateCheckError() const override;
};
#endif
class StateReaderChromiumUpdater final : public StateReader {
public:
explicit StateReaderChromiumUpdater(base::Value::Dict parsed_json);
private:
// Overrides for StateReader.
std::string GetUpdaterName() const override;
base::Version GetUpdaterVersion(bool is_machine) const override;
bool IsAutoupdateCheckEnabled() const override;
base::Time GetUpdaterLastStartedAU(bool is_machine) const override;
base::Time GetUpdaterLastChecked(bool is_machine) const override;
int GetUpdatePolicy() const override;
update_client::CategorizedError GetLastUpdateCheckError() const override;
base::Time FindTimeKey(std::string_view key) const;
const base::Value::Dict parsed_json_;
};
explicit UpdaterState(bool is_machine);
// Builds the map of state attributes by serializing the state of this object.
Attributes Serialize() const;
static std::optional<State> ReadState(bool is_machine);
static std::string GetUpdaterName();
static base::Version GetUpdaterVersion(bool is_machine);
static bool IsAutoupdateCheckEnabled();
static base::Time GetUpdaterLastStartedAU(bool is_machine);
static base::Time GetUpdaterLastChecked(bool is_machine);
static int GetUpdatePolicy();
static std::string NormalizeTimeDelta(base::TimeDelta delta);
// True if the updater is installed per-machine.
bool is_machine_ = false;
std::optional<State> state_;
};
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_UPDATER_STATE_H_
|