Fix race condition in WebstoreInstallHelper.
[chromium-blink-merge.git] / chromeos / network / policy_applicator.h
blobbca3b06e9daa7445bbd328f2b0f8eb68719f8464
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/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/values.h"
15 #include "chromeos/network/network_profile.h"
17 namespace chromeos {
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 {
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;
36 virtual void UpdateExistingConfigurationWithPropertiesFromPolicy(
37 const base::DictionaryValue& existing_properties,
38 const base::DictionaryValue& new_properties) = 0;
40 // Called after all policies for |profile| were applied. At this point, the
41 // list of networks should be updated.
42 virtual void OnPoliciesApplied(const NetworkProfile& profile) = 0;
44 private:
45 DISALLOW_ASSIGN(ConfigurationHandler);
48 typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap;
50 // |handler| must outlive this object.
51 // |modified_policies| must not be NULL and will be empty afterwards.
52 PolicyApplicator(const NetworkProfile& profile,
53 const GuidToPolicyMap& all_policies,
54 const base::DictionaryValue& global_network_config,
55 ConfigurationHandler* handler,
56 std::set<std::string>* modified_policies);
58 ~PolicyApplicator();
60 void Run();
62 private:
63 // Removes |entry| from the list of pending profile entries.
64 // If all entries were processed, applies the remaining policies and notifies
65 // |handler_|.
66 void ProfileEntryFinished(const std::string& entry);
68 // Called with the properties of the profile |profile_|. Requests the
69 // properties of each entry, which are processed by GetEntryCallback.
70 void GetProfilePropertiesCallback(
71 const base::DictionaryValue& profile_properties);
72 void GetProfilePropertiesError(const std::string& error_name,
73 const std::string& error_message);
75 // Called with the properties of the profile entry |entry|. Checks whether the
76 // entry was previously managed, whether a current policy applies and then
77 // either updates, deletes or not touches the entry.
78 void GetEntryCallback(const std::string& entry,
79 const base::DictionaryValue& entry_properties);
80 void GetEntryError(const std::string& entry,
81 const std::string& error_name,
82 const std::string& error_message);
84 // Sends Shill the command to delete profile entry |entry| from |profile_|.
85 void DeleteEntry(const std::string& entry);
87 // Sends the Shill configuration |shill_dictionary| to Shill. If |write_later|
88 // is true, the configuration is queued for sending until ~PolicyApplicator.
89 void WriteNewShillConfiguration(const base::DictionaryValue& shill_dictionary,
90 const base::DictionaryValue& policy,
91 bool write_later);
93 // Creates new entries for all remaining policies, i.e. for which no matching
94 // Profile entry was found.
95 // This should only be called if all profile entries were processed.
96 void ApplyRemainingPolicies();
98 // Called after all policies are applied or an error occurred. Notifies
99 // |handler_|.
100 void NotifyConfigurationHandlerAndFinish();
102 std::set<std::string> remaining_policies_;
103 std::set<std::string> pending_get_entry_calls_;
104 ConfigurationHandler* handler_;
105 NetworkProfile profile_;
106 GuidToPolicyMap all_policies_;
107 base::DictionaryValue global_network_config_;
108 ScopedVector<base::DictionaryValue> new_shill_configurations_;
109 base::WeakPtrFactory<PolicyApplicator> weak_ptr_factory_;
111 DISALLOW_COPY_AND_ASSIGN(PolicyApplicator);
114 } // namespace chromeos
116 #endif // CHROMEOS_NETWORK_POLICY_APPLICATOR_H_