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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/user_profiles_cleanup.cc
diff --git a/chrome/browser/user_profiles_cleanup.cc b/chrome/browser/user_profiles_cleanup.cc
new file mode 100644
index 0000000000000000000000000000000000000000..53e52041fd8bbd5b01f1414d36170a1d152b0364
--- /dev/null
+++ b/chrome/browser/user_profiles_cleanup.cc
@@ -0,0 +1,153 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/user_profiles_cleanup.h"
+
+#include <string>
+
+#include "base/bind.h"
+#include "base/command_line.h"
+#include "base/files/file_enumerator.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/path_service.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/time/time.h"
+#include "base/version.h"
+#include "base/win/registry.h"
+#include "chrome/browser/policy/policy_path_parser.h"
+#include "chrome/common/chrome_constants.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/common/chrome_version.h"
+#include "chrome/installer/util/google_update_constants.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace {
grt (UTC plus 2) 2016/05/19 03:24:18 nit: blank line after this
zmin 2016/05/19 23:08:02 Done.
+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.
+
+// 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.
+// disk cache dir.
+base::FilePath GetDiskCacheDir() {
+ base::FilePath disk_cache_dir;
+ policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir);
+ if (disk_cache_dir.empty()) {
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+ disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir);
+ }
+ if (disk_cache_dir.ReferencesParent())
+ return base::MakeAbsoluteFilePath(disk_cache_dir);
+ return disk_cache_dir;
+}
+
+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
+ base::win::RegKey key;
+ base::string16 downgrade_version;
+ 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.
+ base::string16(L"Software\\Google\\Update\\ClientState\\") +
+ base::string16(google_update::kChromeUpgradeCode);
+ 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.
+ ERROR_SUCCESS ||
+ key.ReadValue(google_update::kRegDowngradeVersion, &downgrade_version) !=
+ ERROR_SUCCESS) {
+ key.Close();
+ if (key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_QUERY_VALUE) !=
+ ERROR_SUCCESS ||
+ key.ReadValue(google_update::kRegDowngradeVersion,
+ &downgrade_version) != ERROR_SUCCESS) {
+ return base::Version();
+ }
+ }
+ return base::Version(base::UTF16ToASCII(downgrade_version));
+}
+
+base::FilePath GetLastChromeVersionFile(const base::FilePath& user_data_dir) {
+ 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.
+}
+
+base::Version GetLastChromeVersion(const base::FilePath& user_data_dir) {
+ std::string last_chrome_version_str;
+ if (base::ReadFileToString(GetLastChromeVersionFile(user_data_dir),
+ &last_chrome_version_str))
+ return base::Version(
+ 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.
+ return base::Version();
+}
+
+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.
+ if (source_path.empty())
+ return base::FilePath();
+
+ 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.
+ int uniquifier = base::GetUniquePathNumber(source_path, g_delete_suffix);
+ 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.
+ return target_path.InsertBeforeExtensionASCII(
+ base::StringPrintf(" (%d)", uniquifier));
+
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
+ return target_path;
+}
+
+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.
+ const base::FilePath& target,
+ bool recreate) {
+ if (!source.empty() && base::Move(source, target) && recreate)
+ base::CreateDirectory(source);
+}
+
+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.
+ if (path.empty())
+ return;
+ base::FilePath dir_name = path.DirName();
+ // Does not support root directory
+ if (dir_name == path)
+ return;
+
+ base::FilePath::StringType pattern =
+ path.BaseName().value() + L"*" + g_delete_suffix;
+ base::FileEnumerator enumerator(dir_name, false,
+ base::FileEnumerator::DIRECTORIES, pattern);
+ 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.
+ 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.
+ base::DeleteFile(file, true);
+}
+
+void RemoveUnusedUserProfiles() {
+ base::FilePath user_data_dir;
+ PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
+ DeleteDirectoryWithSuffix(user_data_dir);
+ DeleteDirectoryWithSuffix(GetDiskCacheDir());
+}
+
+} // namespace
+
+void ResetUserProfilesAfterDowngrade() {
+ base::FilePath user_data_dir;
+ PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
+ 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.
+
+ if (GetDowngradeVersion() > current_version) {
grt (UTC plus 2) 2016/05/19 15:37:27 if (GetDowngradeVersion() > current_version &&
+ 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
+ // Without the browser process singleton protection, the directory may be
+ // copied twice.
+ base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir));
+ base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir));
+ if (GetLastChromeVersion(user_data_dir) > current_version) {
+ // 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.
+ // later.
+ RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false);
+ RenameDirectory(user_data_dir, temp_user_data_dir, true);
+ }
+ }
+ base::WriteFile(GetLastChromeVersionFile(user_data_dir),
+ 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.
+ current_version.GetString().size());
+}
+
+void RemoveUnusedUserProfilesInBlockingPool() {
+ content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
+ FROM_HERE, base::Bind(&RemoveUnusedUserProfiles),
+ base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
+}

Powered by Google App Engine
This is Rietveld 408576698