summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/policy_service.cc
blob: 0f65c3b00de66fb4f0fd9214e693c03d25b3a0a7 (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
// Copyright (c) 2012 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_service.h"

#include "base/values.h"

namespace policy {

PolicyNamespace::PolicyNamespace() {}

PolicyNamespace::PolicyNamespace(PolicyDomain domain,
                                 const std::string& component_id)
    : domain(domain),
      component_id(component_id) {}

PolicyNamespace::PolicyNamespace(const PolicyNamespace& other)
    : domain(other.domain),
      component_id(other.component_id) {}

PolicyNamespace::~PolicyNamespace() {}

PolicyNamespace& PolicyNamespace::operator=(const PolicyNamespace& other) {
  domain = other.domain;
  component_id = other.component_id;
  return *this;
}

bool PolicyNamespace::operator<(const PolicyNamespace& other) const {
  return domain < other.domain ||
         (domain == other.domain && component_id < other.component_id);
}

bool PolicyNamespace::operator==(const PolicyNamespace& other) const {
  return domain == other.domain && component_id == other.component_id;
}

bool PolicyNamespace::operator!=(const PolicyNamespace& other) const {
  return !(*this == other);
}

PolicyChangeRegistrar::PolicyChangeRegistrar(PolicyService* policy_service,
                                             const PolicyNamespace& ns)
    : policy_service_(policy_service),
      ns_(ns) {}

PolicyChangeRegistrar::~PolicyChangeRegistrar() {
  if (!callback_map_.empty())
    policy_service_->RemoveObserver(ns_.domain, this);
}

void PolicyChangeRegistrar::Observe(const std::string& policy_name,
                                    const UpdateCallback& callback) {
  if (callback_map_.empty())
    policy_service_->AddObserver(ns_.domain, this);
  callback_map_[policy_name] = callback;
}

void PolicyChangeRegistrar::OnPolicyUpdated(const PolicyNamespace& ns,
                                            const PolicyMap& previous,
                                            const PolicyMap& current) {
  if (ns != ns_)
    return;
  for (CallbackMap::iterator it = callback_map_.begin();
       it != callback_map_.end(); ++it) {
    const Value* prev = previous.GetValue(it->first);
    const Value* cur = current.GetValue(it->first);
    if (!base::Value::Equals(prev, cur))
      it->second.Run(prev, cur);
  }
}

}  // namespace policy