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_IMPL_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_IMPL_H_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "components/policy/core/common/configuration_policy_provider.h"
18 #include "components/policy/core/common/policy_bundle.h"
19 #include "components/policy/core/common/policy_service.h"
20 #include "components/policy/policy_export.h"
26 class POLICY_EXPORT PolicyServiceImpl
27 : public PolicyService
,
28 public ConfigurationPolicyProvider::Observer
{
30 typedef std::vector
<ConfigurationPolicyProvider
*> Providers
;
32 // The PolicyServiceImpl will merge policies from |providers|. |providers|
33 // must be sorted in decreasing order of priority; the first provider will
34 // have the highest priority. The PolicyServiceImpl does not take ownership of
35 // the providers, and they must outlive the PolicyServiceImpl.
36 explicit PolicyServiceImpl(const Providers
& providers
);
38 ~PolicyServiceImpl() override
;
40 // PolicyService overrides:
41 void AddObserver(PolicyDomain domain
,
42 PolicyService::Observer
* observer
) override
;
43 void RemoveObserver(PolicyDomain domain
,
44 PolicyService::Observer
* observer
) override
;
45 const PolicyMap
& GetPolicies(const PolicyNamespace
& ns
) const override
;
46 bool IsInitializationComplete(PolicyDomain domain
) const override
;
47 void RefreshPolicies(const base::Closure
& callback
) override
;
50 typedef ObserverList
<PolicyService::Observer
, true> Observers
;
51 typedef std::map
<PolicyDomain
, Observers
*> ObserverMap
;
53 // ConfigurationPolicyProvider::Observer overrides:
54 void OnUpdatePolicy(ConfigurationPolicyProvider
* provider
) override
;
56 // Posts a task to notify observers of |ns| that its policies have changed,
57 // passing along the |previous| and the |current| policies.
58 void NotifyNamespaceUpdated(const PolicyNamespace
& ns
,
59 const PolicyMap
& previous
,
60 const PolicyMap
& current
);
62 // Combines the policies from all the providers, and notifies the observers
63 // of namespaces whose policies have been modified.
64 void MergeAndTriggerUpdates();
66 // Checks if all providers are initialized, and notifies the observers
67 // if the service just became initialized.
68 void CheckInitializationComplete();
70 // Invokes all the refresh callbacks if there are no more refreshes pending.
71 void CheckRefreshComplete();
73 // The providers passed in the constructor, in order of decreasing priority.
76 // Maps each policy namespace to its current policies.
77 PolicyBundle policy_bundle_
;
79 // Maps each policy domain to its observer list.
80 ObserverMap observers_
;
82 // True if all the providers are initialized for the indexed policy domain.
83 bool initialization_complete_
[POLICY_DOMAIN_SIZE
];
85 // Set of providers that have a pending update that was triggered by a
86 // call to RefreshPolicies().
87 std::set
<ConfigurationPolicyProvider
*> refresh_pending_
;
89 // List of callbacks to invoke once all providers refresh after a
90 // RefreshPolicies() call.
91 std::vector
<base::Closure
> refresh_callbacks_
;
93 // Used to create tasks to delay new policy updates while we may be already
94 // processing previous policy updates.
95 base::WeakPtrFactory
<PolicyServiceImpl
> update_task_ptr_factory_
;
97 DISALLOW_COPY_AND_ASSIGN(PolicyServiceImpl
);
100 } // namespace policy
102 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_IMPL_H_