Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / policy / managed_bookmarks_policy_handler.cc
blob7c4abafdb75e9256147814f3f71a3f548cbea5c2
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.
5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h"
7 #include "base/prefs/pref_value_map.h"
8 #include "base/values.h"
9 #include "chrome/common/net/url_fixer_upper.h"
10 #include "chrome/common/pref_names.h"
11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "grit/component_strings.h"
14 #include "policy/policy_constants.h"
15 #include "url/gurl.h"
17 namespace policy {
19 namespace {
21 bool GetBookmark(const base::Value& value,
22 std::string* name,
23 std::string* url) {
24 const base::DictionaryValue* dict = NULL;
25 if (!value.GetAsDictionary(&dict))
26 return false;
27 std::string url_string;
28 if (!dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kName,
29 name) ||
30 !dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kUrl,
31 &url_string)) {
32 return false;
34 GURL gurl = URLFixerUpper::FixupURL(url_string, "");
35 if (!gurl.is_valid())
36 return false;
37 *url = gurl.spec();
38 return true;
41 } // namespace
43 const char ManagedBookmarksPolicyHandler::kName[] = "name";
44 const char ManagedBookmarksPolicyHandler::kUrl[] = "url";
46 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler()
47 : TypeCheckingPolicyHandler(key::kManagedBookmarks,
48 base::Value::TYPE_LIST) {}
50 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
52 bool ManagedBookmarksPolicyHandler::CheckPolicySettings(
53 const PolicyMap& policies,
54 PolicyErrorMap* errors) {
55 const base::Value* value = NULL;
56 if (!CheckAndGetValue(policies, errors, &value))
57 return false;
59 if (!value)
60 return true;
62 const base::ListValue* list = NULL;
63 value->GetAsList(&list);
64 DCHECK(list);
66 for (base::ListValue::const_iterator it = list->begin();
67 it != list->end(); ++it) {
68 std::string name;
69 std::string url;
70 if (!*it || !GetBookmark(**it, &name, &url)) {
71 size_t index = it - list->begin();
72 errors->AddError(policy_name(), index, IDS_POLICY_INVALID_BOOKMARK);
76 return true;
79 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
80 const PolicyMap& policies,
81 PrefValueMap* prefs) {
82 const base::Value* value = policies.GetValue(policy_name());
83 const base::ListValue* list = NULL;
84 if (!value || !value->GetAsList(&list))
85 return;
87 base::ListValue* bookmarks = new base::ListValue();
88 for (base::ListValue::const_iterator it = list->begin();
89 it != list->end(); ++it) {
90 std::string name;
91 std::string url;
92 if (*it && GetBookmark(**it, &name, &url)) {
93 base::DictionaryValue* dict = new base::DictionaryValue();
94 dict->SetString(kName, name);
95 dict->SetString(kUrl, url);
96 bookmarks->Append(dict);
100 prefs->SetValue(prefs::kManagedBookmarks, bookmarks);
103 } // namespace policy