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

Side by Side Diff: chrome/browser/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, 7 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/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 constexpr base::FilePath::CharType g_delete_suffix[] =
32 FILE_PATH_LITERAL(".CHROME_DELETE");
33
34 // Return the disk cache dir override value if exists or empty path for default
35 // disk cache dir.
36 base::FilePath GetDiskCacheDir() {
37 base::FilePath disk_cache_dir;
38 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir);
39 if (disk_cache_dir.empty()) {
40 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
41 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir);
42 }
43 if (disk_cache_dir.ReferencesParent())
44 return base::MakeAbsoluteFilePath(disk_cache_dir);
45 return disk_cache_dir;
46 }
47
48 base::FilePath GetLastChromeVersionFile(const base::FilePath& user_data_dir) {
49 return user_data_dir.Append(FILE_PATH_LITERAL("LastChromeVersion"));
50 }
51
52 base::Version GetLastChromeVersion(const base::FilePath& user_data_dir) {
53 std::string last_chrome_version_str;
54 if (base::ReadFileToString(GetLastChromeVersionFile(user_data_dir),
55 &last_chrome_version_str)) {
56 return base::Version(
57 base::TrimWhitespaceASCII(last_chrome_version_str, base::TRIM_TRAILING)
grt (UTC plus 2) 2016/05/19 23:39:28 if you're going to trim whitespace to be conservat
zmin 2016/05/20 00:52:25 Done.
58 .as_string());
59 }
60 return base::Version();
61 }
62
63 // Return the temporary path that |source_path| will be renamed to later.
64 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) {
65 if (source_path.empty())
66 return base::FilePath();
67
68 base::FilePath target_path(source_path.AddExtension(g_delete_suffix));
69 int uniquifier = base::GetUniquePathNumber(source_path, g_delete_suffix);
70 if (uniquifier < 0)
71 return base::FilePath();
72 if (uniquifier > 0) {
73 return target_path.InsertBeforeExtensionASCII(
74 base::StringPrintf(" (%d)", uniquifier));
75 }
76
77 return target_path;
78 }
79
80 // Rename the |source| directory to |target|. Create |source| directory after
81 // rename if |recreate| is true.
82 void RenameDirectory(const base::FilePath& source,
83 const base::FilePath& target,
84 bool recreate) {
85 if (!source.empty() && !target.empty() && base::Move(source, target) &&
86 recreate)
grt (UTC plus 2) 2016/05/19 23:39:27 nit: braces
zmin 2016/05/20 00:52:25 Done.
87 base::CreateDirectory(source);
88 }
89
90 void DeleteAllRenamedUserDirectories(const base::FilePath& path) {
91 if (path.empty())
92 return;
93 base::FilePath dir_name = path.DirName();
94 // Does not support root directory
95 if (dir_name == path)
96 return;
97
98 base::FilePath::StringType pattern =
99 path.BaseName().value() + FILE_PATH_LITERAL("*") + g_delete_suffix;
100 base::FileEnumerator enumerator(dir_name, false,
101 base::FileEnumerator::DIRECTORIES, pattern);
102 for (base::FilePath dir = enumerator.Next(); !dir.empty();
103 dir = enumerator.Next()) {
104 base::DeleteFile(dir, true);
105 }
106 }
107
108 void DeleteMovedUserData() {
109 base::FilePath user_data_dir;
110 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
111 DeleteAllRenamedUserDirectories(user_data_dir);
112 DeleteAllRenamedUserDirectories(GetDiskCacheDir());
113 }
114
115 } // namespace
116
117 void MoveUserDataForFirstRunAfterDowngrade() {
118 base::FilePath user_data_dir;
119 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
120 base::Version current_version(chrome::kChromeVersion);
121 base::FilePath cur_chrome_exe;
122 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe))
123 return;
124 if (InstallUtil::GetDowngradeVersion(
125 InstallUtil::IsPerUserInstall(cur_chrome_exe)
126 ? HKEY_CURRENT_USER
127 : HKEY_LOCAL_MACHINE) > current_version) {
128 base::FilePath disk_cache_dir(GetDiskCacheDir());
129 // Without the browser process singleton protection, the directory may be
130 // copied multiple times. In order to prevent that from happening, the temp
131 // directory's name will be get before the last chrome version .
grt (UTC plus 2) 2016/05/19 23:39:28 "will be get before the last chrome version" -> "w
zmin 2016/05/20 00:52:25 Done.
132 // Because the deletion will be scheduled after singleton, the directory
grt (UTC plus 2) 2016/05/19 23:39:27 "...after the singleton is acquired,"
zmin 2016/05/20 00:52:25 Done.
133 // can only be moved twice in the worst case.
134 // Also, doing this after the downgrade version check to reduce performance
135 // cost for the normal launch.
136 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir));
137 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir));
138 if (GetLastChromeVersion(user_data_dir) > current_version) {
139 // Do not recreate |disk_cache_dir| as it will be initialized later.
140 RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false);
141 RenameDirectory(user_data_dir, temp_user_data_dir, true);
142 }
143 }
144 base::WriteFile(GetLastChromeVersionFile(user_data_dir),
145 chrome::kChromeVersion, strlen(chrome::kChromeVersion));
146 }
147
148 void DeleteMovedUserDataSoon() {
149 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
150 FROM_HERE, base::Bind(&DeleteMovedUserData),
151 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698