Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / policy / managed_bookmarks_policy_handler.cc
blob8c71fd3fcfa0cb9a12dff45b40fe95ea4d029e94
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"
15 #include "url/gurl.h"
17 using bookmarks::ManagedBookmarksTracker;
19 namespace policy {
21 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
22 Schema chrome_schema)
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))
35 return;
37 base::ListValue* list = NULL;
38 if (!value || !value->GetAsList(&list))
39 return;
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);
52 continue;
55 std::string name;
56 std::string url;
57 base::ListValue* children = NULL;
58 // Every bookmark must have a name, and then either a URL of a list of
59 // child bookmarks.
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);
64 continue;
67 if (children) {
68 // Ignore the URL if this bookmark has child nodes.
69 dict->Remove(ManagedBookmarksTracker::kUrl, NULL);
70 FilterBookmarks(children);
71 } else {
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);
77 continue;
79 dict->SetString(ManagedBookmarksTracker::kUrl, gurl.spec());
82 ++it;
86 } // namespace policy