- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / url_blacklist_policy_handler.cc
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 #include "chrome/browser/policy/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 "chrome/browser/policy/policy_error_map.h"
11 #include "chrome/browser/policy/policy_map.h"
12 #include "chrome/common/pref_names.h"
13 #include "grit/generated_resources.h"
14 #include "policy/policy_constants.h"
15
16 namespace policy {
17
18 URLBlacklistPolicyHandler::URLBlacklistPolicyHandler() {}
19
20 URLBlacklistPolicyHandler::~URLBlacklistPolicyHandler() {}
21
22 bool 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
43 void 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(Value::TYPE_STRING))
75         merged_url_blacklist->Append((*entry)->DeepCopy());
76     }
77   }
78
79   if (disabled_schemes || url_blacklist)
80     prefs->SetValue(prefs::kUrlBlacklist, merged_url_blacklist.release());
81 }
82
83 }  // namespace policy