summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/configuration_policy_reader.cc
blob: f9e39b882dfbc97e0d41d7538a49eecf28a69daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/configuration_policy_reader.h"

#include <map>
#include <vector>

#include "base/stl_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/policy_map.h"

namespace policy {

// This class functions as a container for policy status information used by the
// ConfigurationPolicyReader class. It obtains policy values from a
// ConfigurationPolicyProvider and maps them to their status information.
class ConfigurationPolicyStatusKeeper {
 public:
  ConfigurationPolicyStatusKeeper(ConfigurationPolicyProvider* provider,
                                  PolicyStatusInfo::PolicyLevel policy_level);
  virtual ~ConfigurationPolicyStatusKeeper();

  // Returns a pointer to a DictionaryValue containing the status information
  // of the policy |policy|. The caller acquires ownership of the returned
  // value. Returns NULL if no such policy is stored in this keeper.
  DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const;

 private:
  typedef std::map<ConfigurationPolicyType, PolicyStatusInfo*> PolicyStatusMap;
  typedef std::map<ConfigurationPolicyType, string16> PolicyNameMap;
  typedef ConfigurationPolicyProvider::PolicyDefinitionList
      PolicyDefinitionList;

  // Calls Provide() on the passed in |provider| to get policy values.
  void GetPoliciesFromProvider(ConfigurationPolicyProvider* provider);

  // Mapping from ConfigurationPolicyType to PolicyStatusInfo.
  PolicyStatusMap policy_map_;

  // The level of the policies stored in this keeper (mandatory or
  // recommended).
  PolicyStatusInfo::PolicyLevel policy_level_;

  DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper);
};

// ConfigurationPolicyStatusKeeper
ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper(
    ConfigurationPolicyProvider* provider,
    PolicyStatusInfo::PolicyLevel policy_level) : policy_level_(policy_level) {
  GetPoliciesFromProvider(provider);
}

ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() {
  STLDeleteContainerPairSecondPointers(policy_map_.begin(), policy_map_.end());
  policy_map_.clear();
}

DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus(
    ConfigurationPolicyType policy) const {
  PolicyStatusMap::const_iterator entry = policy_map_.find(policy);
  return entry != policy_map_.end() ?
      (entry->second)->GetDictionaryValue() : NULL;
}

void ConfigurationPolicyStatusKeeper::GetPoliciesFromProvider(
    ConfigurationPolicyProvider* provider) {
  scoped_ptr<PolicyMap> policies(new PolicyMap());
  if (!provider->Provide(policies.get()))
    LOG(WARNING) << "Failed to get policy from provider.";

  PolicyMap::const_iterator policy = policies->begin();
  for ( ; policy != policies->end(); ++policy) {
    string16 name = PolicyStatus::GetPolicyName(policy->first);

    if (name == string16()) {
      NOTREACHED();
    }

    // TODO(simo) actually determine whether the policy is a user or a device
    // one and whether the policy could be enforced or not once this information
    // is available.
    PolicyStatusInfo* info = new PolicyStatusInfo(name,
                                                  PolicyStatusInfo::USER,
                                                  policy_level_,
                                                  policy->second->DeepCopy(),
                                                  PolicyStatusInfo::ENFORCED,
                                                  string16());
    policy_map_[policy->first] = info;
  }
}

// ConfigurationPolicyReader
ConfigurationPolicyReader::ConfigurationPolicyReader(
    ConfigurationPolicyProvider* provider,
    PolicyStatusInfo::PolicyLevel policy_level)
    : provider_(provider),
      policy_level_(policy_level) {
  if (provider_) {
    // Read initial policy.
    policy_keeper_.reset(
        new ConfigurationPolicyStatusKeeper(provider, policy_level));
    registrar_.Init(provider_, this);
  }
}

ConfigurationPolicyReader::~ConfigurationPolicyReader() {
}

void ConfigurationPolicyReader::OnUpdatePolicy() {
  Refresh();
}

void ConfigurationPolicyReader::OnProviderGoingAway() {
  provider_ = NULL;
}

void ConfigurationPolicyReader::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void ConfigurationPolicyReader::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

// static
ConfigurationPolicyReader*
    ConfigurationPolicyReader::CreateManagedPlatformPolicyReader() {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  return new ConfigurationPolicyReader(
    connector->GetManagedPlatformProvider(), PolicyStatusInfo::MANDATORY);
}

// static
ConfigurationPolicyReader*
    ConfigurationPolicyReader::CreateManagedCloudPolicyReader() {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  return new ConfigurationPolicyReader(
      connector->GetManagedCloudProvider(), PolicyStatusInfo::MANDATORY);
}

// static
ConfigurationPolicyReader*
    ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader() {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  return new ConfigurationPolicyReader(
      connector->GetRecommendedPlatformProvider(),
          PolicyStatusInfo::RECOMMENDED);
}

