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;
47 virtual ~SchemaRegistry();
49 const scoped_refptr
<SchemaMap
>& schema_map() const { return schema_map_
; }
51 // Register a single component.
52 void RegisterComponent(const PolicyNamespace
& ns
,
53 const Schema
& schema
);
55 // Register a list of components for a given domain.
56 virtual void RegisterComponents(PolicyDomain domain
,
57 const ComponentMap
& components
);
59 virtual void UnregisterComponent(const PolicyNamespace
& ns
);
61 // Returns true if all domains have registered the initial components.
64 // This indicates that the initial components for |domain| have all been
65 // registered. It must be invoked at least once for each policy domain;
66 // subsequent calls for the same domain are ignored.
67 void SetReady(PolicyDomain domain
);
69 void AddObserver(Observer
* observer
);
70 void RemoveObserver(Observer
* observer
);
72 bool HasObservers() const;
75 void Notify(bool has_new_schemas
);
77 scoped_refptr
<SchemaMap
> schema_map_
;
80 ObserverList
<Observer
, true> observers_
;
81 bool domains_ready_
[POLICY_DOMAIN_SIZE
];
83 DISALLOW_COPY_AND_ASSIGN(SchemaRegistry
);
86 // A registry that combines the maps of other registries.
87 class POLICY_EXPORT CombinedSchemaRegistry
: public SchemaRegistry
,
88 public SchemaRegistry::Observer
{
90 CombinedSchemaRegistry();
91 virtual ~CombinedSchemaRegistry();
93 void Track(SchemaRegistry
* registry
);
94 void Untrack(SchemaRegistry
* registry
);
96 virtual void RegisterComponents(PolicyDomain domain
,
97 const ComponentMap
& components
) OVERRIDE
;
99 virtual void UnregisterComponent(const PolicyNamespace
& ns
) OVERRIDE
;
101 virtual void OnSchemaRegistryUpdated(bool has_new_schemas
) OVERRIDE
;
103 virtual void OnSchemaRegistryReady() OVERRIDE
;
106 void Combine(bool has_new_schemas
);
108 std::set
<SchemaRegistry
*> registries_
;
109 scoped_refptr
<SchemaMap
> own_schema_map_
;
111 DISALLOW_COPY_AND_ASSIGN(CombinedSchemaRegistry
);
114 } // namespace policy
116 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_