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.
4 
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_BUNDLE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_BUNDLE_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 
12 #include "base/macros.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "components/policy/core/common/policy_namespace.h"
15 #include "components/policy/policy_export.h"
16 
17 namespace policy {
18 
19 // Maps policy namespaces to PolicyMaps.
20 class POLICY_EXPORT PolicyBundle {
21  public:
22   using MapType = std::map<PolicyNamespace, std::unique_ptr<PolicyMap>>;
23   using iterator = MapType::iterator;
24   using const_iterator = MapType::const_iterator;
25 
26   PolicyBundle();
27   virtual ~PolicyBundle();
28 
29   // Returns the PolicyMap for namespace |ns|. Creates a new map if necessary.
30   PolicyMap& Get(const PolicyNamespace& ns);
31   const PolicyMap& Get(const PolicyNamespace& ns) const;
32 
33   // Swaps the internal representation of |this| with |other|.
34   void Swap(PolicyBundle* other);
35 
36   // |this| becomes a copy of |other|. Any existing PolicyMaps are dropped.
37   void CopyFrom(const PolicyBundle& other);
38 
39   // Merges the PolicyMaps of |this| with those of |other| for each namespace
40   // in common. Also adds copies of the (namespace, PolicyMap) pairs in |other|
41   // that don't have an entry in |this|.
42   // Each policy in each PolicyMap is replaced only if the policy from |other|
43   // has a higher priority.
44   // See PolicyMap::MergeFrom for details on merging individual PolicyMaps.
45   void MergeFrom(const PolicyBundle& other);
46 
47   // Returns true if |other| has the same keys and value as |this|.
48   bool Equals(const PolicyBundle& other) const;
49 
50   // Returns iterators to the beginning and end of the underlying container.
begin()51   iterator begin() { return policy_bundle_.begin(); }
end()52   iterator end() { return policy_bundle_.end(); }
53 
54   // These can be used to iterate over and read the PolicyMaps, but not to
55   // modify them.
begin()56   const_iterator begin() const { return policy_bundle_.begin(); }
end()57   const_iterator end() const { return policy_bundle_.end(); }
58 
59   // Erases all the existing pairs.
60   void Clear();
61 
62  private:
63   MapType policy_bundle_;
64 
65   // An empty PolicyMap that is returned by const Get() for namespaces that
66   // do not exist in |policy_bundle_|.
67   const PolicyMap kEmpty_;
68 
69   DISALLOW_COPY_AND_ASSIGN(PolicyBundle);
70 };
71 
72 }  // namespace policy
73 
74 #endif  // COMPONENTS_POLICY_CORE_COMMON_POLICY_BUNDLE_H_
75