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_CONFIGURATION_POLICY_PROVIDER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CONFIGURATION_POLICY_PROVIDER_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "components/policy/core/common/policy_bundle.h"
14 #include "components/policy/core/common/policy_namespace.h"
15 #include "components/policy/core/common/schema_registry.h"
16 #include "components/policy/policy_export.h"
17 
18 namespace policy {
19 
20 // A mostly-abstract super class for platform-specific policy providers.
21 // Platform-specific policy providers (Windows Group Policy, gconf,
22 // etc.) should implement a subclass of this class.
23 class POLICY_EXPORT ConfigurationPolicyProvider
24     : public SchemaRegistry::Observer {
25  public:
26   class POLICY_EXPORT Observer {
27    public:
28     virtual ~Observer();
29     virtual void OnUpdatePolicy(ConfigurationPolicyProvider* provider) = 0;
30   };
31 
32   ConfigurationPolicyProvider();
33 
34   // Policy providers can be deleted quite late during shutdown of the browser,
35   // and it's not guaranteed that the message loops will still be running when
36   // this is invoked. Override Shutdown() instead for cleanup code that needs
37   // to post to the FILE thread, for example.
38   ~ConfigurationPolicyProvider() override;
39 
40   // Invoked as soon as the main message loops are spinning. Policy providers
41   // are created early during startup to provide the initial policies; the
42   // Init() call allows them to perform initialization tasks that require
43   // running message loops.
44   // The policy provider will load policy for the components registered in
45   // the |schema_registry| whose domain is supported by this provider.
46   virtual void Init(SchemaRegistry* registry);
47 
48   // Must be invoked before deleting the provider. Implementations can override
49   // this method to do appropriate cleanup while threads are still running, and
50   // must also invoke ConfigurationPolicyProvider::Shutdown().
51   // The provider should keep providing the current policies after Shutdown()
52   // is invoked, it only has to stop updating.
53   virtual void Shutdown();
54 
55   // Returns the current PolicyBundle.
policies()56   const PolicyBundle& policies() const { return policy_bundle_; }
57 
58   // Check whether this provider has completed initialization for the given
59   // policy |domain|. This is used to detect whether initialization is done in
60   // case implementations need to do asynchronous operations for initialization.
61   virtual bool IsInitializationComplete(PolicyDomain domain) const;
62 
63   // Asks the provider to refresh its policies. All the updates caused by this
64   // call will be visible on the next call of OnUpdatePolicy on the observers,
65   // which are guaranteed to happen even if the refresh fails.
66   // It is possible that Shutdown() is called first though, and
67   // OnUpdatePolicy won't be called if that happens.
68   virtual void RefreshPolicies() = 0;
69 
70   // Observers must detach themselves before the provider is deleted.
71   virtual void AddObserver(Observer* observer);
72   virtual void RemoveObserver(Observer* observer);
73 
74   // SchemaRegistry::Observer:
75   void OnSchemaRegistryUpdated(bool has_new_schemas) override;
76   void OnSchemaRegistryReady() override;
77 
78  protected:
79   // Subclasses must invoke this to update the policies currently served by
80   // this provider. UpdatePolicy() takes ownership of |policies|.
81   // The observers are notified after the policies are updated.
82   void UpdatePolicy(std::unique_ptr<PolicyBundle> bundle);
83 
84   SchemaRegistry* schema_registry() const;
85 
86   const scoped_refptr<SchemaMap>& schema_map() const;
87 
88  private:
89   // The policies currently configured at this provider.
90   PolicyBundle policy_bundle_;
91 
92   // Used to validate proper Init() and Shutdown() nesting. This flag is set by
93   // Init() and cleared by Shutdown() and needs to be false in the destructor.
94   bool initialized_;
95 
96   SchemaRegistry* schema_registry_;
97 
98   base::ObserverList<Observer, true> observer_list_;
99 
100   DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyProvider);
101 };
102 
103 }  // namespace policy
104 
105 #endif  // COMPONENTS_POLICY_CORE_COMMON_CONFIGURATION_POLICY_PROVIDER_H_
106