File: updater_state.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (238 lines) | stat: -rw-r--r-- 8,206 bytes parent folder | download | duplicates (5)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// 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.

#include "chrome/browser/component_updater/updater_state.h"

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

#include "base/check.h"
#include "base/enterprise_util.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/values_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/util/util.h"
#include "components/update_client/persisted_data.h"
#include "components/update_client/update_client_errors.h"

namespace component_updater {
namespace {

// These literals must not be changed since they affect the forward and
// backward compatibility with //chrome/updater.
constexpr char kUpdaterPrefsActiveVersion[] = "active_version";
constexpr char kUpdaterPrefsLastChecked[] = "last_checked";
constexpr char kUpdaterPrefsLastStarted[] = "last_started";
}  // namespace

UpdaterState::State::State() = default;
UpdaterState::State::State(const UpdaterState::State&) = default;
UpdaterState::State& UpdaterState::State::operator=(
    const UpdaterState::State&) = default;
UpdaterState::State::~State() = default;

std::unique_ptr<UpdaterState::StateReader> UpdaterState::StateReader::Create(
    bool is_machine) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  if (std::unique_ptr<StateReader> state_reader_chromium_updater =
          [is_machine]() -> std::unique_ptr<StateReader> {
        // Create a `StateReaderChromiumUpdater` instance only if a prefs.json
        // file for the updater can be found and parsed successfully.
        const std::optional<base::FilePath> global_prefs_dir =
            updater::GetInstallDirectory(is_machine
                                             ? updater::UpdaterScope::kSystem
                                             : updater::UpdaterScope::kUser);
        if (!global_prefs_dir)
          return nullptr;
        std::string contents;
        static constexpr char kUpdaterPrefsFilename[] = "prefs.json";
        static constexpr int kMaxPrefsFileSize = 0x20000;  // 128KiB.
        if (!base::ReadFileToStringWithMaxSize(
                global_prefs_dir->AppendASCII(kUpdaterPrefsFilename), &contents,
                kMaxPrefsFileSize)) {
          return nullptr;
        }
        std::optional<base::Value::Dict> parsed_json =
            base::JSONReader::ReadDict(contents);
        return parsed_json ? std::make_unique<StateReaderChromiumUpdater>(
                                 std::move(*parsed_json))
                           : nullptr;
      }()) {
    return state_reader_chromium_updater;
  }
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_MAC)
  return std::make_unique<UpdaterState::StateReaderKeystone>();
#elif BUILDFLAG(IS_WIN)
  return std::make_unique<UpdaterState::StateReaderOmaha>();
#else
  return nullptr;
#endif  // IS_MAC

#else
  return nullptr;
#endif  // GOOGLE_CHROME_BRANDING
}

UpdaterState::StateReaderChromiumUpdater::StateReaderChromiumUpdater(
    base::Value::Dict parsed_json)
    : parsed_json_(std::move(parsed_json)) {}

base::Time UpdaterState::StateReaderChromiumUpdater::FindTimeKey(
    std::string_view key) const {
  return base::ValueToTime(parsed_json_.Find(key)).value_or(base::Time());
}

std::string UpdaterState::StateReaderChromiumUpdater::GetUpdaterName() const {
  return "ChromiumUpdater";
}

base::Version UpdaterState::StateReaderChromiumUpdater::GetUpdaterVersion(
    bool /*is_machine*/) const {
  const std::string* val = parsed_json_.FindString(kUpdaterPrefsActiveVersion);
  return val ? base::Version(*val) : base::Version();
}

bool UpdaterState::StateReaderChromiumUpdater::IsAutoupdateCheckEnabled()
    const {
  return UpdaterState::IsAutoupdateCheckEnabled();
}

base::Time UpdaterState::StateReaderChromiumUpdater::GetUpdaterLastStartedAU(
    bool /*is_machine*/) const {
  return FindTimeKey(kUpdaterPrefsLastStarted);
}

base::Time UpdaterState::StateReaderChromiumUpdater::GetUpdaterLastChecked(
    bool /*is_machine*/) const {
  return FindTimeKey(kUpdaterPrefsLastChecked);
}

