schema_registry.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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. 4 5#ifndef COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_ 6#define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_ 7 8#include <set> 9 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" 19 20namespace policy { 21 22class SchemaMap; 23 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. 28class 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; 38 39 // Invoked when all policy domains become ready. 40 virtual void OnSchemaRegistryReady() = 0; 41 42 protected: 43 virtual ~Observer(); 44 }; 45 46 SchemaRegistry(); 47 virtual ~SchemaRegistry(); 48 49 const scoped_refptr<SchemaMap>& schema_map() const { return schema_map_; } 50 51 // Register a single component. 52 void RegisterComponent(const PolicyNamespace& ns, 53 const Schema& schema); 54 55 // Register a list of components for a given domain. 56 virtual void RegisterComponents(PolicyDomain domain, 57 const ComponentMap& components); 58 59 virtual void UnregisterComponent(const PolicyNamespace& ns); 60 61 // Returns true if all domains have registered the initial components. 62 bool IsReady() const; 63 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); 68 69 void AddObserver(Observer* observer); 70 void RemoveObserver(Observer* observer); 71 72 bool HasObservers() const; 73 74 protected: 75 void Notify(bool has_new_schemas); 76 77 scoped_refptr<SchemaMap> schema_map_; 78 79 private: 80 ObserverList<Observer, true> observers_; 81 bool domains_ready_[POLICY_DOMAIN_SIZE]; 82 83 DISALLOW_COPY_AND_ASSIGN(SchemaRegistry); 84}; 85 86// A registry that combines the maps of other registries. 87class POLICY_EXPORT CombinedSchemaRegistry : public SchemaRegistry, 88 public SchemaRegistry::Observer { 89 public: 90 CombinedSchemaRegistry(); 91 virtual ~CombinedSchemaRegistry(); 92 93 void Track(SchemaRegistry* registry); 94 void Untrack(SchemaRegistry* registry); 95 96 virtual void RegisterComponents(PolicyDomain domain, 97 const ComponentMap& components) OVERRIDE; 98 99 virtual void UnregisterComponent(const PolicyNamespace& ns) OVERRIDE; 100 101 virtual void OnSchemaRegistryUpdated(bool has_new_schemas) OVERRIDE; 102 103 virtual void OnSchemaRegistryReady() OVERRIDE; 104 105 private: 106 void Combine(bool has_new_schemas); 107 108 std::set<SchemaRegistry*> registries_; 109 scoped_refptr<SchemaMap> own_schema_map_; 110 111 DISALLOW_COPY_AND_ASSIGN(CombinedSchemaRegistry); 112}; 113 114} // namespace policy 115 116#endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_REGISTRY_H_ 117