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

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: 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 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..e6f91591cd6080de08144e1cabeaae076bc3bf26
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_discoverer.cc
@@ -0,0 +1,119 @@
+// 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)
+ : detector_observer_(this), profile_(profile), weak_ptr_factory_(this) {
+ PrinterDetector* detector =
+ PrinterDetectorFactory::GetInstance()->Get(profile);
+ DCHECK(detector);
+ detector_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()));
+ }
+
+ // 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. This will change when we're
+ // also doing network discovery -- we'll hold off on issuing this callback
+ // until the network discovery is done as well.
+ 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;
+ PrintersManager* printers_manager =
+ PrintersManagerFactory::GetForBrowserContext(profile_);
+ if (!printers_manager) {
+ LOG(WARNING) << "Failing to get available printers because no "
+ "PrintersManager exists.";
+ return 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> detector_observer_;
+ Profile* profile_;
+ base::WeakPtrFactory<PrinterDiscovererImpl> weak_ptr_factory_;
+};
+
+} // namespace
+
+// static
+std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::CreateForProfile(
+ Profile* profile) {
+ return base::MakeUnique<PrinterDiscovererImpl>(profile);
+}
+
+} // namespace chromeos
« 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