Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/downgrade/user_data_downgrade.cc

Issue 1986823002: Reset user data directory and disk cache directory after downgrade. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cr Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/downgrade/user_data_downgrade.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "base/version.h"
20 #include "base/win/registry.h"
21 #include "chrome/browser/policy/policy_path_parser.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/chrome_version.h"
26 #include "chrome/installer/util/install_util.h"
27 #include "content/public/browser/browser_thread.h"
28
29 namespace {
30
31 bool g_is_browser_test = false;
32
33 // Return the disk cache dir override value if exists or empty path for default
34 // disk cache dir.
35 base::FilePath GetDiskCacheDir() {
36 base::FilePath disk_cache_dir;
37 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir);
38 if (disk_cache_dir.empty()) {
39 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
40 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir);
41 }
42 if (disk_cache_dir.ReferencesParent())
43 return base::MakeAbsoluteFilePath(disk_cache_dir);
44 return disk_cache_dir;
45 }
46
47 base::FilePath GetBrowserVersionFile(const base::FilePath& user_data_dir) {
48 return user_data_dir.Append(g_downgrade_browser_version_file);
49 }
50
51 // Return the temporary path that |source_path| will be renamed to later.
52 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) {
53 if (source_path.empty())
54 return base::FilePath();
55
56 base::FilePath target_path(
57 source_path.AddExtension(g_downgrade_delete_suffix));
58 int uniquifier =
59 base::GetUniquePathNumber(source_path, g_downgrade_delete_suffix);
60 if (uniquifier < 0)
61 return base::FilePath();
62 if (uniquifier > 0) {
63 return target_path.InsertBeforeExtensionASCII(
64 base::StringPrintf(" (%d)", uniquifier));
65 }
66
67 return target_path;
68 }
69
70 // Rename the |source| directory to |target|. Create |source| directory after
71 // rename if |recreate| is true.
72 void RenameDirectory(const base::FilePath& source,
73 const base::FilePath& target,
74 bool recreate) {
75 if (!source.empty() && !target.empty() && base::Move(source, target) &&
76 recreate) {
77 base::CreateDirectory(source);
78 }
79 }
80
81 void DeleteAllRenamedUserDirectories(const base::FilePath& path) {
82 if (path.empty())
83 return;
84 base::FilePath dir_name = path.DirName();
85 // Does not support root directory
86 if (dir_name == path)
87 return;
88
89 base::FilePath::StringType pattern = path.BaseName().value() +
90 FILE_PATH_LITERAL("*") +
91 g_downgrade_delete_suffix;
92 base::FileEnumerator enumerator(dir_name, false,
93 base::FileEnumerator::DIRECTORIES, pattern);
94 for (base::FilePath dir = enumerator.Next(); !dir.empty();
95 dir = enumerator.Next()) {
96 base::DeleteFile(dir, true);
97 }
98 }
99
100 void DeleteMovedUserData(const base::FilePath& user_data_dir,
101 const base::FilePath& disk_cache_dir) {
102 DeleteAllRenamedUserDirectories(user_data_dir);
103 DeleteAllRenamedUserDirectories(disk_cache_dir);
104 }
105
106 } // namespace
107
108 const base::FilePath::CharType g_downgrade_browser_version_file[] =
109 FILE_PATH_LITERAL("Browser Version");
110 const base::FilePath::CharType g_downgrade_delete_suffix[] =
111 FILE_PATH_LITERAL(".CHROME_DELETE");
112
113 void MoveUserDataForFirstRunAfterDowngrade() {
114 base::FilePath user_data_dir;
115 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
116 return;
117 base::Version current_version(chrome::kChromeVersion);
118 base::FilePath cur_chrome_exe;
119 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe))
120 return;
121 base::Version downgrade_version = InstallUtil::GetDowngradeVersion(
122 !InstallUtil::IsPerUserInstall(cur_chrome_exe),
123 BrowserDistribution::GetDistribution());
124 if (downgrade_version.IsValid() && downgrade_version > current_version) {
125 base::FilePath disk_cache_dir(GetDiskCacheDir());
126 // Without the browser process singleton protection, the directory may be
127 // copied multiple times. In order to prevent that from happening, the temp
128 // directory's name will be computed before the BrowserVersion file is
129 // read. Because the deletion will be scheduled after the singleton is
130 // acquired, the directory can only be moved twice in the worst case.
131 // Also, doing this after the downgrade version check to reduce performance
132 // cost for the normal launch.
133 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir));
134 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir));
135 base::Version last_browser_version = GetBrowserVersion(user_data_dir);
136 if (last_browser_version.IsValid() &&
137 last_browser_version > current_version) {
138 // Do not recreate |disk_cache_dir| as it will be initialized later.
139 RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false);
140 RenameDirectory(user_data_dir, temp_user_data_dir, true);
141 }
142 }
143 }
144
145 void UpdateBrowserVersion(const base::FilePath& user_data_dir) {
146 base::WriteFile(GetBrowserVersionFile(user_data_dir), chrome::kChromeVersion,
147 strlen(chrome::kChromeVersion));
148 }
149
150 base::Version GetBrowserVersion(const base::FilePath& user_data_dir) {
151 std::string browser_version_str;
152 if (base::ReadFileToString(GetBrowserVersionFile(user_data_dir),
153 &browser_version_str)) {
154 return base::Version(
155 base::TrimWhitespaceASCII(browser_version_str, base::TRIM_ALL)
156 .as_string());
157 }
158 return base::Version();
159 }
160
161 void DeleteMovedUserDataSoon() {
162 base::FilePath user_data_dir;
163 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
164 if (g_is_browser_test) {
165 // Delete task will always be posted and executed for browser test.
166 content::BrowserThread::PostBlockingPoolTask(
167 FROM_HERE,
168 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir()));
169 } else {
170 content::BrowserThread::PostAfterStartupTask(
171 FROM_HERE, content::BrowserThread::GetBlockingPool()
172 ->GetTaskRunnerWithShutdownBehavior(
173 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN),
174 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir()));
175 }
176 }
177
178 void SimulateDowngradeForTest(bool is_browser_test) {
179 g_is_browser_test = is_browser_test;
180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698