Use DocumentProperties to get default DEVMODE instead of PRINTER_INFO_*.

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=252221

Review URL: https://codereview.chromium.org/168003002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253076 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc
index fbadfe4f..4ba013e 100644
--- a/printing/printing_context_win.cc
+++ b/printing/printing_context_win.cc
@@ -269,7 +269,7 @@
   (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
                        NULL, 2, NULL, 0, &bytes_needed, &count_returned);
   if (bytes_needed) {
-    DCHECK(bytes_needed >= count_returned * sizeof(PRINTER_INFO_2));
+    DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
     scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
     BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
                               NULL, 2, printer_info_buffer.get(),
@@ -277,17 +277,21 @@
                               &count_returned);
     if (ret && count_returned) {  // have printers
       // Open the first successfully found printer.
-      for (DWORD count = 0; count < count_returned; ++count) {
-        PRINTER_INFO_2* info_2 = reinterpret_cast<PRINTER_INFO_2*>(
-            printer_info_buffer.get() + count * sizeof(PRINTER_INFO_2));
-        std::wstring printer_name = info_2->pPrinterName;
-        if (info_2->pDevMode == NULL || printer_name.length() == 0)
+      const PRINTER_INFO_2* info_2 =
+          reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get());
+      const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
+      for (; info_2 < info_2_end; ++info_2) {
+        ScopedPrinterHandle printer;
+        if (!printer.OpenPrinter(info_2->pPrinterName))
           continue;
-        if (!AllocateContext(printer_name, info_2->pDevMode, &context_))
-          break;
-        if (InitializeSettings(*info_2->pDevMode, printer_name,
-                               NULL, 0, false)) {
-          break;
+        scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL);
+        if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(),
+                                          &context_)) {
+          continue;
+        }
+        if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0,
+                               false)) {
+          return OK;
         }
         ReleaseContext();
       }
@@ -336,15 +340,13 @@
   DCHECK(!external_preview) << "Not implemented";
 
   ScopedPrinterHandle printer;
-  LPWSTR device_name_wide =
-      const_cast<wchar_t*>(settings_.device_name().c_str());
-  if (!printer.OpenPrinter(device_name_wide))
+  if (!printer.OpenPrinter(settings_.device_name().c_str()))
     return OnError();
 
   // Make printer changes local to Chrome.
   // See MSDN documentation regarding DocumentProperties.
   scoped_ptr<DEVMODE[]> scoped_dev_mode =
-      CreateDevModeWithColor(printer, device_name_wide,
+      CreateDevModeWithColor(printer, settings_.device_name(),
                              settings_.color() != GRAY);
   if (!scoped_dev_mode)
     return OnError();
@@ -588,15 +590,14 @@
                                             const std::wstring& device_name) {
   DCHECK(!in_print_job_);
 
-  UserDefaultDevMode user_settings;
+  scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL);
 
-  if (!user_settings.Init(printer) ||
-      !AllocateContext(device_name, user_settings.get(), &context_)) {
+  if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) {
     ResetSettings();
     return false;
   }
 
-  return InitializeSettings(*user_settings.get(), device_name, NULL, 0, false);
+  return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false);
 }
 
 // static