Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/printing/printer_discoverer.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/scoped_observer.h" | |
| 12 #include "base/threading/sequenced_task_runner_handle.h" | |
| 13 #include "chrome/browser/chromeos/printer_detector/printer_detector.h" | |
| 14 #include "chrome/browser/chromeos/printer_detector/printer_detector_factory.h" | |
| 15 #include "chrome/browser/chromeos/printing/printers_manager.h" | |
| 16 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chromeos/printing/printer_configuration.h" | |
| 19 | |
| 20 namespace chromeos { | |
| 21 namespace { | |
| 22 | |
| 23 class PrinterDiscovererImpl : public PrinterDiscoverer, | |
| 24 public PrinterDetector::Observer { | |
| 25 public: | |
| 26 explicit PrinterDiscovererImpl(Profile* profile) | |
| 27 : observer_(this), | |
| 28 printers_manager_( | |
| 29 PrintersManagerFactory::GetForBrowserContext(profile)), | |
| 30 weak_ptr_factory_(this) { | |
| 31 PrinterDetector* detector = | |
| 32 PrinterDetectorFactory::GetInstance()->Get(profile); | |
| 33 observer_.Add(detector); | |
| 34 usb_printers_ = detector->GetPrinters(); | |
| 35 } | |
| 36 ~PrinterDiscovererImpl() override = default; | |
| 37 | |
| 38 // PrinterDiscoverer interface function. | |
| 39 void AddObserver(PrinterDiscoverer::Observer* observer) override { | |
| 40 observer_list_.AddObserver(observer); | |
| 41 base::SequencedTaskRunnerHandle::Get()->PostTask( | |
| 42 FROM_HERE, base::Bind(&PrinterDiscovererImpl::WrappedOnPrintersFound, | |
| 43 weak_ptr_factory_.GetWeakPtr(), observer, | |
| 44 GetAvailablePrinters())); | |
| 45 } | |
| 46 | |
| 47 // PrinterDiscoverer interface function. | |
| 48 void RemoveObserver(PrinterDiscoverer::Observer* observer) override { | |
| 49 observer_list_.RemoveObserver(observer); | |
| 50 } | |
| 51 | |
| 52 // PrinterDetector::Observer interface function. | |
| 53 void OnAvailableUsbPrintersChanged( | |
| 54 const std::vector<Printer>& printers) override { | |
| 55 usb_printers_ = printers; | |
| 56 std::vector<Printer> all_printers = GetAvailablePrinters(); | |
| 57 for (PrinterDiscoverer::Observer& observer : observer_list_) { | |
| 58 observer.OnPrintersFound(all_printers); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 // This is a simple wrapper around Observer::OnPrintersFound which lets us | |
| 64 // safely do the initial OnPrintersFound call after an observer is registered. | |
| 65 // This wrapper buys us weak_ptr semantics on the Discoverer object, and also | |
|
skau
2017/03/31 17:50:11
Do you mean you get weak_ptr semantics on the Obse
Carlson
2017/03/31 18:20:07
Long comment is insufficiently long. :)
This is
skau
2017/03/31 21:03:19
That makes more sense. You should probably mentio
Carlson
2017/03/31 22:58:41
Moved the bulk of the comment to the callsite and
| |
| 66 // guards against removal of the observer from the Discoverer before this | |
| 67 // callback is issued. | |
| 68 void WrappedOnPrintersFound(PrinterDiscoverer::Observer* observer, | |
| 69 const std::vector<Printer>& printers) { | |
| 70 if (observer_list_.HasObserver(observer)) { | |
| 71 observer->OnPrintersFound(printers); | |
| 72 // Since USB is the only thing we're worried about at the moment, | |
| 73 // and we don't have to wait for those printers to be scanned, we | |
| 74 // can just tell the observer the initial scan is done now. | |
| 75 observer->OnDiscoveryInitialScanDone(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Get the current set of discovered printers that are not already known | |
| 80 // to the user's PrintersManager. | |
| 81 std::vector<Printer> GetAvailablePrinters() { | |
| 82 // Only know about usb printers for now. Eventually we'll add discovered | |
| 83 // network printers as well. | |
| 84 std::vector<Printer> ret; | |
| 85 | |
| 86 for (const Printer& printer : usb_printers_) { | |
| 87 if (printers_manager_->GetPrinter(printer.id()).get() == nullptr) { | |
| 88 ret.push_back(printer); | |
| 89 } | |
| 90 } | |
| 91 return ret; | |
| 92 } | |
| 93 | |
| 94 std::vector<Printer> usb_printers_; | |
| 95 base::ObserverList<PrinterDiscoverer::Observer> observer_list_; | |
| 96 ScopedObserver<PrinterDetector, PrinterDetector::Observer> observer_; | |
| 97 PrintersManager* printers_manager_; | |
| 98 base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_; | |
| 99 }; | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 // static | |
| 104 std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::Create(Profile* profile) { | |
| 105 return base::MakeUnique<PrinterDiscovererImpl>(profile); | |
| 106 } | |
| 107 | |
| 108 } // namespace chromeos | |
| OLD | NEW |