Commit a61e4499 authored by atwilson@chromium.org's avatar atwilson@chromium.org

Refactored StringToIntEnumListPolicyHandler to be more general purpose

Created new StringMappingListPolicyHandler class to provide general
mapping between policy string lists and generic pref types (int lists,
string lists, etc).

BUG=none

Review URL: https://codereview.chromium.org/341723002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278083 0039d316-1c4b-4281-b951-d872f2087c98
parent 128e3e93
...@@ -484,15 +484,29 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = { ...@@ -484,15 +484,29 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = {
}; };
#if !defined(OS_IOS) #if !defined(OS_IOS)
// Mapping from extension type names to Manifest::Type. void GetExtensionAllowedTypesMap(
StringToIntEnumListPolicyHandler::MappingEntry kExtensionAllowedTypesMap[] = { ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) {
{ "extension", extensions::Manifest::TYPE_EXTENSION }, // Mapping from extension type names to Manifest::Type.
{ "theme", extensions::Manifest::TYPE_THEME }, result->push_back(new StringMappingListPolicyHandler::MappingEntry(
{ "user_script", extensions::Manifest::TYPE_USER_SCRIPT }, "extension", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(
{ "hosted_app", extensions::Manifest::TYPE_HOSTED_APP }, extensions::Manifest::TYPE_EXTENSION))));
{ "legacy_packaged_app", extensions::Manifest::TYPE_LEGACY_PACKAGED_APP }, result->push_back(new StringMappingListPolicyHandler::MappingEntry(
{ "platform_app", extensions::Manifest::TYPE_PLATFORM_APP }, "theme", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(
}; extensions::Manifest::TYPE_THEME))));
result->push_back(new StringMappingListPolicyHandler::MappingEntry(
"user_script", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(
extensions::Manifest::TYPE_USER_SCRIPT))));
result->push_back(new StringMappingListPolicyHandler::MappingEntry(
"hosted_app", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(
extensions::Manifest::TYPE_HOSTED_APP))));
result->push_back(new StringMappingListPolicyHandler::MappingEntry(
"legacy_packaged_app", scoped_ptr<base::Value>(
base::Value::CreateIntegerValue(
extensions::Manifest::TYPE_LEGACY_PACKAGED_APP))));
result->push_back(new StringMappingListPolicyHandler::MappingEntry(
"platform_app", scoped_ptr<base::Value>(base::Value::CreateIntegerValue(
extensions::Manifest::TYPE_PLATFORM_APP))));
}
#endif // !defined(OS_IOS) #endif // !defined(OS_IOS)
} // namespace } // namespace
...@@ -560,11 +574,10 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList( ...@@ -560,11 +574,10 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList(
key::kExtensionInstallSources, key::kExtensionInstallSources,
extensions::pref_names::kAllowedInstallSites))); extensions::pref_names::kAllowedInstallSites)));
handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
new StringToIntEnumListPolicyHandler( new StringMappingListPolicyHandler(
key::kExtensionAllowedTypes, key::kExtensionAllowedTypes,
extensions::pref_names::kAllowedTypes, extensions::pref_names::kAllowedTypes,
kExtensionAllowedTypesMap, base::Bind(GetExtensionAllowedTypesMap))));
kExtensionAllowedTypesMap + arraysize(kExtensionAllowedTypesMap))));
#endif // !defined(OS_IOS) #endif // !defined(OS_IOS)
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS) #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
......
...@@ -151,19 +151,25 @@ bool IntRangePolicyHandlerBase::EnsureInRange(const base::Value* input, ...@@ -151,19 +151,25 @@ bool IntRangePolicyHandlerBase::EnsureInRange(const base::Value* input,
} }
// StringToIntEnumListPolicyHandler implementation ----------------------------- // StringMappingListPolicyHandler implementation -----------------------------
StringToIntEnumListPolicyHandler::StringToIntEnumListPolicyHandler( StringMappingListPolicyHandler::MappingEntry::MappingEntry(
const char* policy_value, scoped_ptr<base::Value> map)
: enum_value(policy_value), mapped_value(map.Pass()) {}
StringMappingListPolicyHandler::MappingEntry::~MappingEntry() {}
StringMappingListPolicyHandler::StringMappingListPolicyHandler(
const char* policy_name, const char* policy_name,
const char* pref_path, const char* pref_path,
const MappingEntry* mapping_begin, const GenerateMapCallback& callback)
const MappingEntry* mapping_end)
: TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST), : TypeCheckingPolicyHandler(policy_name, base::Value::TYPE_LIST),
pref_path_(pref_path), pref_path_(pref_path),
mapping_begin_(mapping_begin), map_getter_(callback) {}
mapping_end_(mapping_end) {}
StringMappingListPolicyHandler::~StringMappingListPolicyHandler() {}
bool StringToIntEnumListPolicyHandler::CheckPolicySettings( bool StringMappingListPolicyHandler::CheckPolicySettings(
const PolicyMap& policies, const PolicyMap& policies,
PolicyErrorMap* errors) { PolicyErrorMap* errors) {
const base::Value* value; const base::Value* value;
...@@ -171,7 +177,7 @@ bool StringToIntEnumListPolicyHandler::CheckPolicySettings( ...@@ -171,7 +177,7 @@ bool StringToIntEnumListPolicyHandler::CheckPolicySettings(
Convert(value, NULL, errors); Convert(value, NULL, errors);
} }
void StringToIntEnumListPolicyHandler::ApplyPolicySettings( void StringMappingListPolicyHandler::ApplyPolicySettings(
const PolicyMap& policies, const PolicyMap& policies,
PrefValueMap* prefs) { PrefValueMap* prefs) {
if (!pref_path_) if (!pref_path_)
...@@ -182,9 +188,9 @@ void StringToIntEnumListPolicyHandler::ApplyPolicySettings( ...@@ -182,9 +188,9 @@ void StringToIntEnumListPolicyHandler::ApplyPolicySettings(
prefs->SetValue(pref_path_, list.release()); prefs->SetValue(pref_path_, list.release());
} }
bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input, bool StringMappingListPolicyHandler::Convert(const base::Value* input,
base::ListValue* output, base::ListValue* output,
PolicyErrorMap* errors) { PolicyErrorMap* errors) {
if (!input) if (!input)
return true; return true;
...@@ -206,17 +212,12 @@ bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input, ...@@ -206,17 +212,12 @@ bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input,
} }
continue; continue;
} }
bool found = false;
for (const MappingEntry* mapping_entry(mapping_begin_); scoped_ptr<base::Value> mapped_value = Map(entry_value);
mapping_entry != mapping_end_; ++mapping_entry) { if (mapped_value) {
if (mapping_entry->enum_value == entry_value) { if (output)
found = true; output->Append(mapped_value.release());
if (output) } else {
output->AppendInteger(mapping_entry->int_value);
break;
}
}
if (!found) {
if (errors) { if (errors) {
errors->AddError(policy_name(), errors->AddError(policy_name(),
entry - list_value->begin(), entry - list_value->begin(),
...@@ -228,6 +229,23 @@ bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input, ...@@ -228,6 +229,23 @@ bool StringToIntEnumListPolicyHandler::Convert(const base::Value* input,
return true; return true;
} }
scoped_ptr<base::Value> StringMappingListPolicyHandler::Map(
const std::string& entry_value) {
// Lazily generate the map of policy strings to mapped values.
if (map_.empty())
map_getter_.Run(&map_);
scoped_ptr<base::Value> return_value;
for (ScopedVector<MappingEntry>::const_iterator it = map_.begin();
it != map_.end(); ++it) {
const MappingEntry* mapping_entry = *it;
if (mapping_entry->enum_value == entry_value) {
return_value = make_scoped_ptr(mapping_entry->mapped_value->DeepCopy());
break;
}
}
return return_value.Pass();
}
// IntRangePolicyHandler implementation ---------------------------------------- // IntRangePolicyHandler implementation ----------------------------------------
...@@ -251,7 +269,7 @@ void IntRangePolicyHandler::ApplyPolicySettings(const PolicyMap& policies, ...@@ -251,7 +269,7 @@ void IntRangePolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
int value_in_range; int value_in_range;
if (value && EnsureInRange(value, &value_in_range, NULL)) { if (value && EnsureInRange(value, &value_in_range, NULL)) {
prefs->SetValue(pref_path_, prefs->SetValue(pref_path_,
base::Value::CreateIntegerValue(value_in_range)); new base::FundamentalValue(value_in_range));
} }
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h" #include "base/memory/scoped_vector.h"
...@@ -159,20 +160,29 @@ class POLICY_EXPORT SimplePolicyHandler : public TypeCheckingPolicyHandler { ...@@ -159,20 +160,29 @@ class POLICY_EXPORT SimplePolicyHandler : public TypeCheckingPolicyHandler {
DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler); DISALLOW_COPY_AND_ASSIGN(SimplePolicyHandler);
}; };
// A policy handler implementation that maps a string enum list to an int enum // Base class that encapsulates logic for mapping from a string enum list
// list as specified by a mapping table. // to a separate matching type value.
class POLICY_EXPORT StringToIntEnumListPolicyHandler class POLICY_EXPORT StringMappingListPolicyHandler
: public TypeCheckingPolicyHandler { : public TypeCheckingPolicyHandler {
public: public:
struct POLICY_EXPORT MappingEntry { // Data structure representing the map between policy strings and
// matching pref values.
class POLICY_EXPORT MappingEntry {
public:
MappingEntry(const char* policy_value, scoped_ptr<base::Value> map);
~MappingEntry();
const char* enum_value; const char* enum_value;
int int_value; scoped_ptr<base::Value> mapped_value;
}; };
StringToIntEnumListPolicyHandler(const char* policy_name, // Callback that generates the map for this instance.
const char* pref_path, typedef base::Callback<void(ScopedVector<MappingEntry>*)> GenerateMapCallback;
const MappingEntry* mapping_begin,
const MappingEntry* mapping_end); StringMappingListPolicyHandler(const char* policy_name,
const char* pref_path,
const GenerateMapCallback& map_generator);
virtual ~StringMappingListPolicyHandler();
// ConfigurationPolicyHandler methods: // ConfigurationPolicyHandler methods:
virtual bool CheckPolicySettings(const PolicyMap& policies, virtual bool CheckPolicySettings(const PolicyMap& policies,
...@@ -187,14 +197,21 @@ class POLICY_EXPORT StringToIntEnumListPolicyHandler ...@@ -187,14 +197,21 @@ class POLICY_EXPORT StringToIntEnumListPolicyHandler
base::ListValue* output, base::ListValue* output,
PolicyErrorMap* errors); PolicyErrorMap* errors);
// Helper method that converts from a policy value string to the associated
// pref value.
scoped_ptr<base::Value> Map(const std::string& entry_value);
// Name of the pref to write. // Name of the pref to write.
const char* pref_path_; const char* pref_path_;
// The mapping table. // The callback invoked to generate the map for this instance.
const MappingEntry* mapping_begin_; GenerateMapCallback map_getter_;
const MappingEntry* mapping_end_;
// Map of string policy values to local pref values. This is generated lazily
// so the generation does not have to happen if no policy is present.
ScopedVector<MappingEntry> map_;
DISALLOW_COPY_AND_ASSIGN(StringToIntEnumListPolicyHandler); DISALLOW_COPY_AND_ASSIGN(StringMappingListPolicyHandler);
}; };
// A policy handler implementation that ensures an int policy's value lies in an // A policy handler implementation that ensures an int policy's value lies in an
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// 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.
#include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
...@@ -17,10 +18,13 @@ namespace policy { ...@@ -17,10 +18,13 @@ namespace policy {
namespace { namespace {
StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = { void GetIntegerTypeMap(
{ "one", 1 }, ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) {
{ "two", 2 }, result->push_back(new StringMappingListPolicyHandler::MappingEntry(
}; "one", scoped_ptr<base::Value>(new base::FundamentalValue(1))));
result->push_back(new StringMappingListPolicyHandler::MappingEntry(
"two", scoped_ptr<base::Value>(new base::FundamentalValue(2))));
}
const char kTestPolicy[] = "unit_test.test_policy"; const char kTestPolicy[] = "unit_test.test_policy";
const char kTestPref[] = "unit_test.test_pref"; const char kTestPref[] = "unit_test.test_pref";
...@@ -49,11 +53,10 @@ TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { ...@@ -49,11 +53,10 @@ TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
base::ListValue list; base::ListValue list;
PolicyMap policy_map; PolicyMap policy_map;
PolicyErrorMap errors; PolicyErrorMap errors;
StringToIntEnumListPolicyHandler handler( StringMappingListPolicyHandler handler(
kTestPolicy, kTestPolicy,
kTestPref, kTestPref,
kTestTypeMap, base::Bind(GetIntegerTypeMap));
kTestTypeMap + arraysize(kTestTypeMap));
policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, list.DeepCopy(), NULL); POLICY_SCOPE_USER, list.DeepCopy(), NULL);
...@@ -85,17 +88,16 @@ TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) { ...@@ -85,17 +88,16 @@ TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty()); EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
} }
TEST(StringToIntEnumListPolicyHandlerTest, ApplyPolicySettings) { TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) {
base::ListValue list; base::ListValue list;
base::ListValue expected; base::ListValue expected;
PolicyMap policy_map; PolicyMap policy_map;
PrefValueMap prefs; PrefValueMap prefs;
base::Value* value; base::Value* value;
StringToIntEnumListPolicyHandler handler( StringMappingListPolicyHandler handler(
kTestPolicy, kTestPolicy,
kTestPref, kTestPref,
kTestTypeMap, base::Bind(GetIntegerTypeMap));
kTestTypeMap + arraysize(kTestTypeMap));
policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, list.DeepCopy(), NULL); POLICY_SCOPE_USER, list.DeepCopy(), NULL);
......
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