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 "components/bookmarks/common/bookmark_pref_names.h"
10 #include "components/bookmarks/managed/managed_bookmarks_tracker.h"
11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "components/url_fixer/url_fixer.h"
14 #include "policy/policy_constants.h"
17 using bookmarks::ManagedBookmarksTracker
;
21 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
23 : SchemaValidatingPolicyHandler(
24 key::kManagedBookmarks
,
25 chrome_schema
.GetKnownProperty(key::kManagedBookmarks
),
26 SCHEMA_ALLOW_INVALID
) {}
28 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
30 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
31 const PolicyMap
& policies
,
32 PrefValueMap
* prefs
) {
33 scoped_ptr
<base::Value
> value
;
34 if (!CheckAndGetValue(policies
, NULL
, &value
))
37 base::ListValue
* list
= NULL
;
38 if (!value
|| !value
->GetAsList(&list
))
41 FilterBookmarks(list
);
42 prefs
->SetValue(bookmarks::prefs::kManagedBookmarks
, value
.Pass());
45 void ManagedBookmarksPolicyHandler::FilterBookmarks(base::ListValue
* list
) {
46 // Remove any non-conforming values found.
47 base::ListValue::iterator it
= list
->begin();
48 while (it
!= list
->end()) {
49 base::DictionaryValue
* dict
= NULL
;
50 if (!*it
|| !(*it
)->GetAsDictionary(&dict
)) {
51 it
= list
->Erase(it
, NULL
);
57 base::ListValue
* children
= NULL
;
58 // Every bookmark must have a name, and then either a URL of a list of
60 if (!dict
->GetString(ManagedBookmarksTracker::kName
, &name
) ||
61 (!dict
->GetList(ManagedBookmarksTracker::kChildren
, &children
) &&
62 !dict
->GetString(ManagedBookmarksTracker::kUrl
, &url
))) {
63 it
= list
->Erase(it
, NULL
);
68 // Ignore the URL if this bookmark has child nodes.
69 dict
->Remove(ManagedBookmarksTracker::kUrl
, NULL
);
70 FilterBookmarks(children
);
72 // Make sure the URL is valid before passing a bookmark to the pref.
73 dict
->Remove(ManagedBookmarksTracker::kChildren
, NULL
);
74 GURL gurl
= url_fixer::FixupURL(url
, "");
75 if (!gurl
.is_valid()) {
76 it
= list
->Erase(it
, NULL
);
79 dict
->SetString(ManagedBookmarksTracker::kUrl
, gurl
.spec());