schema_registry.h revision f2477e01787aa58f445919b809d89e252beef54f
1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <set>
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/basictypes.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/compiler_specific.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/ref_counted.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/observer_list.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/threading/non_thread_safe.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_namespace.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/schema.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/schema_map.h"
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace policy {
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SchemaMap;
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Holds the main reference to the current SchemaMap, and allows a list of
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// observers to get notified whenever it is updated.
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// This object is not thread safe and must be used from the owner's thread,
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// usually UI.
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class POLICY_EXPORT SchemaRegistry : public base::NonThreadSafe {
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  class POLICY_EXPORT Observer {
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   public:
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Invoked whenever schemas are registered or unregistered.
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // |has_new_schemas| is true if a new component has been registered since
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // the last update; this allows observers to ignore updates when
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // components are unregistered but still get a handle to the current map
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // (e.g. for periodic reloads).
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    virtual void OnSchemaRegistryUpdated(bool has_new_schemas) = 0;
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Invoked when all policy domains become ready.
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    virtual void OnSchemaRegistryReady() = 0;
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   protected:
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    virtual ~Observer();
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  };
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SchemaRegistry();
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~SchemaRegistry();
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const scoped_refptr<SchemaMap>& schema_map() const { return schema_map_; }
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Register a single component.
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void RegisterComponent(const PolicyNamespace& ns,
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                         const Schema& schema);
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Register a list of components for a given domain.
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void RegisterComponents(PolicyDomain domain,
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  const ComponentMap& components);
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UnregisterComponent(const PolicyNamespace& ns);
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if all domains have registered the initial components.
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool IsReady() const;
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // This indicates that the initial components for |domain| have all been
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // registered. It must be invoked at least once for each policy domain;
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // subsequent calls for the same domain are ignored.
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void SetReady(PolicyDomain domain);
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void AddObserver(Observer* observer);
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void RemoveObserver(Observer* observer);
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool HasObservers() const;
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) protected:
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Notify(bool has_new_schemas);
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<SchemaMap> schema_map_;
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ObserverList<Observer, true> observers_;
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool domains_ready_[POLICY_DOMAIN_SIZE];
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SchemaRegistry);
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// A registry that combines the maps of other registries.
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class POLICY_EXPORT CombinedSchemaRegistry : public SchemaRegistry,
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                             public SchemaRegistry::Observer {
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CombinedSchemaRegistry();
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~CombinedSchemaRegistry();
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Track(SchemaRegistry* registry);
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Untrack(SchemaRegistry* registry);
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void RegisterComponents(PolicyDomain domain,
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  const ComponentMap& components) OVERRIDE;
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UnregisterComponent(const PolicyNamespace& ns) OVERRIDE;
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnSchemaRegistryUpdated(bool has_new_schemas) OVERRIDE;
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnSchemaRegistryReady() OVERRIDE;
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Combine(bool has_new_schemas);
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::set<SchemaRegistry*> registries_;
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<SchemaMap> own_schema_map_;
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CombinedSchemaRegistry);
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace policy
114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_
116