diff options
author | kmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-04 21:10:15 +0000 |
---|---|---|
committer | kmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-04 21:10:15 +0000 |
commit | 987e51f93d016888da2a5415aca8d257a82b22d4 (patch) | |
tree | 410148405daf7ee8ff3fdcd89f0bd62560edcc70 | |
parent | 290464bb71dc2c3c079e8750b24e544f7a9bfdfc (diff) | |
download | chromium_src-987e51f93d016888da2a5415aca8d257a82b22d4.zip chromium_src-987e51f93d016888da2a5415aca8d257a82b22d4.tar.gz chromium_src-987e51f93d016888da2a5415aca8d257a82b22d4.tar.bz2 |
PrintPreview: Modified code to set the printer using the unique printer id.
BUG=79900
TEST=Install a default printer with a dot in the name and preview a webpage.
Review URL: http://codereview.chromium.org/6915012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84128 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/resources/print_preview.js | 3 | ||||
-rw-r--r-- | printing/print_job_constants.cc | 3 | ||||
-rw-r--r-- | printing/print_job_constants.h | 1 | ||||
-rw-r--r-- | printing/printing_context_mac.h | 2 | ||||
-rw-r--r-- | printing/printing_context_mac.mm | 41 |
5 files changed, 37 insertions, 13 deletions
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js index 1e795bd..bb002f7 100644 --- a/chrome/browser/resources/print_preview.js +++ b/chrome/browser/resources/print_preview.js @@ -292,7 +292,10 @@ function getSettingsJSON() { var printAll = $('all-pages').checked; var printToPDF = (printerName == localStrings.getString('printToPDF')); + // TODO(kmadhusu): Fix the printer device name (http://crbug.com/81488). + return JSON.stringify({'printerName': printerName, + 'deviceName': printerName, 'pageRange': getSelectedPageRanges(), 'printAll': printAll, 'duplex': getDuplexMode(), diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index 82f9adf..d8eb0ce 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -15,6 +15,9 @@ const char kSettingColor[] = "color"; // Number of copies. const char kSettingCopies[] = "copies"; +// Device name: Unique printer identifier. +const char kSettingDeviceName[] = "deviceName"; + // Print job duplex mode. const char kSettingDuplexMode[] = "duplex"; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index 5fe4bc6..0f43b7f 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -10,6 +10,7 @@ namespace printing { extern const char kSettingCollate[]; extern const char kSettingColor[]; extern const char kSettingCopies[]; +extern const char kSettingDeviceName[]; extern const char kSettingDuplexMode[]; extern const char kSettingLandscape[]; extern const char kSettingPageRange[]; diff --git a/printing/printing_context_mac.h b/printing/printing_context_mac.h index ef7e75c..12a999c 100644 --- a/printing/printing_context_mac.h +++ b/printing/printing_context_mac.h @@ -50,7 +50,7 @@ class PrintingContextMac : public PrintingContext { // Updates |print_info_| to use the given printer. // Returns true if the printer was set else returns false. - bool SetPrinter(const std::string& printer_name); + bool SetPrinter(const std::string& device_name); // Sets |copies| in PMPrintSettings. // Returns true if the number of copies is set. diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm index cb60f9f..9333967 100644 --- a/printing/printing_context_mac.mm +++ b/printing/printing_context_mac.mm @@ -92,13 +92,13 @@ PrintingContext::Result PrintingContextMac::UpdatePrintSettings( print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); bool landscape; - std::string printer_name; + std::string device_name; int copies; bool collate; int duplex_mode; bool color; if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || - !job_settings.GetString(kSettingPrinterName, &printer_name) || + !job_settings.GetString(kSettingDeviceName, &device_name) || !job_settings.GetInteger(kSettingCopies, &copies) || !job_settings.GetBoolean(kSettingCollate, &collate) || !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || @@ -106,7 +106,7 @@ PrintingContext::Result PrintingContextMac::UpdatePrintSettings( return OnError(); } - if (!SetPrinter(printer_name)) + if (!SetPrinter(device_name)) return OnError(); if (!SetCopiesInPrintSettings(copies)) @@ -142,19 +142,36 @@ void PrintingContextMac::InitPrintSettingsFromPrintInfo( printer, page_format, ranges, false, &settings_); } -bool PrintingContextMac::SetPrinter(const std::string& printer_name) { - NSString* new_printer_name = base::SysUTF8ToNSString(printer_name); - if (!new_printer_name) +bool PrintingContextMac::SetPrinter(const std::string& device_name) { + DCHECK(print_info_.get()); + PMPrintSession print_session = + static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); + + PMPrinter current_printer; + if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) + return false; + + CFStringRef current_printer_id = PMPrinterGetID(current_printer); + if (!current_printer_id) return false; - if (![[[print_info_.get() printer] name] isEqualToString:new_printer_name]) { - NSPrinter* new_printer = [NSPrinter printerWithName:new_printer_name]; - if (new_printer == nil) - return false; + base::mac::ScopedCFTypeRef<CFStringRef> new_printer_id( + base::SysUTF8ToCFStringRef(device_name)); + if (!new_printer_id.get()) + return false; - [print_info_.get() setPrinter:new_printer]; + if (CFStringCompare(new_printer_id.get(), current_printer_id, 0) == + kCFCompareEqualTo) { + return true; } - return true; + + PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get()); + if (new_printer == NULL) + return false; + + OSStatus status = PMSessionSetCurrentPMPrinter(print_session, new_printer); + PMRelease(new_printer); + return status == noErr; } bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { |