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
;
31 // For debugging and displaying only. Set by provider delivering the policy.
36 // Deletes all members owned by |this|.
37 void DeleteOwnedMembers();
39 // Returns a copy of |this|.
40 scoped_ptr
<Entry
> DeepCopy() const;
42 // Returns true if |this| has higher priority than |other|.
43 bool has_higher_priority_than(const Entry
& other
) const;
45 // Returns true if |this| equals |other|.
46 bool Equals(const Entry
& other
) const;
49 typedef std::map
<std::string
, Entry
> PolicyMapType
;
50 typedef PolicyMapType::const_iterator const_iterator
;
55 // Returns a weak reference to the entry currently stored for key |policy|,
56 // or NULL if not found. Ownership is retained by the PolicyMap.
57 const Entry
* Get(const std::string
& policy
) const;
59 // Returns a weak reference to the value currently stored for key |policy|,
60 // or NULL if not found. Ownership is retained by the PolicyMap.
61 // This is equivalent to Get(policy)->value, when it doesn't return NULL.
62 const base::Value
* GetValue(const std::string
& policy
) const;
64 // Takes ownership of |value| and |external_data_fetcher|. Overwrites any
65 // existing information stored in the map for the key |policy|.
66 void Set(const std::string
& policy
,
71 ExternalDataFetcher
* external_data_fetcher
);
73 // Erase the given |policy|, if it exists in this map.
74 void Erase(const std::string
& policy
);
76 // Swaps the internal representation of |this| with |other|.
77 void Swap(PolicyMap
* other
);
79 // |this| becomes a copy of |other|. Any existing policies are dropped.
80 void CopyFrom(const PolicyMap
& other
);
82 // Returns a copy of |this|.
83 scoped_ptr
<PolicyMap
> DeepCopy() const;
85 // Merges policies from |other| into |this|. Existing policies are only
86 // overridden by those in |other| if they have a higher priority, as defined
87 // by Entry::has_higher_priority_than(). If a policy is contained in both
88 // maps with the same priority, the current value in |this| is preserved.
89 void MergeFrom(const PolicyMap
& other
);
91 // Loads the values in |policies| into this PolicyMap. All policies loaded
92 // will have |level|, |scope| and |source| in their entries. Existing entries
94 void LoadFrom(const base::DictionaryValue
* policies
,
99 // Compares this value map against |other| and stores all key names that have
100 // different values or reference different external data in |differing_keys|.
101 // This includes keys that are present only in one of the maps.
102 // |differing_keys| is not cleared before the keys are added.
103 void GetDifferingKeys(const PolicyMap
& other
,
104 std::set
<std::string
>* differing_keys
) const;
106 // Removes all policies that don't have the specified |level|. This is a
107 // temporary helper method, until mandatory and recommended levels are served
108 // by a single provider.
109 // TODO(joaodasilva): Remove this. http://crbug.com/108999
110 void FilterLevel(PolicyLevel level
);
112 bool Equals(const PolicyMap
& other
) const;
116 const_iterator
begin() const;
117 const_iterator
end() const;
121 // Helper function for Equals().
122 static bool MapEntryEquals(const PolicyMapType::value_type
& a
,
123 const PolicyMapType::value_type
& b
);
127 DISALLOW_COPY_AND_ASSIGN(PolicyMap
);
130 } // namespace policy
132 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_