Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / policy_applicator.h
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/scoped_vector.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 {
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     // 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;
43
44    private:
45     DISALLOW_ASSIGN(ConfigurationHandler);
46   };
47
48   typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap;
49
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);
57
58   ~PolicyApplicator();
59
60   void Run();
61
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);
67
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);
74
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);
83
84   // Sends Shill the command to delete profile entry |entry| from |profile_|.
85   void DeleteEntry(const std::string& entry);
86
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);
92
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();
97
98   // Called after all policies are applied or an error occurred. Notifies
99   // |handler_|.
100   void NotifyConfigurationHandlerAndFinish();
101
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_;
110
111   DISALLOW_COPY_AND_ASSIGN(PolicyApplicator);
112 };
113
114 }  // namespace chromeos
115
116 #endif  // CHROMEOS_NETWORK_POLICY_APPLICATOR_H_