blob: 665dd3256aa93fe695c5c421384585e46e505f8d [file] [log] [blame]
// Copyright 2016 The Chromium OS 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 "authpolicy/policy/preg_policy_encoder.h"
#include <memory>
#include <vector>
#include <base/files/file_path.h>
#include <components/policy/core/common/registry_dict.h>
#include "authpolicy/policy/device_policy_encoder.h"
#include "authpolicy/policy/policy_encoder_helper.h"
#include "authpolicy/policy/user_policy_encoder.h"
namespace em = enterprise_management;
namespace {
const char kRecommendedKey[] = "Recommended";
} // namespace
namespace policy {
bool ParsePRegFilesIntoUserPolicy(const std::vector<base::FilePath>& preg_files,
em::CloudPolicySettings* policy,
bool log_policy_values) {
DCHECK(policy);
RegistryDict mandatory_dict;
for (const base::FilePath& preg_file : preg_files) {
if (!LoadPRegFile(preg_file, &mandatory_dict))
return false;
}
// Recommended policies are stored in their own registry key. This can be
// nullptr if there is no recommended policy.
std::unique_ptr<RegistryDict> recommended_dict =
mandatory_dict.RemoveKey(kRecommendedKey);
// Convert recommended policies first. If a policy is both recommended and
// mandatory, it will be overwritten to be mandatory below.
if (recommended_dict) {
UserPolicyEncoder enc(recommended_dict.get(), POLICY_LEVEL_RECOMMENDED);
enc.LogPolicyValues(log_policy_values);
enc.EncodePolicy(policy);
}
{
UserPolicyEncoder enc(&mandatory_dict, POLICY_LEVEL_MANDATORY);
enc.LogPolicyValues(log_policy_values);
enc.EncodePolicy(policy);
}
return true;
}
bool ParsePRegFilesIntoDevicePolicy(
const std::vector<base::FilePath>& preg_files,
em::ChromeDeviceSettingsProto* policy,
bool log_policy_values) {
DCHECK(policy);
RegistryDict policy_dict;
for (const base::FilePath& preg_file : preg_files) {
if (!LoadPRegFile(preg_file, &policy_dict))
return false;
}
DevicePolicyEncoder encoder(&policy_dict);
encoder.LogPolicyValues(log_policy_values);
encoder.EncodePolicy(policy);
return true;
}
} // namespace policy