Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/network_settings/onc_merger.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
newline
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 9 #include "base/json/json_string_value_serializer.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/chromeos/cros/onc_constants.h" | |
| 13 #include "chrome/browser/chromeos/network_settings/onc_validator.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 namespace onc { | |
| 17 namespace { | |
| 18 | |
| 19 base::DictionaryValue* Merge(const base::DictionaryValue* user, | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
should return a scoped_ptr<base::DictionaryValue>
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 20 const base::DictionaryValue* policy) { | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
This function is too damn long. Please break into
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 21 CHECK(policy != NULL || user != NULL); | |
| 22 if (policy == NULL) return user->DeepCopy(); | |
| 23 if (user == NULL) { | |
| 24 // Don't DeepCopy but use Merge(,) to remove kRecommended fields. | |
| 25 base::DictionaryValue emptyDictionary; | |
| 26 return Merge(&emptyDictionary, policy); | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
so you're making a recursive call? Why not just us
pneubeck (no reviews)
2012/10/02 15:03:02
Done.
| |
| 27 } | |
| 28 | |
| 29 base::DictionaryValue* result = new base::DictionaryValue(); | |
| 30 | |
| 31 // Get the names of the recommended fields. | |
| 32 const ListValue* recommended_value = NULL; | |
| 33 | |
| 34 typedef std::set<std::string> RecommendedSet; | |
| 35 RecommendedSet recommended; | |
| 36 if (policy->GetListWithoutPathExpansion(kRecommended, | |
| 37 &recommended_value)) { | |
| 38 for (ListValue::const_iterator it = recommended_value->begin(); | |
| 39 it != recommended_value->end(); ++it) { | |
| 40 std::string entry; | |
| 41 if ((*it)->GetAsString(&entry)) | |
| 42 recommended.insert(entry); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 // Enforce all policy values that are not dictionaries. | |
| 47 for (base::DictionaryValue::key_iterator it = policy->begin_keys(); | |
| 48 it != policy->end_keys(); ++it) { | |
| 49 const std::string& field_name = *it; | |
| 50 if (field_name == kRecommended) | |
| 51 continue; | |
| 52 | |
| 53 const base::Value* policy_value = NULL; | |
| 54 policy->GetWithoutPathExpansion(field_name, &policy_value); | |
| 55 | |
| 56 if (policy_value->IsType(base::Value::TYPE_DICTIONARY)) | |
| 57 continue; | |
| 58 | |
| 59 RecommendedSet::iterator recommended_it = | |
| 60 recommended.find(field_name); | |
| 61 | |
| 62 bool is_recommended = recommended_it != recommended.end(); | |
| 63 if (is_recommended) | |
| 64 recommended.erase(recommended_it); | |
| 65 | |
| 66 bool will_overwrite = !(is_recommended && user->HasKey(field_name)); | |
| 67 | |
| 68 if (will_overwrite) { | |
| 69 result->Set(field_name, policy_value->DeepCopy()); | |
| 70 } else { | |
| 71 const base::Value* user_value = NULL; | |
| 72 user->GetWithoutPathExpansion(field_name, &user_value); | |
| 73 DCHECK(user_value != NULL); | |
| 74 result->Set(field_name, user_value->DeepCopy()); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // Insert user values that overwrite empty recommended fields. | |
| 79 for (RecommendedSet::iterator it = recommended.begin(); | |
| 80 it != recommended.end(); ++it) { | |
| 81 const std::string& field_name = *it; | |
| 82 const base::Value* user_value = NULL; | |
| 83 user->GetWithoutPathExpansion(field_name, &user_value); | |
| 84 | |
| 85 if (user_value != NULL) | |
| 86 result->Set(field_name, user_value->DeepCopy()); | |
| 87 } | |
| 88 | |
| 89 // Insert policy dictionaries and merge with user's dictionaries if existent. | |
| 90 for (base::DictionaryValue::key_iterator it = policy->begin_keys(); | |
| 91 it != policy->end_keys(); ++it) { | |
| 92 const std::string& field_name = *it; | |
| 93 | |
| 94 const base::DictionaryValue* policy_value = NULL; | |
| 95 if (!policy->GetDictionaryWithoutPathExpansion(field_name, &policy_value)) | |
| 96 continue; | |
| 97 | |
| 98 const base::DictionaryValue* user_value = NULL; | |
| 99 user->GetDictionaryWithoutPathExpansion(field_name, &user_value); | |
| 100 result->Set(field_name, Merge(user_value, policy_value)); | |
| 101 } | |
| 102 | |
| 103 // Insert user dictionaries not existent in the policy. | |
| 104 for (base::DictionaryValue::key_iterator it = user->begin_keys(); | |
| 105 it != user->end_keys(); ++it) { | |
| 106 const std::string& field_name = *it; | |
| 107 | |
| 108 const base::DictionaryValue* user_value = NULL; | |
| 109 if (!user->GetDictionaryWithoutPathExpansion(field_name, &user_value)) | |
| 110 continue; | |
| 111 | |
| 112 const base::DictionaryValue* policy_value = NULL; | |
| 113 if (policy->GetDictionaryWithoutPathExpansion(field_name, &policy_value)) | |
| 114 continue; | |
| 115 else | |
| 116 result->Set(field_name, Merge(user_value, NULL)); | |
| 117 } | |
|
Mattias Nissler (ping if slow)
2012/09/18 16:29:08
Algorithm-wise, wouldn't it be simpler to start wi
pneubeck (no reviews)
2012/10/02 15:03:02
In that case, fields that only exist in user and a
| |
| 118 | |
| 119 return result; | |
| 120 } | |
| 121 | |
| 122 } // namespace | |
| 123 | |
| 124 // static | |
| 125 base::DictionaryValue* ONCMerger::MergeUserSettingsWithPolicy( | |
| 126 const base::DictionaryValue& user, | |
| 127 const base::DictionaryValue& policy) { | |
| 128 base::DictionaryValue* result = Merge(&user, &policy); | |
| 129 ONCValidator::RemoveIgnoredFieldsFromNetwork(result); | |
| 130 return result; | |
| 131 } | |
| 132 | |
| 133 } // namespace onc | |
| 134 } // namespace chromeos | |
| OLD | NEW |