This source file includes following definitions.
- MarkRecommendedFieldnames
- GetEditableFlags
- MergeDictionaries
- MergeNestedDictionaries
- MergeDictionaries
- HasUserPolicy
- HasDevicePolicy
- MergeListOfValues
- MergeValues
- MergeValues
- MergeDictionaries
- MergeValues
- MergeNestedDictionaries
- MergeSettingsAndPoliciesToEffective
- MergeSettingsAndPoliciesToAugmented
#include "chromeos/network/onc/onc_merger.h"
#include <set>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/network/onc/onc_signature.h"
#include "components/onc/onc_constants.h"
namespace chromeos {
namespace onc {
namespace {
typedef scoped_ptr<base::DictionaryValue> DictionaryPtr;
void MarkRecommendedFieldnames(const base::DictionaryValue& policy,
base::DictionaryValue* result) {
const base::ListValue* recommended_value = NULL;
if (!policy.GetListWithoutPathExpansion(::onc::kRecommended,
&recommended_value))
return;
for (base::ListValue::const_iterator it = recommended_value->begin();
it != recommended_value->end(); ++it) {
std::string entry;
if ((*it)->GetAsString(&entry))
result->SetBooleanWithoutPathExpansion(entry, true);
}
}
DictionaryPtr GetEditableFlags(const base::DictionaryValue& policy) {
DictionaryPtr result_editable(new base::DictionaryValue);
MarkRecommendedFieldnames(policy, result_editable.get());
for (base::DictionaryValue::Iterator it(policy); !it.IsAtEnd();
it.Advance()) {
const base::DictionaryValue* child_policy = NULL;
if (it.key() == ::onc::kRecommended ||
!it.value().GetAsDictionary(&child_policy)) {
continue;
}
result_editable->SetWithoutPathExpansion(
it.key(), GetEditableFlags(*child_policy).release());
}
return result_editable.Pass();
}
class MergeListOfDictionaries {
public:
typedef std::vector<const base::DictionaryValue*> DictPtrs;
MergeListOfDictionaries() {
}
virtual ~MergeListOfDictionaries() {
}
DictionaryPtr MergeDictionaries(const DictPtrs &dicts) {
DictionaryPtr result(new base::DictionaryValue);
std::set<std::string> visited;
for (DictPtrs::const_iterator it_outer = dicts.begin();
it_outer != dicts.end(); ++it_outer) {
if (!*it_outer)
continue;
for (base::DictionaryValue::Iterator field(**it_outer); !field.IsAtEnd();
field.Advance()) {
const std::string& key = field.key();
if (key == ::onc::kRecommended || !visited.insert(key).second)
continue;
scoped_ptr<base::Value> merged_value;
if (field.value().IsType(base::Value::TYPE_DICTIONARY)) {
DictPtrs nested_dicts;
for (DictPtrs::const_iterator it_inner = dicts.begin();
it_inner != dicts.end(); ++it_inner) {
const base::DictionaryValue* nested_dict = NULL;
if (*it_inner)
(*it_inner)->GetDictionaryWithoutPathExpansion(key, &nested_dict);
nested_dicts.push_back(nested_dict);
}
DictionaryPtr merged_dict(MergeNestedDictionaries(key, nested_dicts));
if (!merged_dict->empty())
merged_value = merged_dict.Pass();
} else {
std::vector<const base::Value*> values;
for (DictPtrs::const_iterator it_inner = dicts.begin();
it_inner != dicts.end(); ++it_inner) {
const base::Value* value = NULL;
if (*it_inner)
(*it_inner)->GetWithoutPathExpansion(key, &value);
values.push_back(value);
}
merged_value = MergeListOfValues(key, values);
}
if (merged_value)
result->SetWithoutPathExpansion(key, merged_value.release());
}
}
return result.Pass();
}
protected:
virtual scoped_ptr<base::Value> MergeListOfValues(
const std::string& key,
const std::vector<const base::Value*>& values) = 0;
virtual DictionaryPtr MergeNestedDictionaries(const std::string& key,
const DictPtrs &dicts) {
return MergeDictionaries(dicts);
}
private:
DISALLOW_COPY_AND_ASSIGN(MergeListOfDictionaries);
};
class MergeSettingsAndPolicies : public MergeListOfDictionaries {
public:
struct ValueParams {
const base::Value* user_policy;
const base::Value* device_policy;
const base::Value* user_setting;
const base::Value* shared_setting;
const base::Value* active_setting;
bool user_editable;
bool device_editable;
};
MergeSettingsAndPolicies() {}
DictionaryPtr MergeDictionaries(
const base::DictionaryValue* user_policy,
const base::DictionaryValue* device_policy,
const base::DictionaryValue* user_settings,
const base::DictionaryValue* shared_settings,
const base::DictionaryValue* active_settings) {
hasUserPolicy_ = (user_policy != NULL);
hasDevicePolicy_ = (device_policy != NULL);
DictionaryPtr user_editable;
if (user_policy != NULL)
user_editable = GetEditableFlags(*user_policy);
DictionaryPtr device_editable;
if (device_policy != NULL)
device_editable = GetEditableFlags(*device_policy);
std::vector<const base::DictionaryValue*> dicts(kLastIndex, NULL);
dicts[kUserPolicyIndex] = user_policy;
dicts[kDevicePolicyIndex] = device_policy;
dicts[kUserSettingsIndex] = user_settings;
dicts[kSharedSettingsIndex] = shared_settings;
dicts[kActiveSettingsIndex] = active_settings;
dicts[kUserEditableIndex] = user_editable.get();
dicts[kDeviceEditableIndex] = device_editable.get();
return MergeListOfDictionaries::MergeDictionaries(dicts);
}
protected:
virtual scoped_ptr<base::Value> MergeValues(const std::string& key,
const ValueParams& values) = 0;
bool HasUserPolicy() {
return hasUserPolicy_;
}
bool HasDevicePolicy() {
return hasDevicePolicy_;
}
virtual scoped_ptr<base::Value> MergeListOfValues(
const std::string& key,
const std::vector<const base::Value*>& values) OVERRIDE {
bool user_editable = !HasUserPolicy();
if (values[kUserEditableIndex])
values[kUserEditableIndex]->GetAsBoolean(&user_editable);
bool device_editable = !HasDevicePolicy();
if (values[kDeviceEditableIndex])
values[kDeviceEditableIndex]->GetAsBoolean(&device_editable);
ValueParams params;
params.user_policy = values[kUserPolicyIndex];
params.device_policy = values[kDevicePolicyIndex];
params.user_setting = values[kUserSettingsIndex];
params.shared_setting = values[kSharedSettingsIndex];
params.active_setting = values[kActiveSettingsIndex];
params.user_editable = user_editable;
params.device_editable = device_editable;
return MergeValues(key, params);
}
private:
enum {
kUserPolicyIndex,
kDevicePolicyIndex,
kUserSettingsIndex,
kSharedSettingsIndex,
kActiveSettingsIndex,
kUserEditableIndex,
kDeviceEditableIndex,
kLastIndex
};
bool hasUserPolicy_, hasDevicePolicy_;
DISALLOW_COPY_AND_ASSIGN(MergeSettingsAndPolicies);
};
class MergeToEffective : public MergeSettingsAndPolicies {
public:
MergeToEffective() {}
protected:
scoped_ptr<base::Value> MergeValues(const std::string& key,
const ValueParams& values,
std::string* which) {
const base::Value* result = NULL;
which->clear();
if (!values.user_editable) {
result = values.user_policy;
*which = ::onc::kAugmentationUserPolicy;
} else if (!values.device_editable) {
result = values.device_policy;
*which = ::onc::kAugmentationDevicePolicy;
} else if (values.user_setting) {
result = values.user_setting;
*which = ::onc::kAugmentationUserSetting;
} else if (values.shared_setting) {
result = values.shared_setting;
*which = ::onc::kAugmentationSharedSetting;
} else if (values.user_policy) {
result = values.user_policy;
*which = ::onc::kAugmentationUserPolicy;
} else if (values.device_policy) {
result = values.device_policy;
*which = ::onc::kAugmentationDevicePolicy;
} else {
}
if (result)
return make_scoped_ptr(result->DeepCopy());
return scoped_ptr<base::Value>();
}
virtual scoped_ptr<base::Value> MergeValues(
const std::string& key,
const ValueParams& values) OVERRIDE {
std::string which;
return MergeValues(key, values, &which);
}
private:
DISALLOW_COPY_AND_ASSIGN(MergeToEffective);
};
class MergeToAugmented : public MergeToEffective {
public:
MergeToAugmented() {}
DictionaryPtr MergeDictionaries(
const OncValueSignature& signature,
const base::DictionaryValue* user_policy,
const base::DictionaryValue* device_policy,
const base::DictionaryValue* user_settings,
const base::DictionaryValue* shared_settings,
const base::DictionaryValue* active_settings) {
signature_ = &signature;
return MergeToEffective::MergeDictionaries(user_policy,
device_policy,
user_settings,
shared_settings,
active_settings);
}
protected:
virtual scoped_ptr<base::Value> MergeValues(
const std::string& key,
const ValueParams& values) OVERRIDE {
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
if (values.active_setting) {
result->SetWithoutPathExpansion(::onc::kAugmentationActiveSetting,
values.active_setting->DeepCopy());
}
const OncFieldSignature* field = NULL;
if (signature_)
field = GetFieldSignature(*signature_, key);
if (field) {
std::string which_effective;
MergeToEffective::MergeValues(key, values, &which_effective).reset();
if (!which_effective.empty()) {
result->SetStringWithoutPathExpansion(
::onc::kAugmentationEffectiveSetting, which_effective);
}
bool is_credential = onc::FieldIsCredential(*signature_, key);
if (!is_credential) {
if (values.user_policy) {
result->SetWithoutPathExpansion(::onc::kAugmentationUserPolicy,
values.user_policy->DeepCopy());
}
if (values.device_policy) {
result->SetWithoutPathExpansion(::onc::kAugmentationDevicePolicy,
values.device_policy->DeepCopy());
}
}
if (values.user_setting) {
result->SetWithoutPathExpansion(::onc::kAugmentationUserSetting,
values.user_setting->DeepCopy());
}
if (values.shared_setting) {
result->SetWithoutPathExpansion(::onc::kAugmentationSharedSetting,
values.shared_setting->DeepCopy());
}
if (HasUserPolicy() && values.user_editable) {
result->SetBooleanWithoutPathExpansion(::onc::kAugmentationUserEditable,
true);
}
if (HasDevicePolicy() && values.device_editable) {
result->SetBooleanWithoutPathExpansion(
::onc::kAugmentationDeviceEditable, true);
}
} else {
result->SetStringWithoutPathExpansion(
::onc::kAugmentationEffectiveSetting, ::onc::kAugmentationUnmanaged);
}
if (result->empty())
result.reset();
return result.PassAs<base::Value>();
}
virtual DictionaryPtr MergeNestedDictionaries(
const std::string& key,
const DictPtrs &dicts) OVERRIDE {
DictionaryPtr result;
if (signature_) {
const OncValueSignature* enclosing_signature = signature_;
signature_ = NULL;
const OncFieldSignature* field =
GetFieldSignature(*enclosing_signature, key);
if (field)
signature_ = field->value_signature;
result = MergeToEffective::MergeNestedDictionaries(key, dicts);
signature_ = enclosing_signature;
} else {
result = MergeToEffective::MergeNestedDictionaries(key, dicts);
}
return result.Pass();
}
private:
const OncValueSignature* signature_;
DISALLOW_COPY_AND_ASSIGN(MergeToAugmented);
};
}
DictionaryPtr MergeSettingsAndPoliciesToEffective(
const base::DictionaryValue* user_policy,
const base::DictionaryValue* device_policy,
const base::DictionaryValue* user_settings,
const base::DictionaryValue* shared_settings) {
MergeToEffective merger;
return merger.MergeDictionaries(
user_policy, device_policy, user_settings, shared_settings, NULL);
}
DictionaryPtr MergeSettingsAndPoliciesToAugmented(
const OncValueSignature& signature,
const base::DictionaryValue* user_policy,
const base::DictionaryValue* device_policy,
const base::DictionaryValue* user_settings,
const base::DictionaryValue* shared_settings,
const base::DictionaryValue* active_settings) {
MergeToAugmented merger;
return merger.MergeDictionaries(
signature, user_policy, device_policy, user_settings, shared_settings,
active_settings);
}
}
}