// static
ConfigurationPolicyReader*
    ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader() {
  BrowserPolicyConnector* connector =
      g_browser_process->browser_policy_connector();
  return new ConfigurationPolicyReader(
      connector->GetRecommendedCloudProvider(), PolicyStatusInfo::RECOMMENDED);
}

DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus(
    ConfigurationPolicyType policy) const {
  return policy_keeper_->GetPolicyStatus(policy);
}

ConfigurationPolicyReader::ConfigurationPolicyReader()
    : provider_(NULL),
      policy_level_(PolicyStatusInfo::LEVEL_UNDEFINED),
      policy_keeper_(NULL) {
}

void ConfigurationPolicyReader::Refresh() {
  if (!provider_)
    return;

  // Read policy state into a new keeper and swap out old keeper.
  scoped_ptr<ConfigurationPolicyStatusKeeper> new_keeper(
      new ConfigurationPolicyStatusKeeper(provider_, policy_level_));
  policy_keeper_.reset(new_keeper.release());

  FOR_EACH_OBSERVER(Observer, observers_, OnPolicyValuesChanged());
}

// PolicyStatus
PolicyStatus::PolicyStatus(ConfigurationPolicyReader* managed_platform,
                           ConfigurationPolicyReader* managed_cloud,
                           ConfigurationPolicyReader* recommended_platform,
                           ConfigurationPolicyReader* recommended_cloud)
    : managed_platform_(managed_platform),
      managed_cloud_(managed_cloud),
      recommended_platform_(recommended_platform),
      recommended_cloud_(recommended_cloud) {
}

PolicyStatus::~PolicyStatus() {
}

void PolicyStatus::AddObserver(Observer* observer) const {
  managed_platform_->AddObserver(observer);
  managed_cloud_->AddObserver(observer);
  recommended_platform_->AddObserver(observer);
  recommended_cloud_->AddObserver(observer);
}

void PolicyStatus::RemoveObserver(Observer* observer) const {
  managed_platform_->RemoveObserver(observer);
  managed_cloud_->RemoveObserver(observer);
  recommended_platform_->RemoveObserver(observer);
  recommended_cloud_->RemoveObserver(observer);
}

ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_set) const {
  ListValue* result = new ListValue();
  std::vector<DictionaryValue*> unsent_policies;

  *any_policies_set = false;
  const PolicyDefinitionList* supported_policies =
      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();
  const PolicyDefinitionList::Entry* policy = supported_policies->begin;
  for ( ; policy != supported_policies->end; ++policy) {
    if (!AddPolicyFromReaders(policy->policy_type, result)) {
      PolicyStatusInfo info(ASCIIToUTF16(policy->name),
                            PolicyStatusInfo::SOURCE_TYPE_UNDEFINED,
                            PolicyStatusInfo::LEVEL_UNDEFINED,
                            Value::CreateNullValue(),
                            PolicyStatusInfo::STATUS_UNDEFINED,
                            string16());
      unsent_policies.push_back(info.GetDictionaryValue());
    } else {
      *any_policies_set = true;
    }
  }

  // Add policies that weren't actually sent from providers to list.
  std::vector<DictionaryValue*>::const_iterator info = unsent_policies.begin();
  for ( ; info != unsent_policies.end(); ++info)
    result->Append(*info);

  return result;
}

// static
string16 PolicyStatus::GetPolicyName(ConfigurationPolicyType policy_type) {
  static std::map<ConfigurationPolicyType, string16> name_map;
  static const ConfigurationPolicyProvider::PolicyDefinitionList*
      supported_policies;

  if (!supported_policies) {
    supported_policies =
        ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();

    // Create mapping from ConfigurationPolicyTypes to actual policy names.
    const ConfigurationPolicyProvider::PolicyDefinitionList::Entry* entry =
        supported_policies->begin;
    for ( ; entry != supported_policies->end; ++entry)
      name_map[entry->policy_type] = ASCIIToUTF16(entry->name);
  }

  std::map<ConfigurationPolicyType, string16>::const_iterator entry =
      name_map.find(policy_type);

  if (entry == name_map.end())
    return string16();

  return entry->second;
}

bool PolicyStatus::AddPolicyFromReaders(
    ConfigurationPolicyType policy, ListValue* list) const {
  DictionaryValue* mp_policy =
      managed_platform_->GetPolicyStatus(policy);
  DictionaryValue* mc_policy =
      managed_cloud_->GetPolicyStatus(policy);
  DictionaryValue* rp_policy =
      recommended_platform_->GetPolicyStatus(policy);
  DictionaryValue* rc_policy =
      recommended_cloud_->GetPolicyStatus(policy);

  bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy;

  if (mp_policy)
    list->Append(mp_policy);
  if (mc_policy)
    list->Append(mc_policy);
  if (rp_policy)
    list->Append(rp_policy);
  if (rc_policy)
    list->Append(rc_policy);

  return added_policy;
}

} // namespace policy