Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_POLICY_POLICY_MAP_H_ | 5 #ifndef CHROME_BROWSER_POLICY_POLICY_MAP_H_ |
| 6 #define CHROME_BROWSER_POLICY_POLICY_MAP_H_ | 6 #define CHROME_BROWSER_POLICY_POLICY_MAP_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | |
| 11 #include <string> | |
| 10 | 12 |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 #include "policy/configuration_policy_type.h" | |
| 13 | 14 |
| 14 namespace policy { | 15 namespace policy { |
| 15 | 16 |
| 16 struct PolicyDefinitionList; | 17 struct PolicyDefinitionList; |
| 17 | 18 |
| 18 // Wrapper class around a std::map<ConfigurationPolicyType, Value*> that | 19 // The level of a policy determines its enforceability and whether users can |
| 19 // properly cleans up after itself when going out of scope. | 20 // override it or not. The values are listed in increasing order of priority. |
| 20 // Exposes interesting methods of the underlying std::map. | 21 enum PolicyLevel { |
| 22 // RECOMMENDED policies can be overridden by users. They are meant as a | |
| 23 // default value configured by admins, that users can customize. | |
| 24 POLICY_LEVEL_RECOMMENDED, | |
| 25 | |
| 26 // MANDATORY policies must be enforced and users can't circumvent them. | |
| 27 POLICY_LEVEL_MANDATORY, | |
| 28 }; | |
| 29 | |
| 30 // The scope of a policy flags whether it is meant to be applied to the current | |
| 31 // user or to the machine. | |
| 32 enum PolicyScope { | |
| 33 // USER policies apply to sessions of the current user. | |
| 34 POLICY_SCOPE_USER, | |
| 35 | |
| 36 // MACHINE policies apply to any users of the current machine. | |
| 37 POLICY_SCOPE_MACHINE, | |
| 38 }; | |
| 39 | |
| 40 // A mapping of policy names to policy values for a given policy namespace. | |
| 21 class PolicyMap { | 41 class PolicyMap { |
| 22 public: | 42 public: |
| 23 typedef std::map<ConfigurationPolicyType, Value*> PolicyMapType; | 43 // Each policy maps to an Entry which keeps the policy value as well as other |
| 44 // relevant data about the policy. | |
| 45 struct Entry { | |
| 46 PolicyLevel level; | |
| 47 PolicyScope scope; | |
| 48 Value* value; | |
| 49 | |
| 50 Entry() : level(POLICY_LEVEL_RECOMMENDED), | |
|
Mattias Nissler (ping if slow)
2012/01/18 15:07:07
should break before :
http://google-styleguide.go
Joao da Silva
2012/01/18 16:49:29
Done.
| |
| 51 scope(POLICY_SCOPE_USER), | |
| 52 value(NULL) {} | |
| 53 | |
| 54 // Returns true if |this| has higher priority than |other|. | |
| 55 bool has_higher_priority_than(const Entry& other) const; | |
| 56 | |
| 57 // Returns true if |this| equals |other|. | |
| 58 bool Equals(const Entry& other) const; | |
| 59 }; | |
| 60 | |
| 61 typedef std::map<std::string, Entry> PolicyMapType; | |
| 24 typedef PolicyMapType::const_iterator const_iterator; | 62 typedef PolicyMapType::const_iterator const_iterator; |
| 25 | 63 |
| 26 PolicyMap(); | 64 PolicyMap(); |
| 27 virtual ~PolicyMap(); | 65 virtual ~PolicyMap(); |
| 28 | 66 |
| 29 // Returns a weak reference to the value currently stored for key |policy|. | 67 // Returns a weak reference to the entry currently stored for key |policy|, |
| 30 // Ownership is retained by PolicyMap; callers should use Value::DeepCopy | 68 // or NULL if not found. Ownership is retained by the PolicyMap. |
| 31 // if they need a copy that they own themselves. | 69 const Entry* Get(const std::string& policy) const; |
| 32 // Returns NULL if the map does not contain a value for |policy|. | 70 |
| 33 const Value* Get(ConfigurationPolicyType policy) const; | 71 // Returns a weak reference to the value currently stored for key |policy|, |
| 72 // or NULL if not found. Ownership is retained by the PolicyMap. | |
| 73 // This is equivalent to Get(policy)->value, when it doesn't return NULL. | |
| 74 const Value* GetValue(const std::string& policy) const; | |
| 75 | |
| 34 // Takes ownership of |value|. Overwrites any existing value stored in the | 76 // Takes ownership of |value|. Overwrites any existing value stored in the |
| 35 // map for the key |policy|. | 77 // map for the key |policy|. |
| 36 void Set(ConfigurationPolicyType policy, Value* value); | 78 void Set(const std::string& policy, |
| 37 void Erase(ConfigurationPolicyType policy); | 79 PolicyLevel level, |
| 80 PolicyScope scope, | |
| 81 Value* value); | |
| 38 | 82 |
| 83 // Erase the given |policy|, if it exists in this map. | |
| 84 void Erase(const std::string& policy); | |
| 85 | |
| 86 // Swaps the internal representation of |this| with |other|. | |
| 39 void Swap(PolicyMap* other); | 87 void Swap(PolicyMap* other); |
| 88 | |
| 89 // |this| becomes a copy of |other|. Any existing policies are dropped. | |
| 40 void CopyFrom(const PolicyMap& other); | 90 void CopyFrom(const PolicyMap& other); |
| 41 | 91 |
| 42 // Similar to CopyFrom, but doesn't Clear() |this| before merging, and only | 92 // Similar to CopyFrom, but doesn't Clear() |this| before merging, and only |
| 43 // merges keys that aren't already contained in |this|. | 93 // merges keys that aren't already contained in |this|. |
| 94 | |
| 95 // Merges policies from |other| into |this|. Existing policies are only | |
| 96 // overridden by those in |other| if they have a higher priority, as defined | |
| 97 // by Entry::has_higher_priority_than(). If a policy is contained in both | |
| 98 // maps with the same priority, the current value in |this| is preserved. | |
| 44 void MergeFrom(const PolicyMap& other); | 99 void MergeFrom(const PolicyMap& other); |
| 45 | 100 |
| 46 // Loads the values in |policies| into this PolicyMap, mapped to their | 101 // Loads the values in |policies| into this PolicyMap, mapped to their |
| 47 // corresponding policy type. The policies to load, and their types, are | 102 // corresponding policy type. The policies to load, and their types, are |
| 48 // listed in |list|. | 103 // listed in |list|. All policies loaded will have |level| and |scope| in |
| 104 // their entries. | |
| 49 void LoadFrom(const DictionaryValue* policies, | 105 void LoadFrom(const DictionaryValue* policies, |
| 50 const PolicyDefinitionList* list); | 106 const PolicyDefinitionList* list, |
| 107 PolicyLevel level, | |
| 108 PolicyScope scope); | |
| 109 | |
| 110 // Compares this value map against |other| and stores all key names that have | |
| 111 // different values in |differing_keys|. This includes keys that are present | |
| 112 // only in one of the maps. |differing_keys| is not cleared before the keys | |
| 113 // are added. | |
| 114 void GetDifferingKeys(const PolicyMap& other, | |
| 115 std::set<std::string>* differing_keys) const; | |
| 116 | |
| 117 // Removes all policies that don't have the specified |level|. This is a | |
| 118 // temporary helper method, until mandatory and recommended levels are served | |
| 119 // by a single provider. | |
| 120 // TODO(joaodasilva): Remove this. http://crbug.com/108999 | |
| 121 void FilterLevel(PolicyLevel level); | |
| 51 | 122 |
| 52 bool Equals(const PolicyMap& other) const; | 123 bool Equals(const PolicyMap& other) const; |
| 53 bool empty() const; | 124 bool empty() const; |
| 54 size_t size() const; | 125 size_t size() const; |
| 55 | 126 |
| 56 const_iterator begin() const; | 127 const_iterator begin() const; |
| 57 const_iterator end() const; | 128 const_iterator end() const; |
| 58 void Clear(); | 129 void Clear(); |
| 59 | 130 |
| 60 private: | 131 private: |
| 61 // Helper function for Equals(...). | 132 // Helper function for Equals(). |
| 62 static bool MapEntryEquals(const PolicyMapType::value_type& a, | 133 static bool MapEntryEquals(const PolicyMapType::value_type& a, |
| 63 const PolicyMapType::value_type& b); | 134 const PolicyMapType::value_type& b); |
| 64 | 135 |
| 65 PolicyMapType map_; | 136 PolicyMapType map_; |
| 66 | 137 |
| 67 DISALLOW_COPY_AND_ASSIGN(PolicyMap); | 138 DISALLOW_COPY_AND_ASSIGN(PolicyMap); |
| 68 }; | 139 }; |
| 69 | 140 |
| 70 } // namespace policy | 141 } // namespace policy |
| 71 | 142 |
| 72 #endif // CHROME_BROWSER_POLICY_POLICY_MAP_H_ | 143 #endif // CHROME_BROWSER_POLICY_POLICY_MAP_H_ |
| OLD | NEW |