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/path_service.h" |
| 13 #include "base/win/registry.h" |
| 14 |
| 15 namespace cloud_print { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Google Update related constants. |
| 20 const wchar_t kClientsKey[] = L"SOFTWARE\\Google\\Update\\Clients\\"; |
| 21 const wchar_t kClientStateKey[] = L"SOFTWARE\\Google\\Update\\ClientState\\"; |
| 22 const wchar_t* kUsageKey = L"dr"; |
| 23 const wchar_t kVersionKey[] = L"pv"; |
| 24 const wchar_t kNameKey[] = L"name"; |
| 25 const DWORD kInstallerResultFailedCustomError = 1; |
| 26 const wchar_t kRegValueInstallerResult[] = L"InstallerResult"; |
| 27 const wchar_t kRegValueInstallerResultUIString[] = L"InstallerResultUIString"; |
| 28 |
| 29 // Uninstall related constants. |
| 30 const wchar_t kUninstallKey[] = |
| 31 L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"; |
| 32 const wchar_t kInstallLocation[] = L"InstallLocation"; |
| 33 const wchar_t kDisplayVersion[] = L"DisplayVersion"; |
| 34 const wchar_t kDisplayName[] = L"DisplayName"; |
| 35 const wchar_t kPublisher[] = L"Publisher"; |
| 36 const wchar_t kNoModify[] = L"NoModify"; |
| 37 const wchar_t kNoRepair[] = L"NoRepair"; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 |
| 42 void SetGoogleUpdateKeys(const string16& product_id, |
| 43 const string16& product_name) { |
| 44 base::win::RegKey key; |
| 45 if (key.Create(HKEY_LOCAL_MACHINE, |
| 46 (cloud_print::kClientsKey + product_id).c_str(), |
| 47 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 48 LOG(ERROR) << "Unable to open key"; |
| 49 } |
| 50 |
| 51 // Get the version from the resource file. |
| 52 string16 version_string; |
| 53 scoped_ptr<FileVersionInfo> version_info( |
| 54 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 55 |
| 56 if (version_info.get()) { |
| 57 FileVersionInfoWin* version_info_win = |
| 58 static_cast<FileVersionInfoWin*>(version_info.get()); |
| 59 version_string = version_info_win->product_version(); |
| 60 } else { |
| 61 LOG(ERROR) << "Unable to get version string"; |
| 62 // Use a random version string so that Google Update has something to go by. |
| 63 version_string = L"0.0.0.99"; |
| 64 } |
| 65 |
| 66 if (key.WriteValue(kVersionKey, version_string.c_str()) != ERROR_SUCCESS || |
| 67 key.WriteValue(kNameKey, product_name.c_str()) != ERROR_SUCCESS) { |
| 68 LOG(ERROR) << "Unable to set registry keys"; |
| 69 } |
| 70 } |
| 71 |
| 72 void SetGoogleUpdateError(const string16& product_id, const string16& message) { |
| 73 LOG(ERROR) << message; |
| 74 base::win::RegKey key; |
| 75 if (key.Create(HKEY_LOCAL_MACHINE, |
| 76 (cloud_print::kClientStateKey + product_id).c_str(), |
| 77 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 78 LOG(ERROR) << "Unable to open key"; |
| 79 } |
| 80 |
| 81 if (key.WriteValue(kRegValueInstallerResult, |
| 82 kInstallerResultFailedCustomError) != ERROR_SUCCESS || |
| 83 key.WriteValue(kRegValueInstallerResultUIString, |
| 84 message.c_str()) != ERROR_SUCCESS) { |
| 85 LOG(ERROR) << "Unable to set registry keys"; |
| 86 } |
| 87 } |
| 88 |
| 89 void DeleteGoogleUpdateKeys(const string16& product_id) { |
| 90 base::win::RegKey key; |
| 91 if (key.Open(HKEY_LOCAL_MACHINE, |
| 92 (cloud_print::kClientsKey + product_id).c_str(), |
| 93 DELETE) != ERROR_SUCCESS) { |
| 94 LOG(ERROR) << "Unable to open key to delete"; |
| 95 return; |
| 96 } |
| 97 if (key.DeleteKey(L"") != ERROR_SUCCESS) { |
| 98 LOG(ERROR) << "Unable to delete key"; |
| 99 } |
| 100 } |
| 101 |
| 102 void CreateUninstallKey(const string16& uninstall_id, |
| 103 const string16& product_name, |
| 104 const std::string& uninstall_switch) { |
| 105 // Now write the Windows Uninstall entries |
| 106 // Minimal error checking here since the install can continue |
| 107 // if this fails. |
| 108 base::win::RegKey key; |
| 109 if (key.Create(HKEY_LOCAL_MACHINE, |
| 110 (cloud_print::kUninstallKey + uninstall_id).c_str(), |
| 111 KEY_SET_VALUE) != ERROR_SUCCESS) { |
| 112 LOG(ERROR) << "Unable to open key"; |
| 113 return; |
| 114 } |
| 115 |
| 116 base::FilePath unstall_binary; |
| 117 CHECK(PathService::Get(base::FILE_EXE, &unstall_binary)); |
| 118 |
| 119 CommandLine uninstall_command(unstall_binary); |
| 120 uninstall_command.AppendSwitch(uninstall_switch); |
| 121 key.WriteValue(kUninstallKey, |
| 122 uninstall_command.GetCommandLineString().c_str()); |
| 123 key.WriteValue(kInstallLocation, |
| 124 unstall_binary.DirName().value().c_str()); |
| 125 |
| 126 // Get the version resource. |
| 127 scoped_ptr<FileVersionInfo> version_info( |
| 128 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 129 |
| 130 if (version_info.get()) { |
| 131 FileVersionInfoWin* version_info_win = |
| 132 static_cast<FileVersionInfoWin*>(version_info.get()); |
| 133 key.WriteValue(kDisplayVersion, |
| 134 version_info_win->file_version().c_str()); |
| 135 key.WriteValue(kPublisher, version_info_win->company_name().c_str()); |
| 136 } else { |
| 137 LOG(ERROR) << "Unable to get version string"; |
| 138 } |
| 139 key.WriteValue(kDisplayName, product_name.c_str()); |
| 140 key.WriteValue(kNoModify, 1); |
| 141 key.WriteValue(kNoRepair, 1); |
| 142 } |
| 143 |
| 144 void DeleteUninstallKey(const string16& uninstall_id) { |
| 145 ::RegDeleteKey(HKEY_LOCAL_MACHINE, |
| 146 (cloud_print::kUninstallKey + uninstall_id).c_str()); |
| 147 } |
| 148 |
| 149 base::FilePath GetInstallLocation(const string16& uninstall_id) { |
| 150 base::win::RegKey key; |
| 151 if (key.Open(HKEY_LOCAL_MACHINE, |
| 152 (cloud_print::kUninstallKey + uninstall_id).c_str(), |
| 153 KEY_QUERY_VALUE) != ERROR_SUCCESS) { |
| 154 // Not installed. |
| 155 return base::FilePath(); |
| 156 } |
| 157 string16 install_path_value; |
| 158 key.ReadValue(kInstallLocation, &install_path_value); |
| 159 return base::FilePath(install_path_value); |
| 160 } |
| 161 |
| 162 } // namespace cloud_print |
| 163 |
OLD | NEW |