| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 "cloud_print/common/win/install_utils.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/file_version_info_win.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/process/launch.h" |
| 15 #include "base/win/current_module.h" |
| 16 #include "base/win/registry.h" |
| 17 #include "cloud_print/common/win/cloud_print_utils.h" |
| 18 |
| 19 namespace cloud_print { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Google Update related constants. |
| 24 const wchar_t kClientsKey[] = L"SOFTWARE\\Google\\Update\\Clients\\"; |
| 25 const wchar_t kClientStateKey[] = L"SOFTWARE\\Google\\Update\\ClientState\\"; |
| 26 const wchar_t kVersionKey[] = L"pv"; |
| 27 const wchar_t kNameKey[] = L"name"; |
| 28 |
| 29 enum InstallerResult { |
| 30 INSTALLER_RESULT_FAILED_CUSTOM_ERROR = 1, |
| 31 INSTALLER_RESULT_FAILED_SYSTEM_ERROR = 3, |
| 32 }; |
| 33 |
| 34 const wchar_t kRegValueInstallerResult[] = L"InstallerResult"; |
| 35 const wchar_t kRegValueInstallerResultUIString[] = L"InstallerResultUIString"; |
| 36 const wchar_t kRegValueInstallerError[] = L"InstallerError"; |
| 37 |
| 38 // Uninstall related constants. |
| 39 const wchar_t kUninstallKey[] = |
| 40 L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"; |
| 41 const wchar_t kInstallLocation[] = L"InstallLocation"; |
| 42 const wchar_t kUninstallString[] = L"UninstallString"; |
| 43 const wchar_t kDisplayVersion[] = L"DisplayVersion"; |
| 44 const wchar_t kDisplayIcon[] = L"DisplayIcon"; |
| 45 const wchar_t kDisplayName[] = L"DisplayName"; |
| 46 const wchar_t kPublisher[] = L"Publisher"; |
| 47 const wchar_t kNoModify[] = L"NoModify"; |
| 48 const wchar_t kNoRepair[] = L"NoRepair"; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 void SetGoogleUpdateKeys(const base::string16& product_id, |
| 53 const base::string16& product_name) { |
| 54 base::win::RegKey key; |
| 55 if (key.Create(HKEY_LOCAL_MACHINE, |
| 56 (cloud_print::kClientsKey + product_id).c_str(), |
| 57 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 58 LOG(ERROR) << "Unable to open key"; |
| 59 } |
| 60 |
| 61 // Get the version from the resource file. |
| 62 base::string16 version_string; |
| 63 std::unique_ptr<FileVersionInfo> version_info( |
| 64 FileVersionInfo::CreateFileVersionInfoForModule(CURRENT_MODULE())); |
| 65 if (version_info.get()) { |
| 66 FileVersionInfoWin* version_info_win = |
| 67 static_cast<FileVersionInfoWin*>(version_info.get()); |
| 68 version_string = version_info_win->product_version(); |
| 69 } else { |
| 70 LOG(ERROR) << "Unable to get version string"; |
| 71 // Use a random version string so that Google Update has something to go by. |
| 72 version_string = L"0.0.0.99"; |
| 73 } |
| 74 |
| 75 if (key.WriteValue(kVersionKey, version_string.c_str()) != ERROR_SUCCESS || |
| 76 key.WriteValue(kNameKey, product_name.c_str()) != ERROR_SUCCESS) { |
| 77 LOG(ERROR) << "Unable to set registry keys"; |
| 78 } |
| 79 } |
| 80 |
| 81 void SetGoogleUpdateError(const base::string16& product_id, |
| 82 const base::string16& message) { |
| 83 LOG(ERROR) << message; |
| 84 base::win::RegKey key; |
| 85 if (key.Create(HKEY_LOCAL_MACHINE, |
| 86 (cloud_print::kClientStateKey + product_id).c_str(), |
| 87 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 88 LOG(ERROR) << "Unable to open key"; |
| 89 } |
| 90 |
| 91 if (key.WriteValue(kRegValueInstallerResult, |
| 92 INSTALLER_RESULT_FAILED_CUSTOM_ERROR) != ERROR_SUCCESS || |
| 93 key.WriteValue(kRegValueInstallerResultUIString, message.c_str()) != |
| 94 ERROR_SUCCESS) { |
| 95 LOG(ERROR) << "Unable to set registry keys"; |
| 96 } |
| 97 } |
| 98 |
| 99 void SetGoogleUpdateError(const base::string16& product_id, HRESULT hr) { |
| 100 LOG(ERROR) << cloud_print::GetErrorMessage(hr); |
| 101 base::win::RegKey key; |
| 102 if (key.Create(HKEY_LOCAL_MACHINE, |
| 103 (cloud_print::kClientStateKey + product_id).c_str(), |
| 104 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 105 LOG(ERROR) << "Unable to open key"; |
| 106 } |
| 107 |
| 108 if (key.WriteValue(kRegValueInstallerResult, |
| 109 INSTALLER_RESULT_FAILED_SYSTEM_ERROR) != ERROR_SUCCESS || |
| 110 key.WriteValue(kRegValueInstallerError, hr) != ERROR_SUCCESS) { |
| 111 LOG(ERROR) << "Unable to set registry keys"; |
| 112 } |
| 113 } |
| 114 |
| 115 void DeleteGoogleUpdateKeys(const base::string16& product_id) { |
| 116 base::win::RegKey key; |
| 117 if (key.Open(HKEY_LOCAL_MACHINE, |
| 118 (cloud_print::kClientsKey + product_id).c_str(), |
| 119 DELETE) != ERROR_SUCCESS) { |
| 120 LOG(ERROR) << "Unable to open key to delete"; |
| 121 return; |
| 122 } |
| 123 if (key.DeleteKey(L"") != ERROR_SUCCESS) { |
| 124 LOG(ERROR) << "Unable to delete key"; |
| 125 } |
| 126 } |
| 127 |
| 128 void CreateUninstallKey(const base::string16& uninstall_id, |
| 129 const base::string16& product_name, |
| 130 const std::string& uninstall_switch) { |
| 131 // Now write the Windows Uninstall entries |
| 132 // Minimal error checking here since the install can continue |
| 133 // if this fails. |
| 134 base::win::RegKey key; |
| 135 if (key.Create(HKEY_LOCAL_MACHINE, |
| 136 (cloud_print::kUninstallKey + uninstall_id).c_str(), |
| 137 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 138 LOG(ERROR) << "Unable to open key"; |
| 139 return; |
| 140 } |
| 141 |
| 142 base::FilePath unstall_binary; |
| 143 CHECK(PathService::Get(base::FILE_EXE, &unstall_binary)); |
| 144 |
| 145 base::CommandLine uninstall_command(unstall_binary); |
| 146 uninstall_command.AppendSwitch(uninstall_switch); |
| 147 key.WriteValue(kUninstallString, |
| 148 uninstall_command.GetCommandLineString().c_str()); |
| 149 key.WriteValue(kInstallLocation, unstall_binary.DirName().value().c_str()); |
| 150 |
| 151 // Get the version resource. |
| 152 std::unique_ptr<FileVersionInfo> version_info( |
| 153 FileVersionInfo::CreateFileVersionInfoForModule(CURRENT_MODULE())); |
| 154 |
| 155 if (version_info.get()) { |
| 156 FileVersionInfoWin* version_info_win = |
| 157 static_cast<FileVersionInfoWin*>(version_info.get()); |
| 158 key.WriteValue(kDisplayVersion, version_info_win->file_version().c_str()); |
| 159 key.WriteValue(kPublisher, version_info_win->company_name().c_str()); |
| 160 } else { |
| 161 LOG(ERROR) << "Unable to get version string"; |
| 162 } |
| 163 key.WriteValue(kDisplayName, product_name.c_str()); |
| 164 key.WriteValue(kDisplayIcon, unstall_binary.value().c_str()); |
| 165 key.WriteValue(kNoModify, 1); |
| 166 key.WriteValue(kNoRepair, 1); |
| 167 } |
| 168 |
| 169 void DeleteUninstallKey(const base::string16& uninstall_id) { |
| 170 ::RegDeleteKey(HKEY_LOCAL_MACHINE, |
| 171 (cloud_print::kUninstallKey + uninstall_id).c_str()); |
| 172 } |
| 173 |
| 174 base::FilePath GetInstallLocation(const base::string16& uninstall_id) { |
| 175 base::win::RegKey key; |
| 176 if (key.Open(HKEY_LOCAL_MACHINE, |
| 177 (cloud_print::kUninstallKey + uninstall_id).c_str(), |
| 178 KEY_QUERY_VALUE) != ERROR_SUCCESS) { |
| 179 // Not installed. |
| 180 return base::FilePath(); |
| 181 } |
| 182 base::string16 install_path_value; |
| 183 key.ReadValue(kInstallLocation, &install_path_value); |
| 184 return base::FilePath(install_path_value); |
| 185 } |
| 186 |
| 187 void DeleteProgramDir(const std::string& delete_switch) { |
| 188 base::FilePath installer_source; |
| 189 if (!PathService::Get(base::FILE_EXE, &installer_source)) |
| 190 return; |
| 191 // Deletes only subdirs of program files. |
| 192 if (!IsProgramsFilesParent(installer_source)) |
| 193 return; |
| 194 base::FilePath temp_path; |
| 195 if (!base::CreateTemporaryFile(&temp_path)) |
| 196 return; |
| 197 base::CopyFile(installer_source, temp_path); |
| 198 base::DeleteFileAfterReboot(temp_path); |
| 199 base::CommandLine command_line(temp_path); |
| 200 command_line.AppendSwitchPath(delete_switch, installer_source.DirName()); |
| 201 base::LaunchOptions options; |
| 202 if (!base::LaunchProcess(command_line, options).IsValid()) { |
| 203 LOG(ERROR) << "Unable to launch child uninstall."; |
| 204 } |
| 205 } |
| 206 |
| 207 bool IsProgramsFilesParent(const base::FilePath& path) { |
| 208 base::FilePath program_files; |
| 209 if (!PathService::Get(base::DIR_PROGRAM_FILESX86, &program_files)) |
| 210 return false; |
| 211 return program_files.IsParent(path); |
| 212 } |
| 213 |
| 214 } // namespace cloud_print |
| OLD | NEW |