19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Copyright (c) 2012 The Chromium Authors. All rights reserved. 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Use of this source code is governed by a BSD-style license that can be 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// found in the LICENSE file. 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h> 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <setupapi.h> // Must be included after windows.h 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <winspool.h> 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <iomanip> 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/at_exit.h" 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/command_line.h" 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/file_version_info_win.h" 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/files/file_util.h" 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/files/scoped_temp_dir.h" 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/logging.h" 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/path_service.h" 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/process/launch.h" 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/process/process.h" 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/string16.h" 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/strings/string_util.h" 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/win/registry.h" 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/win/scoped_handle.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "base/win/windows_version.h" 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "cloud_print/common/win/cloud_print_utils.h" 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "cloud_print/common/win/install_utils.h" 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "cloud_print/virtual_driver/win/virtual_driver_consts.h" 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "cloud_print/virtual_driver/win/virtual_driver_helpers.h" 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "grit/virtual_driver_setup_resources.h" 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <strsafe.h> // Must be after base headers to avoid deprecation 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // warnings. 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallnamespace cloud_print { 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallnamespace { 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kNameValue[] = L"GCP Virtual Driver"; 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kUninstallId[] = L"{74AA24E0-AC50-4B28-BA46-9CF05467C9B7}"; 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kInstallerName[] = L"virtual_driver_setup.exe"; 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kGcpUrl[] = L"http://www.google.com/cloudprint"; 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kDataFileName[] = L"gcp_driver.gpd"; 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kDriverName[] = L"MXDWDRV.DLL"; 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kUiDriverName[] = L"UNIDRVUI.DLL"; 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t kHelpName[] = L"UNIDRV.HLP"; 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst wchar_t* kDependencyList[] = { 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kDriverName, 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kHelpName, 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kUiDriverName, 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"STDDTYPE.GDL", 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"STDNAMES.GPD", 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"STDSCHEM.GDL", 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"STDSCHMX.GDL", 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"UNIDRV.DLL", 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"UNIRES.DLL", 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall L"XPSSVCS.DLL", 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char kDelete[] = "delete"; 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char kInstallSwitch[] = "install"; 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char kRegisterSwitch[] = "register"; 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char kUninstallSwitch[] = "uninstall"; 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char kUnregisterSwitch[] = "unregister"; 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbase::FilePath GetSystemPath(const base::string16& binary) { 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath path; 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!PathService::Get(base::DIR_SYSTEM, &path)) { 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to get system path."; 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return path; 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return path.Append(binary); 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbase::FilePath GetNativeSystemPath(const base::string16& binary) { 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!IsSystem64Bit()) 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return GetSystemPath(binary); 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath path; 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Sysnative will bypass filesystem redirection and give us 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // the location of the 64bit system32 from a 32 bit process. 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!PathService::Get(base::DIR_WINDOWS, &path)) { 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to get windows path."; 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return path; 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return path.Append(L"sysnative").Append(binary); 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SpoolerServiceCommand(const char* command) { 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath net_path = GetNativeSystemPath(L"net"); 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (net_path.empty()) 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CommandLine command_line(net_path); 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArg(command); 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArg("spooler"); 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArg("/y"); 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::LaunchOptions options; 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall options.wait = true; 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall options.start_hidden = true; 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall VLOG(0) << command_line.GetCommandLineString(); 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::LaunchProcess(command_line, options, NULL); 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT RegisterPortMonitor(bool install, const base::FilePath& install_path) { 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DCHECK(install || install_path.empty()); 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath target_path = GetNativeSystemPath(GetPortMonitorDllName()); 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (target_path.empty()) { 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to get port monitor target path."; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (install) { 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath source_path = 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall install_path.Append(GetPortMonitorDllName()); 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::CopyFile(source_path, target_path)) { 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable copy port monitor dll from " << 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall source_path.value() << " to " << target_path.value(); 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return GetLastHResult(); 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if (!base::PathExists(target_path)) { 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Already removed. Just "succeed" silently. 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath regsvr32_path = GetNativeSystemPath(L"regsvr32.exe"); 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (regsvr32_path.empty()) { 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Can't find regsvr32.exe."; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CommandLine command_line(regsvr32_path); 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArg("/s"); 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!install) { 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArg("/u"); 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Use system32 path here because otherwise ::AddMonitor would fail. 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command_line.AppendArgPath(GetSystemPath(GetPortMonitorDllName())); 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::LaunchOptions options; 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall options.wait = true; 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::win::ScopedHandle regsvr32_handle; 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::LaunchProcess(command_line.GetCommandLineString(), options, 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ®svr32_handle)) { 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to launch regsvr32.exe."; 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DWORD exit_code = S_OK; 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (install) { 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!GetExitCodeProcess(regsvr32_handle.Get(), &exit_code)) { 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to get regsvr32.exe exit code."; 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return GetLastHResult(); 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (exit_code != 0) { 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Regsvr32.exe failed with " << exit_code; 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(exit_code); 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::DeleteFile(target_path, false)) { 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SpoolerServiceCommand("stop"); 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall bool deleted = base::DeleteFile(target_path, false); 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SpoolerServiceCommand("start"); 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(!deleted) { 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to delete " << target_path.value(); 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED); 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDWORDLONG GetVersionNumber() { 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DWORDLONG retval = 0; 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall scoped_ptr<FileVersionInfo> version_info( 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall FileVersionInfo::CreateFileVersionInfoForCurrentModule()); 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (version_info.get()) { 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall FileVersionInfoWin* version_info_win = 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall static_cast<FileVersionInfoWin*>(version_info.get()); 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall VS_FIXEDFILEINFO* fixed_file_info = version_info_win->fixed_file_info(); 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall retval = fixed_file_info->dwFileVersionMS; 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall retval <<= 32; 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall retval |= fixed_file_info->dwFileVersionLS; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return retval; 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUINT CALLBACK CabinetCallback(PVOID data, 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UINT notification, 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UINT_PTR param1, 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UINT_PTR param2) { 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const base::FilePath* temp_path( 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall reinterpret_cast<const base::FilePath*>(data)); 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (notification == SPFILENOTIFY_FILEINCABINET) { 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall FILE_IN_CABINET_INFO* info = 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall reinterpret_cast<FILE_IN_CABINET_INFO*>(param1); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (int i = 0; i < arraysize(kDependencyList); i++) { 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath base_name(info->NameInCabinet); 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base_name = base_name.BaseName(); 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (base::FilePath::CompareEqualIgnoreCase(base_name.value().c_str(), 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kDependencyList[i])) { 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall StringCchCopy(info->FullTargetName, MAX_PATH, 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall temp_path->Append(kDependencyList[i]).value().c_str()); 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FILEOP_DOIT; 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return FILEOP_SKIP; 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return NO_ERROR; 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ReadyDriverDependencies(const base::FilePath& destination) { 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath destination_copy(destination); 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // GetCorePrinterDrivers and GetPrinterDriverPackagePath only exist on 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Vista and later. Winspool.drv must be delayloaded so these calls don't 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // create problems on XP. 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DWORD size = MAX_PATH; 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall wchar_t package_path[MAX_PATH] = {0}; 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CORE_PRINTER_DRIVER driver; 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall GetCorePrinterDrivers(NULL, NULL, L"{D20EA372-DD35-4950-9ED8-A6335AFE79F5}", 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1, &driver); 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall GetPrinterDriverPackagePath(NULL, NULL, NULL, driver.szPackageID, 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall package_path, MAX_PATH, &size); 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SetupIterateCabinet(package_path, 0, &CabinetCallback, &destination_copy); 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Driver files are in the sp3 cab. 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath package_path; 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall PathService::Get(base::DIR_WINDOWS, &package_path); 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall package_path = package_path.Append(L"Driver Cache\\i386\\sp3.cab"); 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SetupIterateCabinet(package_path.value().c_str(), 0, &CabinetCallback, 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &destination_copy); 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Copy the rest from the driver cache or system dir. 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath driver_cache_path; 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall PathService::Get(base::DIR_WINDOWS, &driver_cache_path); 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_cache_path = driver_cache_path.Append(L"Driver Cache\\i386"); 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (size_t i = 0; i < arraysize(kDependencyList); ++i) { 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath dst_path = destination.Append(kDependencyList[i]); 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::PathExists(dst_path)) { 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath src_path = driver_cache_path.Append(kDependencyList[i]); 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::PathExists(src_path)) 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall src_path = GetSystemPath(kDependencyList[i]); 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::CopyFile(src_path, dst_path); 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT InstallDriver(const base::FilePath& install_path) { 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::ScopedTempDir temp_path; 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!temp_path.CreateUniqueTempDir()) 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_CANNOT_MAKE); 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ReadyDriverDependencies(temp_path.path()); 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall std::vector<base::string16> dependent_array; 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Add all files. AddPrinterDriverEx will removes unnecessary. 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (size_t i = 0; i < arraysize(kDependencyList); ++i) { 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath file_path = temp_path.path().Append(kDependencyList[i]); 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (base::PathExists(file_path)) 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dependent_array.push_back(file_path.value()); 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(WARNING) << "File is missing: " << file_path.BaseName().value(); 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Set up paths for the files we depend on. 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath data_file = install_path.Append(kDataFileName); 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath xps_path = temp_path.path().Append(kDriverName); 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath ui_path = temp_path.path().Append(kUiDriverName); 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::FilePath ui_help_path = temp_path.path().Append(kHelpName); 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!base::PathExists(xps_path)) { 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_BAD_DRIVER); 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DRIVER_INFO_6 driver_info = {0}; 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Set up supported print system version. Must be 3. 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.cVersion = 3; 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // None of the print API structures likes constant strings even though they 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // don't modify the string. const_casting is the cleanest option. 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pDataFile = const_cast<LPWSTR>(data_file.value().c_str()); 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pHelpFile = const_cast<LPWSTR>(ui_help_path.value().c_str()); 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pDriverPath = const_cast<LPWSTR>(xps_path.value().c_str()); 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pConfigFile = const_cast<LPWSTR>(ui_path.value().c_str()); 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 dependent_files(JoinString(dependent_array, L'\n')); 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dependent_files.push_back(L'\n'); 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall std::replace(dependent_files.begin(), dependent_files.end(), L'\n', L'\0'); 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pDependentFiles = &dependent_files[0]; 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // Set up user visible strings. 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 manufacturer = LoadLocalString(IDS_GOOGLE); 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pszMfgName = const_cast<LPWSTR>(manufacturer.c_str()); 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pszProvider = const_cast<LPWSTR>(manufacturer.c_str()); 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pszOEMUrl = const_cast<LPWSTR>(kGcpUrl); 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.dwlDriverVersion = GetVersionNumber(); 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver_info.pName = const_cast<LPWSTR>(driver_name.c_str()); 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!::AddPrinterDriverEx(NULL, 6, reinterpret_cast<BYTE*>(&driver_info), 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall APD_COPY_NEW_FILES | APD_COPY_FROM_DIRECTORY)) { 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to add printer driver"; 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return GetLastHResult(); 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT UninstallDriver() { 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int tries = 3; 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while (!DeletePrinterDriverEx(NULL, 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NULL, 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const_cast<LPWSTR>(driver_name.c_str()), 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DPD_DELETE_UNUSED_FILES, 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 0) && tries > 0) { 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (GetLastError() == ERROR_UNKNOWN_PRINTER_DRIVER) { 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(WARNING) << "Print driver is already uninstalled."; 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // After deleting the printer it can take a few seconds before 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // the driver is free for deletion. Retry a few times before giving up. 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(WARNING) << "Attempt to delete printer driver failed. Retrying."; 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall tries--; 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sleep(2000); 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (tries <= 0) { 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HRESULT result = GetLastHResult(); 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to delete printer driver."; 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT InstallPrinter(void) { 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall PRINTER_INFO_2 printer_info = {0}; 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // None of the print API structures likes constant strings even though they 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // don't modify the string. const_casting is the cleanest option. 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pDriverName = const_cast<LPWSTR>(driver_name.c_str()); 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pPrinterName = const_cast<LPWSTR>(driver_name.c_str()); 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pComment = const_cast<LPWSTR>(driver_name.c_str()); 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pLocation = const_cast<LPWSTR>(kGcpUrl); 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 port_name; 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pPortName = const_cast<LPWSTR>(kPortName); 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.Attributes = PRINTER_ATTRIBUTE_DIRECT|PRINTER_ATTRIBUTE_LOCAL; 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_info.pPrintProcessor = const_cast<LPWSTR>(L"winprint"); 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HANDLE handle = AddPrinter(NULL, 2, reinterpret_cast<BYTE*>(&printer_info)); 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (handle == NULL) { 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HRESULT result = GetLastHResult(); 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to add printer"; 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ClosePrinter(handle); 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT UninstallPrinter(void) { 3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HANDLE handle = NULL; 3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall PRINTER_DEFAULTS printer_defaults = {0}; 3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printer_defaults.DesiredAccess = PRINTER_ALL_ACCESS; 3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); 3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!OpenPrinter(const_cast<LPWSTR>(driver_name.c_str()), 3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &handle, 3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &printer_defaults)) { 3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // If we can't open the printer, it was probably already removed. 3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(WARNING) << "Unable to open printer"; 3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!DeletePrinter(handle)) { 3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HRESULT result = GetLastHResult(); 3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to delete printer"; 3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ClosePrinter(handle); 3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ClosePrinter(handle); 3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbool IsOSSupported() { 3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // We don't support XP service pack 2 or older. 3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall base::win::Version version = base::win::GetVersion(); 3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (version > base::win::VERSION_XP) || 3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ((version == base::win::VERSION_XP) && 3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (base::win::OSInfo::GetInstance()->service_pack().major >= 3)); 3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT RegisterVirtualDriver(const base::FilePath& install_path) { 3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall HRESULT result = S_OK; 3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DCHECK(base::DirectoryExists(install_path)); 3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (!IsOSSupported()) { 3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Requires XP SP3 or later."; 3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION); 3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = InstallDriver(install_path); 3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (FAILED(result)) { 4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to install driver."; 4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = RegisterPortMonitor(true, install_path); 4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (FAILED(result)) { 4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to register port monitor."; 4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = InstallPrinter(); 4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (FAILED(result) && 4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result != HRESULT_FROM_WIN32(ERROR_PRINTER_ALREADY_EXISTS)) { 4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LOG(ERROR) << "Unable to install printer."; 4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return S_OK; 417} 418 419HRESULT TryUnregisterVirtualDriver() { 420 HRESULT result = S_OK; 421 result = UninstallPrinter(); 422 if (FAILED(result)) { 423 LOG(ERROR) << "Unable to delete printer."; 424 return result; 425 } 426 result = UninstallDriver(); 427 if (FAILED(result)) { 428 LOG(ERROR) << "Unable to remove driver."; 429 return result; 430 } 431 // The second argument is ignored if the first is false. 432 result = RegisterPortMonitor(false, base::FilePath()); 433 if (FAILED(result)) { 434 LOG(ERROR) << "Unable to remove port monitor."; 435 return result; 436 } 437 return S_OK; 438} 439 440HRESULT UnregisterVirtualDriver() { 441 HRESULT hr = S_FALSE; 442 for (int i = 0; i < 2; ++i) { 443 hr = TryUnregisterVirtualDriver(); 444 if (SUCCEEDED(hr)) { 445 break; 446 } 447 // Restart spooler and try again. 448 SpoolerServiceCommand("stop"); 449 SpoolerServiceCommand("start"); 450 } 451 return hr; 452} 453 454HRESULT DoUninstall() { 455 DeleteGoogleUpdateKeys(kGoogleUpdateProductId); 456 HRESULT result = UnregisterVirtualDriver(); 457 if (FAILED(result)) 458 return result; 459 DeleteUninstallKey(kUninstallId); 460 DeleteProgramDir(kDelete); 461 return S_OK; 462} 463 464HRESULT DoUnregister() { 465 return UnregisterVirtualDriver(); 466} 467 468HRESULT DoRegister(const base::FilePath& install_path) { 469 HRESULT result = UnregisterVirtualDriver(); 470 if (FAILED(result)) 471 return result; 472 return RegisterVirtualDriver(install_path); 473} 474 475HRESULT DoDelete(const base::FilePath& install_path) { 476 if (install_path.value().empty()) 477 return E_INVALIDARG; 478 if (!base::DirectoryExists(install_path)) 479 return S_FALSE; 480 Sleep(5000); // Give parent some time to exit. 481 return base::DeleteFile(install_path, true) ? S_OK : E_FAIL; 482} 483 484HRESULT DoInstall(const base::FilePath& install_path) { 485 HRESULT result = UnregisterVirtualDriver(); 486 if (FAILED(result)) { 487 LOG(ERROR) << "Unable to unregister."; 488 return result; 489 } 490 base::FilePath old_install_path = GetInstallLocation(kUninstallId); 491 if (!old_install_path.value().empty() && 492 install_path != old_install_path) { 493 if (base::DirectoryExists(old_install_path)) 494 base::DeleteFile(old_install_path, true); 495 } 496 CreateUninstallKey(kUninstallId, LoadLocalString(IDS_DRIVER_NAME), 497 kUninstallSwitch); 498 result = RegisterVirtualDriver(install_path); 499 if (FAILED(result)) 500 return result; 501 SetGoogleUpdateKeys(kGoogleUpdateProductId, kNameValue); 502 return result; 503} 504 505HRESULT ExecuteCommands() { 506 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 507 508 base::FilePath exe_path; 509 if (FAILED(PathService::Get(base::DIR_EXE, &exe_path)) || 510 !base::DirectoryExists(exe_path)) { 511 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); 512 } 513 514 if (command_line.HasSwitch(kDelete)) { 515 return DoDelete(command_line.GetSwitchValuePath(kDelete)); 516 } else if (command_line.HasSwitch(kUninstallSwitch)) { 517 return DoUninstall(); 518 } else if (command_line.HasSwitch(kInstallSwitch)) { 519 return DoInstall(exe_path); 520 } else if (command_line.HasSwitch(kUnregisterSwitch)) { 521 return DoUnregister(); 522 } else if (command_line.HasSwitch(kRegisterSwitch)) { 523 return DoRegister(exe_path); 524 } 525 526 return E_INVALIDARG; 527} 528 529} // namespace 530 531} // namespace cloud_print 532 533int WINAPI WinMain(__in HINSTANCE hInstance, 534 __in HINSTANCE hPrevInstance, 535 __in LPSTR lpCmdLine, 536 __in int nCmdShow) { 537 using namespace cloud_print; 538 539 base::AtExitManager at_exit_manager; 540 CommandLine::Init(0, NULL); 541 542 HRESULT retval = ExecuteCommands(); 543 544 if (retval == HRESULT_FROM_WIN32(ERROR_BAD_DRIVER)) { 545 SetGoogleUpdateError(kGoogleUpdateProductId, 546 LoadLocalString(IDS_ERROR_NO_XPS)); 547 } else if (FAILED(retval)) { 548 SetGoogleUpdateError(kGoogleUpdateProductId, retval); 549 } 550 551 VLOG(0) << GetErrorMessage(retval) 552 << " HRESULT=0x" << std::setbase(16) << retval; 553 554 // Installer is silent by default as required by Google Update. 555 if (CommandLine::ForCurrentProcess()->HasSwitch("verbose")) { 556 DisplayWindowsMessage(NULL, retval, LoadLocalString(IDS_DRIVER_NAME)); 557 } 558 return retval; 559} 560