OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/schema_registry.h" | 5 #include "chrome/browser/policy/schema_registry.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace policy { | 9 namespace policy { |
10 | 10 |
11 SchemaRegistry::Observer::~Observer() {} | 11 SchemaRegistry::Observer::~Observer() {} |
12 | 12 |
13 SchemaRegistry::SchemaRegistry() : schema_map_(new SchemaMap) {} | 13 SchemaRegistry::SchemaRegistry() : schema_map_(new SchemaMap) { |
| 14 for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i) |
| 15 domains_ready_[i] = false; |
| 16 #if !defined(ENABLE_EXTENSIONS) |
| 17 domains_ready_[POLICY_DOMAIN_EXTENSIONS] = true; |
| 18 #endif |
| 19 } |
14 | 20 |
15 SchemaRegistry::~SchemaRegistry() {} | 21 SchemaRegistry::~SchemaRegistry() {} |
16 | 22 |
17 void SchemaRegistry::RegisterComponent(const PolicyNamespace& ns, | 23 void SchemaRegistry::RegisterComponent(const PolicyNamespace& ns, |
18 const Schema& schema) { | 24 const Schema& schema) { |
19 ComponentMap map; | 25 ComponentMap map; |
20 map[ns.component_id] = schema; | 26 map[ns.component_id] = schema; |
21 RegisterComponents(ns.domain, map); | 27 RegisterComponents(ns.domain, map); |
22 } | 28 } |
23 | 29 |
(...skipping 13 matching lines...) Expand all Loading... |
37 void SchemaRegistry::UnregisterComponent(const PolicyNamespace& ns) { | 43 void SchemaRegistry::UnregisterComponent(const PolicyNamespace& ns) { |
38 DomainMap map(schema_map_->GetDomains()); | 44 DomainMap map(schema_map_->GetDomains()); |
39 if (map[ns.domain].erase(ns.component_id) != 0) { | 45 if (map[ns.domain].erase(ns.component_id) != 0) { |
40 schema_map_ = new SchemaMap(map); | 46 schema_map_ = new SchemaMap(map); |
41 Notify(false); | 47 Notify(false); |
42 } else { | 48 } else { |
43 NOTREACHED(); | 49 NOTREACHED(); |
44 } | 50 } |
45 } | 51 } |
46 | 52 |
| 53 bool SchemaRegistry::IsReady() const { |
| 54 for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i) { |
| 55 if (!domains_ready_[i]) |
| 56 return false; |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
| 61 void SchemaRegistry::SetReady(PolicyDomain domain) { |
| 62 if (domains_ready_[domain]) |
| 63 return; |
| 64 domains_ready_[domain] = true; |
| 65 if (IsReady()) |
| 66 FOR_EACH_OBSERVER(Observer, observers_, OnSchemaRegistryReady()); |
| 67 } |
| 68 |
47 void SchemaRegistry::AddObserver(Observer* observer) { | 69 void SchemaRegistry::AddObserver(Observer* observer) { |
48 observers_.AddObserver(observer); | 70 observers_.AddObserver(observer); |
49 } | 71 } |
50 | 72 |
51 void SchemaRegistry::RemoveObserver(Observer* observer) { | 73 void SchemaRegistry::RemoveObserver(Observer* observer) { |
52 observers_.RemoveObserver(observer); | 74 observers_.RemoveObserver(observer); |
53 } | 75 } |
54 | 76 |
55 void SchemaRegistry::Notify(bool has_new_schemas) { | 77 void SchemaRegistry::Notify(bool has_new_schemas) { |
56 FOR_EACH_OBSERVER( | 78 FOR_EACH_OBSERVER( |
57 Observer, observers_, OnSchemaRegistryUpdated(has_new_schemas)); | 79 Observer, observers_, OnSchemaRegistryUpdated(has_new_schemas)); |
58 } | 80 } |
59 | 81 |
60 bool SchemaRegistry::HasObservers() const { | 82 bool SchemaRegistry::HasObservers() const { |
61 return observers_.might_have_observers(); | 83 return observers_.might_have_observers(); |
62 } | 84 } |
63 | 85 |
64 CombinedSchemaRegistry::CombinedSchemaRegistry() | 86 CombinedSchemaRegistry::CombinedSchemaRegistry() |
65 : own_schema_map_(new SchemaMap) {} | 87 : own_schema_map_(new SchemaMap) { |
| 88 // The combined registry is always ready, since it can always start tracking |
| 89 // another registry that is not ready yet and going from "ready" to "not |
| 90 // ready" is not allowed. |
| 91 for (int i = 0; i < POLICY_DOMAIN_SIZE; ++i) |
| 92 SetReady(static_cast<PolicyDomain>(i)); |
| 93 } |
66 | 94 |
67 CombinedSchemaRegistry::~CombinedSchemaRegistry() {} | 95 CombinedSchemaRegistry::~CombinedSchemaRegistry() {} |
68 | 96 |
69 void CombinedSchemaRegistry::Track(SchemaRegistry* registry) { | 97 void CombinedSchemaRegistry::Track(SchemaRegistry* registry) { |
70 registries_.insert(registry); | 98 registries_.insert(registry); |
71 registry->AddObserver(this); | 99 registry->AddObserver(this); |
72 // Recombine the maps only if the |registry| has any components other than | 100 // Recombine the maps only if the |registry| has any components other than |
73 // POLICY_DOMAIN_CHROME. | 101 // POLICY_DOMAIN_CHROME. |
74 if (registry->schema_map()->HasComponents()) | 102 if (registry->schema_map()->HasComponents()) |
75 Combine(true); | 103 Combine(true); |
(...skipping 28 matching lines...) Expand all Loading... |
104 Combine(false); | 132 Combine(false); |
105 } else { | 133 } else { |
106 NOTREACHED(); | 134 NOTREACHED(); |
107 } | 135 } |
108 } | 136 } |
109 | 137 |
110 void CombinedSchemaRegistry::OnSchemaRegistryUpdated(bool has_new_schemas) { | 138 void CombinedSchemaRegistry::OnSchemaRegistryUpdated(bool has_new_schemas) { |
111 Combine(has_new_schemas); | 139 Combine(has_new_schemas); |
112 } | 140 } |
113 | 141 |
| 142 void CombinedSchemaRegistry::OnSchemaRegistryReady() { |
| 143 // Ignore. |
| 144 } |
| 145 |
114 void CombinedSchemaRegistry::Combine(bool has_new_schemas) { | 146 void CombinedSchemaRegistry::Combine(bool has_new_schemas) { |
115 // If two registries publish a Schema for the same component then it's | 147 // If two registries publish a Schema for the same component then it's |
116 // undefined which version gets in the combined registry. | 148 // undefined which version gets in the combined registry. |
117 // | 149 // |
118 // The common case is that both registries want policy for the same component, | 150 // The common case is that both registries want policy for the same component, |
119 // and the Schemas should be the same; in that case this makes no difference. | 151 // and the Schemas should be the same; in that case this makes no difference. |
120 // | 152 // |
121 // But if the Schemas are different then one of the components is out of date. | 153 // But if the Schemas are different then one of the components is out of date. |
122 // In that case the policy loaded will be valid only for one of them, until | 154 // In that case the policy loaded will be valid only for one of them, until |
123 // the outdated components are updated. This is a known limitation of the | 155 // the outdated components are updated. This is a known limitation of the |
(...skipping 10 matching lines...) Expand all Loading... |
134 comp_it != reg_component_map.end(); ++comp_it) { | 166 comp_it != reg_component_map.end(); ++comp_it) { |
135 map[domain_it->first][comp_it->first] = comp_it->second; | 167 map[domain_it->first][comp_it->first] = comp_it->second; |
136 } | 168 } |
137 } | 169 } |
138 } | 170 } |
139 schema_map_ = new SchemaMap(map); | 171 schema_map_ = new SchemaMap(map); |
140 Notify(has_new_schemas); | 172 Notify(has_new_schemas); |
141 } | 173 } |
142 | 174 |
143 } // namespace policy | 175 } // namespace policy |
OLD | NEW |