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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "components/policy/core/common/policy_namespace.h"
16 #include "components/policy/core/common/schema.h"
17 #include "components/policy/core/common/schema_map.h"
18 #include "components/policy/policy_export.h"
24 // Holds the main reference to the current SchemaMap, and allows a list of
25 // observers to get notified whenever it is updated.
26 // This object is not thread safe and must be used from the owner's thread,
28 class POLICY_EXPORT SchemaRegistry
: public base::NonThreadSafe
{
30 class POLICY_EXPORT Observer
{
32 // Invoked whenever schemas are registered or unregistered.
33 // |has_new_schemas| is true if a new component has been registered since
34 // the last update; this allows observers to ignore updates when
35 // components are unregistered but still get a handle to the current map
36 // (e.g. for periodic reloads).
37 virtual void OnSchemaRegistryUpdated(bool has_new_schemas
) = 0;
39 // Invoked when all policy domains become ready.
40 virtual void OnSchemaRegistryReady() = 0;
46 // This observer is only meant to be used by subclasses.
47 class POLICY_EXPORT InternalObserver
{
49 // Invoked when |registry| is about to be destroyed.
50 virtual void OnSchemaRegistryShuttingDown(SchemaRegistry
* registry
) = 0;
53 virtual ~InternalObserver();
57 virtual ~SchemaRegistry();
59 const scoped_refptr
<SchemaMap
>& schema_map() const { return schema_map_
; }
61 // Register a single component.
62 void RegisterComponent(const PolicyNamespace
& ns
,
63 const Schema
& schema
);
65 // Register a list of components for a given domain.
66 virtual void RegisterComponents(PolicyDomain domain
,
67 const ComponentMap
& components
);
69 virtual void UnregisterComponent(const PolicyNamespace
& ns
);
71 // Returns true if all domains have registered the initial components.
74 // This indicates that the initial components for |domain| have all been
75 // registered. It must be invoked at least once for each policy domain;
76 // subsequent calls for the same domain are ignored.
77 void SetReady(PolicyDomain domain
);
79 void AddObserver(Observer
* observer
);
80 void RemoveObserver(Observer
* observer
);
82 void AddInternalObserver(InternalObserver
* observer
);
83 void RemoveInternalObserver(InternalObserver
* observer
);
86 void Notify(bool has_new_schemas
);
88 scoped_refptr
<SchemaMap
> schema_map_
;
91 ObserverList
<Observer
, true> observers_
;
92 ObserverList
<InternalObserver
, true> internal_observers_
;
93 bool domains_ready_
[POLICY_DOMAIN_SIZE
];
95 DISALLOW_COPY_AND_ASSIGN(SchemaRegistry
);
98 // A registry that combines the maps of other registries.
99 class POLICY_EXPORT CombinedSchemaRegistry
100 : public SchemaRegistry
,
101 public SchemaRegistry::Observer
,
102 public SchemaRegistry::InternalObserver
{
104 CombinedSchemaRegistry();
105 ~CombinedSchemaRegistry() override
;
107 void Track(SchemaRegistry
* registry
);
110 void RegisterComponents(PolicyDomain domain
,
111 const ComponentMap
& components
) override
;
112 void UnregisterComponent(const PolicyNamespace
& ns
) override
;
114 // SchemaRegistry::Observer:
115 void OnSchemaRegistryUpdated(bool has_new_schemas
) override
;
116 void OnSchemaRegistryReady() override
;
118 // SchemaRegistry::InternalObserver:
119 void OnSchemaRegistryShuttingDown(SchemaRegistry
* registry
) override
;
122 void Combine(bool has_new_schemas
);
124 std::set
<SchemaRegistry
*> registries_
;
125 scoped_refptr
<SchemaMap
> own_schema_map_
;
127 DISALLOW_COPY_AND_ASSIGN(CombinedSchemaRegistry
);
130 // A registry that wraps another schema registry.
131 class POLICY_EXPORT ForwardingSchemaRegistry
132 : public SchemaRegistry
,
133 public SchemaRegistry::Observer
,
134 public SchemaRegistry::InternalObserver
{
136 // This registry will stop updating its SchemaMap when |wrapped| is
138 explicit ForwardingSchemaRegistry(SchemaRegistry
* wrapped
);
139 ~ForwardingSchemaRegistry() override
;
142 void RegisterComponents(PolicyDomain domain
,
143 const ComponentMap
& components
) override
;
144 void UnregisterComponent(const PolicyNamespace
& ns
) override
;
146 // SchemaRegistry::Observer:
147 void OnSchemaRegistryUpdated(bool has_new_schemas
) override
;
148 void OnSchemaRegistryReady() override
;
150 // SchemaRegistry::InternalObserver:
151 void OnSchemaRegistryShuttingDown(SchemaRegistry
* registry
) override
;
154 SchemaRegistry
* wrapped_
;
156 DISALLOW_COPY_AND_ASSIGN(ForwardingSchemaRegistry
);
159 } // namespace policy
161 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_