| 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 const std::vector<std::unique_ptr<chromeos::Printer>>& printers, |
| 51 PrinterList* list) { |
| 52 for (const auto& 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); |
| 80 } | 90 } |
| 81 | 91 |
| 82 void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer, | 92 void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer, |
| 83 const PrinterSetupCallback& cb, | 93 const PrinterSetupCallback& cb, |
| 84 bool success) { | 94 bool success) { |
| 85 // It's expected that debug daemon posts callbacks on the UI thread. | 95 // It's expected that debug daemon posts callbacks on the UI thread. |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 87 | 97 |
| 88 HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb); | 98 HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb); |
| 89 } | 99 } |
| 90 | 100 |
| 91 void OnPrinterAddError(const PrinterSetupCallback& cb) { | 101 void OnPrinterAddError(const PrinterSetupCallback& cb) { |
| 92 // It's expected that debug daemon posts callbacks on the UI thread. | 102 // It's expected that debug daemon posts callbacks on the UI thread. |
| 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 94 | 104 |
| 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 std::string GetPPDPath(const chromeos::Printer& printer) { | 109 void AddPrinter(std::unique_ptr<chromeos::Printer> printer, |
| 100 // TODO(skau): Consult the PPD Provider for the correct file path. | 110 const std::string& ppd_path, |
| 101 return printer.ppd_reference().user_supplied_ppd_url; | 111 bool ipp_everywhere, |
| 102 } | 112 const PrinterSetupCallback& cb) { |
| 113 // Always push configuration to CUPS. It may need an update. |
| 114 const std::string printer_name = printer->id(); |
| 115 const std::string printer_uri = printer->uri(); |
| 103 | 116 |
| 104 } // namespace | |
| 105 | |
| 106 std::string GetDefaultPrinterOnBlockingPoolThread() { | |
| 107 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 108 // TODO(crbug.com/660898): Add default printers to ChromeOS. | |
| 109 return ""; | |
| 110 } | |
| 111 | |
| 112 void EnumeratePrinters(Profile* profile, const EnumeratePrintersCallback& cb) { | |
| 113 // PrinterPrefManager is not thread safe and must be called from the UI | |
| 114 // thread. | |
| 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 116 | |
| 117 PrinterList printer_list; | |
| 118 | |
| 119 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 120 switches::kEnableNativeCups)) { | |
| 121 chromeos::PrinterPrefManager* prefs = | |
| 122 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | |
| 123 std::vector<std::unique_ptr<chromeos::Printer>> printers = | |
| 124 prefs->GetPrinters(); | |
| 125 for (const std::unique_ptr<chromeos::Printer>& printer : printers) { | |
| 126 printer_list.push_back(ToBasicInfo(*printer)); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 cb.Run(printer_list); | |
| 131 } | |
| 132 | |
| 133 void ConfigurePrinterAndFetchCapabilities(Profile* profile, | |
| 134 const std::string& printer_name, | |
| 135 const PrinterSetupCallback& cb) { | |
| 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 137 | |
| 138 chromeos::PrinterPrefManager* prefs = | |
| 139 chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | |
| 140 std::unique_ptr<chromeos::Printer> printer = prefs->GetPrinter(printer_name); | |
| 141 | |
| 142 // Check if configuration is viable. | |
| 143 bool ipp_everywhere = printer->IsIppEverywhere(); | |
| 144 std::string ppd_path = GetPPDPath(*printer); | |
| 145 if (!ipp_everywhere && ppd_path.empty()) { | |
| 146 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 // Always push configuration to CUPS. It may need an update. | |
| 151 std::string printer_uri = printer->uri(); | |
| 152 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( | 117 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( |
| 153 printer_name, printer_uri, ppd_path, ipp_everywhere, | 118 printer_name, printer_uri, ppd_path, ipp_everywhere, |
| 154 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), | 119 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), |
| 155 base::Bind(&OnPrinterAddError, cb)); | 120 base::Bind(&OnPrinterAddError, cb)); |
| 156 } | 121 } |
| 157 | 122 |
| 123 void PPDResolve(std::unique_ptr<chromeos::Printer> printer, |
| 124 const PrinterSetupCallback& cb, |
| 125 chromeos::printing::PpdProvider::CallbackResultCode result, |
| 126 base::FilePath path) { |
| 127 switch (result) { |
| 128 case chromeos::printing::PpdProvider::SUCCESS: { |
| 129 DCHECK(!path.empty()); |
| 130 AddPrinter(std::move(printer), path.value() /* ppd path */, |
| 131 false /* non-ipp-everywhere */, cb); |
| 132 break; |
| 133 } |
| 134 case chromeos::printing::PpdProvider::NOT_FOUND: |
| 135 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); |
| 136 break; |
| 137 default: |
| 138 HandlePrinterSetup(std::move(printer), FAILURE, cb); |
| 139 break; |
| 140 } |
| 141 } |
| 142 |
| 143 class PrinterBackendProxyChromeos : public PrinterBackendProxy { |
| 144 public: |
| 145 explicit PrinterBackendProxyChromeos(Profile* profile) { |
| 146 prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); |
| 147 ppd_provider_ = chromeos::printing::CreateProvider(profile); |
| 148 } |
| 149 |
| 150 ~PrinterBackendProxyChromeos() override { |
| 151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 152 } |
| 153 |
| 154 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override { |
| 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 // TODO(crbug.com/660898): Add default printers to ChromeOS. |
| 157 |
| 158 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 159 base::Bind(cb, "")); |
| 160 }; |
| 161 |
| 162 void EnumeratePrinters(const EnumeratePrintersCallback& cb) override { |
| 163 // PrinterPrefManager is not thread safe and must be called from the UI |
| 164 // thread. |
| 165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 166 |
| 167 PrinterList printer_list; |
| 168 |
| 169 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 170 switches::kEnableNativeCups)) { |
| 171 AddPrintersToList(prefs_->GetPrinters(), &printer_list); |
| 172 } |
| 173 |
| 174 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 175 base::Bind(cb, 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 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 188 base::Bind(cb, nullptr)); |
| 189 return; |
| 190 } |
| 191 |
| 192 if (printer->IsIppEverywhere()) { |
| 193 // ChromeOS registers printer by GUID rather than display name. |
| 194 AddPrinter(std::move(printer), "" /* empty ppd path */, |
| 195 true /* ipp everywhere */, cb); |
| 196 return; |
| 197 } |
| 198 |
| 199 // Ref taken because printer is moved. |
| 200 const chromeos::Printer::PpdReference& ppd_ref = printer->ppd_reference(); |
| 201 ppd_provider_->Resolve(ppd_ref, |
| 202 base::Bind(&PPDResolve, base::Passed(&printer), cb)); |
| 203 }; |
| 204 |
| 205 private: |
| 206 chromeos::PrinterPrefManager* prefs_; |
| 207 std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_; |
| 208 |
| 209 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); |
| 210 }; |
| 211 |
| 212 } // namespace |
| 213 |
| 214 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( |
| 215 Profile* profile) { |
| 216 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); |
| 217 } |
| 218 |
| 158 } // namespace printing | 219 } // namespace printing |
| OLD | NEW |