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