Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / policy_service_impl.h
blob355128955eccd8f5db95cf60f1f0c747f325ee40
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
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 "base/threading/thread_checker.h"
18 #include "components/policy/core/common/configuration_policy_provider.h"
19 #include "components/policy/core/common/policy_bundle.h"
20 #include "components/policy/core/common/policy_service.h"
21 #include "components/policy/policy_export.h"
23 namespace policy {
25 class PolicyMap;
27 class POLICY_EXPORT PolicyServiceImpl
28 : public PolicyService,
29 public ConfigurationPolicyProvider::Observer {
30 public:
31 typedef std::vector<ConfigurationPolicyProvider*> Providers;
33 // The PolicyServiceImpl will merge policies from |providers|. |providers|
34 // must be sorted in decreasing order of priority; the first provider will
35 // have the highest priority. The PolicyServiceImpl does not take ownership of
36 // the providers, and they must outlive the PolicyServiceImpl.
37 explicit PolicyServiceImpl(const Providers& providers);
39 ~PolicyServiceImpl() override;
41 // PolicyService overrides:
42 void AddObserver(PolicyDomain domain,
43 PolicyService::Observer* observer) override;
44 void RemoveObserver(PolicyDomain domain,
45 PolicyService::Observer* observer) override;
46 const PolicyMap& GetPolicies(const PolicyNamespace& ns) const override;
47 bool IsInitializationComplete(PolicyDomain domain) const override;
48 void RefreshPolicies(const base::Closure& callback) override;
50 private:
51 typedef base::ObserverList<PolicyService::Observer, true> Observers;
52 typedef std::map<PolicyDomain, Observers*> ObserverMap;
54 // ConfigurationPolicyProvider::Observer overrides:
55 void OnUpdatePolicy(ConfigurationPolicyProvider* provider) override;
57 // Posts a task to notify observers of |ns| that its policies have changed,
58 // passing along the |previous| and the |current| policies.
59 void NotifyNamespaceUpdated(const PolicyNamespace& ns,
60 const PolicyMap& previous,
61 const PolicyMap& current);
63 // Combines the policies from all the providers, and notifies the observers
64 // of namespaces whose policies have been modified.
65 void MergeAndTriggerUpdates();
67 // Checks if all providers are initialized, and notifies the observers
68 // if the service just became initialized.
69 void CheckInitializationComplete();
71 // Invokes all the refresh callbacks if there are no more refreshes pending.
72 void CheckRefreshComplete();
74 // The providers passed in the constructor, in order of decreasing priority.
75 Providers providers_;
77 // Maps each policy namespace to its current policies.
78 PolicyBundle policy_bundle_;
80 // Maps each policy domain to its observer list.
81 ObserverMap observers_;
83 // True if all the providers are initialized for the indexed policy domain.
84 bool initialization_complete_[POLICY_DOMAIN_SIZE];
86 // Set of providers that have a pending update that was triggered by a
87 // call to RefreshPolicies().
88 std::set<ConfigurationPolicyProvider*> refresh_pending_;
90 // List of callbacks to invoke once all providers refresh after a
91 // RefreshPolicies() call.
92 std::vector<base::Closure> refresh_callbacks_;
94 // Used to verify thread-safe usage.
95 base::ThreadChecker thread_checker_;
97 // Used to create tasks to delay new policy updates while we may be already
98 // processing previous policy updates.
99 base::WeakPtrFactory<PolicyServiceImpl> update_task_ptr_factory_;
101 DISALLOW_COPY_AND_ASSIGN(PolicyServiceImpl);
104 } // namespace policy
106 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_IMPL_H_