Commit dd4c3e1e authored by Piotr Pawliczek's avatar Piotr Pawliczek Committed by Commit Bot

Enterprise Printers: Bugfix for missing printers (Device and User policies)

Since M-78, external policies are initialized after CupsPrintersManager. It
causes a problem with Enterprise Printers, because BulkPrinterCalculators are
created during external policies initialization. Since M-78,
EnterprisePrintersProvider (started by CupsPrintersManager) cannot find
objects of BulkPrinterCalculators and always returns empty list of printers.
This patch solves this problem by changing the lifetime of
BulkPrintersCalculator. From now, each BulkPrintersCalculator is created by
the first object that asks for it.
Some unittests require that BulkPrintersCalculator are deleted at the end of
each unittest. The patch solving that problem is in CL:1836660.

BUG=chromium:1010289,chromium:1009062
TEST=on nautilus

Change-Id: Ie9b06f05a67f028204230b6b84162a058457fbd4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838051Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Commit-Queue: Piotr Pawliczek <pawliczek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703123}
parent e517e943
...@@ -16,8 +16,7 @@ namespace policy { ...@@ -16,8 +16,7 @@ namespace policy {
namespace { namespace {
base::WeakPtr<chromeos::BulkPrintersCalculator> GetBulkPrintersCalculator() { base::WeakPtr<chromeos::BulkPrintersCalculator> GetBulkPrintersCalculator() {
return chromeos::BulkPrintersCalculatorFactory::Get()->GetForDevice( return chromeos::BulkPrintersCalculatorFactory::Get()->GetForDevice();
/*create_if_not_exists=*/true);
} }
} // namespace } // namespace
......
...@@ -74,7 +74,7 @@ class DeviceNativePrintersExternalDataHandlerTest : public testing::Test { ...@@ -74,7 +74,7 @@ class DeviceNativePrintersExternalDataHandlerTest : public testing::Test {
std::make_unique<DeviceNativePrintersExternalDataHandler>( std::make_unique<DeviceNativePrintersExternalDataHandler>(
&policy_service_); &policy_service_);
external_printers_ = external_printers_ =
chromeos::BulkPrintersCalculatorFactory::Get()->GetForDevice(true); chromeos::BulkPrintersCalculatorFactory::Get()->GetForDevice();
external_printers_->SetAccessMode( external_printers_->SetAccessMode(
chromeos::BulkPrintersCalculator::ALL_ACCESS); chromeos::BulkPrintersCalculator::ALL_ACCESS);
} }
......
...@@ -19,8 +19,7 @@ namespace { ...@@ -19,8 +19,7 @@ namespace {
base::WeakPtr<chromeos::BulkPrintersCalculator> GetBulkPrintersCalculator( base::WeakPtr<chromeos::BulkPrintersCalculator> GetBulkPrintersCalculator(
const std::string& user_id) { const std::string& user_id) {
return chromeos::BulkPrintersCalculatorFactory::Get()->GetForAccountId( return chromeos::BulkPrintersCalculatorFactory::Get()->GetForAccountId(
CloudExternalDataPolicyHandler::GetAccountId(user_id), CloudExternalDataPolicyHandler::GetAccountId(user_id));
/*create_if_not_exists=*/true);
} }
} // namespace } // namespace
......
...@@ -20,27 +20,23 @@ BulkPrintersCalculatorFactory* BulkPrintersCalculatorFactory::Get() { ...@@ -20,27 +20,23 @@ BulkPrintersCalculatorFactory* BulkPrintersCalculatorFactory::Get() {
} }
base::WeakPtr<BulkPrintersCalculator> base::WeakPtr<BulkPrintersCalculator>
BulkPrintersCalculatorFactory::GetForAccountId(const AccountId& account_id, BulkPrintersCalculatorFactory::GetForAccountId(const AccountId& account_id) {
bool create_if_not_exists) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = printers_by_user_.find(account_id); auto it = printers_by_user_.find(account_id);
if (it != printers_by_user_.end()) if (it != printers_by_user_.end())
return it->second->AsWeakPtr(); return it->second->AsWeakPtr();
if (!create_if_not_exists)
return nullptr;
printers_by_user_.emplace(account_id, BulkPrintersCalculator::Create()); printers_by_user_.emplace(account_id, BulkPrintersCalculator::Create());
return printers_by_user_[account_id]->AsWeakPtr(); return printers_by_user_[account_id]->AsWeakPtr();
} }
base::WeakPtr<BulkPrintersCalculator> base::WeakPtr<BulkPrintersCalculator>
BulkPrintersCalculatorFactory::GetForProfile(Profile* profile, BulkPrintersCalculatorFactory::GetForProfile(Profile* profile) {
bool create_if_not_exists) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const user_manager::User* user = const user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(profile); ProfileHelper::Get()->GetUserByProfile(profile);
if (!user) if (!user)
return nullptr; return nullptr;
return GetForAccountId(user->GetAccountId(), create_if_not_exists); return GetForAccountId(user->GetAccountId());
} }
void BulkPrintersCalculatorFactory::RemoveForUserId( void BulkPrintersCalculatorFactory::RemoveForUserId(
...@@ -50,12 +46,10 @@ void BulkPrintersCalculatorFactory::RemoveForUserId( ...@@ -50,12 +46,10 @@ void BulkPrintersCalculatorFactory::RemoveForUserId(
} }
base::WeakPtr<BulkPrintersCalculator> base::WeakPtr<BulkPrintersCalculator>
BulkPrintersCalculatorFactory::GetForDevice(bool create_if_not_exists) { BulkPrintersCalculatorFactory::GetForDevice() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (device_printers_) if (device_printers_)
return device_printers_->AsWeakPtr(); return device_printers_->AsWeakPtr();
if (!create_if_not_exists)
return nullptr;
device_printers_ = BulkPrintersCalculator::Create(); device_printers_ = BulkPrintersCalculator::Create();
return device_printers_->AsWeakPtr(); return device_printers_->AsWeakPtr();
} }
......
...@@ -30,32 +30,24 @@ class BulkPrintersCalculatorFactory { ...@@ -30,32 +30,24 @@ class BulkPrintersCalculatorFactory {
// Returns a WeakPtr to the BulkPrintersCalculator registered for // Returns a WeakPtr to the BulkPrintersCalculator registered for
// |account_id|. // |account_id|.
// If requested BulkPrintersCalculator does not exist, the output depends on // If requested BulkPrintersCalculator does not exist, the object is
// the given parameter |create_if_not_exists|. If it is true, the object is // created and registered. The returned object remains valid until
// created and registered, otherwise nullptr is returned. // RemoveForUserId or Shutdown is called.
// The returned object remains valid until RemoveForUserId or Shutdown is
// called.
base::WeakPtr<BulkPrintersCalculator> GetForAccountId( base::WeakPtr<BulkPrintersCalculator> GetForAccountId(
const AccountId& account_id, const AccountId& account_id);
bool create_if_not_exists);
// Returns a WeakPtr to the BulkPrintersCalculator registered for |profile| // Returns a WeakPtr to the BulkPrintersCalculator registered for |profile|
// which could be nullptr if |profile| does not map to a valid AccountId. // which could be nullptr if |profile| does not map to a valid AccountId.
// If requested BulkPrintersCalculator does not exist, the output depends on // If requested BulkPrintersCalculator does not exist, the object is
// the given parameter |create_if_not_exists|. If it is true, the object is // created and registered. The returned object remains valid until
// created and registered, otherwise nullptr is returned. // RemoveForUserId or Shutdown is called.
// The returned object remains valid until RemoveForUserId or Shutdown is base::WeakPtr<BulkPrintersCalculator> GetForProfile(Profile* profile);
// called.
base::WeakPtr<BulkPrintersCalculator> GetForProfile(
Profile* profile,
bool create_if_not_exists);
// Returns a WeakPtr to the BulkPrintersCalculator registered for the device. // Returns a WeakPtr to the BulkPrintersCalculator registered for the device.
// If requested BulkPrintersCalculator does not exist, the output depends on // If requested BulkPrintersCalculator does not exist, the object is
// the given parameter |create_if_not_exists|. If it is true, the object is // created and registered. The returned object remains valid until Shutdown is
// created and registered, otherwise nullptr is returned. // called.
// The returned object remains valid until Shutdown is called. base::WeakPtr<BulkPrintersCalculator> GetForDevice();
base::WeakPtr<BulkPrintersCalculator> GetForDevice(bool create_if_not_exists);
// Deletes the BulkPrintersCalculator registered for |account_id|. // Deletes the BulkPrintersCalculator registered for |account_id|.
void RemoveForUserId(const AccountId& account_id); void RemoveForUserId(const AccountId& account_id);
......
...@@ -60,8 +60,7 @@ class CalculatorsPoliciesBinderImpl : public CalculatorsPoliciesBinder { ...@@ -60,8 +60,7 @@ class CalculatorsPoliciesBinderImpl : public CalculatorsPoliciesBinder {
: settings_(settings), profile_(profile) { : settings_(settings), profile_(profile) {
pref_change_registrar_.Init(profile->GetPrefs()); pref_change_registrar_.Init(profile->GetPrefs());
// Bind device policies to corresponding instance of BulkPrintersCalculator. // Bind device policies to corresponding instance of BulkPrintersCalculator.
device_printers_ = BulkPrintersCalculatorFactory::Get()->GetForDevice( device_printers_ = BulkPrintersCalculatorFactory::Get()->GetForDevice();
/*create_if_not_exists=*/false);
if (device_printers_ && ++(BindingsCount()[device_printers_.get()]) == 1) { if (device_printers_ && ++(BindingsCount()[device_printers_.get()]) == 1) {
BindSettings(kDeviceNativePrintersAccessMode, BindSettings(kDeviceNativePrintersAccessMode,
&CalculatorsPoliciesBinderImpl::UpdateDeviceAccessMode); &CalculatorsPoliciesBinderImpl::UpdateDeviceAccessMode);
...@@ -71,9 +70,8 @@ class CalculatorsPoliciesBinderImpl : public CalculatorsPoliciesBinder { ...@@ -71,9 +70,8 @@ class CalculatorsPoliciesBinderImpl : public CalculatorsPoliciesBinder {
&CalculatorsPoliciesBinderImpl::UpdateDeviceWhitelist); &CalculatorsPoliciesBinderImpl::UpdateDeviceWhitelist);
} }
// Bind user policies to corresponding instance of BulkPrintersCalculator. // Bind user policies to corresponding instance of BulkPrintersCalculator.
user_printers_ = BulkPrintersCalculatorFactory::Get()->GetForProfile( user_printers_ =
profile, BulkPrintersCalculatorFactory::Get()->GetForProfile(profile);
/*create_if_not_exists=*/false);
if (user_printers_ && ++(BindingsCount()[user_printers_.get()]) == 1) { if (user_printers_ && ++(BindingsCount()[user_printers_.get()]) == 1) {
BindPref(prefs::kRecommendedNativePrintersAccessMode, BindPref(prefs::kRecommendedNativePrintersAccessMode,
&CalculatorsPoliciesBinderImpl::UpdateUserAccessMode); &CalculatorsPoliciesBinderImpl::UpdateUserAccessMode);
......
...@@ -49,16 +49,14 @@ class EnterprisePrintersProviderImpl : public EnterprisePrintersProvider, ...@@ -49,16 +49,14 @@ class EnterprisePrintersProviderImpl : public EnterprisePrintersProvider,
// Binds instances of BulkPrintersCalculator to policies. // Binds instances of BulkPrintersCalculator to policies.
policies_binder_ = CalculatorsPoliciesBinder::Create(settings, profile); policies_binder_ = CalculatorsPoliciesBinder::Create(settings, profile);
// Get instance of BulkPrintersCalculator for device policies. // Get instance of BulkPrintersCalculator for device policies.
device_printers_ = BulkPrintersCalculatorFactory::Get()->GetForDevice( device_printers_ = BulkPrintersCalculatorFactory::Get()->GetForDevice();
/*create_if_not_exists=*/false);
if (device_printers_) { if (device_printers_) {
device_printers_->AddObserver(this); device_printers_->AddObserver(this);
RecalculateCompleteFlagForDevicePrinters(); RecalculateCompleteFlagForDevicePrinters();
} }
// Get instance of BulkPrintersCalculator for user policies. // Get instance of BulkPrintersCalculator for user policies.
user_printers_ = BulkPrintersCalculatorFactory::Get()->GetForProfile( user_printers_ =
profile, BulkPrintersCalculatorFactory::Get()->GetForProfile(profile);
/*create_if_not_exists=*/false);
if (user_printers_) { if (user_printers_) {
user_printers_->AddObserver(this); user_printers_->AddObserver(this);
RecalculateCompleteFlagForUserPrinters(); RecalculateCompleteFlagForUserPrinters();
......
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