- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / policy_service_impl.h
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 CHROME_BROWSER_POLICY_POLICY_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_POLICY_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/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/policy/configuration_policy_provider.h"
17 #include "chrome/browser/policy/policy_bundle.h"
18 #include "chrome/browser/policy/policy_service.h"
19
20 namespace policy {
21
22 class PolicyMap;
23
24 class PolicyServiceImpl : public PolicyService,
25                           public ConfigurationPolicyProvider::Observer {
26  public:
27   typedef std::vector<ConfigurationPolicyProvider*> Providers;
28
29   // The PolicyServiceImpl will merge policies from |providers|. |providers|
30   // must be sorted in decreasing order of priority; the first provider will
31   // have the highest priority. The PolicyServiceImpl does not take ownership of
32   // the providers, and they must outlive the PolicyServiceImpl.
33   explicit PolicyServiceImpl(const Providers& providers);
34   virtual ~PolicyServiceImpl();
35
36   // PolicyService overrides:
37   virtual void AddObserver(PolicyDomain domain,
38                            PolicyService::Observer* observer) OVERRIDE;
39   virtual void RemoveObserver(PolicyDomain domain,
40                               PolicyService::Observer* observer) OVERRIDE;
41   virtual void RegisterPolicyDomain(
42       scoped_refptr<const PolicyDomainDescriptor> descriptor) OVERRIDE;
43   virtual const PolicyMap& GetPolicies(
44       const PolicyNamespace& ns) const OVERRIDE;
45   virtual scoped_refptr<const PolicyDomainDescriptor> GetPolicyDomainDescriptor(
46       PolicyDomain domain) 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 current descriptor.
81   scoped_refptr<const PolicyDomainDescriptor>
82       domain_descriptors_[POLICY_DOMAIN_SIZE];
83
84   // Maps each policy domain to its observer list.
85   ObserverMap observers_;
86
87   // True if all the providers are initialized for the indexed policy domain.
88   bool initialization_complete_[POLICY_DOMAIN_SIZE];
89
90   // Set of providers that have a pending update that was triggered by a
91   // call to RefreshPolicies().
92   std::set<ConfigurationPolicyProvider*> refresh_pending_;
93
94   // List of callbacks to invoke once all providers refresh after a
95   // RefreshPolicies() call.
96   std::vector<base::Closure> refresh_callbacks_;
97
98   // Used to create tasks to delay new policy updates while we may be already
99   // processing previous policy updates.
100   base::WeakPtrFactory<PolicyServiceImpl> update_task_ptr_factory_;
101
102   DISALLOW_COPY_AND_ASSIGN(PolicyServiceImpl);
103 };
104
105 }  // namespace policy
106
107 #endif  // CHROME_BROWSER_POLICY_POLICY_SERVICE_IMPL_H_