Commit b53e31ad authored by Julian Pastarmov's avatar Julian Pastarmov Committed by Commit Bot

Merge old and new list policies that were renamed

This change moves migrating of renamed policies from the PolicyHandler
to a PolicyMigrator that happens right after the PolicyMerger has run.
This guarantees that list policies can be properly merged between
sources even if different sources provide different naming.

BUG=1138572
TEST=browser_tests:PolicyPrefsTest.PolicyToPrefsMapping

Change-Id: Ibe335176ef52dabbe09c076ba57ae04d1c67c1c6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2507771
Commit-Queue: Julian Pastarmov <pastarmovj@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Reviewed-by: default avatarYann Dago <ydago@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826903}
parent 1b7a4da6
......@@ -138,7 +138,7 @@ std::string GetTestPolicy(const char* homepage, int key_version) {
" \"mandatory\": {"
" \"ShowHomeButton\": true,"
" \"RestoreOnStartup\": 4,"
" \"URLBlacklist\": [ \"dev.chromium.org\", \"youtube.com\" ],"
" \"URLBlocklist\": [ \"dev.chromium.org\", \"youtube.com\" ],"
" \"MaxInvalidationFetchDelay\": 1000"
" },"
" \"recommended\": {"
......@@ -167,7 +167,7 @@ void GetExpectedTestPolicy(PolicyMap* expected, const char* homepage) {
base::ListValue list;
list.AppendString("dev.chromium.org");
list.AppendString("youtube.com");
expected->Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
expected->Set(key::kURLBlocklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
POLICY_SOURCE_CLOUD, list.Clone(), nullptr);
expected->Set(key::kMaxInvalidationFetchDelay, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(1000),
......
......@@ -125,7 +125,7 @@ class RestoreOnStartupPolicyTest : public PolicyTest,
base::Value urls(base::Value::Type::LIST);
for (const auto* url_string : kRestoredURLs)
urls.Append(url_string);
policies.Set(key::kURLBlacklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
policies.Set(key::kURLBlocklist, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
POLICY_SOURCE_CLOUD, std::move(urls), nullptr);
provider_.UpdateChromePolicy(policies);
// This should restore the tabs opened at PRE_RunTest below, yet all should
......
......@@ -16,8 +16,10 @@
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/features.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
......@@ -25,6 +27,9 @@
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/values_util.h"
#include "components/policy/policy_constants.h"
#include "components/strings/grit/components_strings.h"
#include "extensions/buildflags/buildflags.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_ANDROID)
#include "components/policy/core/common/android/policy_service_android.h"
......@@ -79,6 +84,81 @@ void RemapProxyPolicies(PolicyMap* policies) {
}
}
// Maps the separate policies for proxy settings into a single Dictionary
// policy. This allows to keep the logic of merging policies from different
// sources simple, as all separate proxy policies should be considered as a
// single whole during merging.
void RemapRenamedPolicies(PolicyMap* policies) {
// For all renamed policies we need to explicitly merge the value of the
// old policy with the new one or else merging will not be carried over
// if desired.
base::Value* merge_list =
policies->GetMutableValue(key::kPolicyListMultipleSourceMergeList);
base::flat_set<std::string> policy_lists_to_merge =
policy::ValueToStringSet(merge_list);
const std::vector<std::pair<const char*, const char*>> renamed_policies = {{
{policy::key::kSafeBrowsingWhitelistDomains,
policy::key::kSafeBrowsingAllowlistDomains},
{policy::key::kSpellcheckLanguageBlacklist,
policy::key::kSpellcheckLanguageBlocklist},
{policy::key::kURLBlacklist, policy::key::kURLBlocklist},
{policy::key::kURLWhitelist, policy::key::kURLAllowlist},
#if !defined(OS_ANDROID)
{policy::key::kAutoplayWhitelist, policy::key::kAutoplayAllowlist},
#endif // !defined(OS_ANDROID)
#if BUILDFLAG(ENABLE_EXTENSIONS)
{policy::key::kExtensionInstallBlacklist,
policy::key::kExtensionInstallBlocklist},
{policy::key::kExtensionInstallWhitelist,
policy::key::kExtensionInstallAllowlist},
{policy::key::kNativeMessagingBlacklist,
policy::key::kNativeMessagingBlocklist},
{policy::key::kNativeMessagingWhitelist,
policy::key::kNativeMessagingAllowlist},
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
#if defined(OS_CHROMEOS)
{policy::key::kAttestationExtensionWhitelist,
policy::key::kAttestationExtensionAllowlist},
{policy::key::kExternalPrintServersWhitelist,
policy::key::kExternalPrintServersAllowlist},
{policy::key::kNativePrintersBulkBlacklist,
policy::key::kPrintersBulkBlocklist},
{policy::key::kNativePrintersBulkWhitelist,
policy::key::kPrintersBulkAllowlist},
{policy::key::kPerAppTimeLimitsWhitelist,
policy::key::kPerAppTimeLimitsAllowlist},
{policy::key::kQuickUnlockModeWhitelist,
policy::key::kQuickUnlockModeAllowlist},
{policy::key::kNoteTakingAppsLockScreenWhitelist,
policy::key::kNoteTakingAppsLockScreenAllowlist},
#if defined(USE_CUPS)
{policy::key::kPrintingAPIExtensionsWhitelist,
policy::key::kPrintingAPIExtensionsAllowlist},
#endif // defined(USE_CUPS)
#endif // defined(OS_CHROMEOS)
}};
for (const auto& policy_pair : renamed_policies) {
PolicyMap::Entry* old_policy = policies->GetMutable(policy_pair.first);
const PolicyMap::Entry* new_policy = policies->Get(policy_pair.second);
if (old_policy && !new_policy) {
PolicyMap::Entry policy_entry = old_policy->DeepCopy();
policy_entry.AddError(
l10n_util::GetStringFUTF8(IDS_POLICY_MIGRATED_NEW_POLICY,
base::UTF8ToUTF16(policy_pair.first)));
// TOTO(pastarmovj): Readd the old_policy error when the tast tests
// for chromeos have been updated.
// old_policy->AddError(
// l10n_util::GetStringFUTF8(IDS_POLICY_MIGRATED_OLD_POLICY,
// base::UTF8ToUTF16(policy_pair.second)));
policies->Set(policy_pair.second, std::move(policy_entry));
}
if (policy_lists_to_merge.contains(policy_pair.first) &&
!policy_lists_to_merge.contains(policy_pair.second)) {
merge_list->Append(base::Value(policy_pair.second));
}
}
}
// Returns the string values of |policy|. Returns an empty set if the values are
// not strings.
base::flat_set<std::string> GetStringListPolicyItems(
......@@ -279,6 +359,7 @@ void PolicyServiceImpl::MergeAndTriggerUpdates() {
PolicyBundle provided_bundle;
provided_bundle.CopyFrom(provider->policies());
RemapProxyPolicies(&provided_bundle.Get(chrome_namespace));
RemapRenamedPolicies(&provided_bundle.Get(chrome_namespace));
bundle.MergeFrom(provided_bundle);
}
......
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