Chromium Code Reviews| 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 2d068a4445eb8035b3a362ef16f72b23dc0d4fc2..a15a526ac3be640dc7500bb8aaee1ede9cebeff6 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 |
| @@ -29,120 +29,11 @@ namespace printing { |
| namespace { |
| -enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; |
| - |
| -// Store the name used in CUPS, Printer#id in |printer_name|, the description |
| -// as the system_driverinfo option value, and the Printer#display_name in |
| -// the |printer_description| field. This will match how Mac OS X presents |
| -// printer information. |
| -printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { |
| - PrinterBasicInfo basic_info; |
| - |
| - // TODO(skau): Unify Mac with the other platforms for display name |
| - // presentation so I can remove this strange code. |
| - basic_info.options[kDriverInfoTagName] = printer.description(); |
| - basic_info.printer_name = printer.id(); |
| - basic_info.printer_description = printer.display_name(); |
| - return basic_info; |
| -} |
| - |
| -void AddPrintersToList( |
| - const std::vector<std::unique_ptr<chromeos::Printer>>& printers, |
| - PrinterList* list) { |
| - for (const auto& printer : printers) { |
| - list->push_back(ToBasicInfo(*printer)); |
| - } |
| -} |
| - |
| -void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, |
| - const PrinterSetupCallback& cb) { |
| - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| - |
| - PrinterBasicInfo basic_info = ToBasicInfo(*printer); |
| - |
| - base::PostTaskAndReplyWithResult( |
| - content::BrowserThread::GetBlockingPool(), FROM_HERE, |
| - base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); |
| -} |
| - |
| -void PostCallbackError(const PrinterSetupCallback& cb) { |
| - content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| - base::Bind(cb, nullptr)); |
| -} |
| - |
| -void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, |
| - SetupResult result, |
| - const PrinterSetupCallback& cb) { |
| - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| - |
| - if (result != SetupResult::SUCCESS) { |
| - LOG(WARNING) << "Print setup failed: " << result; |
| - // TODO(skau): Open printer settings if this is resolvable. |
| - PostCallbackError(cb); |
| - return; |
| - } |
| - |
| - VLOG(1) << "Printer setup successful for " << printer->id() |
| - << " fetching properties"; |
| - |
| - // fetch settings on the blocking pool and invoke callback. |
| - FetchCapabilities(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_CURRENTLY_ON(content::BrowserThread::UI); |
| - |
| - 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_CURRENTLY_ON(content::BrowserThread::UI); |
| - |
| - LOG(WARNING) << "Could not contact debugd"; |
| - PostCallbackError(cb); |
| -} |
| - |
| -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(); |
| - |
| - chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( |
| - printer_name, printer_uri, ppd_path, ipp_everywhere, |
| - base::Bind(&OnPrinterAddResult, base::Passed(&printer), cb), |
| - base::Bind(&OnPrinterAddError, cb)); |
| -} |
| - |
| -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; |
| - } |
| -} |
| - |
| class PrinterBackendProxyChromeos : public PrinterBackendProxy { |
| public: |
| - explicit PrinterBackendProxyChromeos(Profile* profile) { |
| + enum SetupResult { SUCCESS, PPD_NOT_FOUND, FAILURE }; |
| + |
| + explicit PrinterBackendProxyChromeos(Profile* profile) : weak_factory_(this) { |
| prefs_ = chromeos::PrinterPrefManagerFactory::GetForBrowserContext(profile); |
| ppd_provider_ = chromeos::printing::CreateProvider(profile); |
| } |
| @@ -199,13 +90,129 @@ class PrinterBackendProxyChromeos : public PrinterBackendProxy { |
| // 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)); |
| + ppd_provider_->Resolve( |
| + ppd_ref, |
| + base::Bind(&PrinterBackendProxyChromeos::PPDResolve, |
| + weak_factory_.GetWeakPtr(), base::Passed(&printer), cb)); |
| }; |
| private: |
| + // Store the name used in CUPS, Printer#id in |printer_name|, the description |
| + // as the system_driverinfo option value, and the Printer#display_name in |
| + // the |printer_description| field. This will match how Mac OS X presents |
| + // printer information. |
| + printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { |
| + PrinterBasicInfo basic_info; |
| + |
| + // TODO(skau): Unify Mac with the other platforms for display name |
| + // presentation so I can remove this strange code. |
| + basic_info.options[kDriverInfoTagName] = printer.description(); |
| + basic_info.options[kCUPSEnterprisePrinter] = |
| + prefs_->IsEnterprisePrinter(printer.id()) ? kValueTrue : kValueFalse; |
|
xdai1
2017/01/10 19:17:42
The reason I put the functions in the anonymous na
|
| + basic_info.printer_name = printer.id(); |
| + basic_info.printer_description = printer.display_name(); |
| + return basic_info; |
| + } |
| + |
| + void AddPrintersToList( |
| + const std::vector<std::unique_ptr<chromeos::Printer>>& printers, |
| + PrinterList* list) { |
| + for (const auto& printer : printers) { |
| + list->push_back(ToBasicInfo(*printer)); |
| + } |
| + } |
| + |
| + void FetchCapabilities(std::unique_ptr<chromeos::Printer> printer, |
| + const PrinterSetupCallback& cb) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + PrinterBasicInfo basic_info = ToBasicInfo(*printer); |
| + |
| + base::PostTaskAndReplyWithResult( |
| + content::BrowserThread::GetBlockingPool(), FROM_HERE, |
| + base::Bind(&GetSettingsOnBlockingPool, printer->id(), basic_info), cb); |
| + } |
| + |
| + void PostCallbackError(const PrinterSetupCallback& cb) { |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(cb, nullptr)); |
| + } |
| + |
| + void HandlePrinterSetup(std::unique_ptr<chromeos::Printer> printer, |
| + SetupResult result, |
| + const PrinterSetupCallback& cb) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + if (result != SetupResult::SUCCESS) { |
| + LOG(WARNING) << "Print setup failed: " << result; |
| + // TODO(skau): Open printer settings if this is resolvable. |
| + PostCallbackError(cb); |
| + return; |
| + } |
| + |
| + VLOG(1) << "Printer setup successful for " << printer->id() |
| + << " fetching properties"; |
| + |
| + // fetch settings on the blocking pool and invoke callback. |
| + FetchCapabilities(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_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + 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_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + LOG(WARNING) << "Could not contact debugd"; |
| + PostCallbackError(cb); |
| + } |
| + |
| + 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(); |
| + |
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->CupsAddPrinter( |
| + printer_name, printer_uri, ppd_path, ipp_everywhere, |
| + base::Bind(&PrinterBackendProxyChromeos::OnPrinterAddResult, |
| + weak_factory_.GetWeakPtr(), base::Passed(&printer), cb), |
| + base::Bind(&PrinterBackendProxyChromeos::OnPrinterAddError, |
| + weak_factory_.GetWeakPtr(), cb)); |
| + } |
| + |
| + 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; |
| + } |
| + } |
| + |
| chromeos::PrinterPrefManager* prefs_; |
| std::unique_ptr<chromeos::printing::PpdProvider> ppd_provider_; |
| + base::WeakPtrFactory<PrinterBackendProxyChromeos> weak_factory_; |
| DISALLOW_COPY_AND_ASSIGN(PrinterBackendProxyChromeos); |
| }; |