Commit af28a55e authored by Brian Malcolm's avatar Brian Malcolm Committed by Commit Bot

Add LegacyChromePolicyMigrator for migrating non-pref policies

This is needed to handle GPO policies that do not have a corresponding pref.
Without this, logic to handle both policies simultaneously would involve
lots of refactoring of the components that listen for policy changes.

BUG=chromium:1105185

Change-Id: I8e30522941fb9623614882e098891238e26eeaaf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2295754Reviewed-by: default avatarJungshik Shin <jshin@chromium.org>
Reviewed-by: default avatarNicolas Ouellet-Payeur <nicolaso@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Commit-Queue: Brian Malcolm <bmalcolm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789634}
parent 030e694e
...@@ -62,7 +62,7 @@ void BrowserSwitcherPolicyMigrator::Migrate(policy::PolicyBundle* bundle) { ...@@ -62,7 +62,7 @@ void BrowserSwitcherPolicyMigrator::Migrate(policy::PolicyBundle* bundle) {
} }
extension_map.Set("browser_switcher_enabled", entry->DeepCopy()); extension_map.Set("browser_switcher_enabled", entry->DeepCopy());
using Migration = policy::ExtensionPolicyMigrator::Migration; using Migration = policy::PolicyMigrator::Migration;
const Migration migrations[] = { const Migration migrations[] = {
Migration("alternative_browser_path", Migration("alternative_browser_path",
policy::key::kAlternativeBrowserPath), policy::key::kAlternativeBrowserPath),
......
...@@ -5,10 +5,8 @@ ...@@ -5,10 +5,8 @@
#include "chrome/browser/policy/chrome_extension_policy_migrator.h" #include "chrome/browser/policy/chrome_extension_policy_migrator.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "extensions/common/hashed_extension_id.h" #include "extensions/common/hashed_extension_id.h"
#include "ui/base/l10n/l10n_util.h"
namespace policy { namespace policy {
...@@ -46,29 +44,7 @@ void ChromeExtensionPolicyMigrator::CopyPoliciesIfUnset( ...@@ -46,29 +44,7 @@ void ChromeExtensionPolicyMigrator::CopyPoliciesIfUnset(
PolicyDomain::POLICY_DOMAIN_CHROME, /* component_id */ std::string())); PolicyDomain::POLICY_DOMAIN_CHROME, /* component_id */ std::string()));
for (const auto& migration : migrations) { for (const auto& migration : migrations) {
PolicyMap::Entry* entry = extension_map->GetMutable(migration.old_name); CopyPolicyIfUnset(*extension_map, &chrome_map, migration);
if (entry) {
if (!chrome_map.Get(migration.new_name)) {
VLOG(3) << "Extension policy is configured: '" << migration.old_name
<< "'. Copied to '" << migration.new_name << "'.";
auto new_entry = entry->DeepCopy();
migration.transform.Run(new_entry.value());
new_entry.AddError(
l10n_util::GetStringFUTF8(IDS_POLICY_MIGRATED_NEW_POLICY,
base::UTF8ToUTF16(migration.old_name)));
chrome_map.Set(migration.new_name, std::move(new_entry));
} else {
VLOG(3) << "Extension policy is configured, but conflicts: '"
<< migration.old_name << "' (Chrome policy '"
<< migration.new_name << "' is also set). Skipped.";
}
entry->AddError(
l10n_util::GetStringFUTF8(IDS_POLICY_MIGRATED_OLD_POLICY,
base::UTF8ToUTF16(migration.new_name)));
} else {
VLOG(3) << "Extension policy is not configured: '" << migration.old_name
<< "'. Skipped.";
}
} }
} }
......
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
#ifndef CHROME_BROWSER_POLICY_CHROME_EXTENSION_POLICY_MIGRATOR_H_ #ifndef CHROME_BROWSER_POLICY_CHROME_EXTENSION_POLICY_MIGRATOR_H_
#define CHROME_BROWSER_POLICY_CHROME_EXTENSION_POLICY_MIGRATOR_H_ #define CHROME_BROWSER_POLICY_CHROME_EXTENSION_POLICY_MIGRATOR_H_
#include "components/policy/core/common/extension_policy_migrator.h" #include "components/policy/core/common/policy_migrator.h"
namespace policy { namespace policy {
// ExtensionPolicyMigrator with chrome-specific helper functions. // A helper class that migrates a deprecated policy to a new policy across
class ChromeExtensionPolicyMigrator : public ExtensionPolicyMigrator { // domain boundaries, by setting up the new policy based on the old one. It can
// migrate a deprecated extension policy to a new Chrome policy.
//
// PolicyMigrator with chrome-specific helper functions.
class ChromeExtensionPolicyMigrator : public PolicyMigrator {
public: public:
~ChromeExtensionPolicyMigrator() override {} ~ChromeExtensionPolicyMigrator() override {}
......
...@@ -51,7 +51,7 @@ void SetPolicy(PolicyMap* policy, ...@@ -51,7 +51,7 @@ void SetPolicy(PolicyMap* policy,
class TestingPolicyMigrator : public ChromeExtensionPolicyMigrator { class TestingPolicyMigrator : public ChromeExtensionPolicyMigrator {
public: public:
void Migrate(PolicyBundle* bundle) override { void Migrate(PolicyBundle* bundle) override {
using Migration = ExtensionPolicyMigrator::Migration; using Migration = PolicyMigrator::Migration;
const Migration migrations[] = { const Migration migrations[] = {
Migration(kOldPolicy1, kNewPolicy1), Migration(kOldPolicy1, kNewPolicy1),
Migration(kOldPolicy2, kNewPolicy2), Migration(kOldPolicy2, kNewPolicy2),
......
...@@ -213,7 +213,7 @@ void ProfilePolicyConnector::Init( ...@@ -213,7 +213,7 @@ void ProfilePolicyConnector::Init(
} }
#endif #endif
std::vector<std::unique_ptr<ExtensionPolicyMigrator>> migrators; std::vector<std::unique_ptr<PolicyMigrator>> migrators;
#if defined(OS_WIN) #if defined(OS_WIN)
migrators.push_back( migrators.push_back(
std::make_unique<browser_switcher::BrowserSwitcherPolicyMigrator>()); std::make_unique<browser_switcher::BrowserSwitcherPolicyMigrator>());
...@@ -344,7 +344,7 @@ ConfigurationPolicyProvider* ProfilePolicyConnector::GetPlatformProvider( ...@@ -344,7 +344,7 @@ ConfigurationPolicyProvider* ProfilePolicyConnector::GetPlatformProvider(
std::unique_ptr<PolicyService> std::unique_ptr<PolicyService>
ProfilePolicyConnector::CreatePolicyServiceWithInitializationThrottled( ProfilePolicyConnector::CreatePolicyServiceWithInitializationThrottled(
const std::vector<ConfigurationPolicyProvider*>& policy_providers, const std::vector<ConfigurationPolicyProvider*>& policy_providers,
std::vector<std::unique_ptr<ExtensionPolicyMigrator>> migrators, std::vector<std::unique_ptr<PolicyMigrator>> migrators,
ConfigurationPolicyProvider* user_policy_delegate) { ConfigurationPolicyProvider* user_policy_delegate) {
DCHECK(user_policy_delegate); DCHECK(user_policy_delegate);
......
...@@ -26,7 +26,7 @@ class ProxiedPoliciesPropagatedWatcher; ...@@ -26,7 +26,7 @@ class ProxiedPoliciesPropagatedWatcher;
class CloudPolicyStore; class CloudPolicyStore;
class ConfigurationPolicyProvider; class ConfigurationPolicyProvider;
class ExtensionPolicyMigrator; class PolicyMigrator;
class PolicyService; class PolicyService;
class PolicyServiceImpl; class PolicyServiceImpl;
class SchemaRegistry; class SchemaRegistry;
...@@ -114,7 +114,7 @@ class ProfilePolicyConnector final { ...@@ -114,7 +114,7 @@ class ProfilePolicyConnector final {
// [2] i.e. g_browser_process->local_state() // [2] i.e. g_browser_process->local_state()
std::unique_ptr<PolicyService> CreatePolicyServiceWithInitializationThrottled( std::unique_ptr<PolicyService> CreatePolicyServiceWithInitializationThrottled(
const std::vector<ConfigurationPolicyProvider*>& policy_providers, const std::vector<ConfigurationPolicyProvider*>& policy_providers,
std::vector<std::unique_ptr<ExtensionPolicyMigrator>> migrators, std::vector<std::unique_ptr<PolicyMigrator>> migrators,
ConfigurationPolicyProvider* user_policy_delegate); ConfigurationPolicyProvider* user_policy_delegate);
// Called when primary user policies that are proxied to the device-wide // Called when primary user policies that are proxied to the device-wide
......
...@@ -97,8 +97,6 @@ jumbo_source_set("internal") { ...@@ -97,8 +97,6 @@ jumbo_source_set("internal") {
"config_dir_policy_loader.h", "config_dir_policy_loader.h",
"configuration_policy_provider.cc", "configuration_policy_provider.cc",
"configuration_policy_provider.h", "configuration_policy_provider.h",
"extension_policy_migrator.cc",
"extension_policy_migrator.h",
"external_data_fetcher.cc", "external_data_fetcher.cc",
"external_data_fetcher.h", "external_data_fetcher.h",
"external_data_manager.h", "external_data_manager.h",
...@@ -106,6 +104,8 @@ jumbo_source_set("internal") { ...@@ -106,6 +104,8 @@ jumbo_source_set("internal") {
"features.h", "features.h",
"json_schema_constants.cc", "json_schema_constants.cc",
"json_schema_constants.h", "json_schema_constants.h",
"legacy_chrome_policy_migrator.cc",
"legacy_chrome_policy_migrator.h",
"management/management_service.cc", "management/management_service.cc",
"management/management_service.h", "management/management_service.h",
"management/platform_management_service.cc", "management/platform_management_service.cc",
...@@ -123,6 +123,8 @@ jumbo_source_set("internal") { ...@@ -123,6 +123,8 @@ jumbo_source_set("internal") {
"policy_map.h", "policy_map.h",
"policy_merger.cc", "policy_merger.cc",
"policy_merger.h", "policy_merger.h",
"policy_migrator.cc",
"policy_migrator.h",
"policy_namespace.cc", "policy_namespace.cc",
"policy_namespace.h", "policy_namespace.h",
"policy_proto_decoders.cc", "policy_proto_decoders.cc",
...@@ -178,6 +180,7 @@ jumbo_source_set("internal") { ...@@ -178,6 +180,7 @@ jumbo_source_set("internal") {
"//services/network/public/cpp", "//services/network/public/cpp",
"//third_party/libxml:xml_writer", "//third_party/libxml:xml_writer",
"//third_party/re2", "//third_party/re2",
"//ui/base",
"//url", "//url",
] ]
...@@ -389,6 +392,7 @@ source_set("unit_tests") { ...@@ -389,6 +392,7 @@ source_set("unit_tests") {
"cloud/device_management_service_unittest.cc", "cloud/device_management_service_unittest.cc",
"cloud/user_info_fetcher_unittest.cc", "cloud/user_info_fetcher_unittest.cc",
"generate_policy_source_unittest.cc", "generate_policy_source_unittest.cc",
"legacy_chrome_policy_migrator_unittest.cc",
"management/management_service_unittest.cc", "management/management_service_unittest.cc",
"plist_writer_unittest.cc", "plist_writer_unittest.cc",
"policy_bundle_unittest.cc", "policy_bundle_unittest.cc",
...@@ -483,6 +487,7 @@ source_set("unit_tests") { ...@@ -483,6 +487,7 @@ source_set("unit_tests") {
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
"//third_party/libxml:xml_reader", "//third_party/libxml:xml_reader",
"//ui/base",
] ]
if (is_chromeos) { if (is_chromeos) {
deps += [ "//chromeos/system" ] deps += [ "//chromeos/system" ]
......
...@@ -8,4 +8,5 @@ include_rules = [ ...@@ -8,4 +8,5 @@ include_rules = [
"+net/traffic_annotation", "+net/traffic_annotation",
"+services/network/test", "+services/network/test",
"+third_party/libxml/chromium", "+third_party/libxml/chromium",
"+ui/base/l10n",
] ]
...@@ -13,11 +13,11 @@ ...@@ -13,11 +13,11 @@
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
#include "base/values.h" #include "base/values.h"
#include "components/policy/core/common/configuration_policy_provider.h" #include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/extension_policy_migrator.h"
#include "components/policy/core/common/external_data_fetcher.h" #include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h" #include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_bundle.h" #include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h" #include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_migrator.h"
#include "components/policy/core/common/policy_namespace.h" #include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_types.h" #include "components/policy/core/common/policy_types.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
......
// Copyright 2019 The Chromium 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 "components/policy/core/common/extension_policy_migrator.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
namespace policy {
namespace {
void DoNothing(base::Value* val) {}
} // namespace
ExtensionPolicyMigrator::Migration::Migration(Migration&&) = default;
ExtensionPolicyMigrator::Migration::Migration(const char* old_name_,
const char* new_name_)
: Migration(old_name_, new_name_, base::BindRepeating(&DoNothing)) {}
ExtensionPolicyMigrator::Migration::Migration(const char* old_name_,
const char* new_name_,
ValueTransform transform_)
: old_name(old_name_),
new_name(new_name_),
transform(std::move(transform_)) {}
ExtensionPolicyMigrator::Migration::~Migration() = default;
} // namespace policy
// Copyright 2020 The Chromium 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 "components/policy/core/common/legacy_chrome_policy_migrator.h"
#include <string>
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
namespace policy {
LegacyChromePolicyMigrator::LegacyChromePolicyMigrator(const char* old_name,
const char* new_name)
: migration_(old_name, new_name) {}
LegacyChromePolicyMigrator::LegacyChromePolicyMigrator(
const char* old_name,
const char* new_name,
Migration::ValueTransform transform)
: migration_(old_name, new_name, transform) {}
LegacyChromePolicyMigrator::~LegacyChromePolicyMigrator() = default;
void LegacyChromePolicyMigrator::Migrate(policy::PolicyBundle* bundle) {
policy::PolicyMap& chrome_map =
bundle->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, ""));
CopyPolicyIfUnset(chrome_map, &chrome_map, migration_);
}
} // namespace policy
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_POLICY_CORE_COMMON_LEGACY_CHROME_POLICY_MIGRATOR_H_
#define COMPONENTS_POLICY_CORE_COMMON_LEGACY_CHROME_POLICY_MIGRATOR_H_
#include "components/policy/core/common/policy_migrator.h"
namespace policy {
// LegacyChromePolicyMigrator migrates a deprecated Chrome domain policy to a
// new name, setting up the new policy based on the old one.
//
// This is intended to be used for policies that do not have a corresponding
// pref. If the policy has a pref, please use
// |LegacyPoliciesDeprecatingPolicyHandler| instead.
class POLICY_EXPORT LegacyChromePolicyMigrator : public PolicyMigrator {
public:
using Migration = PolicyMigrator::Migration;
LegacyChromePolicyMigrator(const char* old_name, const char* new_name);
LegacyChromePolicyMigrator(const char* old_name,
const char* new_name,
Migration::ValueTransform transform);
~LegacyChromePolicyMigrator() override;
LegacyChromePolicyMigrator(const LegacyChromePolicyMigrator&) = delete;
LegacyChromePolicyMigrator& operator=(const LegacyChromePolicyMigrator&) =
delete;
void Migrate(policy::PolicyBundle* bundle) override;
private:
Migration migration_;
};
} // namespace policy
#endif // COMPONENTS_POLICY_CORE_COMMON_LEGACY_CHROME_POLICY_MIGRATOR_H_
// Copyright 2020 The Chromium 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 "components/policy/core/common/legacy_chrome_policy_migrator.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
namespace policy {
namespace {
const char kOldPolicy[] = "OldPolicy";
const char kNewPolicy[] = "NewPolicy";
const char kOtherPolicy[] = "OtherPolicy";
const int kOldValue = 111;
const int kNewValue = 222;
const int kTransformedValue = 333;
const int kOtherValue = 999;
void MultiplyByThree(base::Value* val) {
*val = base::Value(val->GetInt() * 3);
}
void SetPolicy(PolicyMap* policy,
const char* policy_name,
std::unique_ptr<base::Value> value) {
policy->Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
POLICY_SOURCE_CLOUD, std::move(value), nullptr);
}
} // namespace
TEST(LegacyChromePolicyMigratorTest, CopyPolicyIfUnset) {
PolicyBundle bundle;
PolicyMap& chrome_map = bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
SetPolicy(&chrome_map, kOldPolicy, std::make_unique<base::Value>(kOldValue));
SetPolicy(&chrome_map, kOtherPolicy,
std::make_unique<base::Value>(kOtherValue));
LegacyChromePolicyMigrator migrator(kOldPolicy, kNewPolicy);
migrator.Migrate(&bundle);
// kOldPolicy should have been copied to kNewPolicy, kOtherPolicy remains
EXPECT_EQ(3u, chrome_map.size());
ASSERT_TRUE(chrome_map.GetValue(kNewPolicy));
// Old Value should be copied over.
EXPECT_EQ(base::Value(kOldValue), *chrome_map.GetValue(kNewPolicy));
// Other Value should be unchanged.
EXPECT_EQ(base::Value(kOtherValue), *chrome_map.GetValue(kOtherPolicy));
base::RepeatingCallback<base::string16(int)> l10nlookup =
base::BindRepeating(&l10n_util::GetStringUTF16);
// Old policy should always be marked deprecated
EXPECT_FALSE(
chrome_map.Get(kOldPolicy)->GetLocalizedErrors(l10nlookup).empty());
EXPECT_FALSE(
chrome_map.Get(kNewPolicy)->GetLocalizedErrors(l10nlookup).empty());
}
TEST(LegacyChromePolicyMigratorTest, TransformPolicy) {
PolicyBundle bundle;
PolicyMap& chrome_map = bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
SetPolicy(&chrome_map, kOldPolicy, std::make_unique<base::Value>(kOldValue));
LegacyChromePolicyMigrator migrator(kOldPolicy, kNewPolicy,
base::BindRepeating(&MultiplyByThree));
migrator.Migrate(&bundle);
ASSERT_TRUE(chrome_map.GetValue(kNewPolicy));
// Old Value should be transformed
EXPECT_EQ(base::Value(kTransformedValue), *chrome_map.GetValue(kNewPolicy));
}
TEST(LegacyChromePolicyMigratorTest, IgnoreOldIfNewIsSet) {
PolicyBundle bundle;
PolicyMap& chrome_map = bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
SetPolicy(&chrome_map, kOldPolicy, std::make_unique<base::Value>(kOldValue));
SetPolicy(&chrome_map, kNewPolicy, std::make_unique<base::Value>(kNewValue));
LegacyChromePolicyMigrator migrator(kOldPolicy, kNewPolicy);
migrator.Migrate(&bundle);
// New Value is unchanged
EXPECT_EQ(base::Value(kNewValue), *chrome_map.GetValue(kNewPolicy));
// Should be no warning on new policy
base::RepeatingCallback<base::string16(int)> l10nlookup =
base::BindRepeating(&l10n_util::GetStringUTF16);
// Old policy should always be marked deprecated
EXPECT_FALSE(
chrome_map.Get(kOldPolicy)->GetLocalizedErrors(l10nlookup).empty());
// No warnings on new policy because it was unchanged.
EXPECT_TRUE(
chrome_map.Get(kNewPolicy)->GetLocalizedErrors(l10nlookup).empty());
}
} // namespace policy
// Copyright 2020 The Chromium 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 "components/policy/core/common/policy_migrator.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace policy {
namespace {
void DoNothing(base::Value* val) {}
} // namespace
PolicyMigrator::~PolicyMigrator() = default;
void PolicyMigrator::CopyPolicyIfUnset(PolicyMap& source,
PolicyMap* dest,
const Migration& migration) {
PolicyMap::Entry* entry = source.GetMutable(migration.old_name);
if (entry) {
if (!dest->Get(migration.new_name)) {
VLOG(3) << "Legacy policy '" << migration.old_name
<< "' has been copied to '" << migration.new_name << "'.";
auto new_entry = entry->DeepCopy();
migration.transform.Run(new_entry.value());
new_entry.AddError(
l10n_util::GetStringFUTF8(IDS_POLICY_MIGRATED_NEW_POLICY,
base::UTF8ToUTF16(migration.old_name)));
dest->Set(migration.new_name, std::move(new_entry));
} else {
VLOG(3) << "Legacy policy '" << migration.old_name
<< "' is ignored because '" << migration.new_name
<< "' is also set. ";
}
entry->AddError(l10n_util::GetStringFUTF8(
IDS_POLICY_MIGRATED_OLD_POLICY, base::UTF8ToUTF16(migration.new_name)));
} else {
VLOG(3) << "Legacy policy '" << migration.old_name << "' is not set.";
}
}
PolicyMigrator::Migration::Migration(Migration&&) = default;
PolicyMigrator::Migration::Migration(const char* old_name, const char* new_name)
: Migration(old_name, new_name, base::BindRepeating(&DoNothing)) {}
PolicyMigrator::Migration::Migration(const char* old_name,
const char* new_name,
ValueTransform transform)
: old_name(old_name), new_name(new_name), transform(std::move(transform)) {}
PolicyMigrator::Migration::~Migration() = default;
} // namespace policy
// Copyright 2018 The Chromium Authors. All rights reserved. // Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_POLICY_CORE_COMMON_EXTENSION_POLICY_MIGRATOR_H_ #ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_MIGRATOR_H_
#define COMPONENTS_POLICY_CORE_COMMON_EXTENSION_POLICY_MIGRATOR_H_ #define COMPONENTS_POLICY_CORE_COMMON_POLICY_MIGRATOR_H_
#include <memory> #include <memory>
...@@ -11,47 +11,53 @@ ...@@ -11,47 +11,53 @@
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/values.h" #include "base/values.h"
#include "components/policy/core/common/policy_bundle.h" #include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_export.h" #include "components/policy/policy_export.h"
namespace policy { namespace policy {
// A helper class that migrates a deprecated policy to a new policy across // A helper class that migrates a deprecated policy to a new policy -
// domain boundaries, by setting up the new policy based on the old one. It can // potentially across domain boundaries, by setting up the new policy based on
// migrate a deprecated extension policy to a new Chrome policy. // the old one. It can migrate a deprecated policy to a new policy.
// //
// For migrations that are only in the Chrome domain: you should use // For migrations that are only in the Chrome domain and which are accessed via
// |LegacyPoliciesDeprecatingPolicyHandler| instead. // prefs: you should use |LegacyPoliciesDeprecatingPolicyHandler| instead.
class POLICY_EXPORT ExtensionPolicyMigrator { class POLICY_EXPORT PolicyMigrator {
public: public:
virtual ~ExtensionPolicyMigrator() {} virtual ~PolicyMigrator();
// If there are deprecated policies in |bundle|, set the value of the new // If there are deprecated policies in |bundle|, set the value of the new
// policies accordingly. // policies accordingly.
virtual void Migrate(PolicyBundle* bundle) = 0; virtual void Migrate(PolicyBundle* bundle) = 0;
// Indicates how to rename a policy when migrating from the extension domain // Indicates how to rename a policy when migrating the old policy to the new
// to the Chrome domain. // policy.
struct POLICY_EXPORT Migration { struct POLICY_EXPORT Migration {
using ValueTransform = base::RepeatingCallback<void(base::Value*)>; using ValueTransform = base::RepeatingCallback<void(base::Value*)>;
Migration(Migration&&); Migration(Migration&&);
Migration(const char* old_name_, const char* new_name_); Migration(const char* old_name, const char* new_name);
Migration(const char* old_name_, Migration(const char* old_name,
const char* new_name_, const char* new_name,
ValueTransform transform_); ValueTransform transform);
~Migration(); ~Migration();
// Old name for the policy, in the extension domain. // Old name for the policy
const char* old_name; const char* old_name;
// New name for the policy, in the Chrome domain. // New name for the policy, in the Chrome domain.
const char* new_name; const char* new_name;
// Function to use to convert values from the old namespace to the new // Function to use to convert values from the old policy to the new
// namespace (e.g. convert value types). It should mutate the Value in // policy (e.g. convert value types). It should mutate the Value in
// place. By default, it does no transform. // place. By default, it does no transform.
ValueTransform transform; ValueTransform transform;
}; };
protected:
static void CopyPolicyIfUnset(PolicyMap& source,
PolicyMap* dest,
const Migration& migration);
}; };
} // namespace policy } // namespace policy
#endif // COMPONENTS_POLICY_CORE_COMMON_EXTENSION_POLICY_MIGRATOR_H_ #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_MIGRATOR_H_
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "components/policy/core/common/configuration_policy_provider.h" #include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/extension_policy_migrator.h"
#include "components/policy/core/common/policy_bundle.h" #include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_migrator.h"
#include "components/policy/core/common/policy_service.h" #include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_export.h" #include "components/policy/policy_export.h"
...@@ -31,14 +31,13 @@ class POLICY_EXPORT PolicyServiceImpl ...@@ -31,14 +31,13 @@ class POLICY_EXPORT PolicyServiceImpl
public ConfigurationPolicyProvider::Observer { public ConfigurationPolicyProvider::Observer {
public: public:
using Providers = std::vector<ConfigurationPolicyProvider*>; using Providers = std::vector<ConfigurationPolicyProvider*>;
using Migrators = std::vector<std::unique_ptr<ExtensionPolicyMigrator>>; using Migrators = std::vector<std::unique_ptr<PolicyMigrator>>;
// Creates a new PolicyServiceImpl with the list of // Creates a new PolicyServiceImpl with the list of
// ConfigurationPolicyProviders, in order of decreasing priority. // ConfigurationPolicyProviders, in order of decreasing priority.
explicit PolicyServiceImpl( explicit PolicyServiceImpl(
Providers providers, Providers providers,
Migrators migrators = Migrators migrators = std::vector<std::unique_ptr<PolicyMigrator>>());
std::vector<std::unique_ptr<ExtensionPolicyMigrator>>());
// Creates a new PolicyServiceImpl with the list of // Creates a new PolicyServiceImpl with the list of
// ConfigurationPolicyProviders, in order of decreasing priority. // ConfigurationPolicyProviders, in order of decreasing priority.
...@@ -47,8 +46,7 @@ class POLICY_EXPORT PolicyServiceImpl ...@@ -47,8 +46,7 @@ class POLICY_EXPORT PolicyServiceImpl
// |UnthrottleInitialization| has been called. // |UnthrottleInitialization| has been called.
static std::unique_ptr<PolicyServiceImpl> CreateWithThrottledInitialization( static std::unique_ptr<PolicyServiceImpl> CreateWithThrottledInitialization(
Providers providers, Providers providers,
Migrators migrators = Migrators migrators = std::vector<std::unique_ptr<PolicyMigrator>>());
std::vector<std::unique_ptr<ExtensionPolicyMigrator>>());
~PolicyServiceImpl() override; ~PolicyServiceImpl() override;
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/values.h" #include "base/values.h"
#include "components/policy/core/common/extension_policy_migrator.h"
#include "components/policy/core/common/external_data_fetcher.h" #include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h" #include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/mock_policy_service.h" #include "components/policy/core/common/mock_policy_service.h"
#include "components/policy/core/common/policy_migrator.h"
#include "components/policy/core/common/policy_types.h" #include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h" #include "components/policy/policy_constants.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
...@@ -96,7 +96,7 @@ class ChangePolicyObserver : public PolicyService::Observer { ...@@ -96,7 +96,7 @@ class ChangePolicyObserver : public PolicyService::Observer {
bool observer_invoked_; bool observer_invoked_;
}; };
class MockPolicyMigrator : public ExtensionPolicyMigrator { class MockPolicyMigrator : public PolicyMigrator {
public: public:
MOCK_METHOD1(Migrate, void(PolicyBundle* bundle)); MOCK_METHOD1(Migrate, void(PolicyBundle* 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