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 <memory>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/threading/thread_checker.h"
19 #include "components/policy/core/common/configuration_policy_provider.h"
20 #include "components/policy/core/common/policy_bundle.h"
21 #include "components/policy/core/common/policy_service.h"
22 #include "components/policy/policy_export.h"
23 
24 namespace policy {
25 
26 class PolicyMap;
27 
28 class POLICY_EXPORT PolicyServiceImpl
29     : public PolicyService,
30       public ConfigurationPolicyProvider::Observer {
31  public:
32   using Providers = std::vector<ConfigurationPolicyProvider*>;
33 
34   // Creates a new PolicyServiceImpl with the list of
35   // ConfigurationPolicyProviders, in order of decreasing priority.
36   explicit PolicyServiceImpl(Providers providers);
37   ~PolicyServiceImpl() override;
38 
39   // PolicyService overrides:
40   void AddObserver(PolicyDomain domain,
41                    PolicyService::Observer* observer) override;
42   void RemoveObserver(PolicyDomain domain,
43                       PolicyService::Observer* observer) override;
44   const PolicyMap& GetPolicies(const PolicyNamespace& ns) const override;
45   bool IsInitializationComplete(PolicyDomain domain) const override;
46   void RefreshPolicies(const base::Closure& callback) override;
47 
48  private:
49   using Observers = base::ObserverList<PolicyService::Observer, true>;
50 
51   // ConfigurationPolicyProvider::Observer overrides:
52   void OnUpdatePolicy(ConfigurationPolicyProvider* provider) override;
53 
54   // Posts a task to notify observers of |ns| that its policies have changed,
55   // passing along the |previous| and the |current| policies.
56   void NotifyNamespaceUpdated(const PolicyNamespace& ns,
57                               const PolicyMap& previous,
58                               const PolicyMap& current);
59 
60   // Combines the policies from all the providers, and notifies the observers
61   // of namespaces whose policies have been modified.
62   void MergeAndTriggerUpdates();
63 
64   // Checks if all providers are initialized, and notifies the observers
65   // if the service just became initialized.
66   void CheckInitializationComplete();
67 
68   // Invokes all the refresh callbacks if there are no more refreshes pending.
69   void CheckRefreshComplete();
70 
71   // The providers, in order of decreasing priority.
72   Providers providers_;
73 
74   // Maps each policy namespace to its current policies.
75   PolicyBundle policy_bundle_;
76 
77   // Maps each policy domain to its observer list.
78   std::map<PolicyDomain, std::unique_ptr<Observers>> observers_;
79 
80   // True if all the providers are initialized for the indexed policy domain.
81   bool initialization_complete_[POLICY_DOMAIN_SIZE];
82 
83   // Set of providers that have a pending update that was triggered by a
84   // call to RefreshPolicies().
85   std::set<ConfigurationPolicyProvider*> refresh_pending_;
86 
87   // List of callbacks to invoke once all providers refresh after a
88   // RefreshPolicies() call.
89   std::vector<base::Closure> refresh_callbacks_;
90 
91   // Used to verify thread-safe usage.
92   base::ThreadChecker thread_checker_;
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