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