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

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: Address xdai@ 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 : observer_(this),
29 printers_manager_(
30 PrintersManagerFactory::GetForBrowserContext(profile)),
stevenjb 2017/04/10 19:49:55 Even though this class is associated with |profile
Carlson 2017/04/10 22:16:42 Hmm, this is a good point. Practically speaking
stevenjb 2017/04/11 16:27:42 There may be but I don't know off the top of my he
Carlson 2017/04/11 17:47:45 Done.
31 weak_ptr_factory_(this) {
32 PrinterDetector* detector =
33 PrinterDetectorFactory::GetInstance()->Get(profile);
stevenjb 2017/04/10 19:49:55 DCHECK(detector);
Carlson 2017/04/10 22:16:42 Done.
34 observer_.Add(detector);
35 usb_printers_ = detector->GetPrinters();
36 }
37 ~PrinterDiscovererImpl() override = default;
38
39 // PrinterDiscoverer interface function.
40 void AddObserver(PrinterDiscoverer::Observer* observer) override {
41 observer_list_.AddObserver(observer);
42 // WrappedOnPrintersFound is a simple wrapper around
43 // Observer::OnPrintersFound which lets us safely do the initial
44 // OnPrintersFound call to the registered observer. This wrapper buys us
45 // weak_ptr semantics on 'this', and also guards against removal of the
46 // observer from the Discoverer before this callback is issued.
47 base::SequencedTaskRunnerHandle::Get()->PostTask(
48 FROM_HERE, base::Bind(&PrinterDiscovererImpl::WrappedOnPrintersFound,
49 weak_ptr_factory_.GetWeakPtr(), observer,
50 GetAvailablePrinters()));
stevenjb 2017/04/10 19:49:55 This is awkward. I would suggest instead providing
Carlson 2017/04/10 22:16:41 I don't think I understand what you're suggesting
stevenjb 2017/04/11 16:27:42 So, that is what I was suggesting, it's a pretty c
skau 2017/04/11 17:12:49 The behavior for detection is a little abnormal.
stevenjb 2017/04/11 17:19:15 What confuses me is that "actively scan" appears t
Carlson 2017/04/11 17:47:45 I agree the API seems weird given the current impl
51 }
52
53 // PrinterDiscoverer interface function.
54 void RemoveObserver(PrinterDiscoverer::Observer* observer) override {
55 observer_list_.RemoveObserver(observer);
56 }
57
58 // PrinterDetector::Observer interface function.
59 void OnAvailableUsbPrintersChanged(
60 const std::vector<Printer>& printers) override {
61 usb_printers_ = printers;
62 std::vector<Printer> all_printers = GetAvailablePrinters();
63 for (PrinterDiscoverer::Observer& observer : observer_list_) {
64 observer.OnPrintersFound(all_printers);
65 }
66 }
67
68 private:
69 // Wrapper for doing the initial OnPrintersFound call on an observer of this
70 // object.
71 void WrappedOnPrintersFound(PrinterDiscoverer::Observer* observer,
72 const std::vector<Printer>& printers) {
73 if (observer_list_.HasObserver(observer)) {
74 observer->OnPrintersFound(printers);
75 // Since USB is the only thing we're worried about at the moment,
76 // and we don't have to wait for those printers to be scanned, we
77 // can just tell the observer the initial scan is done now.
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
89 for (const Printer& printer : usb_printers_) {
90 if (printers_manager_->GetPrinter(printer.id()).get() == nullptr) {
91 ret.push_back(printer);
92 }
93 }
94 return ret;
95 }
96
97 std::vector<Printer> usb_printers_;
98 base::ObserverList<PrinterDiscoverer::Observer> observer_list_;
99 ScopedObserver<PrinterDetector, PrinterDetector::Observer> observer_;
stevenjb 2017/04/10 19:49:55 nit: detector_observer_ would be a little more cle
Carlson 2017/04/10 22:16:42 Done.
100 PrintersManager* printers_manager_;
101 base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_;
102 };
103
104 } // namespace
105
106 // static
107 std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::Create(Profile* profile) {
108 return base::MakeUnique<PrinterDiscovererImpl>(profile);
109 }
110
111 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698