Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / policy / managed_bookmarks_policy_handler.cc
blobe0370678a85e38b4f09575358b6b9a7e71b11617
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"
15 #include "url/gurl.h"
17 namespace policy {
19 const char ManagedBookmarksPolicyHandler::kName[] = "name";
20 const char ManagedBookmarksPolicyHandler::kUrl[] = "url";
21 const char ManagedBookmarksPolicyHandler::kChildren[] = "children";
23 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
24 Schema chrome_schema)
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))
37 return;
39 base::ListValue* list = NULL;
40 if (!value || !value->GetAsList(&list))
41 return;
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);
54 continue;
57 std::string name;
58 std::string url;
59 base::ListValue* children = NULL;
60 // Every bookmark must have a name, and then either a URL of a list of
61 // child bookmarks.
62 if (!dict->GetString(kName, &name) ||
63 (!dict->GetList(kChildren, &children) &&
64 !dict->GetString(kUrl, &url))) {
65 it = list->Erase(it, NULL);
66 continue;
69 if (children) {
70 // Ignore the URL if this bookmark has child nodes.
71 dict->Remove(kUrl, NULL);
72 FilterBookmarks(children);
73 } else {
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);
79 continue;
81 dict->SetString(kUrl, gurl.spec());
84 ++it;
88 } // namespace policy