1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/printing/enterprise/enterprise_printers_provider.h"
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/hash/md5.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ash/printing/enterprise/bulk_printers_calculator.h"
#include "chrome/browser/ash/printing/enterprise/bulk_printers_calculator_factory.h"
#include "chrome/browser/ash/printing/enterprise/calculators_policies_binder.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/printing/printer_configuration.h"
#include "chromeos/printing/printer_translator.h"
#include "components/device_event_log/device_event_log.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"
namespace ash {
namespace {
std::vector<std::string> ConvertToVector(const base::Value::List& list) {
std::vector<std::string> string_list;
for (const base::Value& value : list) {
if (value.is_string()) {
string_list.push_back(value.GetString());
}
}
return string_list;
}
class EnterprisePrintersProviderImpl : public EnterprisePrintersProvider,
public BulkPrintersCalculator::Observer {
public:
EnterprisePrintersProviderImpl(CrosSettings* settings, Profile* profile)
: profile_(profile) {
// initialization of pref_change_registrar
pref_change_registrar_.Init(profile->GetPrefs());
auto* factory = BulkPrintersCalculatorFactory::Get();
if (!factory) {
DVLOG(1) << "Factory is null. Policies are unbound. This is only "
"expected in unit tests";
return;
}
// Get instance of BulkPrintersCalculator for device policies.
device_printers_ = factory->GetForDevice();
if (device_printers_) {
devices_binder_ =
CalculatorsPoliciesBinder::DeviceBinder(settings, device_printers_);
device_printers_->AddObserver(this);
RecalculateCompleteFlagForDevicePrinters();
}
// Calculate account_id_ and get instance of BulkPrintersCalculator for user
// policies.
const user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(profile);
if (user) {
account_id_ = user->GetAccountId();
user_printers_ = factory->GetForAccountId(account_id_);
// Binds instances of BulkPrintersCalculator to policies.
profile_binder_ = CalculatorsPoliciesBinder::UserBinder(
profile->GetPrefs(), user_printers_);
user_printers_->AddObserver(this);
RecalculateCompleteFlagForUserPrinters();
}
// Binds policy with recommended printers (deprecated). This method calls
// indirectly RecalculateCurrentPrintersList() that prepares the first
// version of final list of printers.
BindPref(prefs::kRecommendedPrinters,
&EnterprisePrintersProviderImpl::UpdateUserRecommendedPrinters);
}
EnterprisePrintersProviderImpl(const EnterprisePrintersProviderImpl&) =
delete;
EnterprisePrintersProviderImpl& operator=(
const EnterprisePrintersProviderImpl&) = delete;
~EnterprisePrintersProviderImpl() override {
if (device_printers_)
device_printers_->RemoveObserver(this);
if (user_printers_) {
user_printers_->RemoveObserver(this);
}
}
void AddObserver(EnterprisePrintersProvider::Observer* observer) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(observer);
observer->OnPrintersChanged(complete_, printers_);
}
void RemoveObserver(EnterprisePrintersProvider::Observer* observer) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(observer);
}
// BulkPrintersCalculator::Observer implementation
void OnPrintersChanged(const BulkPrintersCalculator* sender) override {
if (device_printers_ && sender == device_printers_.get()) {
RecalculateCompleteFlagForDevicePrinters();
} else if (user_printers_ && sender == user_printers_.get()) {
RecalculateCompleteFlagForUserPrinters();
}
RecalculateCurrentPrintersList();
}
private:
// This method process value from the deprecated policy with recommended
// printers. It is called when value of the policy changes.
void UpdateUserRecommendedPrinters() {
recommended_printers_.clear();
std::vector<std::string> data = FromPrefs(prefs::kRecommendedPrinters);
for (const auto& printer_json : data) {
std::optional<base::Value> printer_value = base::JSONReader::Read(
printer_json, base::JSON_ALLOW_TRAILING_COMMAS);
if (!printer_value.has_value() || !printer_value.value().is_dict()) {
LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: "
<< printer_json;
continue;
}
// Policy printers don't have id's but the ids only need to be locally
// unique so we'll hash the record. This will not collide with the
// UUIDs generated for user entries.
std::string id = base::MD5String(printer_json);
base::Value::Dict& printer_dictionary = printer_value.value().GetDict();
printer_dictionary.Set(chromeos::kPrinterId, id);
auto new_printer =
chromeos::RecommendedPrinterToPrinter(printer_dictionary);
if (!new_printer) {
LOG(WARNING) << "Recommended printer is malformed.";
continue;
}
bool inserted = recommended_printers_.insert({id, *new_printer}).second;
if (!inserted) {
// Printer is already in the list.
LOG(WARNING) << "Duplicate printer ignored: " << id;
continue;
}
}
RecalculateCurrentPrintersList();
}
// These three methods calculate resultant list of printers and complete flag.
void RecalculateCompleteFlagForUserPrinters() {
if (!user_printers_) {
user_printers_is_complete_ = true;
return;
}
user_printers_is_complete_ =
user_printers_->IsComplete() &&
(user_printers_->IsDataPolicySet() ||
!PolicyWithDataIsSet(policy::key::kPrintersBulkConfiguration));
}
void RecalculateCompleteFlagForDevicePrinters() {
if (!device_printers_) {
device_printers_is_complete_ = true;
return;
}
device_printers_is_complete_ =
device_printers_->IsComplete() &&
(device_printers_->IsDataPolicySet() ||
!PolicyWithDataIsSet(policy::key::kDevicePrinters));
}
void RecalculateCurrentPrintersList() {
complete_ = true;
// Enterprise printers from user policy, device policy, as well as printers
// from the legacy `Printers` policy.
std::unordered_map<std::string, chromeos::Printer> all_printers =
recommended_printers_;
if (device_printers_) {
complete_ = complete_ && device_printers_is_complete_;
std::unordered_map<std::string, chromeos::Printer> printers =
device_printers_->GetPrinters();
PRINTER_LOG(DEBUG)
<< "EnterprisePrintersProvider::RecalculateCurrentPrintersList()"
<< "-device-printers: complete=" << device_printers_is_complete_
<< " count=" << printers.size();
all_printers.merge(std::move(printers));
}
if (user_printers_) {
complete_ = complete_ && user_printers_is_complete_;
std::unordered_map<std::string, chromeos::Printer> printers =
user_printers_->GetPrinters();
PRINTER_LOG(DEBUG)
<< "EnterprisePrintersProvider::RecalculateCurrentPrintersList()"
<< "-user-printers: complete=" << user_printers_is_complete_
<< " count=" << printers.size();
all_printers.merge(std::move(printers));
}
// Update `printers_` with the recalculated result.
printers_.clear();
printers_.reserve(all_printers.size());
std::ranges::transform(all_printers, std::back_inserter(printers_),
[](const auto& p) { return p.second; });
for (auto& observer : observers_) {
observer.OnPrintersChanged(complete_, printers_);
}
}
typedef void (EnterprisePrintersProviderImpl::*SimpleMethod)();
// Binds given user policy to given method and calls this method once.
void BindPref(const char* policy_name, SimpleMethod method_to_call) {
pref_change_registrar_.Add(
policy_name,
base::BindRepeating(method_to_call, base::Unretained(this)));
(this->*method_to_call)();
}
// Extracts the list of strings named |policy_name| from user policies.
std::vector<std::string> FromPrefs(const std::string& policy_name) {
return ConvertToVector(profile_->GetPrefs()->GetList(policy_name));
}
// Checks if given policy is set and if it is a dictionary
bool PolicyWithDataIsSet(const char* policy_name) {
policy::ProfilePolicyConnector* policy_connector =
profile_->GetProfilePolicyConnector();
if (!policy_connector) {
// something is wrong
return false;
}
const policy::PolicyNamespace policy_namespace =
policy::PolicyNamespace(policy::PolicyDomain::POLICY_DOMAIN_CHROME, "");
const policy::PolicyMap& policy_map =
policy_connector->policy_service()->GetPolicies(policy_namespace);
const base::Value* value =
policy_map.GetValue(policy_name, base::Value::Type::DICT);
return value != nullptr;
}
// current partial results
std::unordered_map<std::string, chromeos::Printer> recommended_printers_;
bool device_printers_is_complete_ = true;
bool user_printers_is_complete_ = true;
// current final results
bool complete_ = false;
std::vector<chromeos::Printer> printers_;
// Calculators for bulk printers from device and user policies. Unowned.
base::WeakPtr<BulkPrintersCalculator> device_printers_;
base::WeakPtr<BulkPrintersCalculator> user_printers_;
// Policies binder (bridge between policies and calculators). Owned.
std::unique_ptr<CalculatorsPoliciesBinder> devices_binder_;
std::unique_ptr<CalculatorsPoliciesBinder> profile_binder_;
// Profile (user) settings.
raw_ptr<Profile> profile_;
AccountId account_id_;
PrefChangeRegistrar pref_change_registrar_;
base::ObserverList<EnterprisePrintersProvider::Observer>::Unchecked
observers_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace
// static
void EnterprisePrintersProvider::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterListPref(prefs::kRecommendedPrinters);
CalculatorsPoliciesBinder::RegisterProfilePrefs(registry);
}
// static
std::unique_ptr<EnterprisePrintersProvider> EnterprisePrintersProvider::Create(
CrosSettings* settings,
Profile* profile) {
return std::make_unique<EnterprisePrintersProviderImpl>(settings, profile);
}
} // namespace ash
|