• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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.
4 
5 #include "chrome/browser/policy/configuration_policy_handler_android.h"
6 
7 #include "base/prefs/pref_value_map.h"
8 #include "base/values.h"
9 #include "chrome/common/net/url_fixer_upper.h"
10 #include "chrome/common/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/component_strings.h"
14 #include "policy/policy_constants.h"
15 #include "url/gurl.h"
16 
17 namespace policy {
18 
19 namespace {
20 
GetBookmark(const base::Value & value,std::string * name,std::string * url)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";
44 const char ManagedBookmarksPolicyHandler::kUrl[] = "url";
45 
ManagedBookmarksPolicyHandler()46 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler()
47     : TypeCheckingPolicyHandler(key::kManagedBookmarks,
48                                 base::Value::TYPE_LIST) {}
49 
~ManagedBookmarksPolicyHandler()50 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
51 
CheckPolicySettings(const PolicyMap & policies,PolicyErrorMap * errors)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(), IDS_POLICY_INVALID_BOOKMARK, index);
73     }
74   }
75 
76   return true;
77 }
78 
ApplyPolicySettings(const PolicyMap & policies,PrefValueMap * prefs)79 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
80     const PolicyMap& policies,
81     PrefValueMap* prefs) {
82   const base::Value* value = policies.GetValue(policy_name());
83   const base::ListValue* list = NULL;
84   if (!value || !value->GetAsList(&list))
85     return;
86 
87   base::ListValue* bookmarks = new base::ListValue();
88   for (base::ListValue::const_iterator it = list->begin();
89        it != list->end(); ++it) {
90     std::string name;
91     std::string url;
92     if (*it && GetBookmark(**it, &name, &url)) {
93       base::DictionaryValue* dict = new base::DictionaryValue();
94       dict->SetString(kName, name);
95       dict->SetString(kUrl, url);
96       bookmarks->Append(dict);
97     }
98   }
99 
100   prefs->SetValue(prefs::kManagedBookmarks, bookmarks);
101 }
102 
103 }  // namespace policy
104