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