Commit 28fa7dc1 authored by skau's avatar skau Committed by Commit bot

Add a PrinterDiscoverer fake object.

We will eventually support detecting printers using mDNS and autodetect
USB devices.  This will take a little while to implement.  Providing a
fake in the meantime since the system is behind a flag.  The fake may be
useful for some tests in the future.

BUG=631352

Review-Url: https://codereview.chromium.org/2344393002
Cr-Commit-Position: refs/heads/master@{#422641}
parent 95acf245
......@@ -345,8 +345,11 @@
'network/shill_property_handler.h',
'network/shill_property_util.cc',
'network/shill_property_util.h',
'printing/fake_printer_discoverer.cc',
'printing/fake_printer_discoverer.h',
'printing/printer_configuration.cc',
'printing/printer_configuration.h',
'printing/printer_discoverer.h',
'printing/printer_translator.cc',
'printing/printer_translator.h',
'process_proxy/process_output_watcher.cc',
......
// Copyright 2016 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 "chromeos/printing/fake_printer_discoverer.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "chromeos/printing/printer_discoverer.h"
namespace chromeos {
// static
std::unique_ptr<PrinterDiscoverer> PrinterDiscoverer::Create() {
return base::MakeUnique<FakePrinterDiscoverer>();
}
FakePrinterDiscoverer::~FakePrinterDiscoverer() {}
bool FakePrinterDiscoverer::StartDiscovery() {
discovery_running_ = true;
for (auto observer : observers_)
observer->OnDiscoveryStarted();
base::SequencedTaskRunnerHandle::Get()->PostNonNestableDelayedTask(
FROM_HERE, base::Bind(&FakePrinterDiscoverer::EmitPrinters,
weak_ptr_factory_.GetWeakPtr(), 0, 2),
base::TimeDelta::FromMilliseconds(2000));
return true;
}
bool FakePrinterDiscoverer::StopDiscovery() {
discovery_running_ = false;
for (auto observer : observers_)
observer->OnDiscoveryStopping();
return true;
}
void FakePrinterDiscoverer::AddObserver(PrinterDiscoverer::Observer* observer) {
observers_.push_back(observer);
}
void FakePrinterDiscoverer::RemoveObserver(
PrinterDiscoverer::Observer* observer) {
auto found = std::find(observers_.begin(), observers_.end(), observer);
if (found != observers_.end())
observers_.erase(found);
}
FakePrinterDiscoverer::FakePrinterDiscoverer()
: discovery_running_(false), weak_ptr_factory_(this) {
for (int i = 0; i < 12; i++) {
// printer doesn't have a ppd
printers_.emplace_back(base::StringPrintf("GUID%2d", i));
printers_[i].set_display_name(base::StringPrintf("PrinterName%2d", i));
printers_[i].set_description(
base::StringPrintf("Printer%2dDescription", i));
printers_[i].set_manufacturer("Chromium");
printers_[i].set_model(i % 3 == 0 ? "Inkjet" : "Laser Maker");
printers_[i].set_uri(
base::StringPrintf("lpr://192.168.1.%d:9100/bldg/printer", i));
printers_[i].set_uuid(
base::StringPrintf("UUID-%4d-%4d-%4d-UUID", i * 3, i * 2, i));
}
}
void FakePrinterDiscoverer::EmitPrinters(size_t start, size_t end) {
if (!discovery_running_ || start >= printers_.size() || end > start)
return;
size_t clipped_start = std::max(static_cast<size_t>(0), start);
size_t clipped_end = std::min(printers_.size(), end);
if (!observers_.empty()) {
std::vector<Printer> subset(printers_.cbegin() + clipped_start,
printers_.cbegin() + clipped_end);
for (auto observer : observers_)
observer->OnPrintersFound(subset);
}
// schedule another emit if elements remain.
if (end < printers_.size()) {
base::SequencedTaskRunnerHandle::Get()->PostNonNestableDelayedTask(
FROM_HERE,
base::Bind(&FakePrinterDiscoverer::EmitPrinters,
weak_ptr_factory_.GetWeakPtr(), clipped_end, end + end),
base::TimeDelta::FromMilliseconds(2000));
} else {
// We're done. Notify observers.
discovery_running_ = false;
for (auto observer : observers_)
observer->OnDiscoveryDone();
}
}
} // namespace chromeos
// Copyright 2016 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.
#ifndef CHROMEOS_PRINTING_FAKE_PRINTER_DISCOVERER_H_
#define CHROMEOS_PRINTING_FAKE_PRINTER_DISCOVERER_H_
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/printing/printer_configuration.h"
#include "chromeos/printing/printer_discoverer.h"
namespace chromeos {
class FakePrinterDiscoverer : public PrinterDiscoverer {
public:
FakePrinterDiscoverer();
~FakePrinterDiscoverer();
bool StartDiscovery() override;
bool StopDiscovery() override;
void AddObserver(PrinterDiscoverer::Observer* observer) override;
void RemoveObserver(PrinterDiscoverer::Observer* observer) override;
private:
void EmitPrinters(size_t start, size_t end);
std::vector<chromeos::Printer> printers_;
bool discovery_running_;
std::vector<PrinterDiscoverer::Observer*> observers_;
base::WeakPtrFactory<FakePrinterDiscoverer> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(FakePrinterDiscoverer);
};
} // namespace chromeos
#endif // CHROMEOS_PRINTING_FAKE_PRINTER_DISCOVERER_H_
// Copyright 2016 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.
#ifndef CHROMEOS_PRINTING_PRINTER_DISCOVERER_H_
#define CHROMEOS_PRINTING_PRINTER_DISCOVERER_H_
#include <memory>
#include <vector>
#include "chromeos/printing/printer_configuration.h"
namespace chromeos {
// Interface for printer discovery. Constructs Printer objects from USB and
// zeroconf (DNS-SD) printers.
class PrinterDiscoverer {
public:
// Interface for objects interested in detected printers.
class Observer {
public:
// Called after discovery has started.
virtual void OnDiscoveryStarted() {}
// Called when discovery is stopping. OnPrintersFound will not be
// called after this occurs.
virtual void OnDiscoveryStopping() {}
// Called after all printers have been discovered and the observers
// notified.
virtual void OnDiscoveryDone() {}
// Called with a collection of printers as they are discovered. Printer
// objects must be copied if they are retained outside of the scope of this
// function.
virtual void OnPrintersFound(const std::vector<Printer>& printers) {}
};
// Static factory
static std::unique_ptr<PrinterDiscoverer> Create();
// Begin scanning for printers. Found printers will be reported to the
// attached observer.
virtual bool StartDiscovery() = 0;
// Stop scanning for printers. Returns false if scanning could not be stopped
// and new results will continue to be be sent to observers. Returns true if
// scanning was stopped successfully. No calls to the attached observer will
// be made after this returns if it returned true.
virtual bool StopDiscovery() = 0;
// Add an observer that will be notified of discovered printers. Ownership of
// |observer| is not taken by the discoverer. It is an error to add an
// oberserver more than once.
virtual void AddObserver(PrinterDiscoverer::Observer* observer) = 0;
// Remove an observer of printer discovery.
virtual void RemoveObserver(PrinterDiscoverer::Observer* observer) = 0;
};
} // namespace chromeos
#endif // CHROMEOS_PRINTING_PRINTER_DISCOVERER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment