OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <comdef.h> | 5 #include <comdef.h> |
6 #include <iomanip> | 6 #include <iomanip> |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <winspool.h> | 8 #include <winspool.h> |
9 #include <setupapi.h> // Must be included after windows.h | 9 #include <setupapi.h> // Must be included after windows.h |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 L"UNIRES.DLL", | 56 L"UNIRES.DLL", |
57 L"XPSSVCS.DLL", | 57 L"XPSSVCS.DLL", |
58 }; | 58 }; |
59 | 59 |
60 const char kDelete[] = "delete"; | 60 const char kDelete[] = "delete"; |
61 const char kInstallSwitch[] = "install"; | 61 const char kInstallSwitch[] = "install"; |
62 const char kRegisterSwitch[] = "register"; | 62 const char kRegisterSwitch[] = "register"; |
63 const char kUninstallSwitch[] = "uninstall"; | 63 const char kUninstallSwitch[] = "uninstall"; |
64 const char kUnregisterSwitch[] = "unregister"; | 64 const char kUnregisterSwitch[] = "unregister"; |
65 | 65 |
66 base::FilePath GetSystemPath(const string16& binary) { | 66 base::FilePath GetSystemPath(const base::string16& binary) { |
67 base::FilePath path; | 67 base::FilePath path; |
68 if (!PathService::Get(base::DIR_SYSTEM, &path)) { | 68 if (!PathService::Get(base::DIR_SYSTEM, &path)) { |
69 LOG(ERROR) << "Unable to get system path."; | 69 LOG(ERROR) << "Unable to get system path."; |
70 return path; | 70 return path; |
71 } | 71 } |
72 return path.Append(binary); | 72 return path.Append(binary); |
73 } | 73 } |
74 | 74 |
75 base::FilePath GetNativeSystemPath(const string16& binary) { | 75 base::FilePath GetNativeSystemPath(const base::string16& binary) { |
76 if (!IsSystem64Bit()) | 76 if (!IsSystem64Bit()) |
77 return GetSystemPath(binary); | 77 return GetSystemPath(binary); |
78 base::FilePath path; | 78 base::FilePath path; |
79 // Sysnative will bypass filesystem redirection and give us | 79 // Sysnative will bypass filesystem redirection and give us |
80 // the location of the 64bit system32 from a 32 bit process. | 80 // the location of the 64bit system32 from a 32 bit process. |
81 if (!PathService::Get(base::DIR_WINDOWS, &path)) { | 81 if (!PathService::Get(base::DIR_WINDOWS, &path)) { |
82 LOG(ERROR) << "Unable to get windows path."; | 82 LOG(ERROR) << "Unable to get windows path."; |
83 return path; | 83 return path; |
84 } | 84 } |
85 return path.Append(L"sysnative").Append(binary); | 85 return path.Append(L"sysnative").Append(binary); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 247 } |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 HRESULT InstallDriver(const base::FilePath& install_path) { | 251 HRESULT InstallDriver(const base::FilePath& install_path) { |
252 base::ScopedTempDir temp_path; | 252 base::ScopedTempDir temp_path; |
253 if (!temp_path.CreateUniqueTempDir()) | 253 if (!temp_path.CreateUniqueTempDir()) |
254 return HRESULT_FROM_WIN32(ERROR_CANNOT_MAKE); | 254 return HRESULT_FROM_WIN32(ERROR_CANNOT_MAKE); |
255 ReadyDriverDependencies(temp_path.path()); | 255 ReadyDriverDependencies(temp_path.path()); |
256 | 256 |
257 std::vector<string16> dependent_array; | 257 std::vector<base::string16> dependent_array; |
258 // Add all files. AddPrinterDriverEx will removes unnecessary. | 258 // Add all files. AddPrinterDriverEx will removes unnecessary. |
259 for (size_t i = 0; i < arraysize(kDependencyList); ++i) { | 259 for (size_t i = 0; i < arraysize(kDependencyList); ++i) { |
260 base::FilePath file_path = temp_path.path().Append(kDependencyList[i]); | 260 base::FilePath file_path = temp_path.path().Append(kDependencyList[i]); |
261 if (base::PathExists(file_path)) | 261 if (base::PathExists(file_path)) |
262 dependent_array.push_back(file_path.value()); | 262 dependent_array.push_back(file_path.value()); |
263 else | 263 else |
264 LOG(WARNING) << "File is missing: " << file_path.BaseName().value(); | 264 LOG(WARNING) << "File is missing: " << file_path.BaseName().value(); |
265 } | 265 } |
266 | 266 |
267 // Set up paths for the files we depend on. | 267 // Set up paths for the files we depend on. |
(...skipping 12 matching lines...) Expand all Loading... |
280 // Set up supported print system version. Must be 3. | 280 // Set up supported print system version. Must be 3. |
281 driver_info.cVersion = 3; | 281 driver_info.cVersion = 3; |
282 | 282 |
283 // None of the print API structures likes constant strings even though they | 283 // None of the print API structures likes constant strings even though they |
284 // don't modify the string. const_casting is the cleanest option. | 284 // don't modify the string. const_casting is the cleanest option. |
285 driver_info.pDataFile = const_cast<LPWSTR>(data_file.value().c_str()); | 285 driver_info.pDataFile = const_cast<LPWSTR>(data_file.value().c_str()); |
286 driver_info.pHelpFile = const_cast<LPWSTR>(ui_help_path.value().c_str()); | 286 driver_info.pHelpFile = const_cast<LPWSTR>(ui_help_path.value().c_str()); |
287 driver_info.pDriverPath = const_cast<LPWSTR>(xps_path.value().c_str()); | 287 driver_info.pDriverPath = const_cast<LPWSTR>(xps_path.value().c_str()); |
288 driver_info.pConfigFile = const_cast<LPWSTR>(ui_path.value().c_str()); | 288 driver_info.pConfigFile = const_cast<LPWSTR>(ui_path.value().c_str()); |
289 | 289 |
290 string16 dependent_files(JoinString(dependent_array, L'\n')); | 290 base::string16 dependent_files(JoinString(dependent_array, L'\n')); |
291 dependent_files.push_back(L'\n'); | 291 dependent_files.push_back(L'\n'); |
292 std::replace(dependent_files.begin(), dependent_files.end(), L'\n', L'\0'); | 292 std::replace(dependent_files.begin(), dependent_files.end(), L'\n', L'\0'); |
293 driver_info.pDependentFiles = &dependent_files[0]; | 293 driver_info.pDependentFiles = &dependent_files[0]; |
294 | 294 |
295 // Set up user visible strings. | 295 // Set up user visible strings. |
296 string16 manufacturer = LoadLocalString(IDS_GOOGLE); | 296 base::string16 manufacturer = LoadLocalString(IDS_GOOGLE); |
297 driver_info.pszMfgName = const_cast<LPWSTR>(manufacturer.c_str()); | 297 driver_info.pszMfgName = const_cast<LPWSTR>(manufacturer.c_str()); |
298 driver_info.pszProvider = const_cast<LPWSTR>(manufacturer.c_str()); | 298 driver_info.pszProvider = const_cast<LPWSTR>(manufacturer.c_str()); |
299 driver_info.pszOEMUrl = const_cast<LPWSTR>(kGcpUrl); | 299 driver_info.pszOEMUrl = const_cast<LPWSTR>(kGcpUrl); |
300 driver_info.dwlDriverVersion = GetVersionNumber(); | 300 driver_info.dwlDriverVersion = GetVersionNumber(); |
301 string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); | 301 base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); |
302 driver_info.pName = const_cast<LPWSTR>(driver_name.c_str()); | 302 driver_info.pName = const_cast<LPWSTR>(driver_name.c_str()); |
303 | 303 |
304 if (!::AddPrinterDriverEx(NULL, 6, reinterpret_cast<BYTE*>(&driver_info), | 304 if (!::AddPrinterDriverEx(NULL, 6, reinterpret_cast<BYTE*>(&driver_info), |
305 APD_COPY_NEW_FILES | APD_COPY_FROM_DIRECTORY)) { | 305 APD_COPY_NEW_FILES | APD_COPY_FROM_DIRECTORY)) { |
306 LOG(ERROR) << "Unable to add printer driver"; | 306 LOG(ERROR) << "Unable to add printer driver"; |
307 return GetLastHResult(); | 307 return GetLastHResult(); |
308 } | 308 } |
309 return S_OK; | 309 return S_OK; |
310 } | 310 } |
311 | 311 |
312 HRESULT UninstallDriver() { | 312 HRESULT UninstallDriver() { |
313 int tries = 3; | 313 int tries = 3; |
314 string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); | 314 base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); |
315 while (!DeletePrinterDriverEx(NULL, | 315 while (!DeletePrinterDriverEx(NULL, |
316 NULL, | 316 NULL, |
317 const_cast<LPWSTR>(driver_name.c_str()), | 317 const_cast<LPWSTR>(driver_name.c_str()), |
318 DPD_DELETE_UNUSED_FILES, | 318 DPD_DELETE_UNUSED_FILES, |
319 0) && tries > 0) { | 319 0) && tries > 0) { |
320 if (GetLastError() == ERROR_UNKNOWN_PRINTER_DRIVER) { | 320 if (GetLastError() == ERROR_UNKNOWN_PRINTER_DRIVER) { |
321 LOG(WARNING) << "Print driver is already uninstalled."; | 321 LOG(WARNING) << "Print driver is already uninstalled."; |
322 return S_OK; | 322 return S_OK; |
323 } | 323 } |
324 // After deleting the printer it can take a few seconds before | 324 // After deleting the printer it can take a few seconds before |
325 // the driver is free for deletion. Retry a few times before giving up. | 325 // the driver is free for deletion. Retry a few times before giving up. |
326 LOG(WARNING) << "Attempt to delete printer driver failed. Retrying."; | 326 LOG(WARNING) << "Attempt to delete printer driver failed. Retrying."; |
327 tries--; | 327 tries--; |
328 Sleep(2000); | 328 Sleep(2000); |
329 } | 329 } |
330 if (tries <= 0) { | 330 if (tries <= 0) { |
331 HRESULT result = GetLastHResult(); | 331 HRESULT result = GetLastHResult(); |
332 LOG(ERROR) << "Unable to delete printer driver."; | 332 LOG(ERROR) << "Unable to delete printer driver."; |
333 return result; | 333 return result; |
334 } | 334 } |
335 return S_OK; | 335 return S_OK; |
336 } | 336 } |
337 | 337 |
338 HRESULT InstallPrinter(void) { | 338 HRESULT InstallPrinter(void) { |
339 PRINTER_INFO_2 printer_info = {0}; | 339 PRINTER_INFO_2 printer_info = {0}; |
340 | 340 |
341 // None of the print API structures likes constant strings even though they | 341 // None of the print API structures likes constant strings even though they |
342 // don't modify the string. const_casting is the cleanest option. | 342 // don't modify the string. const_casting is the cleanest option. |
343 string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); | 343 base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); |
344 printer_info.pDriverName = const_cast<LPWSTR>(driver_name.c_str()); | 344 printer_info.pDriverName = const_cast<LPWSTR>(driver_name.c_str()); |
345 printer_info.pPrinterName = const_cast<LPWSTR>(driver_name.c_str()); | 345 printer_info.pPrinterName = const_cast<LPWSTR>(driver_name.c_str()); |
346 printer_info.pComment = const_cast<LPWSTR>(driver_name.c_str()); | 346 printer_info.pComment = const_cast<LPWSTR>(driver_name.c_str()); |
347 printer_info.pLocation = const_cast<LPWSTR>(kGcpUrl); | 347 printer_info.pLocation = const_cast<LPWSTR>(kGcpUrl); |
348 string16 port_name; | 348 base::string16 port_name; |
349 printer_info.pPortName = const_cast<LPWSTR>(kPortName); | 349 printer_info.pPortName = const_cast<LPWSTR>(kPortName); |
350 printer_info.Attributes = PRINTER_ATTRIBUTE_DIRECT|PRINTER_ATTRIBUTE_LOCAL; | 350 printer_info.Attributes = PRINTER_ATTRIBUTE_DIRECT|PRINTER_ATTRIBUTE_LOCAL; |
351 printer_info.pPrintProcessor = L"winprint"; | 351 printer_info.pPrintProcessor = L"winprint"; |
352 HANDLE handle = AddPrinter(NULL, 2, reinterpret_cast<BYTE*>(&printer_info)); | 352 HANDLE handle = AddPrinter(NULL, 2, reinterpret_cast<BYTE*>(&printer_info)); |
353 if (handle == NULL) { | 353 if (handle == NULL) { |
354 HRESULT result = GetLastHResult(); | 354 HRESULT result = GetLastHResult(); |
355 LOG(ERROR) << "Unable to add printer"; | 355 LOG(ERROR) << "Unable to add printer"; |
356 return result; | 356 return result; |
357 } | 357 } |
358 ClosePrinter(handle); | 358 ClosePrinter(handle); |
359 return S_OK; | 359 return S_OK; |
360 } | 360 } |
361 | 361 |
362 HRESULT UninstallPrinter(void) { | 362 HRESULT UninstallPrinter(void) { |
363 HANDLE handle = NULL; | 363 HANDLE handle = NULL; |
364 PRINTER_DEFAULTS printer_defaults = {0}; | 364 PRINTER_DEFAULTS printer_defaults = {0}; |
365 printer_defaults.DesiredAccess = PRINTER_ALL_ACCESS; | 365 printer_defaults.DesiredAccess = PRINTER_ALL_ACCESS; |
366 string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); | 366 base::string16 driver_name = LoadLocalString(IDS_DRIVER_NAME); |
367 if (!OpenPrinter(const_cast<LPWSTR>(driver_name.c_str()), | 367 if (!OpenPrinter(const_cast<LPWSTR>(driver_name.c_str()), |
368 &handle, | 368 &handle, |
369 &printer_defaults)) { | 369 &printer_defaults)) { |
370 // If we can't open the printer, it was probably already removed. | 370 // If we can't open the printer, it was probably already removed. |
371 LOG(WARNING) << "Unable to open printer"; | 371 LOG(WARNING) << "Unable to open printer"; |
372 return S_OK; | 372 return S_OK; |
373 } | 373 } |
374 if (!DeletePrinter(handle)) { | 374 if (!DeletePrinter(handle)) { |
375 HRESULT result = GetLastHResult(); | 375 HRESULT result = GetLastHResult(); |
376 LOG(ERROR) << "Unable to delete printer"; | 376 LOG(ERROR) << "Unable to delete printer"; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 VLOG(0) << _com_error(retval).ErrorMessage() << " HRESULT=0x" << | 544 VLOG(0) << _com_error(retval).ErrorMessage() << " HRESULT=0x" << |
545 std::setbase(16) << retval; | 545 std::setbase(16) << retval; |
546 | 546 |
547 // Installer is silent by default as required by Google Update. | 547 // Installer is silent by default as required by Google Update. |
548 if (CommandLine::ForCurrentProcess()->HasSwitch("verbose")) { | 548 if (CommandLine::ForCurrentProcess()->HasSwitch("verbose")) { |
549 cloud_print::DisplayWindowsMessage(NULL, retval, | 549 cloud_print::DisplayWindowsMessage(NULL, retval, |
550 cloud_print::LoadLocalString(IDS_DRIVER_NAME)); | 550 cloud_print::LoadLocalString(IDS_DRIVER_NAME)); |
551 } | 551 } |
552 return retval; | 552 return retval; |
553 } | 553 } |
OLD | NEW |