1// Copyright (c) 2012 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_BROWSER_CONFIGURATION_POLICY_PREF_STORE_H_
6#define COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_PREF_STORE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "base/prefs/pref_store.h"
14#include "base/values.h"
15#include "components/policy/core/common/policy_map.h"
16#include "components/policy/core/common/policy_service.h"
17#include "components/policy/core/common/policy_types.h"
18#include "components/policy/policy_export.h"
19
20class PrefValueMap;
21
22namespace policy {
23
24class ConfigurationPolicyHandlerList;
25
26// An implementation of PrefStore that bridges policy settings as read from the
27// PolicyService to preferences. Converts POLICY_DOMAIN_CHROME policies a given
28// PolicyLevel to their corresponding preferences.
29class POLICY_EXPORT ConfigurationPolicyPrefStore
30    : public PrefStore,
31      public PolicyService::Observer {
32 public:
33  // Does not take ownership of |service| nor |handler_list|, which must outlive
34  // the store. Only policies of the given |level| will be mapped.
35  ConfigurationPolicyPrefStore(
36      PolicyService* service,
37      const ConfigurationPolicyHandlerList* handler_list,
38      PolicyLevel level);
39
40  // PrefStore methods:
41  virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE;
42  virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE;
43  virtual bool HasObservers() const OVERRIDE;
44  virtual bool IsInitializationComplete() const OVERRIDE;
45  virtual bool GetValue(const std::string& key,
46                        const base::Value** result) const OVERRIDE;
47
48  // PolicyService::Observer methods:
49  virtual void OnPolicyUpdated(const PolicyNamespace& ns,
50                               const PolicyMap& previous,
51                               const PolicyMap& current) OVERRIDE;
52  virtual void OnPolicyServiceInitialized(PolicyDomain domain) OVERRIDE;
53
54 private:
55  virtual ~ConfigurationPolicyPrefStore();
56
57  // Refreshes policy information, rereading policy from the policy service and
58  // sending out change notifications as appropriate.
59  void Refresh();
60
61  // Returns a new PrefValueMap containing the preference values that correspond
62  // to the policies currently provided by the policy service.
63  PrefValueMap* CreatePreferencesFromPolicies();
64
65  // The PolicyService from which policy settings are read.
66  PolicyService* policy_service_;
67
68  // The policy handlers used to convert policies into their corresponding
69  // preferences.
70  const ConfigurationPolicyHandlerList* handler_list_;
71
72  // The policy level that this PrefStore uses.
73  PolicyLevel level_;
74
75  // Current policy preferences.
76  scoped_ptr<PrefValueMap> prefs_;
77
78  ObserverList<PrefStore::Observer, true> observers_;
79
80  DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyPrefStore);
81};
82
83}  // namespace policy
84
85#endif  // COMPONENTS_POLICY_CORE_BROWSER_CONFIGURATION_POLICY_PREF_STORE_H_
86