Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: chrome/browser/policy/managed_bookmarks_policy_handler.cc

Issue 307993005: Enable the ManagedBookmarks policy on all platforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h" 5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h"
6 6
7 #include "base/prefs/pref_value_map.h" 7 #include "base/prefs/pref_value_map.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/common/net/url_fixer_upper.h" 9 #include "chrome/common/net/url_fixer_upper.h"
10 #include "chrome/common/pref_names.h" 10 #include "components/bookmarks/common/bookmark_pref_names.h"
11 #include "components/policy/core/browser/policy_error_map.h" 11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h" 12 #include "components/policy/core/common/policy_map.h"
13 #include "grit/components_strings.h" 13 #include "grit/components_strings.h"
14 #include "policy/policy_constants.h" 14 #include "policy/policy_constants.h"
15 #include "url/gurl.h" 15 #include "url/gurl.h"
16 16
17 namespace policy { 17 namespace policy {
18 18
19 namespace {
20
21 bool GetBookmark(const base::Value& value,
22 std::string* name,
23 std::string* url) {
24 const base::DictionaryValue* dict = NULL;
25 if (!value.GetAsDictionary(&dict))
26 return false;
27 std::string url_string;
28 if (!dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kName,
29 name) ||
30 !dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kUrl,
31 &url_string)) {
32 return false;
33 }
34 GURL gurl = URLFixerUpper::FixupURL(url_string, "");
35 if (!gurl.is_valid())
36 return false;
37 *url = gurl.spec();
38 return true;
39 }
40
41 } // namespace
42
43 const char ManagedBookmarksPolicyHandler::kName[] = "name"; 19 const char ManagedBookmarksPolicyHandler::kName[] = "name";
44 const char ManagedBookmarksPolicyHandler::kUrl[] = "url"; 20 const char ManagedBookmarksPolicyHandler::kUrl[] = "url";
21 const char ManagedBookmarksPolicyHandler::kChildren[] = "children";
45 22
46 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler() 23 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
47 : TypeCheckingPolicyHandler(key::kManagedBookmarks, 24 Schema chrome_schema)
48 base::Value::TYPE_LIST) {} 25 : SchemaValidatingPolicyHandler(
26 key::kManagedBookmarks,
27 chrome_schema.GetKnownProperty(key::kManagedBookmarks),
28 SCHEMA_ALLOW_INVALID) {}
49 29
50 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {} 30 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
51 31
52 bool ManagedBookmarksPolicyHandler::CheckPolicySettings(
53 const PolicyMap& policies,
54 PolicyErrorMap* errors) {
55 const base::Value* value = NULL;
56 if (!CheckAndGetValue(policies, errors, &value))
57 return false;
58
59 if (!value)
60 return true;
61
62 const base::ListValue* list = NULL;
63 value->GetAsList(&list);
64 DCHECK(list);
65
66 for (base::ListValue::const_iterator it = list->begin();
67 it != list->end(); ++it) {
68 std::string name;
69 std::string url;
70 if (!*it || !GetBookmark(**it, &name, &url)) {
71 size_t index = it - list->begin();
72 errors->AddError(policy_name(), index, IDS_POLICY_INVALID_BOOKMARK);
73 }
74 }
75
76 return true;
77 }
78
79 void ManagedBookmarksPolicyHandler::ApplyPolicySettings( 32 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
80 const PolicyMap& policies, 33 const PolicyMap& policies,
81 PrefValueMap* prefs) { 34 PrefValueMap* prefs) {
82 const base::Value* value = policies.GetValue(policy_name()); 35 scoped_ptr<base::Value> value;
83 const base::ListValue* list = NULL; 36 if (!CheckAndGetValue(policies, NULL, &value))
37 return;
38
39 base::ListValue* list = NULL;
84 if (!value || !value->GetAsList(&list)) 40 if (!value || !value->GetAsList(&list))
85 return; 41 return;
86 42
87 base::ListValue* bookmarks = new base::ListValue(); 43 FilterBookmarks(list);
88 for (base::ListValue::const_iterator it = list->begin(); 44 prefs->SetValue(prefs::kManagedBookmarks, value.release());
89 it != list->end(); ++it) { 45 }
46
47 void ManagedBookmarksPolicyHandler::FilterBookmarks(base::ListValue* list) {
48 base::ListValue::iterator it = list->begin();
49 while (it != list->end()) {
50 base::DictionaryValue* dict = NULL;
pastarmovj 2014/05/30 14:07:13 I think having a short one liners about the logic
Joao da Silva 2014/05/30 20:54:35 Done.
51 if (!*it || !(*it)->GetAsDictionary(&dict)) {
52 it = list->Erase(it, NULL);
53 continue;
54 }
55
90 std::string name; 56 std::string name;
91 std::string url; 57 std::string url;
92 if (*it && GetBookmark(**it, &name, &url)) { 58 base::ListValue* children = NULL;
93 base::DictionaryValue* dict = new base::DictionaryValue(); 59 if (!dict->GetString(kName, &name) ||
94 dict->SetString(kName, name); 60 (!dict->GetList(kChildren, &children) &&
95 dict->SetString(kUrl, url); 61 !dict->GetString(kUrl, &url))) {
96 bookmarks->Append(dict); 62 it = list->Erase(it, NULL);
63 continue;
97 } 64 }
65
66 if (children) {
67 dict->Remove(kUrl, NULL);
68 FilterBookmarks(children);
69 } else {
70 dict->Remove(kChildren, NULL);
71 GURL gurl = URLFixerUpper::FixupURL(url, "");
72 if (!gurl.is_valid()) {
73 it = list->Erase(it, NULL);
74 continue;
75 }
76 dict->SetString(kUrl, gurl.spec());
77 }
78
79 ++it;
98 } 80 }
99
100 prefs->SetValue(prefs::kManagedBookmarks, bookmarks);
101 } 81 }
102 82
103 } // namespace policy 83 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698