int UpdaterState::StateReaderChromiumUpdater::GetUpdatePolicy() const {
  return UpdaterState::GetUpdatePolicy();
}

update_client::CategorizedError
UpdaterState::StateReaderChromiumUpdater::GetLastUpdateCheckError() const {
  return {
      .category = static_cast<update_client::ErrorCategory>(
          parsed_json_
              .FindInt(update_client::kLastUpdateCheckErrorCategoryPreference)
              .value_or(0)),
      .code =
          parsed_json_.FindInt(update_client::kLastUpdateCheckErrorPreference)
              .value_or(0),
      .extra =
          parsed_json_
              .FindInt(update_client::kLastUpdateCheckErrorExtraCode1Preference)
              .value_or(0)};
}

UpdaterState::State UpdaterState::StateReader::Read(bool is_machine) const {
  State state;
  state.updater_name = GetUpdaterName();
  state.updater_version = GetUpdaterVersion(is_machine);
  state.last_autoupdate_started = GetUpdaterLastStartedAU(is_machine);
  state.last_checked = GetUpdaterLastChecked(is_machine);
  state.is_autoupdate_check_enabled = IsAutoupdateCheckEnabled();
  state.update_policy = [this] {
    const int update_policy = GetUpdatePolicy();
    CHECK((update_policy >= 0 && update_policy <= 3) || update_policy == -1);
    return update_policy;
  }();
  state.last_update_check_error = GetLastUpdateCheckError();
  return state;
}

UpdaterState::UpdaterState(bool is_machine)
    : is_machine_(is_machine), state_(ReadState(is_machine)) {}

UpdaterState::~UpdaterState() = default;

UpdaterState::Attributes UpdaterState::GetState(bool is_machine) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  return UpdaterState(is_machine).Serialize();
#else
  return {};
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
}

std::optional<UpdaterState::State> UpdaterState::ReadState(bool is_machine) {
  std::unique_ptr<UpdaterState::StateReader> state_reader =
      UpdaterState::StateReader::Create(is_machine);
  if (!state_reader) {
    return std::nullopt;
  }
  return state_reader->Read(is_machine);
}

UpdaterState::Attributes UpdaterState::Serialize() const {
  Attributes attributes;

  attributes["ismachine"] = is_machine_ ? "1" : "0";

  if (state_) {
    attributes["name"] = state_->updater_name;

    if (state_->updater_version.IsValid()) {
      attributes["version"] = state_->updater_version.GetString();
    }

    const base::Time now = base::Time::NowFromSystemTime();
    if (!state_->last_autoupdate_started.is_null()) {
      attributes["laststarted"] =
          NormalizeTimeDelta(now - state_->last_autoupdate_started);
    }
    if (!state_->last_checked.is_null()) {
      attributes["lastchecked"] =
          NormalizeTimeDelta(now - state_->last_checked);
    }

    attributes["autoupdatecheckenabled"] =
        state_->is_autoupdate_check_enabled ? "1" : "0";

    attributes["updatepolicy"] = base::NumberToString(state_->update_policy);
    attributes["lastupdatecheckerrorcode"] =
        state_->last_update_check_error.code;
    attributes["lastupdatecheckerrorcat"] =
        static_cast<int>(state_->last_update_check_error.category);
    attributes["lastupdatecheckextracode1"] =
        state_->last_update_check_error.extra;
  }

  return attributes;
}

std::string UpdaterState::NormalizeTimeDelta(base::TimeDelta delta) {
  const base::TimeDelta two_weeks = base::Days(14);
  const base::TimeDelta two_months = base::Days(56);

  std::string val;  // Contains the value to return in hours.
  if (delta <= two_weeks) {
    val = "0";
  } else if (two_weeks < delta && delta <= two_months) {
    val = "336";  // 2 weeks in hours.
  } else {
    val = "1344";  // 2*28 days in hours.
  }

  CHECK(!val.empty());
  return val;
}

}  // namespace component_updater