Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
| diff --git a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
| index 5068fb081ab50dfb3d593fd7a0a3fbfe77f7150c..1cde2f85c77fba72d3f8c0df76f84428dd550b7d 100644 |
| --- a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
| +++ b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc |
| @@ -309,7 +309,7 @@ std::string GetDefaultPrinterOnFileThread() { |
| DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| scoped_refptr<printing::PrintBackend> print_backend( |
| - printing::PrintBackend::CreateInstance(NULL)); |
| + printing::PrintBackend::CreateInstance(nullptr)); |
| std::string default_printer = print_backend->GetDefaultPrinterName(); |
| VLOG(1) << "Default Printer: " << default_printer; |
| @@ -341,12 +341,9 @@ gfx::Size GetDefaultPdfMediaSizeMicrons() { |
| pdf_media_size.height() * deviceMicronsPerDeviceUnit); |
| } |
| -typedef base::Callback<void(const base::DictionaryValue*)> |
| - GetPdfCapabilitiesCallback; |
| - |
| -std::unique_ptr<base::DictionaryValue> GetPdfCapabilitiesOnFileThread( |
| +std::unique_ptr<base::DictionaryValue> GetPdfCapabilities( |
| const std::string& locale) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| cloud_devices::CloudDeviceDescription description; |
| using namespace cloud_devices::printer; |
| @@ -396,117 +393,103 @@ std::unique_ptr<base::DictionaryValue> GetPdfCapabilitiesOnFileThread( |
| return std::unique_ptr<base::DictionaryValue>(description.root().DeepCopy()); |
| } |
| -std::unique_ptr<base::DictionaryValue> GetLocalPrinterCapabilitiesOnFileThread( |
| - const std::string& printer_name) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| - |
| - scoped_refptr<printing::PrintBackend> print_backend( |
| - printing::PrintBackend::CreateInstance(NULL)); |
| - |
| - VLOG(1) << "Get printer capabilities start for " << printer_name; |
| - crash_keys::ScopedPrinterInfo crash_key( |
| - print_backend->GetPrinterDriverInfo(printer_name)); |
| - |
| - if (!print_backend->IsValidPrinter(printer_name)) { |
| - LOG(WARNING) << "Invalid printer " << printer_name; |
| - return std::unique_ptr<base::DictionaryValue>(); |
| - } |
| - |
| - printing::PrinterSemanticCapsAndDefaults info; |
| - if (!print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, &info)) { |
| - LOG(WARNING) << "Failed to get capabilities for " << printer_name; |
| - return std::unique_ptr<base::DictionaryValue>(); |
| - } |
| - |
| - std::unique_ptr<base::DictionaryValue> description( |
| - cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info)); |
| - if (!description) { |
| - LOG(WARNING) << "Failed to convert capabilities for " << printer_name; |
| - return std::unique_ptr<base::DictionaryValue>(); |
| - } |
| - |
| - return description; |
| +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); |
| +#else |
| + return std::make_pair(printer.printer_name, printer.printer_description); |
| +#endif |
| } |
| void EnumeratePrintersOnFileThread(base::ListValue* printers) { |
| DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| scoped_refptr<printing::PrintBackend> print_backend( |
| - printing::PrintBackend::CreateInstance(NULL)); |
| + printing::PrintBackend::CreateInstance(nullptr)); |
| VLOG(1) << "Enumerate printers start"; |
| printing::PrinterList printer_list; |
| print_backend->EnumeratePrinters(&printer_list); |
| - for (printing::PrinterList::iterator it = printer_list.begin(); |
| - it != printer_list.end(); ++it) { |
| + for (const printing::PrinterBasicInfo& printer : printer_list) { |
| base::DictionaryValue* printer_info = new base::DictionaryValue; |
| printers->Append(printer_info); |
| - std::string printer_name; |
| - std::string printer_description; |
| -#if defined(OS_MACOSX) |
| - // On Mac, |it->printer_description| specifies the printer name and |
| - // |it->printer_name| specifies the device name / printer queue name. |
| - printer_name = it->printer_description; |
| - if (!it->options[kDriverNameTagName].empty()) |
| - printer_description = it->options[kDriverNameTagName]; |
| -#else |
| - printer_name = it->printer_name; |
| - printer_description = it->printer_description; |
| -#endif |
| - printer_info->SetString(printing::kSettingDeviceName, it->printer_name); |
| + |
| + const auto printer_name_description = GetPrinterNameAndDescription(printer); |
| + const std::string& printer_name = printer_name_description.first; |
| + const std::string& printer_description = printer_name_description.second; |
| + printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); |
| + printer_info->SetString(printing::kSettingPrinterName, printer_name); |
| printer_info->SetString(printing::kSettingPrinterDescription, |
| printer_description); |
| - printer_info->SetString(printing::kSettingPrinterName, printer_name); |
| - VLOG(1) << "Found printer " << printer_name |
| - << " with device name " << it->printer_name; |
| base::DictionaryValue* options = new base::DictionaryValue; |
| printer_info->Set(printing::kSettingPrinterOptions, options); |
| - for (std::map<std::string, std::string>::iterator opt = it->options.begin(); |
| - opt != it->options.end(); |
| - ++opt) { |
| - options->SetString(opt->first, opt->second); |
| - } |
| + for (const auto opt_it : printer.options) |
| + options->SetString(opt_it.first, opt_it.second); |
| VLOG(1) << "Found printer " << printer_name << " with device name " |
| - << it->printer_name; |
| + << printer.printer_name; |
| } |
| VLOG(1) << "Enumerate printers finished, found " << printers->GetSize() |
| << " printers"; |
| } |
| -typedef base::Callback<void(const base::DictionaryValue*)> |
| - GetPrinterCapabilitiesSuccessCallback; |
| -typedef base::Callback<void(const std::string&)> |
| - GetPrinterCapabilitiesFailureCallback; |
| - |
| -void GetPrinterCapabilitiesOnFileThread( |
| - const std::string& printer_name, |
| - const std::string& locale, |
| - const GetPrinterCapabilitiesSuccessCallback& success_cb, |
| - const GetPrinterCapabilitiesFailureCallback& failure_cb) { |
| +std::unique_ptr<base::DictionaryValue> GetPrinterCapabilitiesOnFileThread( |
| + const std::string& device_name) { |
| DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| - DCHECK(!printer_name.empty()); |
| + 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_capabilities( |
| - printer_name == kLocalPdfPrinterId |
| - ? GetPdfCapabilitiesOnFileThread(locale) |
| - : GetLocalPrinterCapabilitiesOnFileThread(printer_name)); |
| + std::unique_ptr<base::DictionaryValue> printer_info; |
| + if (!print_backend->IsValidPrinter(device_name)) { |
| + LOG(WARNING) << "Invalid printer " << device_name; |
| + return printer_info; |
| + } |
| + |
| + printing::PrinterSemanticCapsAndDefaults info; |
| + if (!print_backend->GetPrinterSemanticCapsAndDefaults(device_name, &info)) { |
| + LOG(WARNING) << "Failed to get capabilities for " << device_name; |
| + return printer_info; |
| + } |
| + |
| + std::unique_ptr<base::DictionaryValue> printer_capabilities = |
| + cloud_print::PrinterSemanticCapsAndDefaultsToCdd(info); |
| if (!printer_capabilities) { |
| - BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| - base::Bind(failure_cb, printer_name)); |
| - return; |
| + LOG(WARNING) << "Failed to convert capabilities for " << device_name; |
| + return printer_info; |
| } |
| - std::unique_ptr<base::DictionaryValue> printer_info( |
| - new base::DictionaryValue); |
| - printer_info->SetString(kPrinterId, printer_name); |
| - printer_info->Set(kPrinterCapabilities, printer_capabilities.release()); |
| + printing::PrinterBasicInfo basic_info; |
| + if (!print_backend->GetPrinterBasicInfo(device_name, &basic_info)) |
| + return printer_info; |
| + |
| + 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; |
| - BrowserThread::PostTask( |
| - BrowserThread::UI, FROM_HERE, |
| - base::Bind(success_cb, base::Owned(printer_info.release()))); |
| + printer_info.reset(new 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, printer_capabilities.release()); |
| + return printer_info; |
| } |
| base::LazyInstance<printing::StickySettings> g_sticky_settings = |
| @@ -1120,17 +1103,22 @@ void PrintPreviewHandler::HandleGetPrinterCapabilities( |
| if (!ret || printer_name.empty()) |
| return; |
| - GetPrinterCapabilitiesSuccessCallback success_cb = |
| + if (printer_name == kLocalPdfPrinterId) { |
| + std::unique_ptr<base::DictionaryValue> printer_info( |
| + new base::DictionaryValue); |
| + printer_info->SetString(kPrinterId, printer_name); |
| + printer_info->Set( |
| + kPrinterCapabilities, |
| + GetPdfCapabilities(g_browser_process->GetApplicationLocale())); |
| + SendPrinterCapabilities(printer_name, std::move(printer_info)); |
| + return; |
| + } |
| + |
| + BrowserThread::PostTaskAndReplyWithResult( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&GetPrinterCapabilitiesOnFileThread, printer_name), |
| base::Bind(&PrintPreviewHandler::SendPrinterCapabilities, |
| - weak_factory_.GetWeakPtr()); |
| - GetPrinterCapabilitiesFailureCallback failure_cb = |
| - base::Bind(&PrintPreviewHandler::SendFailedToGetPrinterCapabilities, |
| - weak_factory_.GetWeakPtr()); |
| - BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| - base::Bind(&GetPrinterCapabilitiesOnFileThread, |
| - printer_name, |
| - g_browser_process->GetApplicationLocale(), |
| - success_cb, failure_cb)); |
| + weak_factory_.GetWeakPtr(), printer_name)); |
| } |
| void PrintPreviewHandler::OnSigninComplete() { |
| @@ -1245,7 +1233,7 @@ void PrintPreviewHandler::GetNumberFormatAndMeasurementSystem( |
| void PrintPreviewHandler::HandleGetInitialSettings( |
| const base::ListValue* /*args*/) { |
| - // Send before SendInitialSettings to allow cloud printer auto select. |
| + // Send before SendInitialSettings() to allow cloud printer auto select. |
| SendCloudPrintEnabled(); |
| BrowserThread::PostTaskAndReplyWithResult( |
| BrowserThread::FILE, FROM_HERE, |
| @@ -1324,20 +1312,20 @@ void PrintPreviewHandler::SendAccessToken(const std::string& type, |
| } |
| void PrintPreviewHandler::SendPrinterCapabilities( |
| - const base::DictionaryValue* settings_info) { |
| + const std::string& printer_name, |
| + std::unique_ptr<base::DictionaryValue> settings_info) { |
| + if (!settings_info) { |
| + VLOG(1) << "Get printer capabilities failed"; |
| + base::StringValue printer_name_value(printer_name); |
| + web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities", |
| + printer_name_value); |
|
dpapad
2016/05/16 21:11:43
Nit (optional): Inline printer_name_value
web_ui()
Lei Zhang
2016/05/16 21:27:09
Done.
|
| + return; |
| + } |
| VLOG(1) << "Get printer capabilities finished"; |
| web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", |
| *settings_info); |
| } |
| -void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( |
| - const std::string& printer_name) { |
| - VLOG(1) << "Get printer capabilities failed"; |
| - base::StringValue printer_name_value(printer_name); |
| - web_ui()->CallJavascriptFunction("failedToGetPrinterCapabilities", |
| - printer_name_value); |
| -} |
| - |
| void PrintPreviewHandler::SetupPrinterList(const base::ListValue* printers) { |
| if (!has_logged_printers_count_) { |
| UMA_HISTOGRAM_COUNTS("PrintPreview.NumberOfPrinters", printers->GetSize()); |