1// Copyright 2014 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#include "components/policy/core/browser/url_blacklist_policy_handler.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "base/prefs/pref_value_map.h"
9#include "base/values.h"
10#include "components/policy/core/browser/policy_error_map.h"
11#include "components/policy/core/common/policy_map.h"
12#include "components/policy/core/common/policy_pref_names.h"
13#include "grit/components_strings.h"
14#include "policy/policy_constants.h"
15
16namespace policy {
17
18URLBlacklistPolicyHandler::URLBlacklistPolicyHandler() {}
19
20URLBlacklistPolicyHandler::~URLBlacklistPolicyHandler() {}
21
22bool URLBlacklistPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
23                                                    PolicyErrorMap* errors) {
24  const base::Value* disabled_schemes =
25      policies.GetValue(key::kDisabledSchemes);
26  const base::Value* url_blacklist = policies.GetValue(key::kURLBlacklist);
27
28  if (disabled_schemes && !disabled_schemes->IsType(base::Value::TYPE_LIST)) {
29    errors->AddError(key::kDisabledSchemes,
30                     IDS_POLICY_TYPE_ERROR,
31                     ValueTypeToString(base::Value::TYPE_LIST));
32  }
33
34  if (url_blacklist && !url_blacklist->IsType(base::Value::TYPE_LIST)) {
35    errors->AddError(key::kURLBlacklist,
36                     IDS_POLICY_TYPE_ERROR,
37                     ValueTypeToString(base::Value::TYPE_LIST));
38  }
39
40  return true;
41}
42
43void URLBlacklistPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
44                                                    PrefValueMap* prefs) {
45  const base::Value* url_blacklist_policy =
46      policies.GetValue(key::kURLBlacklist);
47  const base::ListValue* url_blacklist = NULL;
48  if (url_blacklist_policy)
49    url_blacklist_policy->GetAsList(&url_blacklist);
50  const base::Value* disabled_schemes_policy =
51      policies.GetValue(key::kDisabledSchemes);
52  const base::ListValue* disabled_schemes = NULL;
53  if (disabled_schemes_policy)
54    disabled_schemes_policy->GetAsList(&disabled_schemes);
55
56  scoped_ptr<base::ListValue> merged_url_blacklist(new base::ListValue());
57
58  // We start with the DisabledSchemes because we have size limit when
59  // handling URLBlacklists.
60  if (disabled_schemes) {
61    for (base::ListValue::const_iterator entry(disabled_schemes->begin());
62         entry != disabled_schemes->end(); ++entry) {
63      std::string entry_value;
64      if ((*entry)->GetAsString(&entry_value)) {
65        entry_value.append("://*");
66        merged_url_blacklist->AppendString(entry_value);
67      }
68    }
69  }
70
71  if (url_blacklist) {
72    for (base::ListValue::const_iterator entry(url_blacklist->begin());
73         entry != url_blacklist->end(); ++entry) {
74      if ((*entry)->IsType(base::Value::TYPE_STRING))
75        merged_url_blacklist->Append((*entry)->DeepCopy());
76    }
77  }
78
79  if (disabled_schemes || url_blacklist) {
80    prefs->SetValue(policy_prefs::kUrlBlacklist,
81                    merged_url_blacklist.release());
82  }
83}
84
85}  // namespace policy
86