 Chromium Code Reviews
 Chromium Code Reviews Issue 2542363002:
  Interrogate PpdProvider from PrintPreview.  (Closed)
    
  
    Issue 2542363002:
  Interrogate PpdProvider from PrintPreview.  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h" | 5 #include "chrome/browser/ui/webui/print_preview/printer_backend_proxy.h" | 
| 6 | 6 | 
| 7 #include <memory> | 7 #include <memory> | 
| 8 #include <vector> | 8 #include <vector> | 
| 9 | 9 | 
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" | 
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" | 
| 12 #include "base/logging.h" | 12 #include "base/logging.h" | 
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" | 
| 14 #include "base/values.h" | 14 #include "base/values.h" | 
| 15 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" | |
| 15 #include "chrome/browser/chromeos/printing/printer_pref_manager.h" | 16 #include "chrome/browser/chromeos/printing/printer_pref_manager.h" | 
| 16 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 17 #include "chrome/browser/chromeos/printing/printer_pref_manager_factory.h" | 
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" | 
| 18 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" | 19 #include "chrome/browser/ui/webui/print_preview/printer_capabilities.h" | 
| 19 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" | 
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" | 
| 21 #include "chromeos/dbus/debug_daemon_client.h" | 22 #include "chromeos/dbus/debug_daemon_client.h" | 
| 23 #include "chromeos/printing/ppd_provider.h" | |
| 22 #include "chromeos/printing/printer_configuration.h" | 24 #include "chromeos/printing/printer_configuration.h" | 
| 23 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" | 
| 24 #include "printing/backend/print_backend_consts.h" | 26 #include "printing/backend/print_backend_consts.h" | 
| 25 | 27 | 
| 26 namespace printing { | 28 namespace printing { | 
| 27 | 29 | 
| 28 namespace { | 30 namespace { | 
| 29 | 31 | 
| 30 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; | 32 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; | 
| 31 | 33 | 
| 32 // Store the name used in CUPS, Printer#id in |printer_name|, the description | 34 // Store the name used in CUPS, Printer#id in |printer_name|, the description | 
| 33 // as the system_driverinfo option value, and the Printer#display_name in | 35 // as the system_driverinfo option value, and the Printer#display_name in | 
| 34 // the |printer_description| field. This will match how Mac OS X presents | 36 // the |printer_description| field. This will match how Mac OS X presents | 
| 35 // printer information. | 37 // printer information. | 
| 36 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { | 38 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { | 
| 37 PrinterBasicInfo basic_info; | 39 PrinterBasicInfo basic_info; | 
| 38 | 40 | 
| 39 // TODO(skau): Unify Mac with the other platforms for display name | 41 // TODO(skau): Unify Mac with the other platforms for display name | 
| 40 // presentation so I can remove this strange code. | 42 // presentation so I can remove this strange code. | 
| 41 basic_info.options[kDriverInfoTagName] = printer.description(); | 43 basic_info.options[kDriverInfoTagName] = printer.description(); | 
| 42 basic_info.printer_name = printer.id(); | 44 basic_info.printer_name = printer.id(); | 
| 43 basic_info.printer_description = printer.display_name(); | 45 basic_info.printer_description = printer.display_name(); | 
| 44 return basic_info; | 46 return basic_info; | 
| 45 } | 47 } | 
| 46 | 48 | 
| 49 void AddPrintersToList( | |
| 50 PrinterList* list, | |
| 
Carlson
2016/12/02 19:10:22
Aren't output parameters usually last?
 
skau
2016/12/05 23:13:34
yes.  It's apparently in the style guide. https://
 | |
| 51 const std::vector<std::unique_ptr<chromeos::Printer>>& printers) { | |
| 52 for (const std::unique_ptr<chromeos::Printer>& printer : printers) { | |
| 53 list->push_back(ToBasicInfo(*printer)); | |
| 54 } | |
| 55 } | |
| 56 | |
| 47 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, | 57 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, | 
| 48 const PrinterSetupCallback& cb) { | 58 const PrinterSetupCallback& cb) { | 
| 49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 50 | 60 | 
| 51 PrinterBasicInfo basic_info = ToBasicInfo(*printer); | 61 PrinterBasicInfo basic_info = ToBasicInfo(*printer); | 
| 52 | 62 | 
| 53 base::PostTaskAndReplyWithResult( | 63 base::PostTaskAndReplyWithResult( | 
| 54 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 64 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 
| 55 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); | 65 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); | 
| 56 } | 66 } | 
| 57 | 67 | 
| 58 void PostCallbackError(const PrinterSetupCallback& cb) { | 68 void PostCallbackError(const PrinterSetupCallback& cb) { | 
| 59 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 69 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 
| 60 base::Bind(cb, nullptr)); | 70 base::Bind(cb, nullptr)); | 
| 61 } | 71 } | 
| 62 | 72 | 
| 63 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, | 73 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, | 
| 64 SetupResult result, | 74 SetupResult result, | 
| 65 const PrinterSetupCallback& cb) { | 75 const PrinterSetupCallback& cb) { | 
| 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 67 | 77 | 
| 68 if (result != SetupResult::SUCCESS) { | 78 if (result != SetupResult::SUCCESS) { | 
| 69 LOG(WARNING) << "Print setup failed"; | 79 LOG(WARNING) << "Print setup failed: " << result; | 
| 70 // TODO(skau): Open printer settings if this is resolvable. | 80 // TODO(skau): Open printer settings if this is resolvable. | 
| 71 PostCallbackError(cb); | 81 PostCallbackError(cb); | 
| 72 return; | 82 return; | 
| 73 } | 83 } | 
| 74 | 84 | 
| 75 VLOG(1) << "Printer setup successful for " << printer->id() | 85 VLOG(1) << "Printer setup successful for " << printer->id() | 
| 76 << " fetching properties"; | 86 << " fetching properties"; | 
| 77 | 87 | 
| 78 // fetch settings on the blocking pool and invoke callback. | 88 // fetch settings on the blocking pool and invoke callback. | 
| 79 FetchCapabilities(std::move(printer), cb); | 89 FetchCapabilities(std::move(printer), cb); | 
| (...skipping 15 matching lines...) Expand all Loading... | |
| 95 LOG(WARNING) << "Could not contact debugd"; | 105 LOG(WARNING) << "Could not contact debugd"; | 
| 96 PostCallbackError(cb); | 106 PostCallbackError(cb); | 
| 97 } | 107 } | 
| 98 | 108 | 
| 99 bool IsIppEverywhere(const chromeos::Printer& printer) { | 109 bool IsIppEverywhere(const chromeos::Printer& printer) { | 
| 100 // TODO(skau): Use uri, effective_make and effective_model to determine if | 110 // TODO(skau): Use uri, effective_make and effective_model to determine if | 
| 101 // we should do an IPP Everywhere configuration. | 111 // we should do an IPP Everywhere configuration. | 
| 102 return false; | 112 return false; | 
| 103 } | 113 } | 
| 104 | 114 | 
| 105 std::string GetPPDPath(const chromeos::Printer& printer) { | 115 void AddPrinter(std::unique_ptr<chromeos::Printer> printer, | 
| 106 // TODO(skau): Consult the PPD Provider for the correct file path. | 116 const std::string& ppd_path, | 
| 107 return printer.ppd_reference().user_supplied_ppd_url; | 117 bool ipp_everywhere, | 
| 108 } | 118 const PrinterSetupCallback& cb) { | 
| 119 // Always push configuration to CUPS. It may need an update. | |
| 120 const std::string printer_name = printer->id(); | |
| 121 const std::string printer_uri = printer->uri(); | |
| 109 | 122 | 
| 110 } // namespace | |
| 111 | |
| 112 std::string GetDefaultPrinterOnBlockingPoolThread() { | |
| 113 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 114 // TODO(crbug.com/660898): Add default printers to ChromeOS. | |
| 115 return ""; | |
| 116 } | |
| 117 | |
| 118 void EnumeratePrinters(Profile* profile, const EnumeratePrintersCallback& cb) { | |
| 119 // PrinterPrefManager is not thread safe and must be called from the UI | |
| 120 // thread. | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 | |
| 123 PrinterList printer_list; | |
| 124 | |
| 125 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 126 switches::kEnableNativeCups)) { | |
| 127 chromeos::PrinterPrefManager* prefs = | |
| 128 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | |
| 129 std::vector<std::unique_ptr<chromeos::Printer>> printers = | |
| 130 prefs->GetPrinters(); | |
| 131 for (const std::unique_ptr<chromeos::Printer>& printer : printers) { | |
| 132 printer_list.push_back(ToBasicInfo(*printer)); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 cb.Run(printer_list); | |
| 137 } | |
| 138 | |
| 139 void ConfigurePrinterAndFetchCapabilities(Profile* profile, | |
| 140 const std::string& printer_name, | |
| 141 const PrinterSetupCallback& cb) { | |
| 142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 143 | |
| 144 chromeos::PrinterPrefManager* prefs = | |
| 145 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | |
| 146 std::unique_ptr<chromeos::Printer> printer = prefs->GetPrinter(printer_name); | |
| 147 | |
| 148 // Check if configuration is viable. | |
| 149 bool ipp_everywhere = IsIppEverywhere(*printer); | |
| 150 std::string ppd_path = GetPPDPath(*printer); | |
| 151 if (!ipp_everywhere && ppd_path.empty()) { | |
| 152 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 // Always push configuration to CUPS. It may need an update. | |
| 157 std::string printer_uri = printer->uri(); | |
| 158 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( | 123 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( | 
| 159 printer_name, printer_uri, ppd_path, ipp_everywhere, | 124 printer_name, printer_uri, ppd_path, ipp_everywhere, | 
| 160 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), | 125 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), | 
| 161 base::Bind(&OnPrinterAddError, cb)); | 126 base::Bind(&OnPrinterAddError, cb)); | 
| 162 } | 127 } | 
| 163 | 128 | 
| 129 void PPDResolve(std::unique_ptr<chromeos::Printer> printer, | |
| 130 const PrinterSetupCallback& cb, | |
| 131 chromeos::printing::PpdProvider::CallbackResultCode result, | |
| 132 base::FilePath path) { | |
| 133 switch (result) { | |
| 134 case chromeos::printing::PpdProvider::SUCCESS: { | |
| 135 DCHECK(!path.empty()); | |
| 136 AddPrinter(std::move(printer), path.value() /* ppd path */, | |
| 137 false /* non-ipp-everywhere */, cb); | |
| 138 break; | |
| 139 } | |
| 140 case chromeos::printing::PpdProvider::NOT_FOUND: | |
| 141 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); | |
| 142 break; | |
| 143 default: | |
| 144 HandlePrinterSetup(std::move(printer), FAILURE, cb); | |
| 145 break; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 class PrinterBackendProxyChromeos : public PrinterBackendProxy { | |
| 150 public: | |
| 151 explicit PrinterBackendProxyChromeos(Profile* profile) { | |
| 152 prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | |
| 153 ppd_provider_ = chromeos::printing::CreateProvider(profile); | |
| 154 } | |
| 155 | |
| 156 std::string GetDefaultPrinterOnBlockingPoolThread() override { | |
| 157 DCHECK( | |
| 158 content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 159 // TODO(crbug.com/660898): Add default printers to ChromeOS. | |
| 160 return ""; | |
| 161 }; | |
| 162 | |
| 163 void EnumeratePrinters(const EnumeratePrintersCallback& cb) override { | |
| 164 // PrinterPrefManager is not thread safe and must be called from the UI | |
| 165 // thread. | |
| 166 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 167 | |
| 168 PrinterList printer_list; | |
| 169 | |
| 170 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 171 switches::kEnableNativeCups)) { | |
| 172 AddPrintersToList(&printer_list, prefs_->GetPrinters()); | |
| 173 } | |
| 174 | |
| 175 cb.Run(printer_list); | |
| 176 }; | |
| 177 | |
| 178 void ConfigurePrinterAndFetchCapabilities( | |
| 179 const std::string& printer_name, | |
| 180 const PrinterSetupCallback& cb) override { | |
| 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 182 | |
| 183 std::unique_ptr<chromeos::Printer> printer = | |
| 184 prefs_->GetPrinter(printer_name); | |
| 185 if (!printer) { | |
| 186 // If the printer was removed, the lookup will fail. | |
| 187 cb.Run(nullptr); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 191 if (IsIppEverywhere(*printer)) { | |
| 192 // ChromeOS registers printer by GUID rather than display name. | |
| 193 AddPrinter(std::move(printer), "" /* empty ppd path */, | |
| 194 true /* ipp everywhere */, cb); | |
| 195 return; | |
| 196 } | |
| 197 | |
| 198 // Ref taken because printer is moved. | |
| 199 const chromeos::Printer::PpdReference ppd_ref = printer->ppd_reference(); | |
| 200 ppd_provider_->Resolve(ppd_ref, | |
| 201 base::Bind(&PPDResolve, base::Passed(&printer), cb)); | |
| 
Carlson
2016/12/02 19:10:22
Do we need WeakPtr semantics here?
 
skau
2016/12/05 23:13:34
No.  We're transferring ownership of the printer.
 
Carlson
2016/12/05 23:45:58
I'm not sure I understand.  What guarantees do we
 
skau
2016/12/06 23:20:08
Spoke offline.  Resolve is invoked on the UI threa
 | |
| 202 }; | |
| 203 | |
| 204 private: | |
| 205 chromeos::PrinterPrefManager* prefs_; | |
| 206 std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_; | |
| 207 }; | |
| 208 | |
| 209 } // namespace | |
| 210 | |
| 211 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( | |
| 212 Profile* profile) { | |
| 213 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); | |
| 214 } | |
| 215 | |
| 164 } // namespace printing | 216 } // namespace printing | 
| OLD | NEW |