| OLD | NEW |
| (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/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 bool g_is_browser_test = false; |
| 34 |
| 35 // Return the disk cache dir override value if exists or empty path for default |
| 36 // disk cache dir. |
| 37 base::FilePath GetDiskCacheDir() { |
| 38 base::FilePath disk_cache_dir; |
| 39 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir); |
| 40 if (disk_cache_dir.empty()) { |
| 41 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 42 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir); |
| 43 } |
| 44 if (disk_cache_dir.ReferencesParent()) |
| 45 return base::MakeAbsoluteFilePath(disk_cache_dir); |
| 46 return disk_cache_dir; |
| 47 } |
| 48 |
| 49 base::FilePath GetLastChromeVersionFile(const base::FilePath& user_data_dir) { |
| 50 const base::FilePath::StringType last_chrome_version = |
| 51 FILE_PATH_LITERAL("Last Chrome Version"); |
| 52 return user_data_dir.Append(last_chrome_version); |
| 53 } |
| 54 |
| 55 base::Version GetLastChromeVersion(const base::FilePath& user_data_dir) { |
| 56 std::string last_chrome_version_str; |
| 57 if (base::ReadFileToString(GetLastChromeVersionFile(user_data_dir), |
| 58 &last_chrome_version_str)) { |
| 59 return base::Version( |
| 60 base::TrimWhitespaceASCII(last_chrome_version_str, base::TRIM_ALL) |
| 61 .as_string()); |
| 62 } |
| 63 return base::Version(); |
| 64 } |
| 65 |
| 66 // Return the temporary path that |source_path| will be renamed to later. |
| 67 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) { |
| 68 if (source_path.empty()) |
| 69 return base::FilePath(); |
| 70 |
| 71 base::FilePath target_path(source_path.AddExtension(g_delete_suffix)); |
| 72 int uniquifier = base::GetUniquePathNumber(source_path, g_delete_suffix); |
| 73 if (uniquifier < 0) |
| 74 return base::FilePath(); |
| 75 if (uniquifier > 0) { |
| 76 return target_path.InsertBeforeExtensionASCII( |
| 77 base::StringPrintf(" (%d)", uniquifier)); |
| 78 } |
| 79 |
| 80 return target_path; |
| 81 } |
| 82 |
| 83 // Rename the |source| directory to |target|. Create |source| directory after |
| 84 // rename if |recreate| is true. |
| 85 void RenameDirectory(const base::FilePath& source, |
| 86 const base::FilePath& target, |
| 87 bool recreate) { |
| 88 if (!source.empty() && !target.empty() && base::Move(source, target) && |
| 89 recreate) { |
| 90 base::CreateDirectory(source); |
| 91 } |
| 92 } |
| 93 |
| 94 void DeleteAllRenamedUserDirectories(const base::FilePath& path) { |
| 95 if (path.empty()) |
| 96 return; |
| 97 base::FilePath dir_name = path.DirName(); |
| 98 // Does not support root directory |
| 99 if (dir_name == path) |
| 100 return; |
| 101 |
| 102 base::FilePath::StringType pattern = |
| 103 path.BaseName().value() + FILE_PATH_LITERAL("*") + g_delete_suffix; |
| 104 base::FileEnumerator enumerator(dir_name, false, |
| 105 base::FileEnumerator::DIRECTORIES, pattern); |
| 106 for (base::FilePath dir = enumerator.Next(); !dir.empty(); |
| 107 dir = enumerator.Next()) { |
| 108 base::DeleteFile(dir, true); |
| 109 } |
| 110 } |
| 111 |
| 112 void DeleteMovedUserData(const base::FilePath& user_data_dir, |
| 113 const base::FilePath& disk_cache_dir) { |
| 114 DeleteAllRenamedUserDirectories(user_data_dir); |
| 115 DeleteAllRenamedUserDirectories(disk_cache_dir); |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 void MoveUserDataForFirstRunAfterDowngrade() { |
| 121 base::FilePath user_data_dir; |
| 122 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 123 base::Version current_version(chrome::kChromeVersion); |
| 124 base::FilePath cur_chrome_exe; |
| 125 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe)) |
| 126 return; |
| 127 // Avoid registry access for browser test. |
| 128 if (g_is_browser_test || |
| 129 InstallUtil::GetDowngradeVersion( |
| 130 !InstallUtil::IsPerUserInstall(cur_chrome_exe), |
| 131 BrowserDistribution::GetDistribution()) > current_version) { |
| 132 base::FilePath disk_cache_dir(GetDiskCacheDir()); |
| 133 // Without the browser process singleton protection, the directory may be |
| 134 // copied multiple times. In order to prevent that from happening, the temp |
| 135 // directory's name will be computed before the LastChromeVersion file is |
| 136 // read. Because the deletion will be scheduled after the singleton is |
| 137 // acquired, the directory can only be moved twice in the worst case. |
| 138 // Also, doing this after the downgrade version check to reduce performance |
| 139 // cost for the normal launch. |
| 140 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir)); |
| 141 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir)); |
| 142 if (GetLastChromeVersion(user_data_dir) > current_version) { |
| 143 // Do not recreate |disk_cache_dir| as it will be initialized later. |
| 144 RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false); |
| 145 RenameDirectory(user_data_dir, temp_user_data_dir, true); |
| 146 } |
| 147 } |
| 148 base::WriteFile(GetLastChromeVersionFile(user_data_dir), |
| 149 chrome::kChromeVersion, strlen(chrome::kChromeVersion)); |
| 150 } |
| 151 |
| 152 void DeleteMovedUserDataSoon() { |
| 153 base::FilePath user_data_dir; |
| 154 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 155 if (g_is_browser_test) { |
| 156 // Delete task will always be posted and executed for browser test. |
| 157 content::BrowserThread::PostBlockingPoolTask( |
| 158 FROM_HERE, |
| 159 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); |
| 160 } else { |
| 161 content::BrowserThread::PostAfterStartupTask( |
| 162 FROM_HERE, content::BrowserThread::GetBlockingPool() |
| 163 ->GetTaskRunnerWithShutdownBehavior( |
| 164 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), |
| 165 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); |
| 166 } |
| 167 } |
| 168 |
| 169 void SimulateDowngradeForTest() { |
| 170 g_is_browser_test = true; |
| 171 } |
| OLD | NEW |