Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Side by Side Diff: chrome/browser/chromeos/printing/printer_discoverer.cc

Issue 2790603003: Make CUPS USB printing play better with the settings page. This change does several things: (Closed)
Patch Set: stop latching PrintersManager, add comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Implementation of PrinterDiscoverer interface.
24 class PrinterDiscovererImpl : public PrinterDiscoverer,
25 public PrinterDetector::Observer {
26 public:
27 explicit PrinterDiscovererImpl(Profile* profile)
28 : detector_observer_(this), profile_(profile), weak_ptr_factory_(this) {
29 PrinterDetector* detector =
30 PrinterDetectorFactory::GetInstance()->Get(profile);
31 DCHECK(detector);
32 detector_observer_.Add(detector);
33 usb_printers_ = detector->GetPrinters();
34 }
35 ~PrinterDiscovererImpl() override = default;
36
37 // PrinterDiscoverer interface function.
38 void AddObserver(PrinterDiscoverer::Observer* observer) override {
39 observer_list_.AddObserver(observer);
40 // WrappedOnPrintersFound is a simple wrapper around
41 // Observer::OnPrintersFound which lets us safely do the initial
42 // OnPrintersFound call to the registered observer. This wrapper buys us
43 // weak_ptr semantics on 'this', and also guards against removal of the
44 // observer from the Discoverer before this callback is issued.
45 base::SequencedTaskRunnerHandle::Get()->PostTask(
46 FROM_HERE, base::Bind(&PrinterDiscovererImpl::WrappedOnPrintersFound,
47 weak_ptr_factory_.GetWeakPtr(), observer,
48 GetAvailablePrinters()));
49 }
50
51 // PrinterDiscoverer interface function.
52 void RemoveObserver(PrinterDiscoverer::Observer* observer) override {
53 observer_list_.RemoveObserver(observer);
54 }
55
56 // PrinterDetector::Observer interface function.
57 void OnAvailableUsbPrintersChanged(
58 const std::vector<Printer>& printers) override {
59 usb_printers_ = printers;
60 std::vector<Printer> all_printers = GetAvailablePrinters();
61 for (PrinterDiscoverer::Observer& observer : observer_list_) {
62 observer.OnPrintersFound(all_printers);
63 }
64 }
65
66 private:
67 // Wrapper for doing the initial OnPrintersFound call on an observer of this
68 // object.
69 void WrappedOnPrintersFound(PrinterDiscoverer::Observer* observer,
70 const std::vector<Printer>& printers) {
71 if (observer_list_.HasObserver(observer)) {
72 observer->OnPrintersFound(printers);
73 // Since USB is the only thing we're worried about at the moment, and we
74 // don't have to wait for those printers to be scanned, we can just tell
75 // the observer the initial scan is done now. This will change when we're
76 // also doing network discovery -- we'll hold off on issuing this callback
77 // until the network discovery is done as well.
78 observer->OnDiscoveryInitialScanDone();
79 }
80 }
81
82 // Get the current set of discovered printers that are not already known
83 // to the user's PrintersManager.
84 std::vector<Printer> GetAvailablePrinters() {
85 // Only know about usb printers for now. Eventually we'll add discovered
86 // network printers as well.
87 std::vector<Printer> ret;
88 PrintersManager* printers_manager =
89 PrintersManagerFactory::GetForBrowserContext(profile_);
90 if (!printers_manager) {
91 LOG(WARNING) << "Failing to get available printers because no "
92 "PrintersManager exists.";
93 return ret;
94 }
95
96 for (const Printer& printer : usb_printers_) {
97 if (printers_manager->GetPrinter(printer.id()).get() == nullptr) {
98 ret.push_back(printer);
99 }
100 }
101 return ret;
102 }
103
104 std::vector<Printer> usb_printers_;
105 base::ObserverList<PrinterDiscoverer::Observer> observer_list_;
106 ScopedObserver<PrinterDetector, PrinterDetector::Observer> detector_observer_;
107 Profile* profile_;
108 base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_;
109 };
110
111 } // namespace
112
113 // static
114 std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::CreateForProfile(
115 Profile* profile) {
116 return base::MakeUnique<PrinterDiscovererImpl>(profile);
117 }
118
119 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/printing/printer_discoverer.h ('k') | chrome/browser/chromeos/printing/printers_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698