Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / schema_registry.h
blob4031f752f797bada6e635323a8bb2dc075762bcf
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_
8 #include <set>
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"
20 namespace policy {
22 class SchemaMap;
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,
27 // usually UI.
28 class POLICY_EXPORT SchemaRegistry : public base::NonThreadSafe {
29 public:
30 class POLICY_EXPORT Observer {
31 public:
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() {}
42 protected:
43 virtual ~Observer();
46 // This observer is only meant to be used by subclasses.
47 class POLICY_EXPORT InternalObserver {
48 public:
49 // Invoked when |registry| is about to be destroyed.
50 virtual void OnSchemaRegistryShuttingDown(SchemaRegistry* registry) = 0;
52 protected:
53 virtual ~InternalObserver();
56 SchemaRegistry();
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.
72 bool IsReady() const;
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);
85 protected:
86 void Notify(bool has_new_schemas);
88 scoped_refptr<SchemaMap> schema_map_;
90 private:
91 base::ObserverList<Observer, true> observers_;
92 base::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 {
103 public:
104 CombinedSchemaRegistry();
105 ~CombinedSchemaRegistry() override;
107 void Track(SchemaRegistry* registry);
109 // SchemaRegistry:
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;
117 // SchemaRegistry::InternalObserver:
118 void OnSchemaRegistryShuttingDown(SchemaRegistry* registry) override;
120 private:
121 void Combine(bool has_new_schemas);
123 std::set<SchemaRegistry*> registries_;
124 scoped_refptr<SchemaMap> own_schema_map_;
126 DISALLOW_COPY_AND_ASSIGN(CombinedSchemaRegistry);
129 // A registry that wraps another schema registry.
130 class POLICY_EXPORT ForwardingSchemaRegistry
131 : public SchemaRegistry,
132 public SchemaRegistry::Observer,
133 public SchemaRegistry::InternalObserver {
134 public:
135 // This registry will stop updating its SchemaMap when |wrapped| is
136 // destroyed.
137 explicit ForwardingSchemaRegistry(SchemaRegistry* wrapped);
138 ~ForwardingSchemaRegistry() override;
140 // SchemaRegistry:
141 void RegisterComponents(PolicyDomain domain,
142 const ComponentMap& components) override;
143 void UnregisterComponent(const PolicyNamespace& ns) override;
145 // SchemaRegistry::Observer:
146 void OnSchemaRegistryUpdated(bool has_new_schemas) override;
148 // SchemaRegistry::InternalObserver:
149 void OnSchemaRegistryShuttingDown(SchemaRegistry* registry) override;
151 private:
152 SchemaRegistry* wrapped_;
154 DISALLOW_COPY_AND_ASSIGN(ForwardingSchemaRegistry);
157 } // namespace policy
159 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_