| OLD | NEW |
| 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 // 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; |
| 55 } |
| 56 |
| 90 std::string name; | 57 std::string name; |
| 91 std::string url; | 58 std::string url; |
| 92 if (*it && GetBookmark(**it, &name, &url)) { | 59 base::ListValue* children = NULL; |
| 93 base::DictionaryValue* dict = new base::DictionaryValue(); | 60 // Every bookmark must have a name, and then either a URL of a list of |
| 94 dict->SetString(kName, name); | 61 // child bookmarks. |
| 95 dict->SetString(kUrl, url); | 62 if (!dict->GetString(kName, &name) || |
| 96 bookmarks->Append(dict); | 63 (!dict->GetList(kChildren, &children) && |
| 64 !dict->GetString(kUrl, &url))) { |
| 65 it = list->Erase(it, NULL); |
| 66 continue; |
| 97 } | 67 } |
| 68 |
| 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; |
| 80 } |
| 81 dict->SetString(kUrl, gurl.spec()); |
| 82 } |
| 83 |
| 84 ++it; |
| 98 } | 85 } |
| 99 | |
| 100 prefs->SetValue(prefs::kManagedBookmarks, bookmarks); | |
| 101 } | 86 } |
| 102 | 87 |
| 103 } // namespace policy | 88 } // namespace policy |
| OLD | NEW |