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 /printing/printing_context_mac.mm | |
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
Diffstat (limited to 'printing/printing_context_mac.mm')
-rw-r--r-- | printing/printing_context_mac.mm | 41 |
1 files changed, 29 insertions, 12 deletions
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) { |