Home | History | Annotate | Download | only in network
      1 // Copyright 2013 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 CHROMEOS_NETWORK_POLICY_APPLICATOR_H_
      6 #define CHROMEOS_NETWORK_POLICY_APPLICATOR_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/values.h"
     16 #include "chromeos/network/network_profile.h"
     17 
     18 namespace chromeos {
     19 
     20 // This class compares (entry point is Run()) |modified_policies| with the
     21 // existing entries in the provided Shill profile |profile|. It fetches all
     22 // entries in parallel (GetProfilePropertiesCallback), compares each entry with
     23 // the current policies (GetEntryCallback) and adds all missing policies
     24 // (~PolicyApplicator).
     25 class PolicyApplicator : public base::RefCounted<PolicyApplicator> {
     26  public:
     27   class ConfigurationHandler {
     28     public:
     29      virtual ~ConfigurationHandler() {}
     30      // Write the new configuration with the properties |shill_properties| to
     31      // Shill. This configuration comes from a policy. Any conflicting or
     32      // existing configuration for the same network will have been removed
     33      // before.
     34      virtual void CreateConfigurationFromPolicy(
     35          const base::DictionaryValue& shill_properties) = 0;
     36 
     37      virtual void UpdateExistingConfigurationWithPropertiesFromPolicy(
     38          const base::DictionaryValue& existing_properties,
     39          const base::DictionaryValue& new_properties) = 0;
     40 
     41      // Called after all policies were applied. At this point, the list of
     42      // networks should be updated.
     43      virtual void OnPoliciesApplied() = 0;
     44 
     45     private:
     46      DISALLOW_ASSIGN(ConfigurationHandler);
     47   };
     48 
     49   typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap;
     50 
     51   // |modified_policies| must not be NULL and will be empty afterwards.
     52   PolicyApplicator(base::WeakPtr<ConfigurationHandler> handler,
     53                    const NetworkProfile& profile,
     54                    const GuidToPolicyMap& all_policies,
     55                    const base::DictionaryValue& global_network_config,
     56                    std::set<std::string>* modified_policies);
     57 
     58   void Run();
     59 
     60  private:
     61   friend class base::RefCounted<PolicyApplicator>;
     62 
     63   // Called with the properties of the profile |profile_|. Requests the
     64   // properties of each entry, which are processed by GetEntryCallback.
     65   void GetProfilePropertiesCallback(
     66       const base::DictionaryValue& profile_properties);
     67 
     68   // Called with the properties of the profile entry |entry|. Checks whether the
     69   // entry was previously managed, whether a current policy applies and then
     70   // either updates, deletes or not touches the entry.
     71   void GetEntryCallback(const std::string& entry,
     72                         const base::DictionaryValue& entry_properties);
     73 
     74   // Sends Shill the command to delete profile entry |entry| from |profile_|.
     75   void DeleteEntry(const std::string& entry);
     76 
     77   // Sends the Shill configuration |shill_dictionary| to Shill. If |write_later|
     78   // is true, the configuration is queued for sending until ~PolicyApplicator.
     79   void WriteNewShillConfiguration(const base::DictionaryValue& shill_dictionary,
     80                                   const base::DictionaryValue& policy,
     81                                   bool write_later);
     82 
     83   // Called once all Profile entries are processed. Calls
     84   // ApplyRemainingPolicies.
     85   virtual ~PolicyApplicator();
     86 
     87   // Creates new entries for all remaining policies, i.e. for which no matching
     88   // Profile entry was found.
     89   void ApplyRemainingPolicies();
     90 
     91   std::set<std::string> remaining_policies_;
     92   base::WeakPtr<ConfigurationHandler> handler_;
     93   NetworkProfile profile_;
     94   GuidToPolicyMap all_policies_;
     95   base::DictionaryValue global_network_config_;
     96   ScopedVector<base::DictionaryValue> new_shill_configurations_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(PolicyApplicator);
     99 };
    100 
    101 }  // namespace chromeos
    102 
    103 #endif  // CHROMEOS_NETWORK_POLICY_APPLICATOR_H_
    104