Chromium Code Reviews| 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 | |
| 20 namespace printing { | |
| 21 | |
| 22 const char kPrinterId[] = "printerId"; | |
| 23 const char kPrinterCapabilities[] = "capabilities"; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Returns a Dictionary representing printer capabilities as CDD. | |
| 28 std::unique_ptr<base::DictionaryValue> | |
| 29 GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { | |
| 30 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 31 DCHECK(!device_name.empty()); | |
| 32 | |
| 33 scoped_refptr<printing::PrintBackend> print_backend( | |
| 34 printing::PrintBackend::CreateInstance(nullptr)); | |
| 35 | |
| 36 VLOG(1) << "Get printer capabilities start for " << device_name; | |
| 37 crash_keys::ScopedPrinterInfo crash_key( | |
| 38 print_backend->GetPrinterDriverInfo(device_name)); | |
| 39 | |
| 40 std::unique_ptr<base::DictionaryValue> printer_info; | |
| 41 if (!print_backend->IsValidPrinter(device_name)) { | |
| 42 LOG(WARNING) << "Invalid printer " << device_name; | |
| 43 return nullptr; | |
| 44 } | |
| 45 | |
| 46 printing::PrinterSemanticCapsAndDefaults info; | |
| 47 if (!print_backend->GetPrinterSemanticCapsAndDefaults(device_name, &info)) { | |
| 48 LOG(WARNING) << "Failed to get capabilities for " << device_name; | |
| 49 return nullptr; | |
| 50 } | |
| 51 | |
| 52 std::unique_ptr<base::DictionaryValue> printer_capabilities = | |
| 53 cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info); | |
| 54 if (!printer_capabilities) { | |
| 55 LOG(WARNING) << "Failed to convert capabilities for " << device_name; | |
| 56 return nullptr; | |
| 57 } | |
| 58 | |
| 59 return printer_capabilities; | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 std::pair<std::string, std::string> GetPrinterNameAndDescription( | |
| 65 const printing::PrinterBasicInfo& printer) { | |
| 66 #if defined(OS_MACOSX) | |
| 67 // On Mac, |printer.printer_description| specifies the printer name and | |
| 68 // |printer.printer_name| specifies the device name / printer queue name. | |
| 69 const std::string& real_name = printer.printer_description; | |
| 70 std::string real_description; | |
| 71 const auto it = printer.options.find(kDriverNameTagName); | |
| 72 if (it != printer.options.end()) | |
| 73 real_description = it->second; | |
| 74 return std::make_pair(real_name, real_description); | |
| 75 #elif defined(OS_CHROMEOS) | |
| 76 // For Chrome OS |printer.printer_display_name| is the human readable name and | |
| 77 // |printer.printer_name| is the name used in CUPS. | |
| 78 return std::make_pair(printer.printer_display_name, | |
| 79 printer.printer_description); | |
| 80 #else | |
| 81 return std::make_pair(printer.printer_name, printer.printer_description); | |
| 82 #endif | |
| 83 } | |
| 84 | |
| 85 // Constructs the JSON representing printer capabilities information suitable | |
|
Lei Zhang
2016/11/02 23:09:22
Duplicates the comment in the header.
skau
2016/11/03 22:38:08
Done.
| |
| 86 // for passing to the WebUI. | |
| 87 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( | |
| 88 const std::string& device_name, | |
| 89 const printing::PrinterBasicInfo& basic_info) { | |
| 90 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 91 | |
| 92 const auto printer_name_description = | |
| 93 GetPrinterNameAndDescription(basic_info); | |
| 94 const std::string& printer_name = printer_name_description.first; | |
| 95 const std::string& printer_description = printer_name_description.second; | |
| 96 | |
| 97 auto printer_info = base::MakeUnique<base::DictionaryValue>(); | |
| 98 printer_info->SetString(kPrinterId, device_name); | |
| 99 printer_info->SetString(printing::kSettingPrinterName, printer_name); | |
| 100 printer_info->SetString(printing::kSettingPrinterDescription, | |
| 101 printer_description); | |
| 102 | |
| 103 printer_info->Set(kPrinterCapabilities, | |
| 104 GetPrinterCapabilitiesOnBlockingPoolThread(device_name)); | |
| 105 return printer_info; | |
| 106 } | |
| 107 | |
| 108 } // namespace printing | |
| OLD | NEW |