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.
5 #include "cloud_print/common/win/install_utils.h"
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/registry.h"
16 #include "cloud_print/common/win/cloud_print_utils.h"
18 namespace cloud_print
{
22 // Google Update related constants.
23 const wchar_t kClientsKey
[] = L
"SOFTWARE\\Google\\Update\\Clients\\";
24 const wchar_t kClientStateKey
[] = L
"SOFTWARE\\Google\\Update\\ClientState\\";
25 const wchar_t kVersionKey
[] = L
"pv";
26 const wchar_t kNameKey
[] = L
"name";
28 enum InstallerResult
{
29 INSTALLER_RESULT_FAILED_CUSTOM_ERROR
= 1,
30 INSTALLER_RESULT_FAILED_SYSTEM_ERROR
= 3,
33 const wchar_t kRegValueInstallerResult
[] = L
"InstallerResult";
34 const wchar_t kRegValueInstallerResultUIString
[] = L
"InstallerResultUIString";
35 const wchar_t kRegValueInstallerError
[] = L
"InstallerError";
37 // Uninstall related constants.
38 const wchar_t kUninstallKey
[] =
39 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
40 const wchar_t kInstallLocation
[] = L
"InstallLocation";
41 const wchar_t kUninstallString
[] = L
"UninstallString";
42 const wchar_t kDisplayVersion
[] = L
"DisplayVersion";
43 const wchar_t kDisplayIcon
[] = L
"DisplayIcon";
44 const wchar_t kDisplayName
[] = L
"DisplayName";
45 const wchar_t kPublisher
[] = L
"Publisher";
46 const wchar_t kNoModify
[] = L
"NoModify";
47 const wchar_t kNoRepair
[] = L
"NoRepair";
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";
61 // Get the version from the resource file.
62 base::string16 version_string
;
63 scoped_ptr
<FileVersionInfo
> version_info(
64 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE());
66 if (version_info
.get()) {
67 FileVersionInfoWin
* version_info_win
=
68 static_cast<FileVersionInfoWin
*>(version_info
.get());
69 version_string
= version_info_win
->product_version();
71 LOG(ERROR
) << "Unable to get version string";
72 // Use a random version string so that Google Update has something to go by.
73 version_string
= L
"0.0.0.99";
76 if (key
.WriteValue(kVersionKey
, version_string
.c_str()) != ERROR_SUCCESS
||
77 key
.WriteValue(kNameKey
, product_name
.c_str()) != ERROR_SUCCESS
) {
78 LOG(ERROR
) << "Unable to set registry keys";
82 void SetGoogleUpdateError(const base::string16
& product_id
,
83 const base::string16
& message
) {
84 LOG(ERROR
) << message
;
85 base::win::RegKey key
;
86 if (key
.Create(HKEY_LOCAL_MACHINE
,
87 (cloud_print::kClientStateKey
+ product_id
).c_str(),
88 KEY_SET_VALUE
) != ERROR_SUCCESS
) {
89 LOG(ERROR
) << "Unable to open key";
92 if (key
.WriteValue(kRegValueInstallerResult
,
93 INSTALLER_RESULT_FAILED_CUSTOM_ERROR
) != ERROR_SUCCESS
||
94 key
.WriteValue(kRegValueInstallerResultUIString
,
95 message
.c_str()) != ERROR_SUCCESS
) {
96 LOG(ERROR
) << "Unable to set registry keys";
100 void SetGoogleUpdateError(const base::string16
& product_id
, HRESULT hr
) {
101 LOG(ERROR
) << cloud_print::GetErrorMessage(hr
);
102 base::win::RegKey key
;
103 if (key
.Create(HKEY_LOCAL_MACHINE
,
104 (cloud_print::kClientStateKey
+ product_id
).c_str(),
105 KEY_SET_VALUE
) != ERROR_SUCCESS
) {
106 LOG(ERROR
) << "Unable to open key";
109 if (key
.WriteValue(kRegValueInstallerResult
,
110 INSTALLER_RESULT_FAILED_SYSTEM_ERROR
) != ERROR_SUCCESS
||
111 key
.WriteValue(kRegValueInstallerError
, hr
) != ERROR_SUCCESS
) {
112 LOG(ERROR
) << "Unable to set registry keys";
116 void DeleteGoogleUpdateKeys(const base::string16
& product_id
) {
117 base::win::RegKey key
;
118 if (key
.Open(HKEY_LOCAL_MACHINE
,
119 (cloud_print::kClientsKey
+ product_id
).c_str(),
120 DELETE
) != ERROR_SUCCESS
) {
121 LOG(ERROR
) << "Unable to open key to delete";
124 if (key
.DeleteKey(L
"") != ERROR_SUCCESS
) {
125 LOG(ERROR
) << "Unable to delete key";
129 void CreateUninstallKey(const base::string16
& uninstall_id
,
130 const base::string16
& product_name
,
131 const std::string
& uninstall_switch
) {
132 // Now write the Windows Uninstall entries
133 // Minimal error checking here since the install can continue
135 base::win::RegKey key
;
136 if (key
.Create(HKEY_LOCAL_MACHINE
,
137 (cloud_print::kUninstallKey
+ uninstall_id
).c_str(),
138 KEY_SET_VALUE
) != ERROR_SUCCESS
) {
139 LOG(ERROR
) << "Unable to open key";
143 base::FilePath unstall_binary
;
144 CHECK(PathService::Get(base::FILE_EXE
, &unstall_binary
));
146 base::CommandLine
uninstall_command(unstall_binary
);
147 uninstall_command
.AppendSwitch(uninstall_switch
);
148 key
.WriteValue(kUninstallString
,
149 uninstall_command
.GetCommandLineString().c_str());
150 key
.WriteValue(kInstallLocation
,
151 unstall_binary
.DirName().value().c_str());
153 // Get the version resource.
154 scoped_ptr
<FileVersionInfo
> version_info(
155 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE());
157 if (version_info
.get()) {
158 FileVersionInfoWin
* version_info_win
=
159 static_cast<FileVersionInfoWin
*>(version_info
.get());
160 key
.WriteValue(kDisplayVersion
,
161 version_info_win
->file_version().c_str());
162 key
.WriteValue(kPublisher
, version_info_win
->company_name().c_str());
164 LOG(ERROR
) << "Unable to get version string";
166 key
.WriteValue(kDisplayName
, product_name
.c_str());
167 key
.WriteValue(kDisplayIcon
, unstall_binary
.value().c_str());
168 key
.WriteValue(kNoModify
, 1);
169 key
.WriteValue(kNoRepair
, 1);
172 void DeleteUninstallKey(const base::string16
& uninstall_id
) {
173 ::RegDeleteKey(HKEY_LOCAL_MACHINE
,
174 (cloud_print::kUninstallKey
+ uninstall_id
).c_str());
177 base::FilePath
GetInstallLocation(const base::string16
& uninstall_id
) {
178 base::win::RegKey key
;
179 if (key
.Open(HKEY_LOCAL_MACHINE
,
180 (cloud_print::kUninstallKey
+ uninstall_id
).c_str(),
181 KEY_QUERY_VALUE
) != ERROR_SUCCESS
) {
183 return base::FilePath();
185 base::string16 install_path_value
;
186 key
.ReadValue(kInstallLocation
, &install_path_value
);
187 return base::FilePath(install_path_value
);
190 void DeleteProgramDir(const std::string
& delete_switch
) {
191 base::FilePath installer_source
;
192 if (!PathService::Get(base::FILE_EXE
, &installer_source
))
194 // Deletes only subdirs of program files.
195 if (!IsProgramsFilesParent(installer_source
))
197 base::FilePath temp_path
;
198 if (!base::CreateTemporaryFile(&temp_path
))
200 base::CopyFile(installer_source
, temp_path
);
201 base::DeleteFileAfterReboot(temp_path
);
202 base::CommandLine
command_line(temp_path
);
203 command_line
.AppendSwitchPath(delete_switch
, installer_source
.DirName());
204 base::LaunchOptions options
;
205 if (!base::LaunchProcess(command_line
, options
).IsValid()) {
206 LOG(ERROR
) << "Unable to launch child uninstall.";
210 bool IsProgramsFilesParent(const base::FilePath
& path
) {
211 base::FilePath program_files
;
212 if (!PathService::Get(base::DIR_PROGRAM_FILESX86
, &program_files
))
214 return program_files
.IsParent(path
);
217 } // namespace cloud_print