1// Copyright (c) 2011 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 CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_H_
6#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_H_
7#pragma once
8
9#include <map>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/values.h"
15#include "chrome/browser/policy/configuration_policy_store_interface.h"
16#include "policy/configuration_policy_type.h"
17
18namespace policy {
19
20class PolicyMap;
21
22// A mostly-abstract super class for platform-specific policy providers.
23// Platform-specific policy providers (Windows Group Policy, gconf,
24// etc.) should implement a subclass of this class.
25class ConfigurationPolicyProvider {
26 public:
27  class Observer {
28   public:
29    virtual ~Observer() {}
30    virtual void OnUpdatePolicy() = 0;
31    virtual void OnProviderGoingAway() = 0;
32  };
33
34  // Used for static arrays of policy values that is used to initialize an
35  // instance of the ConfigurationPolicyProvider.
36  struct PolicyDefinitionList {
37    struct Entry {
38      ConfigurationPolicyType policy_type;
39      Value::ValueType value_type;
40      const char* name;
41    };
42
43    const Entry* begin;
44    const Entry* end;
45  };
46
47  explicit ConfigurationPolicyProvider(const PolicyDefinitionList* policy_list);
48
49  virtual ~ConfigurationPolicyProvider();
50
51  // Must be implemented by provider subclasses to specify the provider-specific
52  // policy decisions. The preference service invokes this |Provide| method when
53  // it needs a policy provider to specify its policy choices. In |Provide|, the
54  // |ConfigurationPolicyProvider| must make calls to the |Apply| method of
55  // |store| to apply specific policies. Returns true if the policy could be
56  // provided, otherwise false.
57  virtual bool Provide(ConfigurationPolicyStoreInterface* store) = 0;
58
59  // Check whether this provider has completed initialization. This is used to
60  // detect whether initialization is done in case providers implementations
61  // need to do asynchronous operations for initialization.
62  virtual bool IsInitializationComplete() const;
63
64 protected:
65  // Decodes the value tree and writes the configuration to the given |store|.
66  void ApplyPolicyValueTree(const DictionaryValue* policies,
67                             ConfigurationPolicyStoreInterface* store);
68
69  // Writes the configuration found in the already-decoded map |policies| to
70  // the given |store|.
71  void ApplyPolicyMap(const PolicyMap* policies,
72                      ConfigurationPolicyStoreInterface* store);
73
74  const PolicyDefinitionList* policy_definition_list() const {
75    return policy_definition_list_;
76  }
77
78 private:
79  friend class ConfigurationPolicyObserverRegistrar;
80
81  // Temporarily needed for access to ApplyPolicyValueTree as long as we need
82  // to support old-style policy.
83  friend class UserPolicyCache;
84
85  virtual void AddObserver(ConfigurationPolicyProvider::Observer* observer) = 0;
86  virtual void RemoveObserver(
87      ConfigurationPolicyProvider::Observer* observer) = 0;
88
89  // Contains the default mapping from policy values to the actual names.
90  const ConfigurationPolicyProvider::PolicyDefinitionList*
91      policy_definition_list_;
92
93 private:
94  DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyProvider);
95};
96
97// Manages observers for a ConfigurationPolicyProvider. Is used to register
98// observers, and automatically removes them upon destruction.
99// Implementation detail: to avoid duplicate bookkeeping of registered
100// observers, this registrar class acts as a proxy for notifications (since it
101// needs to register itself anyway to get OnProviderGoingAway notifications).
102class ConfigurationPolicyObserverRegistrar
103    : ConfigurationPolicyProvider::Observer {
104 public:
105  ConfigurationPolicyObserverRegistrar();
106  ~ConfigurationPolicyObserverRegistrar();
107  void Init(ConfigurationPolicyProvider* provider,
108            ConfigurationPolicyProvider::Observer* observer);
109
110  // ConfigurationPolicyProvider::Observer implementation:
111  virtual void OnUpdatePolicy();
112  virtual void OnProviderGoingAway();
113
114 private:
115  ConfigurationPolicyProvider* provider_;
116  ConfigurationPolicyProvider::Observer* observer_;
117
118  DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyObserverRegistrar);
119};
120
121}  // namespace policy
122
123#endif  // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_H_
124