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

Side by Side Diff: chrome/browser/user_profiles_cleanup.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: add code in mini_installer 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_profiles_cleanup.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 "content/public/browser/browser_thread.h"
28
29 namespace {
grt (UTC plus 2) 2016/05/19 03:24:18 nit: blank line after this
zmin 2016/05/19 23:08:02 Done.
30 base::FilePath::StringType g_delete_suffix = L".CHROME_DELETE";
grt (UTC plus 2) 2016/05/19 03:24:18 static objects are banned since they introduce exi
zmin 2016/05/19 23:08:02 Done.
31
32 // Get disk cache dir override value if exists. Return empty path for default
grt (UTC plus 2) 2016/05/19 15:37:28 "Returns the..."
zmin 2016/05/19 23:08:01 Done.
33 // disk cache dir.
34 base::FilePath GetDiskCacheDir() {
35 base::FilePath disk_cache_dir;
36 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir);
37 if (disk_cache_dir.empty()) {
38 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
39 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir);
40 }
41 if (disk_cache_dir.ReferencesParent())
42 return base::MakeAbsoluteFilePath(disk_cache_dir);
43 return disk_cache_dir;
44 }
45
46 base::Version GetDowngradeVersion() {
grt (UTC plus 2) 2016/05/19 15:37:27 this function belongs in chrome/installer/util som
zmin 2016/05/19 23:08:02 Done. Unit test will be uploaded later as I need t
47 base::win::RegKey key;
48 base::string16 downgrade_version;
49 base::string16 key_path =
grt (UTC plus 2) 2016/05/19 15:37:28 BrowserDistribution::GetDistribution()->GetStateKe
zmin 2016/05/19 23:08:02 Done.
50 base::string16(L"Software\\Google\\Update\\ClientState\\") +
51 base::string16(google_update::kChromeUpgradeCode);
52 if (key.Open(HKEY_LOCAL_MACHINE, key_path.c_str(), KEY_QUERY_VALUE) !=
grt (UTC plus 2) 2016/05/19 15:37:27 Google Update interactions are always in the 32-bi
grt (UTC plus 2) 2016/05/19 15:37:28 use InstallUtil::IsPerUserInstall to determine whe
zmin 2016/05/19 23:08:02 Done.
zmin 2016/05/19 23:08:02 Done.
53 ERROR_SUCCESS ||
54 key.ReadValue(google_update::kRegDowngradeVersion, &downgrade_version) !=
55 ERROR_SUCCESS) {
56 key.Close();
57 if (key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_QUERY_VALUE) !=
58 ERROR_SUCCESS ||
59 key.ReadValue(google_update::kRegDowngradeVersion,
60 &downgrade_version) != ERROR_SUCCESS) {
61 return base::Version();
62 }
63 }
64 return base::Version(base::UTF16ToASCII(downgrade_version));
65 }
66
67 base::FilePath GetLastChromeVersionFile(const base::FilePath& user_data_dir) {
68 return user_data_dir.Append(L"LastChromeVersion");
grt (UTC plus 2) 2016/05/19 15:37:28 L"..." -> FILE_PATH_LITERAL("...")
zmin 2016/05/19 23:08:02 Done.
69 }
70
71 base::Version GetLastChromeVersion(const base::FilePath& user_data_dir) {
72 std::string last_chrome_version_str;
73 if (base::ReadFileToString(GetLastChromeVersionFile(user_data_dir),
74 &last_chrome_version_str))
75 return base::Version(
76 base::CollapseWhitespaceASCII(last_chrome_version_str, false));
grt (UTC plus 2) 2016/05/19 15:37:28 why not TrimWhitespaceASCII?
zmin 2016/05/19 23:08:02 Done.
77 return base::Version();
78 }
79
80 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) {
grt (UTC plus 2) 2016/05/19 15:37:28 doc comment
zmin 2016/05/19 23:08:02 Done.
81 if (source_path.empty())
82 return base::FilePath();
83
84 base::FilePath target_path(source_path.value() + g_delete_suffix);
grt (UTC plus 2) 2016/05/19 15:37:27 use FilePath methods whenever possible. is this eq
zmin 2016/05/19 23:08:03 Done.
85 int uniquifier = base::GetUniquePathNumber(source_path, g_delete_suffix);
86 if (uniquifier > 0)
grt (UTC plus 2) 2016/05/19 15:37:27 nit: add braces for multi-line body
zmin 2016/05/19 23:08:02 Done.
87 return target_path.InsertBeforeExtensionASCII(
88 base::StringPrintf(" (%d)", uniquifier));
89
grt (UTC plus 2) 2016/05/19 15:37:27 what if uniquifier < 0?
zmin 2016/05/19 23:08:02 Good question. <0 means the dir have been copied
90 return target_path;
91 }
92
93 void RenameDirectory(const base::FilePath& source,
grt (UTC plus 2) 2016/05/19 15:37:28 add a doc comment
zmin 2016/05/19 23:08:02 Done.
94 const base::FilePath& target,
95 bool recreate) {
96 if (!source.empty() && base::Move(source, target) && recreate)
97 base::CreateDirectory(source);
98 }
99
100 void DeleteDirectoryWithSuffix(base::FilePath path) {
grt (UTC plus 2) 2016/05/19 15:37:27 const-ref
grt (UTC plus 2) 2016/05/19 15:37:27 DeleteAllRenamedUserDirectories?
zmin 2016/05/19 23:08:01 Done.
zmin 2016/05/19 23:08:02 Done.
101 if (path.empty())
102 return;
103 base::FilePath dir_name = path.DirName();
104 // Does not support root directory
105 if (dir_name == path)
106 return;
107
108 base::FilePath::StringType pattern =
109 path.BaseName().value() + L"*" + g_delete_suffix;
110 base::FileEnumerator enumerator(dir_name, false,
111 base::FileEnumerator::DIRECTORIES, pattern);
112 for (base::FilePath file = enumerator.Next(); !file.empty();
grt (UTC plus 2) 2016/05/19 15:37:28 file -> dir
zmin 2016/05/19 23:08:03 Done.
113 file = enumerator.Next())
grt (UTC plus 2) 2016/05/19 15:37:27 nit: braces since the for() spans multiple lines
zmin 2016/05/19 23:08:02 Done.
114 base::DeleteFile(file, true);
115 }
116
117 void RemoveUnusedUserProfiles() {
118 base::FilePath user_data_dir;
119 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
120 DeleteDirectoryWithSuffix(user_data_dir);
121 DeleteDirectoryWithSuffix(GetDiskCacheDir());
122 }
123
124 } // namespace
125
126 void ResetUserProfilesAfterDowngrade() {
127 base::FilePath user_data_dir;
128 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
129 base::Version current_version(CHROME_VERSION_STRING);
grt (UTC plus 2) 2016/05/19 15:37:28 CHROME_VERSION_STRING -> chrome::kChromeVersion
zmin 2016/05/19 23:08:02 Done.
130
131 if (GetDowngradeVersion() > current_version) {
grt (UTC plus 2) 2016/05/19 15:37:27 if (GetDowngradeVersion() > current_version &&
132 base::FilePath disk_cache_dir(GetDiskCacheDir());
grt (UTC plus 2) 2016/05/19 15:37:28 keep locals in the narrowest scope possible (https
zmin 2016/05/19 23:08:02 More comments added
133 // Without the browser process singleton protection, the directory may be
134 // copied twice.
135 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir));
136 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir));
137 if (GetLastChromeVersion(user_data_dir) > current_version) {
138 // We don't have to recreate |disk_cache_dir| because it'll be initialized
grt (UTC plus 2) 2016/05/19 15:37:27 while use of personal pronouns isn't banned (it wa
zmin 2016/05/19 23:08:02 Done.
139 // 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 current_version.GetString().c_str(),
grt (UTC plus 2) 2016/05/19 15:37:28 stash current_version.GetString() in a local var s
grt (UTC plus 2) 2016/05/19 15:37:28 c_str() -> data() since you don't write the string
zmin 2016/05/19 23:08:02 Done.
146 current_version.GetString().size());
147 }
148
149 void RemoveUnusedUserProfilesInBlockingPool() {
150 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
151 FROM_HERE, base::Bind(&RemoveUnusedUserProfiles),
152 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698