File: user_data_downgrade.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 (132 lines) | stat: -rw-r--r-- 4,631 bytes parent folder | download | duplicates (6)
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
// 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/downgrade/user_data_downgrade.h"

#include <string>

#include "base/check.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/downgrade/snapshot_manager.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"

namespace downgrade {

namespace {

base::Version GetVersionFromFileName(const base::FilePath& path) {
#if BUILDFLAG(IS_WIN)
  // On Windows, for Unicode-aware applications, native pathnames are wchar_t
  // arrays encoded in UTF-16.
  return base::Version(base::WideToUTF8(path.BaseName().value()));
#elif BUILDFLAG(IS_POSIX)
  // On most platforms, native pathnames are char arrays, and the encoding
  // may or may not be specified.  On Mac OS X, native pathnames are encoded
  // in UTF-8.
  return base::Version(path.BaseName().value());
#endif  // BUILDFLAG(IS_WIN)
}

bool IsValidSnapshotDirectory(const base::FilePath& path) {
  base::Version snapshot_version = GetVersionFromFileName(path);
  return snapshot_version.IsValid() &&
         base::PathExists(path.Append(kDowngradeLastVersionFile));
}

}  // namespace

const base::FilePath::StringViewType kDowngradeLastVersionFile(
    FILE_PATH_LITERAL("Last Version"));
const base::FilePath::StringViewType kDowngradeDeleteSuffix(
    FILE_PATH_LITERAL(".CHROME_DELETE"));

const base::FilePath::StringViewType kSnapshotsDir(
    FILE_PATH_LITERAL("Snapshots"));

base::FilePath GetLastVersionFile(const base::FilePath& user_data_dir) {
  DCHECK(!user_data_dir.empty());
  return user_data_dir.Append(kDowngradeLastVersionFile);
}

std::optional<base::Version> GetLastVersion(
    const base::FilePath& user_data_dir) {
  DCHECK(!user_data_dir.empty());
  std::string last_version_str;
  if (base::ReadFileToString(GetLastVersionFile(user_data_dir),
                             &last_version_str)) {
    base::Version version(
        base::TrimWhitespaceASCII(last_version_str, base::TRIM_ALL));
    if (version.IsValid())
      return version;
  }
  return std::nullopt;
}

base::FilePath GetDiskCacheDir() {
  base::FilePath disk_cache_dir =
      g_browser_process->local_state()->GetFilePath(prefs::kDiskCacheDir);
  if (disk_cache_dir.ReferencesParent())
    return base::MakeAbsoluteFilePath(disk_cache_dir);
  return disk_cache_dir;
}

base::flat_set<base::Version> GetAvailableSnapshots(
    const base::FilePath& snapshot_dir) {
  std::vector<base::Version> result;
  base::FileEnumerator enumerator(snapshot_dir, false,
                                  base::FileEnumerator::DIRECTORIES);
  for (base::FilePath path = enumerator.Next(); !path.empty();
       path = enumerator.Next()) {
    base::Version snapshot_version = GetVersionFromFileName(path);
    if (!snapshot_version.IsValid() ||
        !base::PathExists(path.Append(kDowngradeLastVersionFile))) {
      continue;
    }
    result.push_back(std::move(snapshot_version));
  }
  return base::flat_set<base::Version>(std::move(result));
}

std::vector<base::FilePath> GetInvalidSnapshots(
    const base::FilePath& snapshot_dir) {
  std::vector<base::FilePath> result;
  base::FileEnumerator enumerator(snapshot_dir, false,
                                  base::FileEnumerator::DIRECTORIES);
  for (base::FilePath path = enumerator.Next(); !path.empty();
       path = enumerator.Next()) {
    if (!IsValidSnapshotDirectory(path))
      result.push_back(std::move(path));
  }
  return result;
}

std::optional<base::Version> GetSnapshotToRestore(
    const base::Version& version,
    const base::FilePath& user_data_dir) {
  DCHECK(version.IsValid());
  base::FilePath top_snapshot_dir = user_data_dir.Append(kSnapshotsDir);
  auto available_snapshots = GetAvailableSnapshots(top_snapshot_dir);

  auto upper_bound = available_snapshots.upper_bound(version);
  if (upper_bound != available_snapshots.begin())
    return *--upper_bound;
  return std::nullopt;
}

void RemoveDataForProfile(base::Time delete_begin,
                          const base::FilePath& profile_path,
                          uint64_t remove_mask) {
  SnapshotManager snapshot_manager(profile_path.DirName());
  snapshot_manager.DeleteSnapshotDataForProfile(
      delete_begin, profile_path.BaseName(), remove_mask);
}

}  // namespace downgrade