summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/policy_map.cc
blob: 3e9e4e60ec6298043351d31b0e54c50e7e166855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/policy_map.h"

#include <algorithm>

#include "base/stl_util.h"
#include "policy/policy_constants.h"

namespace policy {

PolicyMap::PolicyMap() {
}

PolicyMap::~PolicyMap() {
  Clear();
}

const Value* PolicyMap::Get(ConfigurationPolicyType policy) const {
  PolicyMapType::const_iterator entry = map_.find(policy);
  return entry == map_.end() ? NULL : entry->second;
}

void PolicyMap::Set(ConfigurationPolicyType policy, Value* value) {
  std::swap(map_[policy], value);
  delete value;
}

void PolicyMap::Erase(ConfigurationPolicyType policy) {
  const const_iterator entry = map_.find(policy);
  if (entry != map_.end()) {
    delete entry->second;
    map_.erase(entry->first);
  }
}

void PolicyMap::Swap(PolicyMap* other) {
  map_.swap(other->map_);
}

void PolicyMap::CopyFrom(const PolicyMap& other) {
  Clear();
  for (const_iterator i = other.begin(); i != other.end(); ++i) {
    Set(i->first, i->second->DeepCopy());
  }
}

void PolicyMap::LoadFrom(
    const DictionaryValue* policies,
    const PolicyDefinitionList* list) {
  const PolicyDefinitionList::Entry* entry;
  for (entry = list->begin; entry != list->end; ++entry) {
    Value* value;
    if (policies->Get(entry->name, &value))
      Set(entry->policy_type, value->DeepCopy());
  }
}

bool PolicyMap::Equals(const PolicyMap& other) const {
  return other.map_.size() == map_.size() &&
      std::equal(map_.begin(), map_.end(), other.map_.begin(), MapEntryEquals);
}

bool PolicyMap::empty() const {
  return map_.empty();
}

size_t PolicyMap::size() const {
  return map_.size();
}

PolicyMap::const_iterator PolicyMap::begin() const {
  return map_.begin();
}

PolicyMap::const_iterator PolicyMap::end() const {
  return map_.end();
}

void PolicyMap::Clear() {
  STLDeleteValues(&map_);
}

// static
bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a,
                               const PolicyMap::PolicyMapType::value_type& b) {
  return a.first == b.first && Value::Equals(a.second, b.second);
}

}  // namespace policy