| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_piece.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" |
| 16 #include "chrome/common/crash_keys.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "printing/backend/print_backend.h" |
| 19 #include "printing/backend/print_backend_consts.h" |
| 20 |
| 21 namespace printing { |
| 22 |
| 23 const char kPrinterId[] = "printerId"; |
| 24 const char kPrinterCapabilities[] = "capabilities"; |
| 25 |
| 26 namespace { |
| 27 |
| 28 // Returns a Dictionary representing printer capabilities as CDD. |
| 29 std::unique_ptr<base::DictionaryValue> |
| 30 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { |
| 31 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 32 DCHECK(!device_name.empty()); |
| 33 |
| 34 scoped_refptr<PrintBackend> print_backend( |
| 35 PrintBackend::CreateInstance(nullptr)); |
| 36 |
| 37 VLOG(1) << "Get printer capabilities start for " << device_name; |
| 38 crash_keys::ScopedPrinterInfo crash_key( |
| 39 print_backend->GetPrinterDriverInfo(device_name)); |
| 40 |
| 41 std::unique_ptr<base::DictionaryValue> printer_info; |
| 42 if (!print_backend->IsValidPrinter(device_name)) { |
| 43 LOG(WARNING) << "Invalid printer " << device_name; |
| 44 return nullptr; |
| 45 } |
| 46 |
| 47 PrinterSemanticCapsAndDefaults info; |
| 48 if (!print_backend->GetPrinterSemanticCapsAndDefaults(device_name, &info)) { |
| 49 LOG(WARNING) << "Failed to get capabilities for " << device_name; |
| 50 return nullptr; |
| 51 } |
| 52 |
| 53 std::unique_ptr<base::DictionaryValue> printer_capabilities = |
| 54 cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info); |
| 55 if (!printer_capabilities) { |
| 56 LOG(WARNING) << "Failed to convert capabilities for " << device_name; |
| 57 return nullptr; |
| 58 } |
| 59 |
| 60 return printer_capabilities; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 std::pair<std::string, std::string> GetPrinterNameAndDescription( |
| 66 const PrinterBasicInfo& printer) { |
| 67 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 68 // On Mac, |printer.printer_description| specifies the printer name and |
| 69 // |printer.printer_name| specifies the device name / printer queue name. |
| 70 // Chrome OS emulates the Mac behavior. |
| 71 const std::string& real_name = printer.printer_description; |
| 72 std::string real_description; |
| 73 const auto it = printer.options.find(kDriverNameTagName); |
| 74 if (it != printer.options.end()) |
| 75 real_description = it->second; |
| 76 return std::make_pair(real_name, real_description); |
| 77 #else |
| 78 return std::make_pair(printer.printer_name, printer.printer_description); |
| 79 #endif |
| 80 } |
| 81 |
| 82 std::unique_ptr<base::DictionaryValue> GetSettingsOnBlockingPool( |
| 83 const std::string& device_name, |
| 84 const PrinterBasicInfo& basic_info) { |
| 85 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 86 |
| 87 const auto printer_name_description = |
| 88 GetPrinterNameAndDescription(basic_info); |
| 89 const std::string& printer_name = printer_name_description.first; |
| 90 const std::string& printer_description = printer_name_description.second; |
| 91 |
| 92 auto printer_info = base::MakeUnique<base::DictionaryValue>(); |
| 93 printer_info->SetString(kPrinterId, device_name); |
| 94 printer_info->SetString(kSettingPrinterName, printer_name); |
| 95 printer_info->SetString(kSettingPrinterDescription, printer_description); |
| 96 |
| 97 printer_info->Set(kPrinterCapabilities, |
| 98 GetPrinterCapabilitiesOnBlockingPoolThread(device_name)); |
| 99 return printer_info; |
| 100 } |
| 101 |
| 102 } // namespace printing |
| OLD | NEW |