Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview/printer_capabilities.cc |
| diff --git a/chrome/browser/ui/webui/print_preview/printer_capabilities.cc b/chrome/browser/ui/webui/print_preview/printer_capabilities.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c3cf1c1b03e5e99f97c517f71f955160967c30e6 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/print_preview/printer_capabilities.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/string_piece.h" |
| +#include "base/values.h" |
| +#include "chrome/common/cloud_print/cloud_print_cdd_conversion.h" |
| +#include "chrome/common/crash_keys.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "printing/backend/print_backend.h" |
| + |
| +namespace printing { |
| + |
| +const char kPrinterId[] = "printerId"; |
| +const char kPrinterCapabilities[] = "capabilities"; |
| + |
| +namespace { |
| + |
| +// Returns a Dictionary representing printer capabilities as CDD. |
| +std::unique_ptr<base::DictionaryValue> |
| +GetPrinterCapabilitiesOnBlockingPoolThread(const std::string& device_name) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + DCHECK(!device_name.empty()); |
| + |
| + scoped_refptr<printing::PrintBackend> print_backend( |
| + printing::PrintBackend::CreateInstance(nullptr)); |
| + |
| + VLOG(1) << "Get printer capabilities start for " << device_name; |
| + crash_keys::ScopedPrinterInfo crash_key( |
| + print_backend->GetPrinterDriverInfo(device_name)); |
| + |
| + std::unique_ptr<base::DictionaryValue> printer_info; |
| + if (!print_backend->IsValidPrinter(device_name)) { |
| + LOG(WARNING) << "Invalid printer " << device_name; |
| + return nullptr; |
| + } |
| + |
| + printing::PrinterSemanticCapsAndDefaults info; |
| + if (!print_backend->GetPrinterSemanticCapsAndDefaults(device_name, &info)) { |
| + LOG(WARNING) << "Failed to get capabilities for " << device_name; |
| + return nullptr; |
| + } |
| + |
| + std::unique_ptr<base::DictionaryValue> printer_capabilities = |
| + cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info); |
| + if (!printer_capabilities) { |
| + LOG(WARNING) << "Failed to convert capabilities for " << device_name; |
| + return nullptr; |
| + } |
| + |
| + return printer_capabilities; |
| +} |
| + |
| +} // namespace |
| + |
| +std::pair<std::string, std::string> GetPrinterNameAndDescription( |
| + const printing::PrinterBasicInfo& printer) { |
| +#if defined(OS_MACOSX) |
| + // On Mac, |printer.printer_description| specifies the printer name and |
| + // |printer.printer_name| specifies the device name / printer queue name. |
| + const std::string& real_name = printer.printer_description; |
| + std::string real_description; |
| + const auto it = printer.options.find(kDriverNameTagName); |
| + if (it != printer.options.end()) |
| + real_description = it->second; |
| + return std::make_pair(real_name, real_description); |
| +#elif defined(OS_CHROMEOS) |
| + // For Chrome OS |printer.printer_display_name| is the human readable name and |
| + // |printer.printer_name| is the name used in CUPS. |
| + return std::make_pair(printer.printer_display_name, |
| + printer.printer_description); |
| +#else |
| + return std::make_pair(printer.printer_name, printer.printer_description); |
| +#endif |
| +} |
| + |
| +// 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.
|
| +// for passing to the WebUI. |
| +std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( |
| + const std::string& device_name, |
| + const printing::PrinterBasicInfo& basic_info) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + const auto printer_name_description = |
| + GetPrinterNameAndDescription(basic_info); |
| + const std::string& printer_name = printer_name_description.first; |
| + const std::string& printer_description = printer_name_description.second; |
| + |
| + auto printer_info = base::MakeUnique<base::DictionaryValue>(); |
| + printer_info->SetString(kPrinterId, device_name); |
| + printer_info->SetString(printing::kSettingPrinterName, printer_name); |
| + printer_info->SetString(printing::kSettingPrinterDescription, |
| + printer_description); |
| + |
| + printer_info->Set(kPrinterCapabilities, |
| + GetPrinterCapabilitiesOnBlockingPoolThread(device_name)); |
| + return printer_info; |
| +} |
| + |
| +} // namespace printing |