1 // Copyright 2013 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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "components/policy/core/common/external_data_fetcher.h"
15 #include "components/policy/core/common/policy_types.h"
16 #include "components/policy/policy_export.h"
20 // A mapping of policy names to policy values for a given policy namespace.
21 class POLICY_EXPORT PolicyMap
{
23 // Each policy maps to an Entry which keeps the policy value as well as other
24 // relevant data about the policy.
25 struct POLICY_EXPORT Entry
{
29 ExternalDataFetcher
* external_data_fetcher
;
33 // Deletes all members owned by |this|.
34 void DeleteOwnedMembers();
36 // Returns a copy of |this|.
37 scoped_ptr
<Entry
> DeepCopy() const;
39 // Returns true if |this| has higher priority than |other|.
40 bool has_higher_priority_than(const Entry
& other
) const;
42 // Returns true if |this| equals |other|.
43 bool Equals(const Entry
& other
) const;
46 typedef std::map
<std::string
, Entry
> PolicyMapType
;
47 typedef PolicyMapType::const_iterator const_iterator
;
52 // Returns a weak reference to the entry currently stored for key |policy|,
53 // or NULL if not found. Ownership is retained by the PolicyMap.
54 const Entry
* Get(const std::string
& policy
) const;
56 // Returns a weak reference to the value currently stored for key |policy|,
57 // or NULL if not found. Ownership is retained by the PolicyMap.
58 // This is equivalent to Get(policy)->value, when it doesn't return NULL.
59 const base::Value
* GetValue(const std::string
& policy
) const;
61 // Takes ownership of |value| and |external_data_fetcher|. Overwrites any
62 // existing information stored in the map for the key |policy|.
63 void Set(const std::string
& policy
,
67 ExternalDataFetcher
* external_data_fetcher
);
69 // Erase the given |policy|, if it exists in this map.
70 void Erase(const std::string
& policy
);
72 // Swaps the internal representation of |this| with |other|.
73 void Swap(PolicyMap
* other
);
75 // |this| becomes a copy of |other|. Any existing policies are dropped.
76 void CopyFrom(const PolicyMap
& other
);
78 // Returns a copy of |this|.
79 scoped_ptr
<PolicyMap
> DeepCopy() const;
81 // Merges policies from |other| into |this|. Existing policies are only
82 // overridden by those in |other| if they have a higher priority, as defined
83 // by Entry::has_higher_priority_than(). If a policy is contained in both
84 // maps with the same priority, the current value in |this| is preserved.
85 void MergeFrom(const PolicyMap
& other
);
87 // Loads the values in |policies| into this PolicyMap. All policies loaded
88 // will have |level| and |scope| in their entries. Existing entries are
90 void LoadFrom(const base::DictionaryValue
* policies
,
94 // Compares this value map against |other| and stores all key names that have
95 // different values or reference different external data in |differing_keys|.
96 // This includes keys that are present only in one of the maps.
97 // |differing_keys| is not cleared before the keys are added.
98 void GetDifferingKeys(const PolicyMap
& other
,
99 std::set
<std::string
>* differing_keys
) const;
101 // Removes all policies that don't have the specified |level|. This is a
102 // temporary helper method, until mandatory and recommended levels are served
103 // by a single provider.
104 // TODO(joaodasilva): Remove this. http://crbug.com/108999
105 void FilterLevel(PolicyLevel level
);
107 bool Equals(const PolicyMap
& other
) const;
111 const_iterator
begin() const;
112 const_iterator
end() const;
116 // Helper function for Equals().
117 static bool MapEntryEquals(const PolicyMapType::value_type
& a
,
118 const PolicyMapType::value_type
& b
);
122 DISALLOW_COPY_AND_ASSIGN(PolicyMap
);
125 } // namespace policy
127 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_