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