Commit df87d813 authored by Justin Carlson's avatar Justin Carlson Committed by Commit Bot

Refactor SyncedPrintersManager.

This change brings SyncedPrintersManager closer to PrinterDetector
style interfaces, and aligns function names/styles with the design doc
for CUPS printers management.  It also removes a bunch of unique_ptr
wrapping of printers that are passed around, since these are not at
all performance critical and the unique_ptr stuff hampers readability
(and leads to subtle bugs when unique_ptrs are both used in a bind and
passed as a parameter at the same time).

Also convert it to the factory-function pattern while I'm mucking with
stuff and clean up the header.


Bug: 742487
Change-Id: Ibc9dd0a2f94dad8cb7a1f4adb1897668800311bf
Reviewed-on: https://chromium-review.googlesource.com/575289Reviewed-by: default avatarSky Malice <skym@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Commit-Queue: Justin Carlson <justincarlson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488276}
parent 7489031e
...@@ -34,78 +34,59 @@ class SyncedPrintersManager : public KeyedService { ...@@ -34,78 +34,59 @@ class SyncedPrintersManager : public KeyedService {
public: public:
class Observer { class Observer {
public: public:
virtual void OnPrinterAdded(const Printer& printer) = 0; virtual void OnConfiguredPrintersChanged(
virtual void OnPrinterUpdated(const Printer& printer) = 0; const std::vector<Printer>& printers) = 0;
virtual void OnPrinterRemoved(const Printer& printer) = 0; virtual void OnEnterprisePrintersChanged(
const std::vector<Printer>& printers) = 0;
}; };
SyncedPrintersManager(Profile* profile, static std::unique_ptr<SyncedPrintersManager> Create(
std::unique_ptr<PrintersSyncBridge> sync_bridge); Profile* profile,
~SyncedPrintersManager() override; std::unique_ptr<PrintersSyncBridge> sync_bridge);
~SyncedPrintersManager() override = default;
// Register the printing preferences with the |registry|. // Register the printing preferences with the |registry|.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
// Returns the printers that are saved in preferences. // Returns the printers that are saved in preferences.
std::vector<std::unique_ptr<Printer>> GetPrinters() const; virtual std::vector<Printer> GetConfiguredPrinters() const = 0;
// Returns printers from enterprise policy. // Returns printers from enterprise policy.
std::vector<std::unique_ptr<Printer>> GetRecommendedPrinters() const; virtual std::vector<Printer> GetEnterprisePrinters() const = 0;
// Returns the printer with id |printer_id|, or nullptr if no such // Returns the printer with id |printer_id|, or nullptr if no such printer
// printer exists. // exists. Searches both Configured and Enterprise printers.
std::unique_ptr<Printer> GetPrinter(const std::string& printer_id) const; virtual std::unique_ptr<Printer> GetPrinter(
const std::string& printer_id) const = 0;
// Adds or updates a printer in profile preferences. The |printer| is // Adds or updates a printer in profile preferences. The |printer| is
// identified by its id field. Those with an empty id are treated as new // identified by its id field. Those with an empty id are treated as new
// printers. // printers.
void RegisterPrinter(std::unique_ptr<Printer> printer); virtual void UpdateConfiguredPrinter(const Printer& printer) = 0;
// Remove printer from preferences with the id |printer_id|. Returns true if // Remove printer from preferences with the id |printer_id|. Returns true if
// the printer was successfully removed. // the printer was successfully removed.
bool RemovePrinter(const std::string& printer_id); virtual bool RemoveConfiguredPrinter(const std::string& printer_id) = 0;
// Attach |observer| for notification of events. |observer| is expected to // Attach |observer| for notification of events. |observer| is expected to
// live on the same thread (UI) as this object. OnPrinter* methods are // live on the same thread (UI) as this object. OnPrinter* methods are
// invoked inline so calling RegisterPrinter in response to OnPrinterAdded is // invoked inline so calling RegisterPrinter in response to OnPrinterAdded is
// forbidden. // forbidden.
void AddObserver(SyncedPrintersManager::Observer* observer); virtual void AddObserver(SyncedPrintersManager::Observer* observer) = 0;
// Remove |observer| so that it no longer receives notifications. After the // Remove |observer| so that it no longer receives notifications. After the
// completion of this method, the |observer| can be safely destroyed. // completion of this method, the |observer| can be safely destroyed.
void RemoveObserver(SyncedPrintersManager::Observer* observer); virtual void RemoveObserver(SyncedPrintersManager::Observer* observer) = 0;
// Returns a ModelTypeSyncBridge for the sync client. // Returns a ModelTypeSyncBridge for the sync client.
PrintersSyncBridge* GetSyncBridge(); virtual PrintersSyncBridge* GetSyncBridge() = 0;
// Registers that the printer was installed in CUPS. This is independent of // Registers that the printer was installed in CUPS. This is independent of
// whether a printer is saved in profile preferences. // whether a printer is saved in profile preferences.
void PrinterInstalled(const Printer& printer); virtual void PrinterInstalled(const Printer& printer) = 0;
// Returns true if |printer| is currently installed in CUPS. // Returns true if |printer| is currently installed in CUPS.
bool IsConfigurationCurrent(const Printer& printer) const; virtual bool IsConfigurationCurrent(const Printer& printer) const = 0;
private:
// Updates the in-memory recommended printer list.
void UpdateRecommendedPrinters();
Profile* profile_;
PrefChangeRegistrar pref_change_registrar_;
// The backend for profile printers.
std::unique_ptr<PrintersSyncBridge> sync_bridge_;
// Contains the keys for all recommended printers in order so we can return
// the list of recommended printers in the order they were received.
std::vector<std::string> recommended_printer_ids_;
std::map<std::string, std::unique_ptr<Printer>> recommended_printers_;
// Map of printer ids to installation timestamps.
std::map<std::string, base::Time> installed_printer_timestamps_;
base::ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(SyncedPrintersManager);
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -64,7 +64,8 @@ SyncedPrintersManager* SyncedPrintersManagerFactory::BuildServiceInstanceFor( ...@@ -64,7 +64,8 @@ SyncedPrintersManager* SyncedPrintersManagerFactory::BuildServiceInstanceFor(
store_factory, base::BindRepeating(base::IgnoreResult( store_factory, base::BindRepeating(base::IgnoreResult(
&base::debug::DumpWithoutCrashing))); &base::debug::DumpWithoutCrashing)));
return new SyncedPrintersManager(profile, std::move(sync_bridge)); return SyncedPrintersManager::Create(profile, std::move(sync_bridge))
.release();
} }
} // namespace chromeos } // namespace chromeos
...@@ -261,11 +261,8 @@ class UsbPrinterDetectorImpl : public UsbPrinterDetector, ...@@ -261,11 +261,8 @@ class UsbPrinterDetectorImpl : public UsbPrinterDetector,
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (result == PrinterSetupResult::kSuccess) { if (result == PrinterSetupResult::kSuccess) {
if (data->is_new) { if (data->is_new) {
// We aren't done with data->printer yet, so we have to copy it instead
// of moving it.
auto printer_copy = base::MakeUnique<Printer>(*data->printer);
SyncedPrintersManagerFactory::GetForBrowserContext(profile_) SyncedPrintersManagerFactory::GetForBrowserContext(profile_)
->RegisterPrinter(std::move(printer_copy)); ->UpdateConfiguredPrinter(*data->printer);
} }
// TODO(justincarlson): If the device was hotplugged, pop a timed // TODO(justincarlson): If the device was hotplugged, pop a timed
// notification that says the printer is now available for printing. // notification that says the printer is now available for printing.
......
...@@ -26,7 +26,7 @@ namespace printers_helper { ...@@ -26,7 +26,7 @@ namespace printers_helper {
namespace { namespace {
using PrinterList = std::vector<std::unique_ptr<chromeos::Printer>>; using PrinterList = std::vector<chromeos::Printer>;
// Returns true if Printer#id, Printer#description, and Printer#uri all match. // Returns true if Printer#id, Printer#description, and Printer#uri all match.
bool PrintersAreMostlyEqual(const chromeos::Printer& left, bool PrintersAreMostlyEqual(const chromeos::Printer& left,
...@@ -40,16 +40,16 @@ bool ListsContainTheSamePrinters(const PrinterList& list_a, ...@@ -40,16 +40,16 @@ bool ListsContainTheSamePrinters(const PrinterList& list_a,
const PrinterList& list_b) { const PrinterList& list_b) {
std::unordered_multimap<std::string, const chromeos::Printer*> map_b; std::unordered_multimap<std::string, const chromeos::Printer*> map_b;
for (const auto& b : list_b) { for (const auto& b : list_b) {
map_b.insert({b->id(), b.get()}); map_b.insert({b.id(), &b});
} }
for (const auto& a : list_a) { for (const auto& a : list_a) {
auto range = map_b.equal_range(a->id()); auto range = map_b.equal_range(a.id());
auto it = std::find_if( auto it = std::find_if(
range.first, range.second, range.first, range.second,
[&a](const std::pair<std::string, const chromeos::Printer*>& entry) [&a](const std::pair<std::string, const chromeos::Printer*>& entry)
-> bool { return PrintersAreMostlyEqual(*a, *(entry.second)); }); -> bool { return PrintersAreMostlyEqual(a, *(entry.second)); });
if (it == range.second) { if (it == range.second) {
// Element in a does not match an element in b. Lists do not contain the // Element in a does not match an element in b. Lists do not contain the
...@@ -88,30 +88,30 @@ chromeos::SyncedPrintersManager* GetPrinterStore( ...@@ -88,30 +88,30 @@ chromeos::SyncedPrintersManager* GetPrinterStore(
void AddPrinter(chromeos::SyncedPrintersManager* manager, void AddPrinter(chromeos::SyncedPrintersManager* manager,
const chromeos::Printer& printer) { const chromeos::Printer& printer) {
manager->RegisterPrinter(base::MakeUnique<chromeos::Printer>(printer)); manager->UpdateConfiguredPrinter(printer);
} }
void RemovePrinter(chromeos::SyncedPrintersManager* manager, int index) { void RemovePrinter(chromeos::SyncedPrintersManager* manager, int index) {
chromeos::Printer testPrinter(CreateTestPrinter(index)); chromeos::Printer testPrinter(CreateTestPrinter(index));
manager->RemovePrinter(testPrinter.id()); manager->RemoveConfiguredPrinter(testPrinter.id());
} }
bool EditPrinterDescription(chromeos::SyncedPrintersManager* manager, bool EditPrinterDescription(chromeos::SyncedPrintersManager* manager,
int index, int index,
const std::string& description) { const std::string& description) {
PrinterList printers = manager->GetPrinters(); PrinterList printers = manager->GetConfiguredPrinters();
std::string printer_id = PrinterId(index); std::string printer_id = PrinterId(index);
auto found = std::find_if( auto found =
printers.begin(), printers.end(), std::find_if(printers.begin(), printers.end(),
[&printer_id](const std::unique_ptr<chromeos::Printer>& printer) -> bool { [&printer_id](const chromeos::Printer& printer) -> bool {
return printer->id() == printer_id; return printer.id() == printer_id;
}); });
if (found == printers.end()) if (found == printers.end())
return false; return false;
(*found)->set_description(description); found->set_description(description);
manager->RegisterPrinter(std::move(*found)); manager->UpdateConfiguredPrinter(*found);
return true; return true;
} }
...@@ -139,17 +139,17 @@ chromeos::SyncedPrintersManager* GetPrinterStore(int index) { ...@@ -139,17 +139,17 @@ chromeos::SyncedPrintersManager* GetPrinterStore(int index) {
} }
int GetVerifierPrinterCount() { int GetVerifierPrinterCount() {
return GetVerifierPrinterStore()->GetPrinters().size(); return GetVerifierPrinterStore()->GetConfiguredPrinters().size();
} }
int GetPrinterCount(int index) { int GetPrinterCount(int index) {
return GetPrinterStore(index)->GetPrinters().size(); return GetPrinterStore(index)->GetConfiguredPrinters().size();
} }
bool AllProfilesContainSamePrinters() { bool AllProfilesContainSamePrinters() {
auto reference_printers = GetPrinterStore(0)->GetPrinters(); auto reference_printers = GetPrinterStore(0)->GetConfiguredPrinters();
for (int i = 1; i < test()->num_clients(); ++i) { for (int i = 1; i < test()->num_clients(); ++i) {
auto printers = GetPrinterStore(i)->GetPrinters(); auto printers = GetPrinterStore(i)->GetConfiguredPrinters();
if (!ListsContainTheSamePrinters(reference_printers, printers)) { if (!ListsContainTheSamePrinters(reference_printers, printers)) {
VLOG(1) << "Printers in client [" << i << "] don't match client 0"; VLOG(1) << "Printers in client [" << i << "] don't match client 0";
return false; return false;
...@@ -160,8 +160,9 @@ bool AllProfilesContainSamePrinters() { ...@@ -160,8 +160,9 @@ bool AllProfilesContainSamePrinters() {
} }
bool ProfileContainsSamePrintersAsVerifier(int index) { bool ProfileContainsSamePrintersAsVerifier(int index) {
return ListsContainTheSamePrinters(GetVerifierPrinterStore()->GetPrinters(), return ListsContainTheSamePrinters(
GetPrinterStore(index)->GetPrinters()); GetVerifierPrinterStore()->GetConfiguredPrinters(),
GetPrinterStore(index)->GetConfiguredPrinters());
} }
PrintersMatchChecker::PrintersMatchChecker() PrintersMatchChecker::PrintersMatchChecker()
......
...@@ -99,7 +99,7 @@ IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, RemoveAndEditPrinters) { ...@@ -99,7 +99,7 @@ IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, RemoveAndEditPrinters) {
ASSERT_TRUE(PrintersMatchChecker().Wait()); ASSERT_TRUE(PrintersMatchChecker().Wait());
EXPECT_EQ(2, GetPrinterCount(0)); EXPECT_EQ(2, GetPrinterCount(0));
EXPECT_EQ(updated_description, EXPECT_EQ(updated_description,
GetPrinterStore(1)->GetPrinters()[0]->description()); GetPrinterStore(1)->GetConfiguredPrinters()[0].description());
} }
IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, ConflictResolution) { IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, ConflictResolution) {
...@@ -125,7 +125,8 @@ IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, ConflictResolution) { ...@@ -125,7 +125,8 @@ IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, ConflictResolution) {
// Conflict resolution shoud run here. // Conflict resolution shoud run here.
ASSERT_TRUE(PrintersMatchChecker().Wait()); ASSERT_TRUE(PrintersMatchChecker().Wait());
// The more recent update should win. // The more recent update should win.
EXPECT_EQ(valid_message, GetPrinterStore(1)->GetPrinters()[0]->description()); EXPECT_EQ(valid_message,
GetPrinterStore(1)->GetConfiguredPrinters()[0].description());
} }
IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, SimpleMerge) { IN_PROC_BROWSER_TEST_F(TwoClientPrintersSyncTest, SimpleMerge) {
......
...@@ -52,11 +52,10 @@ printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) { ...@@ -52,11 +52,10 @@ printing::PrinterBasicInfo ToBasicInfo(const chromeos::Printer& printer) {
return basic_info; return basic_info;
} }
void AddPrintersToList( void AddPrintersToList(const std::vector<chromeos::Printer>& printers,
const std::vector<std::unique_ptr<chromeos::Printer>>& printers, PrinterList* list) {
PrinterList* list) {
for (const auto& printer : printers) { for (const auto& printer : printers) {
list->push_back(ToBasicInfo(*printer)); list->push_back(ToBasicInfo(printer));
} }
} }
...@@ -103,8 +102,8 @@ class PrinterBackendProxyChromeos : public PrinterBackendProxy { ...@@ -103,8 +102,8 @@ class PrinterBackendProxyChromeos : public PrinterBackendProxy {
if (!base::CommandLine::ForCurrentProcess()->HasSwitch( if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableNativeCups)) { switches::kDisableNativeCups)) {
AddPrintersToList(prefs_->GetPrinters(), &printer_list); AddPrintersToList(prefs_->GetConfiguredPrinters(), &printer_list);
AddPrintersToList(prefs_->GetRecommendedPrinters(), &printer_list); AddPrintersToList(prefs_->GetEnterprisePrinters(), &printer_list);
} }
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
......
...@@ -185,15 +185,13 @@ void CupsPrintersHandler::HandleGetCupsPrintersList( ...@@ -185,15 +185,13 @@ void CupsPrintersHandler::HandleGetCupsPrintersList(
std::string callback_id; std::string callback_id;
CHECK(args->GetString(0, &callback_id)); CHECK(args->GetString(0, &callback_id));
std::vector<std::unique_ptr<Printer>> printers = std::vector<Printer> printers =
SyncedPrintersManagerFactory::GetForBrowserContext(profile_) SyncedPrintersManagerFactory::GetForBrowserContext(profile_)
->GetPrinters(); ->GetConfiguredPrinters();
auto printers_list = base::MakeUnique<base::ListValue>(); auto printers_list = base::MakeUnique<base::ListValue>();
for (const std::unique_ptr<Printer>& printer : printers) { for (const Printer& printer : printers) {
std::unique_ptr<base::DictionaryValue> printer_info = printers_list->Append(GetPrinterInfo(printer));
GetPrinterInfo(*printer.get());
printers_list->Append(std::move(printer_info));
} }
auto response = base::MakeUnique<base::DictionaryValue>(); auto response = base::MakeUnique<base::DictionaryValue>();
...@@ -207,10 +205,10 @@ void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) { ...@@ -207,10 +205,10 @@ void CupsPrintersHandler::HandleUpdateCupsPrinter(const base::ListValue* args) {
CHECK(args->GetString(0, &printer_id)); CHECK(args->GetString(0, &printer_id));
CHECK(args->GetString(1, &printer_name)); CHECK(args->GetString(1, &printer_name));
std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); Printer printer(printer_id);
printer->set_display_name(printer_name); printer.set_display_name(printer_name);
SyncedPrintersManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( SyncedPrintersManagerFactory::GetForBrowserContext(profile_)
std::move(printer)); ->UpdateConfiguredPrinter(printer);
} }
void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) {
...@@ -225,7 +223,7 @@ void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { ...@@ -225,7 +223,7 @@ void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) {
return; return;
Printer::PrinterProtocol protocol = printer->GetProtocol(); Printer::PrinterProtocol protocol = printer->GetProtocol();
prefs->RemovePrinter(printer_id); prefs->RemoveConfiguredPrinter(printer_id);
DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient();
client->CupsRemovePrinter(printer_name, client->CupsRemovePrinter(printer_name,
...@@ -355,19 +353,19 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { ...@@ -355,19 +353,19 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) {
std::string printer_ppd_path; std::string printer_ppd_path;
printer_dict->GetString("printerPPDPath", &printer_ppd_path); printer_dict->GetString("printerPPDPath", &printer_ppd_path);
std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); Printer printer(printer_id);
printer->set_display_name(printer_name); printer.set_display_name(printer_name);
printer->set_description(printer_description); printer.set_description(printer_description);
printer->set_manufacturer(printer_manufacturer); printer.set_manufacturer(printer_manufacturer);
printer->set_model(printer_model); printer.set_model(printer_model);
printer->set_uri(printer_uri); printer.set_uri(printer_uri);
bool autoconf = false; bool autoconf = false;
printer_dict->GetBoolean("printerAutoconf", &autoconf); printer_dict->GetBoolean("printerAutoconf", &autoconf);
// Verify that the printer is autoconf or a valid ppd path is present. // Verify that the printer is autoconf or a valid ppd path is present.
if (autoconf) { if (autoconf) {
printer->mutable_ppd_reference()->autoconf = true; printer.mutable_ppd_reference()->autoconf = true;
} else if (!printer_ppd_path.empty()) { } else if (!printer_ppd_path.empty()) {
RecordPpdSource(kUser); RecordPpdSource(kUser);
GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path)); GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path));
...@@ -376,24 +374,24 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { ...@@ -376,24 +374,24 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) {
OnAddPrinterError(); OnAddPrinterError();
return; return;
} }
printer->mutable_ppd_reference()->user_supplied_ppd_url = tmp.spec(); printer.mutable_ppd_reference()->user_supplied_ppd_url = tmp.spec();
} else if (!ppd_manufacturer.empty() && !ppd_model.empty()) { } else if (!ppd_manufacturer.empty() && !ppd_model.empty()) {
RecordPpdSource(kScs); RecordPpdSource(kScs);
// Using the manufacturer and model, get a ppd reference. // Using the manufacturer and model, get a ppd reference.
if (!ppd_provider_->GetPpdReference(ppd_manufacturer, ppd_model, if (!ppd_provider_->GetPpdReference(ppd_manufacturer, ppd_model,
printer->mutable_ppd_reference())) { printer.mutable_ppd_reference())) {
LOG(ERROR) << "Failed to get ppd reference"; LOG(ERROR) << "Failed to get ppd reference";
OnAddPrinterError(); OnAddPrinterError();
return; return;
} }
if (printer->make_and_model().empty()) { if (printer.make_and_model().empty()) {
// In lieu of more accurate information, populate the make and model // In lieu of more accurate information, populate the make and model
// fields with the PPD information. // fields with the PPD information.
printer->set_manufacturer(ppd_manufacturer); printer.set_manufacturer(ppd_manufacturer);
printer->set_model(ppd_model); printer.set_model(ppd_model);
// PPD Model names are actually make and model. // PPD Model names are actually make and model.
printer->set_make_and_model(ppd_model); printer.set_make_and_model(ppd_model);
} }
} else { } else {
// TODO(crbug.com/738514): Support PPD guessing for non-autoconf printers. // TODO(crbug.com/738514): Support PPD guessing for non-autoconf printers.
...@@ -402,28 +400,23 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { ...@@ -402,28 +400,23 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) {
<< "A configuration option must have been selected to add a printer"; << "A configuration option must have been selected to add a printer";
} }
// Copy the printer for the configurer. Ownership needs to be transfered to
// the receiver of the callback.
const Printer printer_copy = *printer;
printer_configurer_->SetUpPrinter( printer_configurer_->SetUpPrinter(
printer_copy, printer, base::Bind(&CupsPrintersHandler::OnAddedPrinter,
base::Bind(&CupsPrintersHandler::OnAddedPrinter, weak_factory_.GetWeakPtr(), printer));
weak_factory_.GetWeakPtr(), base::Passed(&printer)));
} }
void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, void CupsPrintersHandler::OnAddedPrinter(const Printer& printer,
PrinterSetupResult result_code) { PrinterSetupResult result_code) {
std::string printer_name = printer->display_name();
UMA_HISTOGRAM_ENUMERATION("Printing.CUPS.PrinterSetupResult", result_code, UMA_HISTOGRAM_ENUMERATION("Printing.CUPS.PrinterSetupResult", result_code,
PrinterSetupResult::kMaxValue); PrinterSetupResult::kMaxValue);
switch (result_code) { switch (result_code) {
case PrinterSetupResult::kSuccess: { case PrinterSetupResult::kSuccess: {
UMA_HISTOGRAM_ENUMERATION("Printing.CUPS.PrinterAdded", UMA_HISTOGRAM_ENUMERATION("Printing.CUPS.PrinterAdded",
printer->GetProtocol(), Printer::kProtocolMax); printer.GetProtocol(), Printer::kProtocolMax);
auto* manager = auto* manager =
SyncedPrintersManagerFactory::GetForBrowserContext(profile_); SyncedPrintersManagerFactory::GetForBrowserContext(profile_);
manager->PrinterInstalled(*printer); manager->PrinterInstalled(printer);
manager->RegisterPrinter(std::move(printer)); manager->UpdateConfiguredPrinter(printer);
break; break;
} }
case PrinterSetupResult::kPpdNotFound: case PrinterSetupResult::kPpdNotFound:
...@@ -452,7 +445,7 @@ void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, ...@@ -452,7 +445,7 @@ void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer,
CallJavascriptFunction( CallJavascriptFunction(
"cr.webUIListenerCallback", base::Value("on-add-cups-printer"), "cr.webUIListenerCallback", base::Value("on-add-cups-printer"),
base::Value(result_code == PrinterSetupResult::kSuccess), base::Value(result_code == PrinterSetupResult::kSuccess),
base::Value(printer_name)); base::Value(printer.display_name()));
} }
void CupsPrintersHandler::OnAddPrinterError() { void CupsPrintersHandler::OnAddPrinterError() {
......
...@@ -68,8 +68,7 @@ class CupsPrintersHandler : public ::settings::SettingsPageUIHandler, ...@@ -68,8 +68,7 @@ class CupsPrintersHandler : public ::settings::SettingsPageUIHandler,
bool ipp_everywhere); bool ipp_everywhere);
void HandleAddCupsPrinter(const base::ListValue* args); void HandleAddCupsPrinter(const base::ListValue* args);
void OnAddedPrinter(std::unique_ptr<Printer> printer, void OnAddedPrinter(const Printer& printer, PrinterSetupResult result);
PrinterSetupResult result);
void OnAddPrinterError(); void OnAddPrinterError();
// Get a list of all manufacturers for which we have at least one model of // Get a list of all manufacturers for which we have at least one model of
......
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