Assume the foo_test_run depends on everything needed to run the test.
[chromium-blink-merge.git] / chromeos / network / policy_applicator.h
blobdb9ed52ec0d333b052c4c319278c2146570bb2ac
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.
5 #ifndef CHROMEOS_NETWORK_POLICY_APPLICATOR_H_
6 #define CHROMEOS_NETWORK_POLICY_APPLICATOR_H_
8 #include <map>
9 #include <set>
10 #include <string>
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"
18 namespace chromeos {
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;
37 virtual void UpdateExistingConfigurationWithPropertiesFromPolicy(
38 const base::DictionaryValue& existing_properties,
39 const base::DictionaryValue& new_properties) = 0;
41 // Called after all policies were applied. At this point, the list of
42 // networks should be updated.
43 virtual void OnPoliciesApplied() = 0;
45 private:
46 DISALLOW_ASSIGN(ConfigurationHandler);
49 typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap;
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);
58 void Run();
60 private:
61 friend class base::RefCounted<PolicyApplicator>;
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);
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);
74 // Sends Shill the command to delete profile entry |entry| from |profile_|.
75 void DeleteEntry(const std::string& entry);
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);
83 // Adds properties to |properties_to_update|, which are enforced on an
84 // unamaged network by the global network config of the policy.
85 // |entry_properties| are the network's current properties read from its
86 // profile entry.
87 void GetPropertiesForUnmanagedEntry(
88 const base::DictionaryValue& entry_properties,
89 base::DictionaryValue* properties_to_update) const;
91 // Called once all Profile entries are processed. Calls
92 // ApplyRemainingPolicies.
93 virtual ~PolicyApplicator();
95 // Creates new entries for all remaining policies, i.e. for which no matching
96 // Profile entry was found.
97 void ApplyRemainingPolicies();
99 std::set<std::string> remaining_policies_;
100 base::WeakPtr<ConfigurationHandler> handler_;
101 NetworkProfile profile_;
102 GuidToPolicyMap all_policies_;
103 base::DictionaryValue global_network_config_;
104 ScopedVector<base::DictionaryValue> new_shill_configurations_;
106 DISALLOW_COPY_AND_ASSIGN(PolicyApplicator);
109 } // namespace chromeos
111 #endif // CHROMEOS_NETWORK_POLICY_APPLICATOR_H_