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"
21 bool GetBookmark(const base::Value
& value
,
24 const base::DictionaryValue
* dict
= NULL
;
25 if (!value
.GetAsDictionary(&dict
))
27 std::string url_string
;
28 if (!dict
->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kName
,
30 !dict
->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kUrl
,
34 GURL gurl
= URLFixerUpper::FixupURL(url_string
, "");
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
))
62 const base::ListValue
* list
= NULL
;
63 value
->GetAsList(&list
);
66 for (base::ListValue::const_iterator it
= list
->begin();
67 it
!= list
->end(); ++it
) {
70 if (!*it
|| !GetBookmark(**it
, &name
, &url
)) {
71 size_t index
= it
- list
->begin();
72 errors
->AddError(policy_name(), index
, IDS_POLICY_INVALID_BOOKMARK
);
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
))
87 base::ListValue
* bookmarks
= new base::ListValue();
88 for (base::ListValue::const_iterator it
= list
->begin();
89 it
!= list
->end(); ++it
) {
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