| 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_discoverer.h" | 5 #include "chrome/browser/chromeos/printing/printer_discoverer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 11 #include "base/scoped_observer.h" | 11 #include "base/scoped_observer.h" |
| 12 #include "base/threading/sequenced_task_runner_handle.h" | 12 #include "base/threading/sequenced_task_runner_handle.h" |
| 13 #include "chrome/browser/chromeos/printer_detector/printer_detector.h" | 13 #include "chrome/browser/chromeos/printer_detector/usb_printer_detector.h" |
| 14 #include "chrome/browser/chromeos/printer_detector/printer_detector_factory.h" | 14 #include "chrome/browser/chromeos/printer_detector/usb_printer_detector_factory.
h" |
| 15 #include "chrome/browser/chromeos/printing/printers_manager.h" | 15 #include "chrome/browser/chromeos/printing/printers_manager.h" |
| 16 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" | 16 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chromeos/printing/printer_configuration.h" | 18 #include "chromeos/printing/printer_configuration.h" |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Implementation of PrinterDiscoverer interface. | 23 // Implementation of PrinterDiscoverer interface. |
| 24 class PrinterDiscovererImpl : public PrinterDiscoverer, | 24 class PrinterDiscovererImpl : public PrinterDiscoverer, |
| 25 public PrinterDetector::Observer { | 25 public UsbPrinterDetector::Observer { |
| 26 public: | 26 public: |
| 27 explicit PrinterDiscovererImpl(Profile* profile) | 27 explicit PrinterDiscovererImpl(Profile* profile) |
| 28 : detector_observer_(this), profile_(profile), weak_ptr_factory_(this) { | 28 : detector_observer_(this), profile_(profile), weak_ptr_factory_(this) { |
| 29 PrinterDetector* detector = | 29 UsbPrinterDetector* usb_detector = |
| 30 PrinterDetectorFactory::GetInstance()->Get(profile); | 30 UsbPrinterDetectorFactory::GetInstance()->Get(profile); |
| 31 DCHECK(detector); | 31 DCHECK(usb_detector); |
| 32 detector_observer_.Add(detector); | 32 detector_observer_.Add(usb_detector); |
| 33 usb_printers_ = detector->GetPrinters(); | 33 usb_printers_ = usb_detector->GetPrinters(); |
| 34 } | 34 } |
| 35 ~PrinterDiscovererImpl() override = default; | 35 ~PrinterDiscovererImpl() override = default; |
| 36 | 36 |
| 37 // PrinterDiscoverer interface function. | 37 // PrinterDiscoverer interface function. |
| 38 void AddObserver(PrinterDiscoverer::Observer* observer) override { | 38 void AddObserver(PrinterDiscoverer::Observer* observer) override { |
| 39 observer_list_.AddObserver(observer); | 39 observer_list_.AddObserver(observer); |
| 40 // WrappedOnPrintersFound is a simple wrapper around | 40 // WrappedOnPrintersFound is a simple wrapper around |
| 41 // Observer::OnPrintersFound which lets us safely do the initial | 41 // Observer::OnPrintersFound which lets us safely do the initial |
| 42 // OnPrintersFound call to the registered observer. This wrapper buys us | 42 // OnPrintersFound call to the registered observer. This wrapper buys us |
| 43 // weak_ptr semantics on 'this', and also guards against removal of the | 43 // weak_ptr semantics on 'this', and also guards against removal of the |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 for (const Printer& printer : usb_printers_) { | 96 for (const Printer& printer : usb_printers_) { |
| 97 if (printers_manager->GetPrinter(printer.id()).get() == nullptr) { | 97 if (printers_manager->GetPrinter(printer.id()).get() == nullptr) { |
| 98 ret.push_back(printer); | 98 ret.push_back(printer); |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 return ret; | 101 return ret; |
| 102 } | 102 } |
| 103 | 103 |
| 104 std::vector<Printer> usb_printers_; | 104 std::vector<Printer> usb_printers_; |
| 105 base::ObserverList<PrinterDiscoverer::Observer> observer_list_; | 105 base::ObserverList<PrinterDiscoverer::Observer> observer_list_; |
| 106 ScopedObserver<PrinterDetector, PrinterDetector::Observer> detector_observer_; | 106 ScopedObserver<UsbPrinterDetector, UsbPrinterDetector::Observer> |
| 107 detector_observer_; |
| 107 Profile* profile_; | 108 Profile* profile_; |
| 108 base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_; | 109 base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_; |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 } // namespace | 112 } // namespace |
| 112 | 113 |
| 113 // static | 114 // static |
| 114 std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::CreateForProfile( | 115 std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::CreateForProfile( |
| 115 Profile* profile) { | 116 Profile* profile) { |
| 116 return base::MakeUnique<PrinterDiscovererImpl>(profile); | 117 return base::MakeUnique<PrinterDiscovererImpl>(profile); |
| 117 } | 118 } |
| 118 | 119 |
| 119 } // namespace chromeos | 120 } // namespace chromeos |
| OLD | NEW |