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_COMMON_POLICY_SERVICE_IMPL_H_ 6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_IMPL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 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" 21 22 namespace policy { 23 24 class PolicyMap; 25 26 class POLICY_EXPORT PolicyServiceImpl 27 : public PolicyService, 28 public ConfigurationPolicyProvider::Observer { 29 public: 30 typedef std::vector<ConfigurationPolicyProvider*> Providers; 31 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); 37 38 virtual ~PolicyServiceImpl(); 39 40 // PolicyService overrides: 41 virtual void AddObserver(PolicyDomain domain, 42 PolicyService::Observer* observer) OVERRIDE; 43 virtual void RemoveObserver(PolicyDomain domain, 44 PolicyService::Observer* observer) OVERRIDE; 45 virtual const PolicyMap& GetPolicies( 46 const PolicyNamespace& ns) const OVERRIDE; 47 virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE; 48 virtual void RefreshPolicies(const base::Closure& callback) OVERRIDE; 49 50 private: 51 typedef ObserverList<PolicyService::Observer, true> Observers; 52 typedef std::map<PolicyDomain, Observers*> ObserverMap; 53 54 // ConfigurationPolicyProvider::Observer overrides: 55 virtual void OnUpdatePolicy(ConfigurationPolicyProvider* provider) OVERRIDE; 56 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); 62 63 // Combines the policies from all the providers, and notifies the observers 64 // of namespaces whose policies have been modified. 65 void MergeAndTriggerUpdates(); 66 67 // Checks if all providers are initialized, and notifies the observers 68 // if the service just became initialized. 69 void CheckInitializationComplete(); 70 71 // Invokes all the refresh callbacks if there are no more refreshes pending. 72 void CheckRefreshComplete(); 73 74 // The providers passed in the constructor, in order of decreasing priority. 75 Providers providers_; 76 77 // Maps each policy namespace to its current policies. 78 PolicyBundle policy_bundle_; 79 80 // Maps each policy domain to its observer list. 81 ObserverMap observers_; 82 83 // True if all the providers are initialized for the indexed policy domain. 84 bool initialization_complete_[POLICY_DOMAIN_SIZE]; 85 86 // Set of providers that have a pending update that was triggered by a 87 // call to RefreshPolicies(). 88 std::set<ConfigurationPolicyProvider*> refresh_pending_; 89 90 // List of callbacks to invoke once all providers refresh after a 91 // RefreshPolicies() call. 92 std::vector<base::Closure> refresh_callbacks_; 93 94 // Used to create tasks to delay new policy updates while we may be already 95 // processing previous policy updates. 96 base::WeakPtrFactory<PolicyServiceImpl> update_task_ptr_factory_; 97 98 DISALLOW_COPY_AND_ASSIGN(PolicyServiceImpl); 99 }; 100 101 } // namespace policy 102 103 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SERVICE_IMPL_H_ 104