File: policy_bundle.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,381 bytes parent folder | download | duplicates (8)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/policy_bundle.h"

#include "base/check.h"
#include "base/no_destructor.h"
#include "base/notreached.h"

namespace policy {

namespace {
// An empty PolicyMap that is returned by const Get() for namespaces that
// do not exist in |policy_bundle_|.
const PolicyMap& GetEmptyPolicyMap() {
  static const base::NoDestructor<PolicyMap> kEmpty;
  return *kEmpty;
}
}  // namespace

PolicyBundle::PolicyBundle() = default;
PolicyBundle::PolicyBundle(PolicyBundle&&) = default;
PolicyBundle& PolicyBundle::operator=(PolicyBundle&&) = default;

PolicyBundle::~PolicyBundle() = default;

PolicyMap& PolicyBundle::Get(const PolicyNamespace& ns) {
  DCHECK(ns.domain != POLICY_DOMAIN_CHROME || ns.component_id.empty());
  return policy_bundle_[ns];
}

const PolicyMap& PolicyBundle::Get(const PolicyNamespace& ns) const {
  DCHECK(ns.domain != POLICY_DOMAIN_CHROME || ns.component_id.empty());
  const auto it = policy_bundle_.find(ns);
  return it == end() ? GetEmptyPolicyMap() : it->second;
}

PolicyBundle PolicyBundle::Clone() const {
  PolicyBundle clone;
  for (const auto& entry : policy_bundle_) {
    clone.policy_bundle_[entry.first] = entry.second.Clone();
  }
  return clone;
}

void PolicyBundle::MergeFrom(const PolicyBundle& other) {
  DCHECK_NE(this, &other);

  // Iterate over both |this| and |other| in order; skip what's extra in |this|,
  // add what's missing, and merge the namespaces in common.
  auto it_this = policy_bundle_.begin();
  auto end_this = policy_bundle_.end();
  auto it_other = other.begin();
  auto end_other = other.end();

  while (it_this != end_this && it_other != end_other) {
    if (it_this->first == it_other->first) {
      // Same namespace: merge existing PolicyMaps.
      it_this->second.MergeFrom(it_other->second);
      ++it_this;
      ++it_other;
    } else if (it_this->first < it_other->first) {
      // |this| has a PolicyMap that |other| doesn't; skip it.
      ++it_this;
    } else if (it_other->first < it_this->first) {
      // |other| has a PolicyMap that |this| doesn't; copy it.
      policy_bundle_[it_other->first] = it_other->second.Clone();
      ++it_other;
    } else {
      NOTREACHED();
    }
  }

  // Add extra PolicyMaps at the end.
  while (it_other != end_other) {
    policy_bundle_[it_other->first] = it_other->second.Clone();
    ++it_other;
  }
}

bool PolicyBundle::Equals(const PolicyBundle& other) const {
  // Equals() has the peculiarity that an entry with an empty PolicyMap equals
  // an non-existent entry. This handles usage of non-const Get() that doesn't
  // insert any policies.
  auto it_this = begin();
  auto it_other = other.begin();

  while (true) {
    // Skip empty PolicyMaps.
    while (it_this != end() && it_this->second.empty())
      ++it_this;
    while (it_other != other.end() && it_other->second.empty())
      ++it_other;
    if (it_this == end() || it_other == other.end())
      break;
    if (it_this->first != it_other->first ||
        !it_this->second.Equals(it_other->second)) {
      return false;
    }
    ++it_this;
    ++it_other;
  }
  return it_this == end() && it_other == other.end();
}

void PolicyBundle::Clear() {
  policy_bundle_.clear();
}

}  // namespace policy