Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc |
| diff --git a/chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc b/chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc |
| index ffd09bc8fcd5828c1fc05851023f3ed2075a2b67..cfec8d6fdf22fb6dfb3aaa158e1b6791e40e6065 100644 |
| --- a/chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc |
| +++ b/chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc |
| @@ -7,21 +7,36 @@ |
| #include <memory> |
| #include <vector> |
| +#include "base/bind_helpers.h" |
| #include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/threading/sequenced_task_runner_handle.h" |
| #include "base/values.h" |
| #include "chrome/browser/chromeos/printing/printer_pref_manager.h" |
| #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" |
| #include "chrome/common/chrome_switches.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/debug_daemon_client.h" |
| #include "chromeos/printing/printer_configuration.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "printing/backend/print_backend.h" |
| #include "printing/backend/print_backend_consts.h" |
| +#if defined(USE_CUPS) |
| +#include <cups/cups.h> |
| + |
| +#include "printing/backend/cups_connection.h" |
| +#endif |
| + |
| namespace printing { |
| namespace { |
| +enum SetupResult { UNKNOWN, SUCCESS, PPD_NOT_FOUND, FAILURE }; |
| + |
| // Store the name used in CUPS, Printer#id in |printer_name|, the description |
| // as the system_driverinfo option value, and the Printer#display_name in |
| // the |printer_description| field. This will match how Mac OS X presents |
| @@ -37,6 +52,109 @@ printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { |
| return basic_info; |
| } |
| +void SettingsToUIThread(std::unique_ptr<chromeos::Printer> printer, |
| + const PrinterSetupCallback& cb) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + PrinterBasicInfo basic_info = ToBasicInfo(*printer); |
| + std::unique_ptr<base::DictionaryValue> capabilities = |
| + GetSettingsDictionary(printer->id(), basic_info); |
|
Lei Zhang
2016/11/03 23:41:46
If SettingsToUIThread() doesn't have to run on the
skau
2016/11/04 19:28:59
GetSettingsDictionary has to run on the blocking p
|
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(cb, base::Passed(&capabilities))); |
| +} |
| + |
| +void PostCallbackError(const PrinterSetupCallback& cb) { |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(cb, nullptr)); |
| +} |
| + |
| +void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, |
| + printing::SetupResult result, |
| + const PrinterSetupCallback& cb) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + if (result != printing::SetupResult::SUCCESS) { |
| + LOG(WARNING) << "Print setup failed"; |
| + // TODO(skau): Open printer settings if this is resolvable. |
| + PostCallbackError(cb); |
| + return; |
| + } |
| + |
| + VLOG(1) << "Printer setup successful for " << printer->id() |
| + << " fetching properties"; |
| + |
| + // fetch settings on the blocking pool and invoke callback. |
| + content::BrowserThread::PostBlockingPoolTask( |
| + FROM_HERE, base::Bind(&SettingsToUIThread, base::Passed(&printer), cb)); |
| +} |
| + |
| +void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer, |
| + const PrinterSetupCallback& cb, |
| + bool success) { |
| + // It's expected that debug daemon posts callbacks on the UI thread. |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb); |
| +} |
| + |
| +void OnPrinterAddError(const PrinterSetupCallback& cb) { |
| + // It's expected that debug daemon posts callbacks on the UI thread. |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + LOG(WARNING) << "Printer add failure"; |
| + PostCallbackError(cb); |
| +} |
| + |
| +bool IsIppEverywhere(const chromeos::Printer& printer) { |
| + // TODO(skau): Use uri, effective_make and effective_model to determine if |
| + // we should do an IPP Everywhere configuration. |
| + return false; |
| +} |
| + |
| +std::string GetPPDPath(const chromeos::Printer& printer) { |
| + // TODO(skau): Consult the PPD Provider for the correct file path. |
| + return printer.ppd_reference().user_supplied_ppd_url; |
| +} |
| + |
| +bool IsPrinterConfigured(const std::string& printer_id) { |
| + // runs in blocking pool |
|
Lei Zhang
2016/11/03 23:41:46
Replace comment with a DCHECK.
skau
2016/11/04 19:28:59
Code has been removed.
|
| + scoped_refptr<printing::PrintBackend> print_backend( |
| + printing::PrintBackend::CreateInstance(nullptr)); |
| +#if defined(USE_CUPS) |
| + std::unique_ptr<printing::CupsConnection> connection = |
| + base::MakeUnique<printing::CupsConnection>(GURL(), HTTP_ENCRYPT_NEVER, |
| + true); |
| + std::unique_ptr<printing::CupsPrinter> cups_printer = |
| + connection->GetPrinter(printer_id); |
| + return !!cups_printer; |
| +#else |
| + return false; |
| +#endif |
| +} |
| + |
| +void OnPrinterSetup(std::unique_ptr<chromeos::Printer> printer, |
| + const PrinterSetupCallback& cb) { |
| + // This method is expected to run on the UI thread so it has access to the |
| + // message loop. |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + // printer is not in CUPS. Attempt setup. |
|
Lei Zhang
2016/11/03 23:41:46
This is a bit confusing. It sounds like this comme
skau
2016/11/04 19:28:59
Comments have been revised.
|
| + std::string ppd_path = GetPPDPath(*printer); |
| + if (ppd_path.empty()) { |
| + HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); |
| + return; |
| + } |
| + |
| + std::string printer_name = printer->id(); |
| + std::string printer_uri = printer->uri(); |
| + bool ipp_everywhere = IsIppEverywhere(*printer); |
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( |
|
Lei Zhang
2016/11/03 23:41:46
Just curious, why if the "debug daemon" the one we
skau
2016/11/04 19:28:59
The short answer: Some of the CUPS APIs suck
The
|
| + printer_name, printer_uri, ppd_path, ipp_everywhere, |
| + base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), |
| + base::Bind(&OnPrinterAddError, cb)); |
| + return; |
| +} |
| + |
| } // namespace |
| std::string GetDefaultPrinterOnBlockingPoolThread() { |
| @@ -64,4 +182,25 @@ PrinterList EnumeratePrintersOnBlockingPoolThread(Profile* profile) { |
| return printer_list; |
| } |
| +void ConfigurePrinterAndFetchCapabilities(Profile* profile, |
| + const std::string& printer_name, |
| + const PrinterSetupCallback& cb) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + chromeos::PrinterPrefManager* prefs_ = |
| + chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); |
|
Lei Zhang
2016/11/03 23:41:46
Is GetForBrowserContext() thread safe? Usually, on
skau
2016/11/04 19:28:59
It is not. This method now starts on the UI threa
|
| + |
| + std::unique_ptr<chromeos::Printer> printer = prefs_->GetPrinter(printer_name); |
| + |
| + if (IsPrinterConfigured(printer_name)) { |
| + // Printer found. Retrieve settings now. |
| + SettingsToUIThread(std::move(printer), cb); |
| + return; |
| + } |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&OnPrinterSetup, base::Passed(&printer), cb)); |
| +} |
| + |
| } // namespace printing |