Chromium Code Reviews| 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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "chromeos/dbus/debug_daemon_client.h" | 22 #include "chromeos/dbus/debug_daemon_client.h" |
| 23 #include "chromeos/printing/ppd_provider.h" | 23 #include "chromeos/printing/ppd_provider.h" |
| 24 #include "chromeos/printing/printer_configuration.h" | 24 #include "chromeos/printing/printer_configuration.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "printing/backend/print_backend_consts.h" | 26 #include "printing/backend/print_backend_consts.h" |
| 27 | 27 |
| 28 namespace printing { | 28 namespace printing { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; | |
| 33 | |
| 34 // Store the name used in CUPS, Printer#id in |printer_name|, the description | |
| 35 // as the system_driverinfo option value, and the Printer#display_name in | |
| 36 // the |printer_description| field. This will match how Mac OS X presents | |
| 37 // printer information. | |
| 38 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { | |
| 39 PrinterBasicInfo basic_info; | |
| 40 | |
| 41 // TODO(skau): Unify Mac with the other platforms for display name | |
| 42 // presentation so I can remove this strange code. | |
| 43 basic_info.options[kDriverInfoTagName] = printer.description(); | |
| 44 basic_info.printer_name = printer.id(); | |
| 45 basic_info.printer_description = printer.display_name(); | |
| 46 return basic_info; | |
| 47 } | |
| 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 | |
| 57 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, | |
| 58 const PrinterSetupCallback& cb) { | |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 60 | |
| 61 PrinterBasicInfo basic_info = ToBasicInfo(*printer); | |
| 62 | |
| 63 base::PostTaskAndReplyWithResult( | |
| 64 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
| 65 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); | |
| 66 } | |
| 67 | |
| 68 void PostCallbackError(const PrinterSetupCallback& cb) { | |
| 69 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 70 base::Bind(cb, nullptr)); | |
| 71 } | |
| 72 | |
| 73 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, | |
| 74 SetupResult result, | |
| 75 const PrinterSetupCallback& cb) { | |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 77 | |
| 78 if (result != SetupResult::SUCCESS) { | |
| 79 LOG(WARNING) << "Print setup failed: " << result; | |
| 80 // TODO(skau): Open printer settings if this is resolvable. | |
| 81 PostCallbackError(cb); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 VLOG(1) << "Printer setup successful for " << printer->id() | |
| 86 << " fetching properties"; | |
| 87 | |
| 88 // fetch settings on the blocking pool and invoke callback. | |
| 89 FetchCapabilities(std::move(printer), cb); | |
| 90 } | |
| 91 | |
| 92 void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer, | |
| 93 const PrinterSetupCallback& cb, | |
| 94 bool success) { | |
| 95 // It's expected that debug daemon posts callbacks on the UI thread. | |
| 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 97 | |
| 98 HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb); | |
| 99 } | |
| 100 | |
| 101 void OnPrinterAddError(const PrinterSetupCallback& cb) { | |
| 102 // It's expected that debug daemon posts callbacks on the UI thread. | |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 104 | |
| 105 LOG(WARNING) << "Could not contact debugd"; | |
| 106 PostCallbackError(cb); | |
| 107 } | |
| 108 | |
| 109 void AddPrinter(std::unique_ptr<chromeos::Printer> printer, | |
| 110 const std::string& ppd_path, | |
| 111 bool ipp_everywhere, | |
| 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(); | |
| 116 | |
| 117 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( | |
| 118 printer_name, printer_uri, ppd_path, ipp_everywhere, | |
| 119 base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), | |
| 120 base::Bind(&OnPrinterAddError, cb)); | |
| 121 } | |
| 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 { | 32 class PrinterBackendProxyChromeos : public PrinterBackendProxy { |
| 144 public: | 33 public: |
| 145 explicit PrinterBackendProxyChromeos(Profile* profile) { | 34 enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; |
| 35 | |
| 36 explicit PrinterBackendProxyChromeos(Profile* profile) : weak_factory_(this) { | |
| 146 prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); | 37 prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); |
| 147 ppd_provider_ = chromeos::printing::CreateProvider(profile); | 38 ppd_provider_ = chromeos::printing::CreateProvider(profile); |
| 148 } | 39 } |
| 149 | 40 |
| 150 ~PrinterBackendProxyChromeos() override { | 41 ~PrinterBackendProxyChromeos() override { |
| 151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 152 } | 43 } |
| 153 | 44 |
| 154 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override { | 45 void GetDefaultPrinter(const DefaultPrinterCallback& cb) override { |
| 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 | 83 |
| 193 if (printer->IsIppEverywhere()) { | 84 if (printer->IsIppEverywhere()) { |
| 194 // ChromeOS registers printer by GUID rather than display name. | 85 // ChromeOS registers printer by GUID rather than display name. |
| 195 AddPrinter(std::move(printer), "" /* empty ppd path */, | 86 AddPrinter(std::move(printer), "" /* empty ppd path */, |
| 196 true /* ipp everywhere */, cb); | 87 true /* ipp everywhere */, cb); |
| 197 return; | 88 return; |
| 198 } | 89 } |
| 199 | 90 |
| 200 // Ref taken because printer is moved. | 91 // Ref taken because printer is moved. |
| 201 const chromeos::Printer::PpdReference& ppd_ref = printer->ppd_reference(); | 92 const chromeos::Printer::PpdReference& ppd_ref = printer->ppd_reference(); |
| 202 ppd_provider_->Resolve(ppd_ref, | 93 ppd_provider_->Resolve( |
| 203 base::Bind(&PPDResolve, base::Passed(&printer), cb)); | 94 ppd_ref, |
| 95 base::Bind(&PrinterBackendProxyChromeos::PPDResolve, | |
| 96 weak_factory_.GetWeakPtr(), base::Passed(&printer), cb)); | |
| 204 }; | 97 }; |
| 205 | 98 |
| 206 private: | 99 private: |
| 100 // Store the name used in CUPS, Printer#id in |printer_name|, the description | |
| 101 // as the system_driverinfo option value, and the Printer#display_name in | |
| 102 // the |printer_description| field. This will match how Mac OS X presents | |
| 103 // printer information. | |
| 104 printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { | |
| 105 PrinterBasicInfo basic_info; | |
| 106 | |
| 107 // TODO(skau): Unify Mac with the other platforms for display name | |
| 108 // presentation so I can remove this strange code. | |
| 109 basic_info.options[kDriverInfoTagName] = printer.description(); | |
| 110 basic_info.options[kCUPSEnterprisePrinter] = | |
| 111 prefs_->IsEnterprisePrinter(printer.id()) ? kValueTrue : kValueFalse; | |
|
xdai1
2017/01/10 19:17:42
The reason I put the functions in the anonymous na
| |
| 112 basic_info.printer_name = printer.id(); | |
| 113 basic_info.printer_description = printer.display_name(); | |
| 114 return basic_info; | |
| 115 } | |
| 116 | |
| 117 void AddPrintersToList( | |
| 118 const std::vector<std::unique_ptr<chromeos::Printer>>& printers, | |
| 119 PrinterList* list) { | |
| 120 for (const auto& printer : printers) { | |
| 121 list->push_back(ToBasicInfo(*printer)); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, | |
| 126 const PrinterSetupCallback& cb) { | |
| 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 128 | |
| 129 PrinterBasicInfo basic_info = ToBasicInfo(*printer); | |
| 130 | |
| 131 base::PostTaskAndReplyWithResult( | |
| 132 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
| 133 base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); | |
| 134 } | |
| 135 | |
| 136 void PostCallbackError(const PrinterSetupCallback& cb) { | |
| 137 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 138 base::Bind(cb, nullptr)); | |
| 139 } | |
| 140 | |
| 141 void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, | |
| 142 SetupResult result, | |
| 143 const PrinterSetupCallback& cb) { | |
| 144 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 145 | |
| 146 if (result != SetupResult::SUCCESS) { | |
| 147 LOG(WARNING) << "Print setup failed: " << result; | |
| 148 // TODO(skau): Open printer settings if this is resolvable. | |
| 149 PostCallbackError(cb); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 VLOG(1) << "Printer setup successful for " << printer->id() | |
| 154 << " fetching properties"; | |
| 155 | |
| 156 // fetch settings on the blocking pool and invoke callback. | |
| 157 FetchCapabilities(std::move(printer), cb); | |
| 158 } | |
| 159 | |
| 160 void OnPrinterAddResult(std::unique_ptr<chromeos::Printer> printer, | |
| 161 const PrinterSetupCallback& cb, | |
| 162 bool success) { | |
| 163 // It's expected that debug daemon posts callbacks on the UI thread. | |
| 164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 165 | |
| 166 HandlePrinterSetup(std::move(printer), success ? SUCCESS : FAILURE, cb); | |
| 167 } | |
| 168 | |
| 169 void OnPrinterAddError(const PrinterSetupCallback& cb) { | |
| 170 // It's expected that debug daemon posts callbacks on the UI thread. | |
| 171 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 172 | |
| 173 LOG(WARNING) << "Could not contact debugd"; | |
| 174 PostCallbackError(cb); | |
| 175 } | |
| 176 | |
| 177 void AddPrinter(std::unique_ptr<chromeos::Printer> printer, | |
| 178 const std::string& ppd_path, | |
| 179 bool ipp_everywhere, | |
| 180 const PrinterSetupCallback& cb) { | |
| 181 // Always push configuration to CUPS. It may need an update. | |
| 182 const std::string printer_name = printer->id(); | |
| 183 const std::string printer_uri = printer->uri(); | |
| 184 | |
| 185 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( | |
| 186 printer_name, printer_uri, ppd_path, ipp_everywhere, | |
| 187 base::Bind(&PrinterBackendProxyChromeos::OnPrinterAddResult, | |
| 188 weak_factory_.GetWeakPtr(), base::Passed(&printer), cb), | |
| 189 base::Bind(&PrinterBackendProxyChromeos::OnPrinterAddError, | |
| 190 weak_factory_.GetWeakPtr(), cb)); | |
| 191 } | |
| 192 | |
| 193 void PPDResolve(std::unique_ptr<chromeos::Printer> printer, | |
| 194 const PrinterSetupCallback& cb, | |
| 195 chromeos::printing::PpdProvider::CallbackResultCode result, | |
| 196 base::FilePath path) { | |
| 197 switch (result) { | |
| 198 case chromeos::printing::PpdProvider::SUCCESS: { | |
| 199 DCHECK(!path.empty()); | |
| 200 AddPrinter(std::move(printer), path.value() /* ppd path */, | |
| 201 false /* non-ipp-everywhere */, cb); | |
| 202 break; | |
| 203 } | |
| 204 case chromeos::printing::PpdProvider::NOT_FOUND: | |
| 205 HandlePrinterSetup(std::move(printer), PPD_NOT_FOUND, cb); | |
| 206 break; | |
| 207 default: | |
| 208 HandlePrinterSetup(std::move(printer), FAILURE, cb); | |
| 209 break; | |
| 210 } | |
| 211 } | |
| 212 | |
| 207 chromeos::PrinterPrefManager* prefs_; | 213 chromeos::PrinterPrefManager* prefs_; |
| 208 std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_; | 214 std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_; |
| 215 base::WeakPtrFactory<PrinterBackendProxyChromeos> weak_factory_; | |
| 209 | 216 |
| 210 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); | 217 DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); |
| 211 }; | 218 }; |
| 212 | 219 |
| 213 } // namespace | 220 } // namespace |
| 214 | 221 |
| 215 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( | 222 std::unique_ptr<PrinterBackendProxy> PrinterBackendProxy::Create( |
| 216 Profile* profile) { | 223 Profile* profile) { |
| 217 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); | 224 return base::MakeUnique<PrinterBackendProxyChromeos>(profile); |
| 218 } | 225 } |
| 219 | 226 |
| 220 } // namespace printing | 227 } // namespace printing |
| OLD | NEW |