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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/printing/printer_discoverer.cc
diff --git a/chrome/browser/chromeos/printing/printer_discoverer.cc b/chrome/browser/chromeos/printing/printer_discoverer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..126bf83bb0b76aa1b55708516411741ef7aab404
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_discoverer.cc
@@ -0,0 +1,111 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/printing/printer_discoverer.h"
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/weak_ptr.h"
+#include "base/observer_list.h"
+#include "base/scoped_observer.h"
+#include "base/threading/sequenced_task_runner_handle.h"
+#include "chrome/browser/chromeos/printer_detector/printer_detector.h"
+#include "chrome/browser/chromeos/printer_detector/printer_detector_factory.h"
+#include "chrome/browser/chromeos/printing/printers_manager.h"
+#include "chrome/browser/chromeos/printing/printers_manager_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chromeos/printing/printer_configuration.h"
+
+namespace chromeos {
+namespace {
+
+// Implementation of PrinterDiscoverer interface.
+class PrinterDiscovererImpl : public PrinterDiscoverer,
+ public PrinterDetector::Observer {
+ public:
+ explicit PrinterDiscovererImpl(Profile* profile)
+ : observer_(this),
+ printers_manager_(
+ 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.
+ weak_ptr_factory_(this) {
+ PrinterDetector* detector =
+ PrinterDetectorFactory::GetInstance()->Get(profile);
stevenjb 2017/04/10 19:49:55 DCHECK(detector);
Carlson 2017/04/10 22:16:42 Done.
+ observer_.Add(detector);
+ usb_printers_ = detector->GetPrinters();
+ }
+ ~PrinterDiscovererImpl() override = default;
+
+ // PrinterDiscoverer interface function.
+ void AddObserver(PrinterDiscoverer::Observer* observer) override {
+ observer_list_.AddObserver(observer);
+ // WrappedOnPrintersFound is a simple wrapper around
+ // Observer::OnPrintersFound which lets us safely do the initial
+ // OnPrintersFound call to the registered observer. This wrapper buys us
+ // weak_ptr semantics on 'this', and also guards against removal of the
+ // observer from the Discoverer before this callback is issued.
+ base::SequencedTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(&PrinterDiscovererImpl::WrappedOnPrintersFound,
+ weak_ptr_factory_.GetWeakPtr(), observer,
+ 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
+ }
+
+ // PrinterDiscoverer interface function.
+ void RemoveObserver(PrinterDiscoverer::Observer* observer) override {
+ observer_list_.RemoveObserver(observer);
+ }
+
+ // PrinterDetector::Observer interface function.
+ void OnAvailableUsbPrintersChanged(
+ const std::vector<Printer>& printers) override {
+ usb_printers_ = printers;
+ std::vector<Printer> all_printers = GetAvailablePrinters();
+ for (PrinterDiscoverer::Observer& observer : observer_list_) {
+ observer.OnPrintersFound(all_printers);
+ }
+ }
+
+ private:
+ // Wrapper for doing the initial OnPrintersFound call on an observer of this
+ // object.
+ void WrappedOnPrintersFound(PrinterDiscoverer::Observer* observer,
+ const std::vector<Printer>& printers) {
+ if (observer_list_.HasObserver(observer)) {
+ observer->OnPrintersFound(printers);
+ // Since USB is the only thing we're worried about at the moment,
+ // and we don't have to wait for those printers to be scanned, we
+ // can just tell the observer the initial scan is done now.
+ observer->OnDiscoveryInitialScanDone();
+ }
+ }
+
+ // Get the current set of discovered printers that are not already known
+ // to the user's PrintersManager.
+ std::vector<Printer> GetAvailablePrinters() {
+ // Only know about usb printers for now. Eventually we'll add discovered
+ // network printers as well.
+ std::vector<Printer> ret;
+
+ for (const Printer& printer : usb_printers_) {
+ if (printers_manager_->GetPrinter(printer.id()).get() == nullptr) {
+ ret.push_back(printer);
+ }
+ }
+ return ret;
+ }
+
+ std::vector<Printer> usb_printers_;
+ base::ObserverList<PrinterDiscoverer::Observer> observer_list_;
+ 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.
+ PrintersManager* printers_manager_;
+ base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_;
+};
+
+} // namespace
+
+// static
+std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::Create(Profile* profile) {
+ return base::MakeUnique<PrinterDiscovererImpl>(profile);
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698