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 #ifndef CHROME_BROWSER_POLICY_POLICY_MAP_H_ 6 #define CHROME_BROWSER_POLICY_POLICY_MAP_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/memory/scoped_ptr.h" 13 #include "base/values.h" 14 #include "chrome/browser/policy/external_data_fetcher.h" 15 #include "chrome/browser/policy/policy_types.h" 16 17 namespace policy { 18 19 // A mapping of policy names to policy values for a given policy namespace. 20 class PolicyMap { 21 public: 22 // Each policy maps to an Entry which keeps the policy value as well as other 23 // relevant data about the policy. 24 struct Entry { 25 PolicyLevel level; 26 PolicyScope scope; 27 base::Value* value; 28 ExternalDataFetcher* external_data_fetcher; 29 30 Entry() 31 : level(POLICY_LEVEL_RECOMMENDED), 32 scope(POLICY_SCOPE_USER), 33 value(NULL), 34 external_data_fetcher(NULL) {} 35 36 // Returns true if |this| has higher priority than |other|. 37 bool has_higher_priority_than(const Entry& other) const; 38 39 // Returns true if |this| equals |other|. 40 bool Equals(const Entry& other) const; 41 }; 42 43 typedef std::map<std::string, Entry> PolicyMapType; 44 typedef PolicyMapType::const_iterator const_iterator; 45 46 PolicyMap(); 47 virtual ~PolicyMap(); 48 49 // Returns a weak reference to the entry currently stored for key |policy|, 50 // or NULL if not found. Ownership is retained by the PolicyMap. 51 const Entry* Get(const std::string& policy) const; 52 53 // Returns a weak reference to the value currently stored for key |policy|, 54 // or NULL if not found. Ownership is retained by the PolicyMap. 55 // This is equivalent to Get(policy)->value, when it doesn't return NULL. 56 const base::Value* GetValue(const std::string& policy) const; 57 58 // Takes ownership of |value| and |external_data_fetcher|. Overwrites any 59 // existing information stored in the map for the key |policy|. 60 void Set(const std::string& policy, 61 PolicyLevel level, 62 PolicyScope scope, 63 base::Value* value, 64 ExternalDataFetcher* external_data_fetcher); 65 66 // Erase the given |policy|, if it exists in this map. 67 void Erase(const std::string& policy); 68 69 // Swaps the internal representation of |this| with |other|. 70 void Swap(PolicyMap* other); 71 72 // |this| becomes a copy of |other|. Any existing policies are dropped. 73 void CopyFrom(const PolicyMap& other); 74 75 // Returns a copy of |this|. 76 scoped_ptr<PolicyMap> DeepCopy() const; 77 78 // Merges policies from |other| into |this|. Existing policies are only 79 // overridden by those in |other| if they have a higher priority, as defined 80 // by Entry::has_higher_priority_than(). If a policy is contained in both 81 // maps with the same priority, the current value in |this| is preserved. 82 void MergeFrom(const PolicyMap& other); 83 84 // Loads the values in |policies| into this PolicyMap. All policies loaded 85 // will have |level| and |scope| in their entries. Existing entries are 86 // replaced. 87 void LoadFrom(const base::DictionaryValue* policies, 88 PolicyLevel level, 89 PolicyScope scope); 90 91 // Compares this value map against |other| and stores all key names that have 92 // different values or reference different external data in |differing_keys|. 93 // This includes keys that are present only in one of the maps. 94 // |differing_keys| is not cleared before the keys are added. 95 void GetDifferingKeys(const PolicyMap& other, 96 std::set<std::string>* differing_keys) const; 97 98 // Removes all policies that don't have the specified |level|. This is a 99 // temporary helper method, until mandatory and recommended levels are served 100 // by a single provider. 101 // TODO(joaodasilva): Remove this. http://crbug.com/108999 102 void FilterLevel(PolicyLevel level); 103 104 bool Equals(const PolicyMap& other) const; 105 bool empty() const; 106 size_t size() const; 107 108 const_iterator begin() const; 109 const_iterator end() const; 110 void Clear(); 111 112 private: 113 // Helper function for Equals(). 114 static bool MapEntryEquals(const PolicyMapType::value_type& a, 115 const PolicyMapType::value_type& b); 116 117 PolicyMapType map_; 118 119 DISALLOW_COPY_AND_ASSIGN(PolicyMap); 120 }; 121 122 } // namespace policy 123 124 #endif // CHROME_BROWSER_POLICY_POLICY_MAP_H_ 125