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 "components/bookmarks/common/bookmark_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/components_strings.h"
14 #include "policy/policy_constants.h"
19 const char ManagedBookmarksPolicyHandler::kName
[] = "name";
20 const char ManagedBookmarksPolicyHandler::kUrl
[] = "url";
21 const char ManagedBookmarksPolicyHandler::kChildren
[] = "children";
23 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
25 : SchemaValidatingPolicyHandler(
26 key::kManagedBookmarks
,
27 chrome_schema
.GetKnownProperty(key::kManagedBookmarks
),
28 SCHEMA_ALLOW_INVALID
) {}
30 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
32 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
33 const PolicyMap
& policies
,
34 PrefValueMap
* prefs
) {
35 scoped_ptr
<base::Value
> value
;
36 if (!CheckAndGetValue(policies
, NULL
, &value
))
39 base::ListValue
* list
= NULL
;
40 if (!value
|| !value
->GetAsList(&list
))
43 FilterBookmarks(list
);
44 prefs
->SetValue(prefs::kManagedBookmarks
, value
.release());
47 void ManagedBookmarksPolicyHandler::FilterBookmarks(base::ListValue
* list
) {
48 // Remove any non-conforming values found.
49 base::ListValue::iterator it
= list
->begin();
50 while (it
!= list
->end()) {
51 base::DictionaryValue
* dict
= NULL
;
52 if (!*it
|| !(*it
)->GetAsDictionary(&dict
)) {
53 it
= list
->Erase(it
, NULL
);
59 base::ListValue
* children
= NULL
;
60 // Every bookmark must have a name, and then either a URL of a list of
62 if (!dict
->GetString(kName
, &name
) ||
63 (!dict
->GetList(kChildren
, &children
) &&
64 !dict
->GetString(kUrl
, &url
))) {
65 it
= list
->Erase(it
, NULL
);
70 // Ignore the URL if this bookmark has child nodes.
71 dict
->Remove(kUrl
, NULL
);
72 FilterBookmarks(children
);
74 // Make sure the URL is valid before passing a bookmark to the pref.
75 dict
->Remove(kChildren
, NULL
);
76 GURL gurl
= URLFixerUpper::FixupURL(url
, "");
77 if (!gurl
.is_valid()) {
78 it
= list
->Erase(it
, NULL
);
81 dict
->SetString(kUrl
, gurl
.spec());