blob: 549673bded58e78a69c34a1d64a5c5dbf0484d20 [file] [log] [blame]
[email protected]61dc220c2012-01-05 19:03:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f86369722011-02-17 13:25:232// 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]f04d73f2011-10-25 15:07:127#pragma once
[email protected]f86369722011-02-17 13:25:238
9#include <map>
[email protected]4cbb3422012-01-22 16:25:5210#include <set>
11#include <string>
[email protected]f86369722011-02-17 13:25:2312
13#include "base/values.h"
[email protected]f86369722011-02-17 13:25:2314
15namespace policy {
16
[email protected]aea9fa12011-10-13 22:12:3917struct PolicyDefinitionList;
18
[email protected]4cbb3422012-01-22 16:25:5219// 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.
21enum 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.
32enum 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]f86369722011-02-17 13:25:2341class PolicyMap {
42 public:
[email protected]4cbb3422012-01-22 16:25:5243 // 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]f86369722011-02-17 13:25:2363 typedef PolicyMapType::const_iterator const_iterator;
64
65 PolicyMap();
66 virtual ~PolicyMap();
67
[email protected]4cbb3422012-01-22 16:25:5268 // 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]f86369722011-02-17 13:25:2377 // Takes ownership of |value|. Overwrites any existing value stored in the
78 // map for the key |policy|.
[email protected]4cbb3422012-01-22 16:25:5279 void Set(const std::string& policy,
80 PolicyLevel level,
81 PolicyScope scope,
82 Value* value);
[email protected]f86369722011-02-17 13:25:2383
[email protected]4cbb3422012-01-22 16:25:5284 // 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]f86369722011-02-17 13:25:2388 void Swap(PolicyMap* other);
[email protected]4cbb3422012-01-22 16:25:5289
90 // |this| becomes a copy of |other|. Any existing policies are dropped.
[email protected]5e61ad92011-09-12 16:47:2591 void CopyFrom(const PolicyMap& other);
[email protected]f86369722011-02-17 13:25:2392
[email protected]4cbb3422012-01-22 16:25:5293 // 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]61dc220c2012-01-05 19:03:1097 void MergeFrom(const PolicyMap& other);
98
[email protected]259969e2012-01-24 15:52:1599 // 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]b6e695a92011-09-29 15:08:33102 void LoadFrom(const DictionaryValue* policies,
[email protected]4cbb3422012-01-22 16:25:52103 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]b6e695a92011-09-29 15:08:33118
[email protected]f86369722011-02-17 13:25:23119 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]4cbb3422012-01-22 16:25:52128 // Helper function for Equals().
[email protected]f86369722011-02-17 13:25:23129 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_