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