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