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( | |
|
Lei Zhang
2016/11/03 23:41:46
no printing:: in namespace printing :-P
skau
2016/11/04 19:28:59
Done.
| |
| 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) || defined(OS_CHROMEOS) | |
| 67 // On Mac, |printer.printer_description| specifies the printer name and | |
| 68 // |printer.printer_name| specifies the device name / printer queue name. | |
| 69 // Chrome OS emulates the Mac behavior. | |
| 70 const std::string& real_name = printer.printer_description; | |
| 71 std::string real_description; | |
| 72 const auto it = printer.options.find(kDriverNameTagName); | |
| 73 if (it != printer.options.end()) | |
| 74 real_description = it->second; | |
| 75 return std::make_pair(real_name, real_description); | |
| 76 #else | |
| 77 return std::make_pair(printer.printer_name, printer.printer_description); | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 81 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( | |
| 82 const std::string& device_name, | |
| 83 const printing::PrinterBasicInfo& basic_info) { | |
| 84 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
|
Lei Zhang
2016/11/03 23:41:46
Does this still need to run on the blocking pool?
skau
2016/11/04 19:28:59
Yes it does. This calls GetPrinterCapabilitiesOnB
| |
| 85 | |
| 86 const auto printer_name_description = | |
| 87 GetPrinterNameAndDescription(basic_info); | |
| 88 const std::string& printer_name = printer_name_description.first; | |
| 89 const std::string& printer_description = printer_name_description.second; | |
| 90 | |
| 91 auto printer_info = base::MakeUnique<base::DictionaryValue>(); | |
| 92 printer_info->SetString(kPrinterId, device_name); | |
| 93 printer_info->SetString(printing::kSettingPrinterName, printer_name); | |
| 94 printer_info->SetString(printing::kSettingPrinterDescription, | |
| 95 printer_description); | |
| 96 | |
| 97 printer_info->Set(kPrinterCapabilities, | |
| 98 GetPrinterCapabilitiesOnBlockingPoolThread(device_name)); | |
| 99 return printer_info; | |
| 100 } | |
| 101 | |
| 102 } // namespace printing | |
| OLD | NEW |