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

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

Issue 2542363002: Interrogate PpdProvider from PrintPreview. (Closed)
Patch Set: assert that the proxy is destructed on the ui thread Created 4 years 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 0cffa21e0c591123901b58e4be5f9689266680b7..0c9720b5c03b6d4651147bb4963598b1babdb81b 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
@@ -12,6 +12,7 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
+#include "chrome/browser/chromeos/printing/ppd_provider_factory.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"
@@ -19,6 +20,7 @@
#include "chrome/common/chrome_switches.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon_client.h"
+#include "chromeos/printing/ppd_provider.h"
#include "chromeos/printing/printer_configuration.h"
#include "content/public/browser/browser_thread.h"
#include "printing/backend/print_backend_consts.h"
@@ -44,6 +46,14 @@ printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) {
return basic_info;
}
+void AddPrintersToList(
+ const std::vector<std::unique_ptr<chromeos::Printer>>& printers,
+ PrinterList* list) {
+ for (const std::unique_ptr<chromeos::Printer>& printer : printers) {
+ list->push_back(ToBasicInfo(*printer));
+ }
+}
+
void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer,
const PrinterSetupCallback& cb) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
@@ -66,7 +76,7 @@ void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer,
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (result != SetupResult::SUCCESS) {
- LOG(WARNING) << "Print setup failed";
+ LOG(WARNING) << "Print setup failed: " << result;
// TODO(skau): Open printer settings if this is resolvable.
PostCallbackError(cb);
return;
@@ -102,63 +112,110 @@ bool IsIppEverywhere(const chromeos::Printer& printer) {
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;
-}
+void AddPrinter(std::unique_ptr<chromeos::Printer> printer,
+ const std::string& ppd_path,
+ bool ipp_everywhere,
+ const PrinterSetupCallback& cb) {
+ // Always push configuration to CUPS. It may need an update.
+ const std::string printer_name = printer->id();
+ const std::string printer_uri = printer->uri();
-} // namespace
+ chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter(
+ printer_name, printer_uri, ppd_path, ipp_everywhere,
+ base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb),
+ base::Bind(&OnPrinterAddError, cb));
+}
-std::string GetDefaultPrinterOnBlockingPoolThread() {
- DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
- // TODO(crbug.com/660898): Add default printers to ChromeOS.
- return "";
+void PPDResolve(std::unique_ptr<chromeos::Printer> printer,
+ const PrinterSetupCallback& cb,
+ chromeos::printing::PpdProvider::CallbackResultCode result,
+ base::FilePath path) {
+ switch (result) {
+ case chromeos::printing::PpdProvider::SUCCESS: {
+ DCHECK(!path.empty());
+ AddPrinter(std::move(printer), path.value() /* ppd path */,
+ false /* non-ipp-everywhere */, cb);
+ break;
+ }
+ case chromeos::printing::PpdProvider::NOT_FOUND:
+ HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb);
+ break;
+ default:
+ HandlePrinterSetup(std::move(printer), FAILURE, cb);
+ break;
+ }
}
-void EnumeratePrinters(Profile* profile, const EnumeratePrintersCallback& cb) {
- // PrinterPrefManager is not thread safe and must be called from the UI
- // thread.
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+class PrinterBackendProxyChromeos : public PrinterBackendProxy {
+ public:
+ explicit PrinterBackendProxyChromeos(Profile* profile) {
+ prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
+ ppd_provider_ = chromeos::printing::CreateProvider(profile);
+ }
+
+ ~PrinterBackendProxyChromeos() override {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ }
+
+ void GetDefaultPrinter(const DefaultPrinterCallback& cb) override {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ // TODO(crbug.com/660898): Add default printers to ChromeOS.
+ cb.Run("");
Carlson 2016/12/06 23:31:57 While this conforms to my own sensibilities, I thi
skau 2016/12/07 18:19:47 Done.
+ };
- PrinterList printer_list;
+ void EnumeratePrinters(const EnumeratePrintersCallback& cb) override {
+ // PrinterPrefManager is not thread safe and must be called from the UI
+ // thread.
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
- if (base::CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kEnableNativeCups)) {
- chromeos::PrinterPrefManager* prefs =
- chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
- std::vector<std::unique_ptr<chromeos::Printer>> printers =
- prefs->GetPrinters();
- for (const std::unique_ptr<chromeos::Printer>& printer : printers) {
- printer_list.push_back(ToBasicInfo(*printer));
+ PrinterList printer_list;
+
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableNativeCups)) {
+ AddPrintersToList(prefs_->GetPrinters(), &printer_list);
}
- }
- cb.Run(printer_list);
-}
+ cb.Run(printer_list);
+ };
-void ConfigurePrinterAndFetchCapabilities(Profile* profile,
- const std::string& printer_name,
- const PrinterSetupCallback& cb) {
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ void ConfigurePrinterAndFetchCapabilities(
+ const std::string& printer_name,
+ const PrinterSetupCallback& cb) override {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
- chromeos::PrinterPrefManager* prefs =
- chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile);
- std::unique_ptr<chromeos::Printer> printer = prefs->GetPrinter(printer_name);
+ std::unique_ptr<chromeos::Printer> printer =
+ prefs_->GetPrinter(printer_name);
+ if (!printer) {
+ // If the printer was removed, the lookup will fail.
+ cb.Run(nullptr);
+ return;
+ }
- // Check if configuration is viable.
- bool ipp_everywhere = IsIppEverywhere(*printer);
- std::string ppd_path = GetPPDPath(*printer);
- if (!ipp_everywhere && ppd_path.empty()) {
- HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb);
- return;
- }
+ if (IsIppEverywhere(*printer)) {
+ // ChromeOS registers printer by GUID rather than display name.
+ AddPrinter(std::move(printer), "" /* empty ppd path */,
+ true /* ipp everywhere */, cb);
+ return;
+ }
- // Always push configuration to CUPS. It may need an update.
- std::string printer_uri = printer->uri();
- chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter(
- printer_name, printer_uri, ppd_path, ipp_everywhere,
- base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb),
- base::Bind(&OnPrinterAddError, cb));
+ // Ref taken because printer is moved.
+ const chromeos::Printer::PpdReference& ppd_ref = printer->ppd_reference();
+ ppd_provider_->Resolve(ppd_ref,
+ base::Bind(&PPDResolve, base::Passed(&printer), cb));
+ };
+
+ private:
+ chromeos::PrinterPrefManager* prefs_;
+ std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_;
+
+ DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos);
+};
+
+} // namespace
+
+std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create(
+ Profile* profile) {
+ return base::MakeUnique<PrinterBackendProxyChromeos>(profile);
}
} // namespace printing

Powered by Google App Engine
This is Rietveld 408576698