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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// 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/schema_registry.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "extensions/buildflags/buildflags.h"
namespace policy {
SchemaRegistry::Observer::~Observer() = default;
SchemaRegistry::InternalObserver::~InternalObserver() = default;
SchemaRegistry::SchemaRegistry() : schema_map_(new SchemaMap) {
for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i)
domains_ready_[i] = false;
#if !BUILDFLAG(ENABLE_EXTENSIONS)
SetExtensionsDomainsReady();
#endif
}
SchemaRegistry::~SchemaRegistry() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto& observer : internal_observers_)
observer.OnSchemaRegistryShuttingDown(this);
}
void SchemaRegistry::RegisterComponent(const PolicyNamespace& ns,
const Schema& schema) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ComponentMap map;
map[ns.component_id] = schema;
RegisterComponents(ns.domain, map);
}
void SchemaRegistry::RegisterComponents(PolicyDomain domain,
const ComponentMap& components) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Don't issue notifications if nothing is being registered.
if (components.empty())
return;
// Assume that a schema was updated if the namespace was already registered
// before.
DomainMap map(schema_map_->GetDomains());
for (auto it = components.begin(); it != components.end(); ++it)
map[domain][it->first] = it->second;
schema_map_ = new SchemaMap(std::move(map));
Notify(true);
}
void SchemaRegistry::UnregisterComponent(const PolicyNamespace& ns) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DomainMap map(schema_map_->GetDomains());
if (map[ns.domain].erase(ns.component_id) != 0) {
schema_map_ = new SchemaMap(std::move(map));
Notify(false);
} else {
// Extension might be uninstalled before install so the associated policies
// are unregistered before registered. For example, a policy forced
// extension is removed from forced list during launch due to policy update.
DCHECK(ns.domain != POLICY_DOMAIN_CHROME);
}
}
bool SchemaRegistry::IsReady() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i) {
if (!domains_ready_[i])
return false;
}
return true;
}
void SchemaRegistry::SetDomainReady(PolicyDomain domain) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (domains_ready_[domain])
return;
domains_ready_[domain] = true;
if (IsReady()) {
for (auto& observer : observers_)
observer.OnSchemaRegistryReady();
}
}
void SchemaRegistry::SetAllDomainsReady() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i)
SetDomainReady(static_cast<PolicyDomain>(i));
}
void SchemaRegistry::SetExtensionsDomainsReady() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetDomainReady(POLICY_DOMAIN_EXTENSIONS);
SetDomainReady(POLICY_DOMAIN_SIGNIN_EXTENSIONS);
}
void SchemaRegistry::AddObserver(Observer* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(observer);
}
void SchemaRegistry::RemoveObserver(Observer* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(observer);
}
void SchemaRegistry::AddInternalObserver(InternalObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
internal_observers_.AddObserver(observer);
}
void SchemaRegistry::RemoveInternalObserver(InternalObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
internal_observers_.RemoveObserver(observer);
}
void SchemaRegistry::Notify(bool has_new_schemas) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto& observer : observers_)
observer.OnSchemaRegistryUpdated(has_new_schemas);
}
CombinedSchemaRegistry::CombinedSchemaRegistry()
: own_schema_map_(new SchemaMap) {
// The combined registry is always ready, since it can always start tracking
// another registry that is not ready yet and going from "ready" to "not
// ready" is not allowed.
SetAllDomainsReady();
}
CombinedSchemaRegistry::~CombinedSchemaRegistry() = default;
void CombinedSchemaRegistry::Track(SchemaRegistry* registry) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
registries_.insert(registry);
registry->AddObserver(this);
registry->AddInternalObserver(this);
// Recombine the maps only if the |registry| has any components other than
// POLICY_DOMAIN_CHROME.
if (registry->schema_map()->HasComponents())
Combine(true);
}
void CombinedSchemaRegistry::RegisterComponents(
PolicyDomain domain,
const ComponentMap& components) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DomainMap map(own_schema_map_->GetDomains());
for (auto it = components.begin(); it != components.end(); ++it)
map[domain][it->first] = it->second;
own_schema_map_ = new SchemaMap(std::move(map));
Combine(true);
}
void CombinedSchemaRegistry::UnregisterComponent(const PolicyNamespace& ns) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DomainMap map(own_schema_map_->GetDomains());
if (map[ns.domain].erase(ns.component_id) != 0) {
own_schema_map_ = new SchemaMap(std::move(map));
Combine(false);
} else {
NOTREACHED();
}
}
void CombinedSchemaRegistry::OnSchemaRegistryUpdated(bool has_new_schemas) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Combine(has_new_schemas);
}
void CombinedSchemaRegistry::OnSchemaRegistryShuttingDown(
SchemaRegistry* registry) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
registry->RemoveObserver(this);
registry->RemoveInternalObserver(this);
if (registries_.erase(registry) != 0) {
if (registry->schema_map()->HasComponents())
Combine(false);
} else {
NOTREACHED();
}
}
void CombinedSchemaRegistry::Combine(bool has_new_schemas) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If two registries publish a Schema for the same component then it's
// undefined which version gets in the combined registry.
//
// The common case is that both registries want policy for the same component,
// and the Schemas should be the same; in that case this makes no difference.
//
// But if the Schemas are different then one of the components is out of date.
// In that case the policy loaded will be valid only for one of them, until
// the outdated components are updated. This is a known limitation of the
// way policies are loaded currently, but isn't a problem worth fixing for
// the time being.
DomainMap map(own_schema_map_->GetDomains());
for (auto reg_it = registries_.begin(); reg_it != registries_.end();
++reg_it) {
const DomainMap& reg_domain_map = (*reg_it)->schema_map()->GetDomains();
for (auto domain_it = reg_domain_map.begin();
domain_it != reg_domain_map.end(); ++domain_it) {
const ComponentMap& reg_component_map = domain_it->second;
for (auto comp_it = reg_component_map.begin();
comp_it != reg_component_map.end(); ++comp_it) {
map[domain_it->first][comp_it->first] = comp_it->second;
}
}
}
schema_map_ = new SchemaMap(std::move(map));
Notify(has_new_schemas);
}
ForwardingSchemaRegistry::ForwardingSchemaRegistry(SchemaRegistry* wrapped)
: wrapped_(wrapped) {
schema_map_ = wrapped_->schema_map();
wrapped_->AddObserver(this);
wrapped_->AddInternalObserver(this);
UpdateReadiness();
}
ForwardingSchemaRegistry::~ForwardingSchemaRegistry() {
if (wrapped_) {
wrapped_->RemoveObserver(this);
wrapped_->RemoveInternalObserver(this);
}
}
void ForwardingSchemaRegistry::RegisterComponents(
PolicyDomain domain,
const ComponentMap& components) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// POLICY_DOMAIN_CHROME is skipped to avoid spurious updates when a new
// Profile is created. If the ForwardingSchemaRegistry is used outside
// device-level accounts then this should become configurable.
if (wrapped_ && domain != POLICY_DOMAIN_CHROME)
wrapped_->RegisterComponents(domain, components);
// Ignore otherwise.
}
void ForwardingSchemaRegistry::UnregisterComponent(const PolicyNamespace& ns) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (wrapped_)
wrapped_->UnregisterComponent(ns);
// Ignore otherwise.
}
void ForwardingSchemaRegistry::OnSchemaRegistryUpdated(bool has_new_schemas) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
schema_map_ = wrapped_->schema_map();
Notify(has_new_schemas);
}
void ForwardingSchemaRegistry::OnSchemaRegistryReady() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UpdateReadiness();
}
void ForwardingSchemaRegistry::OnSchemaRegistryShuttingDown(
SchemaRegistry* registry) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(wrapped_, registry);
wrapped_->RemoveObserver(this);
wrapped_->RemoveInternalObserver(this);
wrapped_ = nullptr;
// Keep serving the same |schema_map_|.
}
void ForwardingSchemaRegistry::UpdateReadiness() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (wrapped_->IsReady())
SetAllDomainsReady();
}
} // namespace policy
|