| 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..85cdf6764524eab50b527c8879809ba61627aa72
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/print_preview/printer_capabilities.cc
|
| @@ -0,0 +1,102 @@
|
| +// 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"
|
| +#include "printing/backend/print_backend_consts.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<PrintBackend> print_backend(
|
| + 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;
|
| + }
|
| +
|
| + 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 PrinterBasicInfo& printer) {
|
| +#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
|
| + // On Mac, |printer.printer_description| specifies the printer name and
|
| + // |printer.printer_name| specifies the device name / printer queue name.
|
| + // Chrome OS emulates the Mac behavior.
|
| + 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);
|
| +#else
|
| + return std::make_pair(printer.printer_name, printer.printer_description);
|
| +#endif
|
| +}
|
| +
|
| +std::unique_ptr<base::DictionaryValue> GetSettingsOnBlockingPool(
|
| + const std::string& device_name,
|
| + const 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(kSettingPrinterName, printer_name);
|
| + printer_info->SetString(kSettingPrinterDescription, printer_description);
|
| +
|
| + printer_info->Set(kPrinterCapabilities,
|
| + GetPrinterCapabilitiesOnBlockingPoolThread(device_name));
|
| + return printer_info;
|
| +}
|
| +
|
| +} // namespace printing
|
|
|