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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "components/policy/core/common/policy_namespace.h"
15 #include "components/policy/policy_export.h"
19 // The PolicyService merges policies from all available sources, taking into
20 // account their priorities. Policy clients can retrieve policy for their domain
21 // and register for notifications on policy updates.
23 // The PolicyService is available from BrowserProcess as a global singleton.
24 // There is also a PolicyService for browser-wide policies available from
25 // BrowserProcess as a global singleton.
26 class POLICY_EXPORT PolicyService
{
28 class POLICY_EXPORT Observer
{
30 // Invoked whenever policies for the given |ns| namespace are modified.
31 // This is only invoked for changes that happen after AddObserver is called.
32 // |previous| contains the values of the policies before the update,
33 // and |current| contains the current values.
34 virtual void OnPolicyUpdated(const PolicyNamespace
& ns
,
35 const PolicyMap
& previous
,
36 const PolicyMap
& current
) = 0;
38 // Invoked at most once for each |domain|, when the PolicyService becomes
39 // ready. If IsInitializationComplete() is false, then this will be invoked
40 // once all the policy providers have finished loading their policies for
42 virtual void OnPolicyServiceInitialized(PolicyDomain domain
) {}
45 virtual ~Observer() {}
48 virtual ~PolicyService() {}
50 // Observes changes to all components of the given |domain|.
51 virtual void AddObserver(PolicyDomain domain
, Observer
* observer
) = 0;
53 virtual void RemoveObserver(PolicyDomain domain
, Observer
* observer
) = 0;
55 virtual const PolicyMap
& GetPolicies(const PolicyNamespace
& ns
) const = 0;
57 // The PolicyService loads policy from several sources, and some require
58 // asynchronous loads. IsInitializationComplete() returns true once all
59 // sources have loaded their policies for the given |domain|.
60 // It is safe to read policy from the PolicyService even if
61 // IsInitializationComplete() is false; there will be an OnPolicyUpdated()
62 // notification once new policies become available.
64 // OnPolicyServiceInitialized() is called when IsInitializationComplete()
65 // becomes true, which happens at most once for each domain.
66 // If IsInitializationComplete() is already true for |domain| when an Observer
67 // is registered, then that Observer will not receive an
68 // OnPolicyServiceInitialized() notification.
69 virtual bool IsInitializationComplete(PolicyDomain domain
) const = 0;
71 // Asks the PolicyService to reload policy from all available policy sources.
72 // |callback| is invoked once every source has reloaded its policies, and
73 // GetPolicies() is guaranteed to return the updated values at that point.
74 virtual void RefreshPolicies(const base::Closure
& callback
) = 0;
77 // A registrar that only observes changes to particular policies within the
78 // PolicyMap for the given policy namespace.
79 class POLICY_EXPORT PolicyChangeRegistrar
: public PolicyService::Observer
{
81 typedef base::Callback
<void(const base::Value
*,
82 const base::Value
*)> UpdateCallback
;
84 // Observes updates to the given (domain, component_id) namespace in the given
85 // |policy_service|, and notifies |observer| whenever any of the registered
86 // policy keys changes. Both the |policy_service| and the |observer| must
88 PolicyChangeRegistrar(PolicyService
* policy_service
,
89 const PolicyNamespace
& ns
);
91 virtual ~PolicyChangeRegistrar();
93 // Will invoke |callback| whenever |policy_name| changes its value, as long
94 // as this registrar exists.
95 // Only one callback can be registed per policy name; a second call with the
96 // same |policy_name| will overwrite the previous callback.
97 void Observe(const std::string
& policy_name
, const UpdateCallback
& callback
);
99 // Implementation of PolicyService::Observer:
100 virtual void OnPolicyUpdated(const PolicyNamespace
& ns
,
101 const PolicyMap
& previous
,
102 const PolicyMap
& current
) OVERRIDE
;
105 typedef std::map
<std::string
, UpdateCallback
> CallbackMap
;
107 PolicyService
* policy_service_
;
109 CallbackMap callback_map_
;
111 DISALLOW_COPY_AND_ASSIGN(PolicyChangeRegistrar
);
114 } // namespace policy
116 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_H_