Chromium Code Reviews| 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/google_update_constants.h" | |
| 27 #include "chrome/installer/util/install_util.h" | |
| 28 #include "content/public/browser/browser_thread.h" | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 bool g_is_browser_test = false; | |
| 33 | |
| 34 // Return the disk cache dir override value if exists or empty path for default | |
| 35 // disk cache dir. | |
| 36 base::FilePath GetDiskCacheDir() { | |
| 37 base::FilePath disk_cache_dir; | |
| 38 policy::path_parser::CheckDiskCacheDirPolicy(&disk_cache_dir); | |
| 39 if (disk_cache_dir.empty()) { | |
| 40 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 41 disk_cache_dir = command_line->GetSwitchValuePath(switches::kDiskCacheDir); | |
| 42 } | |
| 43 if (disk_cache_dir.ReferencesParent()) | |
| 44 return base::MakeAbsoluteFilePath(disk_cache_dir); | |
| 45 return disk_cache_dir; | |
| 46 } | |
| 47 | |
| 48 base::FilePath GetLastVersionFile(const base::FilePath& user_data_dir) { | |
| 49 return user_data_dir.Append(kDowngradeLastVersionFile); | |
| 50 } | |
| 51 | |
| 52 // Return the temporary path that |source_path| will be renamed to later. | |
| 53 base::FilePath GetTempDirNameForDelete(const base::FilePath& source_path) { | |
| 54 if (source_path.empty()) | |
| 55 return base::FilePath(); | |
| 56 | |
| 57 base::FilePath target_path(source_path.AddExtension(kDowngradeDeleteSuffix)); | |
| 58 int uniquifier = | |
| 59 base::GetUniquePathNumber(source_path, kDowngradeDeleteSuffix); | |
| 60 if (uniquifier < 0) | |
| 61 return base::FilePath(); | |
| 62 if (uniquifier > 0) { | |
| 63 return target_path.InsertBeforeExtensionASCII( | |
| 64 base::StringPrintf(" (%d)", uniquifier)); | |
| 65 } | |
| 66 | |
| 67 return target_path; | |
| 68 } | |
| 69 | |
| 70 // Rename the |source| directory to |target|. Create |source| directory after | |
| 71 // rename if |recreate| is true. | |
| 72 void RenameDirectory(const base::FilePath& source, | |
| 73 const base::FilePath& target, | |
| 74 bool recreate) { | |
| 75 if (!source.empty() && !target.empty() && base::Move(source, target) && | |
| 76 recreate) { | |
| 77 base::CreateDirectory(source); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void DeleteAllRenamedUserDirectories(const base::FilePath& path) { | |
| 82 if (path.empty()) | |
| 83 return; | |
| 84 base::FilePath dir_name = path.DirName(); | |
| 85 // Does not support root directory | |
| 86 if (dir_name == path) | |
| 87 return; | |
| 88 | |
| 89 base::FilePath::StringType pattern = | |
| 90 path.BaseName().value() + FILE_PATH_LITERAL("*") + kDowngradeDeleteSuffix; | |
| 91 base::FileEnumerator enumerator(dir_name, false, | |
| 92 base::FileEnumerator::DIRECTORIES, pattern); | |
| 93 for (base::FilePath dir = enumerator.Next(); !dir.empty(); | |
| 94 dir = enumerator.Next()) { | |
| 95 base::DeleteFile(dir, true); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void DeleteMovedUserData(const base::FilePath& user_data_dir, | |
| 100 const base::FilePath& disk_cache_dir) { | |
| 101 DeleteAllRenamedUserDirectories(user_data_dir); | |
| 102 DeleteAllRenamedUserDirectories(disk_cache_dir); | |
| 103 } | |
| 104 | |
| 105 enum InstallLevel { SYSTEM_INSTALL, USER_INSTALL, UNKNOWN }; | |
| 106 | |
| 107 InstallLevel GetInstallLevel() { | |
| 108 base::FilePath cur_chrome_exe; | |
| 109 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe)) | |
| 110 return UNKNOWN; | |
| 111 if (InstallUtil::IsPerUserInstall(cur_chrome_exe)) | |
| 112 return USER_INSTALL; | |
| 113 else | |
| 114 return SYSTEM_INSTALL; | |
| 115 } | |
| 116 | |
| 117 } // namespace | |
| 118 | |
| 119 const base::FilePath::CharType kDowngradeLastVersionFile[] = | |
| 120 FILE_PATH_LITERAL("Last Version"); | |
| 121 const base::FilePath::CharType kDowngradeDeleteSuffix[] = | |
| 122 FILE_PATH_LITERAL(".CHROME_DELETE"); | |
| 123 | |
| 124 void MoveUserDataForFirstRunAfterDowngrade() { | |
| 125 base::FilePath user_data_dir; | |
| 126 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) | |
| 127 return; | |
| 128 InstallLevel install_level = GetInstallLevel(); | |
| 129 if (install_level == UNKNOWN) | |
| 130 return; | |
| 131 base::Version current_version(chrome::kChromeVersion); | |
| 132 base::Version downgrade_version = InstallUtil::GetDowngradeVersion( | |
| 133 install_level == SYSTEM_INSTALL, BrowserDistribution::GetDistribution()); | |
| 134 if (downgrade_version.IsValid() && downgrade_version > current_version) { | |
| 135 base::FilePath disk_cache_dir(GetDiskCacheDir()); | |
| 136 // Without the browser process singleton protection, the directory may be | |
| 137 // copied multiple times. In order to prevent that from happening, the temp | |
| 138 // directory's name will be computed before the Chrome Version file is | |
| 139 // read. Because the deletion will be scheduled after the singleton is | |
| 140 // acquired, the directory can only be moved twice in the worst case. | |
| 141 // Also, doing this after the downgrade version check to reduce performance | |
| 142 // cost for the normal launch. | |
| 143 base::FilePath temp_disk_cache_dir(GetTempDirNameForDelete(disk_cache_dir)); | |
| 144 base::FilePath temp_user_data_dir(GetTempDirNameForDelete(user_data_dir)); | |
| 145 base::Version last_version = GetLastVersion(user_data_dir); | |
| 146 if (last_version.IsValid() && last_version > current_version) { | |
| 147 // Do not recreate |disk_cache_dir| as it will be initialized later. | |
| 148 RenameDirectory(disk_cache_dir, temp_disk_cache_dir, false); | |
| 149 RenameDirectory(user_data_dir, temp_user_data_dir, true); | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void UpdateLastVersion(const base::FilePath& user_data_dir) { | |
| 155 base::WriteFile(GetLastVersionFile(user_data_dir), chrome::kChromeVersion, | |
| 156 strlen(chrome::kChromeVersion)); | |
| 157 } | |
| 158 | |
| 159 base::Version GetLastVersion(const base::FilePath& user_data_dir) { | |
|
brettw
2016/06/10 22:42:42
I really think we need some kind of namespace for
| |
| 160 std::string last_version_str; | |
| 161 if (base::ReadFileToString(GetLastVersionFile(user_data_dir), | |
| 162 &last_version_str)) { | |
| 163 return base::Version( | |
| 164 base::TrimWhitespaceASCII(last_version_str, base::TRIM_ALL) | |
| 165 .as_string()); | |
| 166 } | |
| 167 return base::Version(); | |
| 168 } | |
| 169 | |
| 170 void DeleteMovedUserDataSoon() { | |
| 171 base::FilePath user_data_dir; | |
| 172 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 173 if (g_is_browser_test) { | |
| 174 // Delete task will always be posted and executed for browser test. | |
| 175 content::BrowserThread::PostBlockingPoolTask( | |
| 176 FROM_HERE, | |
| 177 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); | |
| 178 } else { | |
| 179 content::BrowserThread::PostAfterStartupTask( | |
| 180 FROM_HERE, content::BrowserThread::GetBlockingPool() | |
| 181 ->GetTaskRunnerWithShutdownBehavior( | |
| 182 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), | |
| 183 base::Bind(&DeleteMovedUserData, user_data_dir, GetDiskCacheDir())); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 bool IsMSIInstall() { | |
| 188 InstallLevel install_level = GetInstallLevel(); | |
| 189 base::win::RegKey key; | |
| 190 DWORD is_msi = 3; | |
| 191 return install_level != UNKNOWN && | |
| 192 key.Open((install_level == SYSTEM_INSTALL) ? HKEY_LOCAL_MACHINE | |
| 193 : HKEY_CURRENT_USER, | |
| 194 BrowserDistribution::GetDistribution()->GetStateKey().c_str(), | |
| 195 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS && | |
| 196 key.ReadValueDW(google_update::kRegMSIField, &is_msi) == | |
| 197 ERROR_SUCCESS && | |
| 198 is_msi != 0; | |
| 199 } | |
| 200 | |
| 201 void SimulateDowngradeForTest(bool is_browser_test) { | |
|
brettw
2016/06/10 22:42:42
I was confused about this function since from the
| |
| 202 g_is_browser_test = is_browser_test; | |
| 203 } | |
| OLD | NEW |