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

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: 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/google_update_constants.h"
27 #include "chrome/installer/util/install_util.h"
28 #include "content/public/browser/browser_thread.h"
29
30 namespace downgrade {
31
32 namespace {
33
34 bool g_is_browser_test = false;
35
36 // Return the disk cache dir override value if exists or empty path for default
37 // disk cache dir.
38 base::FilePath GetDiskCacheDir() {
39 base::FilePath disk_cache_dir;
40 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir);
41 if (disk_cache_dir.empty()) {
42 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
43 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir);
44 }
45 if (disk_cache_dir.ReferencesParent())
46 return base::MakeAbsoluteFilePath(disk_cache_dir);
47 return disk_cache_dir;
48 }
49
50 base::FilePath GetLastVersionFile(const base::FilePath& user_data_dir) {
51 return user_data_dir.Append(kDowngradeLastVersionFile);
52 }
53
54 // Return the temporary path that |source_path| will be renamed to later.
55 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) {
56 if (source_path.empty())
57 return base::FilePath();
58
59 base::FilePath target_path(source_path.AddExtension(kDowngradeDeleteSuffix));
60 int uniquifier =
61 base::GetUniquePathNumber(source_path, kDowngradeDeleteSuffix);
62 if (uniquifier < 0)
63 return base::FilePath();
64 if (uniquifier > 0) {
65 return target_path.InsertBeforeExtensionASCII(
66 base::StringPrintf(" (%d)", uniquifier));
67 }
68
69 return target_path;
70 }
71
72 // Rename the |source| directory to |target|. Create |source| directory after
73 // rename if |recreate| is true.
74 void RenameDirectory(const base::FilePath& source,
75 const base::FilePath& target,
76 bool recreate) {
77 if (!source.empty() && !target.empty() && base::Move(source, target) &&
78 recreate) {
79 base::CreateDirectory(source);
80 }
81 }
82
83 void DeleteAllRenamedUserDirectories(const base::FilePath& path) {
84 if (path.empty())
85 return;
86 base::FilePath dir_name = path.DirName();
87 // Does not support root directory
88 if (dir_name == path)
89 return;
90
91 base::FilePath::StringType pattern =
92 path.BaseName().value() + FILE_PATH_LITERAL("*") + kDowngradeDeleteSuffix;
93 base::FileEnumerator enumerator(dir_name, false,
94 base::FileEnumerator::DIRECTORIES, pattern);
95 for (base::FilePath dir = enumerator.Next(); !dir.empty();
96 dir = enumerator.Next()) {
97 base::DeleteFile(dir, true);
98 }
99 }
100
101 void DeleteMovedUserData(const base::FilePath& user_data_dir,
102 const base::FilePath& disk_cache_dir) {
103 DeleteAllRenamedUserDirectories(user_data_dir);
104 DeleteAllRenamedUserDirectories(disk_cache_dir);
105 }
106
107 enum InstallLevel { SYSTEM_INSTALL, USER_INSTALL, UNKNOWN };
108
109 InstallLevel GetInstallLevel() {
110 base::FilePath cur_chrome_exe;
111 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe))
112 return UNKNOWN;
113 if (InstallUtil::IsPerUserInstall(cur_chrome_exe))
114 return USER_INSTALL;
115 else
116 return SYSTEM_INSTALL;
117 }
118
119 } // namespace
120
121 const base::FilePath::CharType kDowngradeLastVersionFile[] =
122 FILE_PATH_LITERAL("Last Version");
123 const base::FilePath::CharType kDowngradeDeleteSuffix[] =
124 FILE_PATH_LITERAL(".CHROME_DELETE");
125
126 void MoveUserDataForFirstRunAfterDowngrade() {
127 base::FilePath user_data_dir;
128 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
129 return;
130 InstallLevel install_level = GetInstallLevel();
131 if (install_level == UNKNOWN)
132 return;
133 base::Version current_version(chrome::kChromeVersion);
134 base::Version downgrade_version = InstallUtil::GetDowngradeVersion(
135 install_level == SYSTEM_INSTALL, BrowserDistribution::GetDistribution());
136 if (downgrade_version.IsValid() && downgrade_version > current_version) {
137 base::FilePath disk_cache_dir(GetDiskCacheDir());
138 // Without the browser process singleton protection, the directory may be
139 // copied multiple times. In order to prevent that from happening, the temp
140 // directory's name will be computed before the Chrome Version file is
141 // read. Because the deletion will be scheduled after the singleton is
142 // acquired, the directory can only be moved twice in the worst case.
143 // Also, doing this after the downgrade version check to reduce performance
144 // cost for the normal launch.
145 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir));
146 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir));
147 base::Version last_version = GetLastVersion(user_data_dir);
148 if (last_version.IsValid() && last_version > current_version) {
149 // Do not recreate |disk_cache_dir| as it will be initialized later.
150 RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false);
151 RenameDirectory(user_data_dir, temp_user_data_dir, true);
152 }
153 }
154 }
155
156 void UpdateLastVersion(const base::FilePath& user_data_dir) {
157 base::WriteFile(GetLastVersionFile(user_data_dir), chrome::kChromeVersion,
158 strlen(chrome::kChromeVersion));
159 }
160
161 base::Version GetLastVersion(const base::FilePath& user_data_dir) {
162 std::string last_version_str;
163 if (base::ReadFileToString(GetLastVersionFile(user_data_dir),
164 &last_version_str)) {
165 return base::Version(
166 base::TrimWhitespaceASCII(last_version_str, base::TRIM_ALL)
167 .as_string());
168 }
169 return base::Version();
170 }
171
172 void DeleteMovedUserDataSoon() {
173 base::FilePath user_data_dir;
174 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
175 if (g_is_browser_test) {
176 // Delete task will always be posted and executed for browser test.
177 content::BrowserThread::PostBlockingPoolTask(
178 FROM_HERE,
179 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir()));
180 } else {
181 content::BrowserThread::PostAfterStartupTask(
182 FROM_HERE, content::BrowserThread::GetBlockingPool()
183 ->GetTaskRunnerWithShutdownBehavior(
184 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN),
185 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir()));
186 }
187 }
188
189 bool IsMSIInstall() {
190 InstallLevel install_level = GetInstallLevel();
191 base::win::RegKey key;
192 DWORD is_msi = 3;
193 return install_level != UNKNOWN &&
194 key.Open((install_level == SYSTEM_INSTALL) ? HKEY_LOCAL_MACHINE
195 : HKEY_CURRENT_USER,
196 BrowserDistribution::GetDistribution()->GetStateKey().c_str(),
197 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS &&
198 key.ReadValueDW(google_update::kRegMSIField, &is_msi) ==
199 ERROR_SUCCESS &&
200 is_msi != 0;
201 }
202
203 void SetSimulateDowngradeForTest(bool is_browser_test) {
204 g_is_browser_test = is_browser_test;
205 }
206
207 } // namespace downgrade
OLDNEW
« no previous file with comments | « chrome/browser/downgrade/user_data_downgrade.h ('k') | chrome/browser/downgrade/user_data_downgrade_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698