Chromium Code Reviews| Index: chrome/browser/downgrade/user_data_downgrade.cc |
| diff --git a/chrome/browser/downgrade/user_data_downgrade.cc b/chrome/browser/downgrade/user_data_downgrade.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e0000a540572c83f7ab0c98630d16c5c2cd3767b |
| --- /dev/null |
| +++ b/chrome/browser/downgrade/user_data_downgrade.cc |
| @@ -0,0 +1,172 @@ |
| +// 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/downgrade/user_data_downgrade.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/install_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| + |
| +constexpr base::FilePath::CharType g_delete_suffix[] = |
| + FILE_PATH_LITERAL(".CHROME_DELETE"); |
| +bool g_is_browser_test = false; |
| + |
| +// Return the disk cache dir override value if exists or empty path for default |
| +// 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::FilePath GetLastChromeVersionFile(const base::FilePath& user_data_dir) { |
| + const base::FilePath::StringType last_chrome_version = |
| + FILE_PATH_LITERAL("Last Chrome Version"); |
|
sky
2016/06/03 00:04:24
nit: Chrome Version? Generally we don't use 'Last'
zmin
2016/06/03 15:46:52
I use 'Last' here because this file is used to ind
sky
2016/06/03 18:18:03
I'm still of the opinion 'Last' is unnecessary her
|
| + return user_data_dir.Append(last_chrome_version); |
| +} |
| + |
| +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::TrimWhitespaceASCII(last_chrome_version_str, base::TRIM_ALL) |
| + .as_string()); |
| + } |
| + return base::Version(); |
| +} |
| + |
| +// Return the temporary path that |source_path| will be renamed to later. |
| +base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) { |
| + if (source_path.empty()) |
| + return base::FilePath(); |
| + |
| + base::FilePath target_path(source_path.AddExtension(g_delete_suffix)); |
| + int uniquifier = base::GetUniquePathNumber(source_path, g_delete_suffix); |
| + if (uniquifier < 0) |
| + return base::FilePath(); |
| + if (uniquifier > 0) { |
| + return target_path.InsertBeforeExtensionASCII( |
| + base::StringPrintf(" (%d)", uniquifier)); |
| + } |
| + |
| + return target_path; |
| +} |
| + |
| +// Rename the |source| directory to |target|. Create |source| directory after |
| +// rename if |recreate| is true. |
| +void RenameDirectory(const base::FilePath& source, |
| + const base::FilePath& target, |
| + bool recreate) { |
| + if (!source.empty() && !target.empty() && base::Move(source, target) && |
|
sky
2016/06/03 00:04:24
What if Move fails? Doesn't that leave things in a
zmin
2016/06/03 15:46:52
If move failed, we just end up with a higher versi
sky
2016/06/03 18:18:03
That's assuming every file deals with a version fr
|
| + recreate) { |
| + base::CreateDirectory(source); |
| + } |
| +} |
| + |
| +void DeleteAllRenamedUserDirectories(const base::FilePath& path) { |
| + 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() + FILE_PATH_LITERAL("*") + g_delete_suffix; |
| + base::FileEnumerator enumerator(dir_name, false, |
| + base::FileEnumerator::DIRECTORIES, pattern); |
| + for (base::FilePath dir = enumerator.Next(); !dir.empty(); |
| + dir = enumerator.Next()) { |
| + base::DeleteFile(dir, true); |
| + } |
| +} |
| + |
| +void DeleteMovedUserData(const base::FilePath& user_data_dir, |
| + const base::FilePath& disk_cache_dir) { |
| + DeleteAllRenamedUserDirectories(user_data_dir); |
| + DeleteAllRenamedUserDirectories(disk_cache_dir); |
| +} |
| + |
| +} // namespace |
| + |
| +void MoveUserDataForFirstRunAfterDowngrade() { |
| + base::FilePath user_data_dir; |
| + PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
|
sky
2016/06/03 00:04:23
Check return value.
zmin
2016/06/03 15:46:52
Done.
|
| + base::Version current_version(chrome::kChromeVersion); |
| + base::FilePath cur_chrome_exe; |
| + if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe)) |
| + return; |
| + base::Version downgrade_version = InstallUtil::GetDowngradeVersion( |
| + !InstallUtil::IsPerUserInstall(cur_chrome_exe), |
| + BrowserDistribution::GetDistribution()); |
| + if (downgrade_version.IsValid() && downgrade_version > current_version) { |
| + base::FilePath disk_cache_dir(GetDiskCacheDir()); |
| + // Without the browser process singleton protection, the directory may be |
| + // copied multiple times. In order to prevent that from happening, the temp |
| + // directory's name will be computed before the LastChromeVersion file is |
| + // read. Because the deletion will be scheduled after the singleton is |
| + // acquired, the directory can only be moved twice in the worst case. |
| + // Also, doing this after the downgrade version check to reduce performance |
| + // cost for the normal launch. |
| + base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir)); |
| + base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir)); |
| + base::Version last_chrome_version = GetLastChromeVersion(user_data_dir); |
| + if (last_chrome_version.IsValid() && |
|
sky
2016/06/03 00:04:24
Move this earlier on. You don't need to figure out
zmin
2016/06/03 15:46:52
Finding temp dir name is put before this on purpos
|
| + last_chrome_version > current_version) { |
| + // Do not recreate |disk_cache_dir| as it will be initialized 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), |
|
sky
2016/06/03 00:04:23
I find it confusing that a function named MoveUser
zmin
2016/06/03 15:46:52
Done.
|
| + chrome::kChromeVersion, strlen(chrome::kChromeVersion)); |
| +} |
| + |
| +void DeleteMovedUserDataSoon() { |
| + base::FilePath user_data_dir; |
| + PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| + if (g_is_browser_test) { |
| + // Delete task will always be posted and executed for browser test. |
| + content::BrowserThread::PostBlockingPoolTask( |
| + FROM_HERE, |
| + base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); |
| + } else { |
| + content::BrowserThread::PostAfterStartupTask( |
| + FROM_HERE, content::BrowserThread::GetBlockingPool() |
| + ->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), |
| + base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); |
| + } |
| +} |
| + |
| +void SimulateDowngradeForTest(bool is_browser_test) { |
| + g_is_browser_test = is_browser_test; |
| +} |