[email protected] | 61dc220c | 2012-01-05 19:03:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_POLICY_POLICY_MAP_H_ |
| 6 | #define CHROME_BROWSER_POLICY_POLICY_MAP_H_ |
[email protected] | f04d73f | 2011-10-25 15:07:12 | [diff] [blame] | 7 | #pragma once |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 8 | |
| 9 | #include <map> |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 12 | |
| 13 | #include "base/values.h" |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 14 | |
| 15 | namespace policy { |
| 16 | |
[email protected] | aea9fa1 | 2011-10-13 22:12:39 | [diff] [blame] | 17 | struct PolicyDefinitionList; |
| 18 | |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 19 | // The level of a policy determines its enforceability and whether users can |
| 20 | // override it or not. The values are listed in increasing order of priority. |
| 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. |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 41 | class PolicyMap { |
| 42 | public: |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 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() |
| 51 | : level(POLICY_LEVEL_RECOMMENDED), |
| 52 | scope(POLICY_SCOPE_USER), |
| 53 | value(NULL) {} |
| 54 | |
| 55 | // Returns true if |this| has higher priority than |other|. |
| 56 | bool has_higher_priority_than(const Entry& other) const; |
| 57 | |
| 58 | // Returns true if |this| equals |other|. |
| 59 | bool Equals(const Entry& other) const; |
| 60 | }; |
| 61 | |
| 62 | typedef std::map<std::string, Entry> PolicyMapType; |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 63 | typedef PolicyMapType::const_iterator const_iterator; |
| 64 | |
| 65 | PolicyMap(); |
| 66 | virtual ~PolicyMap(); |
| 67 | |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 68 | // Returns a weak reference to the entry currently stored for key |policy|, |
| 69 | // or NULL if not found. Ownership is retained by the PolicyMap. |
| 70 | const Entry* Get(const std::string& policy) const; |
| 71 | |
| 72 | // Returns a weak reference to the value currently stored for key |policy|, |
| 73 | // or NULL if not found. Ownership is retained by the PolicyMap. |
| 74 | // This is equivalent to Get(policy)->value, when it doesn't return NULL. |
| 75 | const Value* GetValue(const std::string& policy) const; |
| 76 | |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 77 | // Takes ownership of |value|. Overwrites any existing value stored in the |
| 78 | // map for the key |policy|. |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 79 | void Set(const std::string& policy, |
| 80 | PolicyLevel level, |
| 81 | PolicyScope scope, |
| 82 | Value* value); |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 83 | |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 84 | // Erase the given |policy|, if it exists in this map. |
| 85 | void Erase(const std::string& policy); |
| 86 | |
| 87 | // Swaps the internal representation of |this| with |other|. |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 88 | void Swap(PolicyMap* other); |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 89 | |
| 90 | // |this| becomes a copy of |other|. Any existing policies are dropped. |
[email protected] | 5e61ad9 | 2011-09-12 16:47:25 | [diff] [blame] | 91 | void CopyFrom(const PolicyMap& other); |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 92 | |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 93 | // Merges policies from |other| into |this|. Existing policies are only |
| 94 | // overridden by those in |other| if they have a higher priority, as defined |
| 95 | // by Entry::has_higher_priority_than(). If a policy is contained in both |
| 96 | // maps with the same priority, the current value in |this| is preserved. |
[email protected] | 61dc220c | 2012-01-05 19:03:10 | [diff] [blame] | 97 | void MergeFrom(const PolicyMap& other); |
| 98 | |
[email protected] | 259969e | 2012-01-24 15:52:15 | [diff] [blame] | 99 | // Loads the values in |policies| into this PolicyMap. All policies loaded |
| 100 | // will have |level| and |scope| in their entries. Existing entries are |
| 101 | // replaced. |
[email protected] | b6e695a9 | 2011-09-29 15:08:33 | [diff] [blame] | 102 | void LoadFrom(const DictionaryValue* policies, |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 103 | PolicyLevel level, |
| 104 | PolicyScope scope); |
| 105 | |
| 106 | // Compares this value map against |other| and stores all key names that have |
| 107 | // different values in |differing_keys|. This includes keys that are present |
| 108 | // only in one of the maps. |differing_keys| is not cleared before the keys |
| 109 | // are added. |
| 110 | void GetDifferingKeys(const PolicyMap& other, |
| 111 | std::set<std::string>* differing_keys) const; |
| 112 | |
| 113 | // Removes all policies that don't have the specified |level|. This is a |
| 114 | // temporary helper method, until mandatory and recommended levels are served |
| 115 | // by a single provider. |
| 116 | // TODO(joaodasilva): Remove this. http://crbug.com/108999 |
| 117 | void FilterLevel(PolicyLevel level); |
[email protected] | b6e695a9 | 2011-09-29 15:08:33 | [diff] [blame] | 118 | |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 119 | bool Equals(const PolicyMap& other) const; |
| 120 | bool empty() const; |
| 121 | size_t size() const; |
| 122 | |
| 123 | const_iterator begin() const; |
| 124 | const_iterator end() const; |
| 125 | void Clear(); |
| 126 | |
| 127 | private: |
[email protected] | 4cbb342 | 2012-01-22 16:25:52 | [diff] [blame] | 128 | // Helper function for Equals(). |
[email protected] | f8636972 | 2011-02-17 13:25:23 | [diff] [blame] | 129 | static bool MapEntryEquals(const PolicyMapType::value_type& a, |
| 130 | const PolicyMapType::value_type& b); |
| 131 | |
| 132 | PolicyMapType map_; |
| 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(PolicyMap); |
| 135 | }; |
| 136 | |
| 137 | } // namespace policy |
| 138 | |
| 139 | #endif // CHROME_BROWSER_POLICY_POLICY_MAP_H_ |