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 "components/policy/core/browser/managed_bookmarks_tracker.h" | 5 #include "components/policy/core/browser/managed_bookmarks_tracker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 namespace policy { | 22 namespace policy { |
23 | 23 |
24 const char ManagedBookmarksTracker::kName[] = "name"; | 24 const char ManagedBookmarksTracker::kName[] = "name"; |
25 const char ManagedBookmarksTracker::kUrl[] = "url"; | 25 const char ManagedBookmarksTracker::kUrl[] = "url"; |
26 const char ManagedBookmarksTracker::kChildren[] = "children"; | 26 const char ManagedBookmarksTracker::kChildren[] = "children"; |
27 | 27 |
28 ManagedBookmarksTracker::ManagedBookmarksTracker( | 28 ManagedBookmarksTracker::ManagedBookmarksTracker( |
29 BookmarkModel* model, | 29 BookmarkModel* model, |
30 PrefService* prefs, | 30 PrefService* prefs, |
31 bool is_supervised, | |
31 const GetManagementDomainCallback& callback) | 32 const GetManagementDomainCallback& callback) |
32 : model_(model), | 33 : model_(model), |
34 is_supervised_(is_supervised), | |
33 managed_node_(NULL), | 35 managed_node_(NULL), |
34 prefs_(prefs), | 36 prefs_(prefs), |
35 get_management_domain_callback_(callback) { | 37 get_management_domain_callback_(callback) { |
36 } | 38 } |
37 | 39 |
38 ManagedBookmarksTracker::~ManagedBookmarksTracker() {} | 40 ManagedBookmarksTracker::~ManagedBookmarksTracker() {} |
39 | 41 |
40 scoped_ptr<base::ListValue> | 42 scoped_ptr<base::ListValue> |
41 ManagedBookmarksTracker::GetInitialManagedBookmarks() { | 43 ManagedBookmarksTracker::GetInitialManagedBookmarks() { |
42 const base::ListValue* list = | 44 const base::ListValue* list = prefs_->GetList(GetPrefName()); |
43 prefs_->GetList(bookmarks::prefs::kManagedBookmarks); | |
44 return make_scoped_ptr(list->DeepCopy()); | 45 return make_scoped_ptr(list->DeepCopy()); |
45 } | 46 } |
46 | 47 |
47 // static | 48 // static |
48 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder, | 49 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder, |
49 const base::ListValue* list, | 50 const base::ListValue* list, |
50 int64 next_node_id) { | 51 int64 next_node_id) { |
51 for (size_t i = 0; i < list->GetSize(); ++i) { | 52 for (size_t i = 0; i < list->GetSize(); ++i) { |
52 // Extract the data for the next bookmark from the |list|. | 53 // Extract the data for the next bookmark from the |list|. |
53 base::string16 title; | 54 base::string16 title; |
(...skipping 14 matching lines...) Expand all Loading... | |
68 child->set_date_added(base::Time::Now()); | 69 child->set_date_added(base::Time::Now()); |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 return next_node_id; | 73 return next_node_id; |
73 } | 74 } |
74 | 75 |
75 void ManagedBookmarksTracker::Init(BookmarkPermanentNode* managed_node) { | 76 void ManagedBookmarksTracker::Init(BookmarkPermanentNode* managed_node) { |
76 managed_node_ = managed_node; | 77 managed_node_ = managed_node; |
77 registrar_.Init(prefs_); | 78 registrar_.Init(prefs_); |
78 registrar_.Add(bookmarks::prefs::kManagedBookmarks, | 79 registrar_.Add(GetPrefName(), |
79 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks, | 80 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks, |
80 base::Unretained(this))); | 81 base::Unretained(this))); |
81 // Reload now just in case something changed since the initial load started. | 82 // Reload now just in case something changed since the initial load started. |
82 ReloadManagedBookmarks(); | 83 ReloadManagedBookmarks(); |
83 } | 84 } |
84 | 85 |
86 // static | |
87 const char* ManagedBookmarksTracker::GetPrefName(bool is_supervised) { | |
88 return is_supervised ? bookmarks::prefs::kSupervisedBookmarks | |
89 : bookmarks::prefs::kManagedBookmarks; | |
90 } | |
91 | |
92 const char* ManagedBookmarksTracker::GetPrefName() const { | |
93 return GetPrefName(is_supervised_); | |
94 } | |
95 | |
96 base::string16 ManagedBookmarksTracker::GetBookmarksFolderTitle() const { | |
97 if (is_supervised_) { | |
98 return l10n_util::GetStringUTF16( | |
99 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME); | |
100 } else { | |
101 std::string domain = get_management_domain_callback_.Run(); | |
102 if (domain.empty()) { | |
103 return l10n_util::GetStringUTF16( | |
104 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME); | |
105 } else { | |
106 return l10n_util::GetStringFUTF16( | |
107 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME, | |
108 base::UTF8ToUTF16(domain)); | |
109 } | |
110 } | |
111 } | |
112 | |
85 void ManagedBookmarksTracker::ReloadManagedBookmarks() { | 113 void ManagedBookmarksTracker::ReloadManagedBookmarks() { |
86 // Update the managed bookmarks folder title, in case the user just signed | 114 // In case the user just signed into or out of a managed account. |
87 // into or out of a managed account. | 115 model_->SetTitle(managed_node_, GetBookmarksFolderTitle()); |
88 base::string16 title; | |
89 std::string domain = get_management_domain_callback_.Run(); | |
90 if (domain.empty()) { | |
91 title = | |
92 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME); | |
93 } else { | |
94 title = l10n_util::GetStringFUTF16( | |
95 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME, base::UTF8ToUTF16(domain)); | |
96 } | |
97 model_->SetTitle(managed_node_, title); | |
98 | 116 |
99 // Recursively update all the managed bookmarks and folders. | 117 // Recursively update all the managed bookmarks and folders. |
100 const base::ListValue* list = | 118 const base::ListValue* list = prefs_->GetList(GetPrefName()); |
101 prefs_->GetList(bookmarks::prefs::kManagedBookmarks); | |
102 UpdateBookmarks(managed_node_, list); | 119 UpdateBookmarks(managed_node_, list); |
103 | 120 |
104 // The managed bookmarks folder isn't visible when that policy isn't present. | 121 // The managed bookmarks folder isn't visible when that policy isn't present. |
105 managed_node_->set_visible(!managed_node_->empty()); | 122 managed_node_->set_visible(!managed_node_->empty()); |
106 } | 123 } |
107 | 124 |
108 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode* folder, | 125 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode* folder, |
109 const base::ListValue* list) { | 126 const base::ListValue* list) { |
110 int folder_index = 0; | 127 int folder_index = 0; |
111 for (size_t i = 0; i < list->GetSize(); ++i) { | 128 for (size_t i = 0; i < list->GetSize(); ++i) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 *children = NULL; | 185 *children = NULL; |
169 const base::DictionaryValue* dict = NULL; | 186 const base::DictionaryValue* dict = NULL; |
170 if (!list->GetDictionary(index, &dict) || | 187 if (!list->GetDictionary(index, &dict) || |
171 !dict->GetString(kName, title) || | 188 !dict->GetString(kName, title) || |
172 (!dict->GetString(kUrl, &spec) && | 189 (!dict->GetString(kUrl, &spec) && |
173 !dict->GetList(kChildren, children))) { | 190 !dict->GetList(kChildren, children))) { |
174 // Should never happen after policy validation. | 191 // Should never happen after policy validation. |
175 NOTREACHED(); | 192 NOTREACHED(); |
176 return false; | 193 return false; |
177 } | 194 } |
178 if (!*children) | 195 if (!*children) { |
179 *url = GURL(spec); | 196 *url = GURL(spec); |
197 if (!url->is_valid()) | |
198 return false; | |
Pam (message me for reviews)
2015/01/14 14:03:37
Adding validation to an existing set of users' dat
Marc Treib
2015/01/14 16:40:50
The policy checks for valid URLs elsewhere, so thi
| |
199 } | |
180 return true; | 200 return true; |
181 } | 201 } |
182 | 202 |
183 } // namespace policy | 203 } // namespace policy |
OLD | NEW |