Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2733)

Unified Diff: chrome/browser/ui/webui/print_preview/printer_backend_proxy_chromeos.cc

Issue 2457933004: Register and select printer on click. (Closed)
Patch Set: lint Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 a061389668cefb1d9dc5b4445fd6b566c62dca76..95a4fc957ed55b0ae940414c59df99e317a66e75 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,20 +7,35 @@
#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"
+#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 };
+
printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) {
PrinterBasicInfo basic_info;
basic_info.printer_display_name = printer.display_name();
@@ -29,6 +44,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);
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(cb, base::Passed(std::move(capabilities))));
Lei Zhang 2016/11/02 23:09:22 base/bind_helper.h says you can do base::Passed(&c
skau 2016/11/03 22:38:08 Done.
+}
+
+void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer,
+ printing::SetupResult result,
+ const PrinterSetupCallback& cb) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ if (result != printing::SetupResult::SUCCESS) {
+ LOG(WARNING) << "Print setup failed";
+ // TODO(skau): Open printer settings if this is resolvable.
Lei Zhang 2016/11/02 23:09:22 I worry when the callback doesn't run. Is that ok
skau 2016/11/03 22:38:08 Not running the callback leaves print preview in t
+ 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(std::move(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(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Lei Zhang 2016/11/02 23:09:22 Use DCHECK_CURRENTLY_ON().
skau 2016/11/03 22:38:08 Thanks! I hadn't seen that.
+
+ 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(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ LOG(WARNING) << "Printer add failure";
+ base::MessageLoop::current()->task_runner()->PostTask(
+ FROM_HERE, base::Bind(cb, nullptr));
+}
+
+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 IsPrinterSetup(const std::string& printer_id) {
Lei Zhang 2016/11/02 23:09:22 Maybe rename this to IsPrinterConfigured? I flip b
skau 2016/11/03 22:38:08 Done.
+ // runs in blocking pool
+ 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(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ // printer is not in CUPS. Attempt setup.
+ std::string ppd_path = GetPPDPath(*printer);
+ if (ppd_path.empty()) {
+ base::MessageLoop::current()->task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&HandlePrinterSetup, base::Passed(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(
+ printer_name, printer_uri, ppd_path, ipp_everywhere,
+ base::Bind(&OnPrinterAddResult, base::Passed(std::move(printer)), cb),
+ base::Bind(&OnPrinterAddError, cb));
+ return;
+}
+
} // namespace
std::string GetDefaultPrinterOnBlockingPoolThread() {
@@ -56,4 +174,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);
+
+ std::unique_ptr<chromeos::Printer> printer = prefs_->GetPrinter(printer_name);
+
+ if (IsPrinterSetup(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(std::move(printer)), cb));
+}
+
} // namespace printing

Powered by Google App Engine
This is Rietveld 408576698