File: document_policy.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (212 lines) | stat: -rw-r--r-- 7,524 bytes parent folder | download | duplicates (6)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/permissions_policy/document_policy.h"

#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "net/http/structured_headers.h"

namespace blink {

// static
std::unique_ptr<DocumentPolicy> DocumentPolicy::CreateWithHeaderPolicy(
    const ParsedDocumentPolicy& header_policy) {
  DocumentPolicyFeatureState feature_defaults;
  for (const auto& entry : GetDocumentPolicyFeatureInfoMap())
    feature_defaults.emplace(entry.first, entry.second.default_value);
  return CreateWithHeaderPolicy(header_policy.feature_state,
                                header_policy.endpoint_map, feature_defaults);
}

// static
std::unique_ptr<DocumentPolicy> DocumentPolicy::CopyStateFrom(
    const DocumentPolicy* source) {
  if (!source)
    return nullptr;

  std::unique_ptr<DocumentPolicy> new_policy =
      DocumentPolicy::CreateWithHeaderPolicy(
          {/* header_policy */ {}, /* endpoint_map */ {}});

  new_policy->internal_feature_state_ = source->internal_feature_state_;
  new_policy->endpoint_map_ = source->endpoint_map_;
  return new_policy;
}

namespace {
net::structured_headers::Item PolicyValueToItem(const PolicyValue& value) {
  switch (value.Type()) {
    case mojom::PolicyValueType::kBool:
      return net::structured_headers::Item{value.BoolValue()};
    case mojom::PolicyValueType::kDecDouble:
      return net::structured_headers::Item{value.DoubleValue()};
    default:
      NOTREACHED();
  }
}

}  // namespace

// static
std::optional<std::string> DocumentPolicy::Serialize(
    const DocumentPolicyFeatureState& policy) {
  return DocumentPolicy::SerializeInternal(policy,
                                           GetDocumentPolicyFeatureInfoMap());
}

// static
std::optional<std::string> DocumentPolicy::SerializeInternal(
    const DocumentPolicyFeatureState& policy,
    const DocumentPolicyFeatureInfoMap& feature_info_map) {
  net::structured_headers::Dictionary root;

  std::vector<std::pair<mojom::DocumentPolicyFeature, PolicyValue>>
      sorted_policy(policy.begin(), policy.end());
  std::sort(sorted_policy.begin(), sorted_policy.end(),
            [&](const auto& a, const auto& b) {
              const std::string& feature_a =
                  feature_info_map.at(a.first).feature_name;
              const std::string& feature_b =
                  feature_info_map.at(b.first).feature_name;
              return feature_a < feature_b;
            });

  for (const auto& policy_entry : sorted_policy) {
    const mojom::DocumentPolicyFeature feature = policy_entry.first;
    const std::string& feature_name = feature_info_map.at(feature).feature_name;
    const PolicyValue& value = policy_entry.second;

    root[feature_name] = net::structured_headers::ParameterizedMember(
        PolicyValueToItem(value), /* parameters */ {});
  }

  return net::structured_headers::SerializeDictionary(root);
}

// static
DocumentPolicyFeatureState DocumentPolicy::MergeFeatureState(
    const DocumentPolicyFeatureState& base_policy,
    const DocumentPolicyFeatureState& override_policy) {
  DocumentPolicyFeatureState result;
  auto i1 = base_policy.begin();
  auto i2 = override_policy.begin();

  // Because std::map is by default ordered in ascending order based on key
  // value, we can run 2 iterators simultaneously through both maps to merge
  // them.
  while (i1 != base_policy.end() || i2 != override_policy.end()) {
    if (i1 == base_policy.end()) {
      result.insert(*i2);
      i2++;
    } else if (i2 == override_policy.end()) {
      result.insert(*i1);
      i1++;
    } else {
      if (i1->first == i2->first) {
        const PolicyValue& base_value = i1->second;
        const PolicyValue& override_value = i2->second;
        // When policy value has strictness ordering e.g. boolean, take the
        // stricter one. In this case a.IsCompatibleWith(b) means a is eq or
        // stricter than b.
        // When policy value does not have strictness ordering, e.g. enum,
        // take override_value. In this case a.IsCompatibleWith(b) means
        // a != b.
        const PolicyValue& new_value =
            base_value.IsCompatibleWith(override_value) ? base_value
                                                        : override_value;
        result.emplace(i1->first, new_value);
        i1++;
        i2++;
      } else if (i1->first < i2->first) {
        result.insert(*i1);
        i1++;
      } else {
        result.insert(*i2);
        i2++;
      }
    }
  }

  return result;
}

bool DocumentPolicy::IsFeatureEnabled(
    mojom::DocumentPolicyFeature feature) const {
  mojom::PolicyValueType feature_type =
      GetDocumentPolicyFeatureInfoMap().at(feature).default_value.Type();
  return IsFeatureEnabled(feature,
                          PolicyValue::CreateMaxPolicyValue(feature_type));
}

bool DocumentPolicy::IsFeatureEnabled(
    mojom::DocumentPolicyFeature feature,
    const PolicyValue& threshold_value) const {
  return threshold_value.IsCompatibleWith(GetFeatureValue(feature));
}

PolicyValue DocumentPolicy::GetFeatureValue(
    mojom::DocumentPolicyFeature feature) const {
  return internal_feature_state_[static_cast<size_t>(feature)];
}

const std::optional<std::string> DocumentPolicy::GetFeatureEndpoint(
    mojom::DocumentPolicyFeature feature) const {
  auto endpoint_it = endpoint_map_.find(feature);
  if (endpoint_it != endpoint_map_.end()) {
    return endpoint_it->second;
  } else {
    return std::nullopt;
  }
}

void DocumentPolicy::UpdateFeatureState(
    const DocumentPolicyFeatureState& feature_state) {
  for (const auto& feature_and_value : feature_state) {
    internal_feature_state_[static_cast<size_t>(feature_and_value.first)] =
        feature_and_value.second;
  }
}

DocumentPolicy::DocumentPolicy(const DocumentPolicyFeatureState& header_policy,
                               const FeatureEndpointMap& endpoint_map,
                               const DocumentPolicyFeatureState& defaults)
    : endpoint_map_(endpoint_map) {
  // Fill the internal feature state with default value first,
  // and overwrite the value if it is specified in the header.
  UpdateFeatureState(defaults);
  UpdateFeatureState(header_policy);
}

// static
std::unique_ptr<DocumentPolicy> DocumentPolicy::CreateWithHeaderPolicy(
    const DocumentPolicyFeatureState& header_policy,
    const FeatureEndpointMap& endpoint_map,
    const DocumentPolicyFeatureState& defaults) {
  std::unique_ptr<DocumentPolicy> new_policy = base::WrapUnique(
      new DocumentPolicy(header_policy, endpoint_map, defaults));
  return new_policy;
}

// static
bool DocumentPolicy::IsPolicyCompatible(
    const DocumentPolicyFeatureState& required_policy,
    const DocumentPolicyFeatureState& incoming_policy) {
  for (const auto& required_entry : required_policy) {
    const auto& feature = required_entry.first;
    const auto& required_value = required_entry.second;
    // Use default value when incoming policy does not specify a value.
    const auto incoming_entry = incoming_policy.find(feature);
    const auto& incoming_value =
        incoming_entry != incoming_policy.end()
            ? incoming_entry->second
            : GetDocumentPolicyFeatureInfoMap().at(feature).default_value;

    if (!incoming_value.IsCompatibleWith(required_value))
      return false;
  }
  return true;
}

}  // namespace blink