| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/chromeos/printing/printer_configurer.h" | 5 #include "chrome/browser/chromeos/printing/printer_configurer.h" |
| 6 | 6 |
| 7 #include <map> |
| 7 #include <memory> | 8 #include <memory> |
| 9 #include <set> |
| 8 #include <string> | 10 #include <string> |
| 9 #include <vector> | 11 #include <vector> |
| 10 | 12 |
| 11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 12 #include "base/callback.h" | 14 #include "base/callback.h" |
| 13 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 17 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" | 19 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 36 | 38 |
| 37 namespace chromeos { | 39 namespace chromeos { |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 // Configures printers by downloading PPDs then adding them to CUPS through | 43 // Configures printers by downloading PPDs then adding them to CUPS through |
| 42 // debugd. This class must be used on the UI thread. | 44 // debugd. This class must be used on the UI thread. |
| 43 class PrinterConfigurerImpl : public PrinterConfigurer { | 45 class PrinterConfigurerImpl : public PrinterConfigurer { |
| 44 public: | 46 public: |
| 45 explicit PrinterConfigurerImpl(Profile* profile) | 47 explicit PrinterConfigurerImpl(Profile* profile) |
| 46 : ppd_provider_(printing::CreateProvider(profile)), weak_factory_(this) {} | 48 : ppd_provider_(CreatePpdProvider(profile)), weak_factory_(this) {} |
| 47 | 49 |
| 48 PrinterConfigurerImpl(const PrinterConfigurerImpl&) = delete; | 50 PrinterConfigurerImpl(const PrinterConfigurerImpl&) = delete; |
| 49 PrinterConfigurerImpl& operator=(const PrinterConfigurerImpl&) = delete; | 51 PrinterConfigurerImpl& operator=(const PrinterConfigurerImpl&) = delete; |
| 50 | 52 |
| 51 ~PrinterConfigurerImpl() override {} | 53 ~PrinterConfigurerImpl() override {} |
| 52 | 54 |
| 53 void SetUpPrinter(const Printer& printer, | 55 void SetUpPrinter(const Printer& printer, |
| 54 const PrinterSetupCallback& callback) override { | 56 const PrinterSetupCallback& callback) override { |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 57 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 56 DCHECK(!printer.id().empty()); | 58 DCHECK(!printer.id().empty()); |
| 57 DCHECK(!printer.uri().empty()); | 59 DCHECK(!printer.uri().empty()); |
| 58 | 60 |
| 59 if (!printer.IsIppEverywhere()) { | 61 if (!printer.IsIppEverywhere()) { |
| 60 ppd_provider_->ResolvePpd( | 62 ppd_provider_->ResolvePpd( |
| 61 printer.ppd_reference(), | 63 printer.ppd_reference(), |
| 62 base::Bind(&PrinterConfigurerImpl::ResolvePpdDone, | 64 base::Bind(&PrinterConfigurerImpl::ResolvePpdDone, |
| 63 weak_factory_.GetWeakPtr(), printer, callback)); | 65 weak_factory_.GetWeakPtr(), printer, callback)); |
| 64 return; | 66 return; |
| 65 } | 67 } |
| 66 | 68 |
| 67 auto* client = chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 69 auto* client = DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 68 | 70 |
| 69 client->CupsAddAutoConfiguredPrinter( | 71 client->CupsAddAutoConfiguredPrinter( |
| 70 printer.id(), printer.uri(), | 72 printer.id(), printer.uri(), |
| 71 base::Bind(&PrinterConfigurerImpl::OnAddedPrinter, | 73 base::Bind(&PrinterConfigurerImpl::OnAddedPrinter, |
| 72 weak_factory_.GetWeakPtr(), printer, callback), | 74 weak_factory_.GetWeakPtr(), printer, callback), |
| 73 base::Bind(&PrinterConfigurerImpl::OnDbusError, | 75 base::Bind(&PrinterConfigurerImpl::OnDbusError, |
| 74 weak_factory_.GetWeakPtr(), callback)); | 76 weak_factory_.GetWeakPtr(), callback)); |
| 75 } | 77 } |
| 76 | 78 |
| 77 private: | 79 private: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 void OnDbusError(const PrinterSetupCallback& cb) { | 114 void OnDbusError(const PrinterSetupCallback& cb) { |
| 113 // The callback is expected to run on the UI thread. | 115 // The callback is expected to run on the UI thread. |
| 114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 115 LOG(WARNING) << "Could not contact debugd"; | 117 LOG(WARNING) << "Could not contact debugd"; |
| 116 cb.Run(PrinterSetupResult::kDbusError); | 118 cb.Run(PrinterSetupResult::kDbusError); |
| 117 } | 119 } |
| 118 | 120 |
| 119 void AddPrinter(const Printer& printer, | 121 void AddPrinter(const Printer& printer, |
| 120 const std::string& ppd_contents, | 122 const std::string& ppd_contents, |
| 121 const PrinterSetupCallback& cb) { | 123 const PrinterSetupCallback& cb) { |
| 122 auto* client = chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 124 auto* client = DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 123 | 125 |
| 124 client->CupsAddManuallyConfiguredPrinter( | 126 client->CupsAddManuallyConfiguredPrinter( |
| 125 printer.id(), printer.uri(), ppd_contents, | 127 printer.id(), printer.uri(), ppd_contents, |
| 126 base::Bind(&PrinterConfigurerImpl::OnAddedPrinter, | 128 base::Bind(&PrinterConfigurerImpl::OnAddedPrinter, |
| 127 weak_factory_.GetWeakPtr(), printer, cb), | 129 weak_factory_.GetWeakPtr(), printer, cb), |
| 128 base::Bind(&PrinterConfigurerImpl::OnDbusError, | 130 base::Bind(&PrinterConfigurerImpl::OnDbusError, |
| 129 weak_factory_.GetWeakPtr(), cb)); | 131 weak_factory_.GetWeakPtr(), cb)); |
| 130 } | 132 } |
| 131 | 133 |
| 132 // Executed on component load API finish. | 134 // Executed on component load API finish. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 LOG(ERROR) << "More than one filter components are requested."; | 174 LOG(ERROR) << "More than one filter components are requested."; |
| 173 cb.Run(PrinterSetupResult::kFatalError); | 175 cb.Run(PrinterSetupResult::kFatalError); |
| 174 return true; | 176 return true; |
| 175 } | 177 } |
| 176 } | 178 } |
| 177 return false; | 179 return false; |
| 178 } | 180 } |
| 179 | 181 |
| 180 void ResolvePpdDone(const Printer& printer, | 182 void ResolvePpdDone(const Printer& printer, |
| 181 const PrinterSetupCallback& cb, | 183 const PrinterSetupCallback& cb, |
| 182 printing::PpdProvider::CallbackResultCode result, | 184 PpdProvider::CallbackResultCode result, |
| 183 const std::string& ppd_contents, | 185 const std::string& ppd_contents, |
| 184 const std::vector<std::string>& ppd_filters) { | 186 const std::vector<std::string>& ppd_filters) { |
| 185 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 186 switch (result) { | 188 switch (result) { |
| 187 case chromeos::printing::PpdProvider::SUCCESS: | 189 case PpdProvider::SUCCESS: |
| 188 DCHECK(!ppd_contents.empty()); | 190 DCHECK(!ppd_contents.empty()); |
| 189 if (!RequiresComponent(printer, cb, ppd_contents, ppd_filters)) { | 191 if (!RequiresComponent(printer, cb, ppd_contents, ppd_filters)) { |
| 190 AddPrinter(printer, ppd_contents, cb); | 192 AddPrinter(printer, ppd_contents, cb); |
| 191 } | 193 } |
| 192 break; | 194 break; |
| 193 case printing::PpdProvider::CallbackResultCode::NOT_FOUND: | 195 case PpdProvider::CallbackResultCode::NOT_FOUND: |
| 194 cb.Run(PrinterSetupResult::kPpdNotFound); | 196 cb.Run(PrinterSetupResult::kPpdNotFound); |
| 195 break; | 197 break; |
| 196 case printing::PpdProvider::CallbackResultCode::SERVER_ERROR: | 198 case PpdProvider::CallbackResultCode::SERVER_ERROR: |
| 197 cb.Run(PrinterSetupResult::kPpdUnretrievable); | 199 cb.Run(PrinterSetupResult::kPpdUnretrievable); |
| 198 break; | 200 break; |
| 199 case printing::PpdProvider::CallbackResultCode::INTERNAL_ERROR: | 201 case PpdProvider::CallbackResultCode::INTERNAL_ERROR: |
| 200 // TODO(skau): Add kPpdTooLarge when it's reported by the PpdProvider. | 202 // TODO(skau): Add kPpdTooLarge when it's reported by the PpdProvider. |
| 201 cb.Run(PrinterSetupResult::kFatalError); | 203 cb.Run(PrinterSetupResult::kFatalError); |
| 202 break; | 204 break; |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 | 207 |
| 206 scoped_refptr<printing::PpdProvider> ppd_provider_; | 208 scoped_refptr<PpdProvider> ppd_provider_; |
| 207 base::WeakPtrFactory<PrinterConfigurerImpl> weak_factory_; | 209 base::WeakPtrFactory<PrinterConfigurerImpl> weak_factory_; |
| 208 }; | 210 }; |
| 209 | 211 |
| 210 } // namespace | 212 } // namespace |
| 211 | 213 |
| 212 // static | 214 // static |
| 213 std::unique_ptr<PrinterConfigurer> PrinterConfigurer::Create(Profile* profile) { | 215 std::unique_ptr<PrinterConfigurer> PrinterConfigurer::Create(Profile* profile) { |
| 214 return base::MakeUnique<PrinterConfigurerImpl>(profile); | 216 return base::MakeUnique<PrinterConfigurerImpl>(profile); |
| 215 } | 217 } |
| 216 | 218 |
| 217 } // namespace chromeos | 219 } // namespace chromeos |
| OLD | NEW |