- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / policy_service.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_H_
6 #define CHROME_BROWSER_POLICY_POLICY_SERVICE_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/policy/policy_map.h"
15 #include "components/policy/core/common/policy_namespace.h"
16
17 namespace policy {
18
19 class PolicyDomainDescriptor;
20
21 // The PolicyService merges policies from all available sources, taking into
22 // account their priorities. Policy clients can retrieve policy for their domain
23 // and register for notifications on policy updates.
24 //
25 // The PolicyService is available from BrowserProcess as a global singleton.
26 // There is also a PolicyService for browser-wide policies available from
27 // BrowserProcess as a global singleton.
28 class PolicyService {
29  public:
30   class Observer {
31    public:
32     // Invoked whenever policies for the given |ns| namespace are modified.
33     // This is only invoked for changes that happen after AddObserver is called.
34     // |previous| contains the values of the policies before the update,
35     // and |current| contains the current values.
36     virtual void OnPolicyUpdated(const PolicyNamespace& ns,
37                                  const PolicyMap& previous,
38                                  const PolicyMap& current) = 0;
39
40     // Invoked at most once for each |domain|, when the PolicyService becomes
41     // ready. If IsInitializationComplete() is false, then this will be invoked
42     // once all the policy providers have finished loading their policies for
43     // |domain|.
44     virtual void OnPolicyServiceInitialized(PolicyDomain domain) {}
45
46    protected:
47     virtual ~Observer() {}
48   };
49
50   virtual ~PolicyService() {}
51
52   // Observes changes to all components of the given |domain|.
53   virtual void AddObserver(PolicyDomain domain, Observer* observer) = 0;
54
55   virtual void RemoveObserver(PolicyDomain domain, Observer* observer) = 0;
56
57   // Registers the |descriptor| of a policy domain, indicated by
58   // |descriptor->domain()|. This overrides the descriptor previously set, if
59   // there was one.
60   // This registration signals that there is interest in receiving policy for
61   // the components listed in the descriptor. The policy providers will try to
62   // load policy for these components, and validate it against the descriptor.
63   // Cached data for components that aren't registered anymore may be dropped.
64   virtual void RegisterPolicyDomain(
65       scoped_refptr<const PolicyDomainDescriptor> descriptor) = 0;
66
67   virtual const PolicyMap& GetPolicies(const PolicyNamespace& ns) const = 0;
68
69   virtual scoped_refptr<const PolicyDomainDescriptor> GetPolicyDomainDescriptor(
70       PolicyDomain domain) const = 0;
71
72   // The PolicyService loads policy from several sources, and some require
73   // asynchronous loads. IsInitializationComplete() returns true once all
74   // sources have loaded their policies for the given |domain|.
75   // It is safe to read policy from the PolicyService even if
76   // IsInitializationComplete() is false; there will be an OnPolicyUpdated()
77   // notification once new policies become available.
78   //
79   // OnPolicyServiceInitialized() is called when IsInitializationComplete()
80   // becomes true, which happens at most once for each domain.
81   // If IsInitializationComplete() is already true for |domain| when an Observer
82   // is registered, then that Observer will not receive an
83   // OnPolicyServiceInitialized() notification.
84   virtual bool IsInitializationComplete(PolicyDomain domain) const = 0;
85
86   // Asks the PolicyService to reload policy from all available policy sources.
87   // |callback| is invoked once every source has reloaded its policies, and
88   // GetPolicies() is guaranteed to return the updated values at that point.
89   virtual void RefreshPolicies(const base::Closure& callback) = 0;
90 };
91
92 // A registrar that only observes changes to particular policies within the
93 // PolicyMap for the given policy namespace.
94 class PolicyChangeRegistrar : public PolicyService::Observer {
95  public:
96   typedef base::Callback<void(const Value*, const Value*)> UpdateCallback;
97
98   // Observes updates to the given (domain, component_id) namespace in the given
99   // |policy_service|, and notifies |observer| whenever any of the registered
100   // policy keys changes. Both the |policy_service| and the |observer| must
101   // outlive |this|.
102   PolicyChangeRegistrar(PolicyService* policy_service,
103                         const PolicyNamespace& ns);
104
105   virtual ~PolicyChangeRegistrar();
106
107   // Will invoke |callback| whenever |policy_name| changes its value, as long
108   // as this registrar exists.
109   // Only one callback can be registed per policy name; a second call with the
110   // same |policy_name| will overwrite the previous callback.
111   void Observe(const std::string& policy_name, const UpdateCallback& callback);
112
113   // Implementation of PolicyService::Observer:
114   virtual void OnPolicyUpdated(const PolicyNamespace& ns,
115                                const PolicyMap& previous,
116                                const PolicyMap& current) OVERRIDE;
117
118  private:
119   typedef std::map<std::string, UpdateCallback> CallbackMap;
120
121   PolicyService* policy_service_;
122   PolicyNamespace ns_;
123   CallbackMap callback_map_;
124
125   DISALLOW_COPY_AND_ASSIGN(PolicyChangeRegistrar);
126 };
127
128 }  // namespace policy
129
130 #endif  // CHROME_BROWSER_POLICY_POLICY_SERVICE_H_