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 "components/policy/core/browser/managed_bookmarks_tracker.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "components/bookmarks/browser/bookmark_model.h"
16 #include "components/bookmarks/browser/bookmark_node.h"
17 #include "components/bookmarks/common/bookmark_pref_names.h"
18 #include "grit/components_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
24 const char ManagedBookmarksTracker::kName[] = "name";
25 const char ManagedBookmarksTracker::kUrl[] = "url";
26 const char ManagedBookmarksTracker::kChildren[] = "children";
28 ManagedBookmarksTracker::ManagedBookmarksTracker(
31 const GetManagementDomainCallback& callback)
35 get_management_domain_callback_(callback) {
38 ManagedBookmarksTracker::~ManagedBookmarksTracker() {}
40 scoped_ptr<base::ListValue>
41 ManagedBookmarksTracker::GetInitialManagedBookmarks() {
42 const base::ListValue* list =
43 prefs_->GetList(bookmarks::prefs::kManagedBookmarks);
44 return make_scoped_ptr(list->DeepCopy());
48 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder,
49 const base::ListValue* list,
51 for (size_t i = 0; i < list->GetSize(); ++i) {
52 // Extract the data for the next bookmark from the |list|.
55 const base::ListValue* children = NULL;
56 if (!LoadBookmark(list, i, &title, &url, &children))
59 BookmarkNode* child = new BookmarkNode(next_node_id++, url);
60 child->SetTitle(title);
61 folder->Add(child, folder->child_count());
63 child->set_type(BookmarkNode::FOLDER);
64 child->set_date_folder_modified(base::Time::Now());
65 next_node_id = LoadInitial(child, children, next_node_id);
67 child->set_type(BookmarkNode::URL);
68 child->set_date_added(base::Time::Now());
75 void ManagedBookmarksTracker::Init(BookmarkPermanentNode* managed_node) {
76 managed_node_ = managed_node;
77 registrar_.Init(prefs_);
78 registrar_.Add(bookmarks::prefs::kManagedBookmarks,
79 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks,
80 base::Unretained(this)));
81 // Reload now just in case something changed since the initial load started.
82 ReloadManagedBookmarks();
85 void ManagedBookmarksTracker::ReloadManagedBookmarks() {
86 // Update the managed bookmarks folder title, in case the user just signed
87 // into or out of a managed account.
89 std::string domain = get_management_domain_callback_.Run();
92 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME);
94 title = l10n_util::GetStringFUTF16(
95 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME, base::UTF8ToUTF16(domain));
97 model_->SetTitle(managed_node_, title);
99 // Recursively update all the managed bookmarks and folders.
100 const base::ListValue* list =
101 prefs_->GetList(bookmarks::prefs::kManagedBookmarks);
102 UpdateBookmarks(managed_node_, list);
104 // The managed bookmarks folder isn't visible when that policy isn't present.
105 managed_node_->set_visible(!managed_node_->empty());
108 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode* folder,
109 const base::ListValue* list) {
110 int folder_index = 0;
111 for (size_t i = 0; i < list->GetSize(); ++i) {
112 // Extract the data for the next bookmark from the |list|.
113 base::string16 title;
115 const base::ListValue* children = NULL;
116 if (!LoadBookmark(list, i, &title, &url, &children)) {
117 // Skip this bookmark from |list| but don't advance |folder_index|.
121 // Look for a bookmark at |folder_index| or ahead that matches the current
122 // bookmark from the pref.
123 const BookmarkNode* existing = NULL;
124 for (int k = folder_index; k < folder->child_count(); ++k) {
125 const BookmarkNode* node = folder->GetChild(k);
126 if (node->GetTitle() == title &&
127 ((children && node->is_folder()) ||
128 (!children && node->url() == url))) {
135 // Reuse the existing node. The Move() is a nop if |existing| is already
136 // at |folder_index|.
137 model_->Move(existing, folder, folder_index);
139 UpdateBookmarks(existing, children);
141 // Create a new node for this bookmark now.
143 const BookmarkNode* sub =
144 model_->AddFolder(folder, folder_index, title);
145 UpdateBookmarks(sub, children);
147 model_->AddURL(folder, folder_index, title, url);
151 // The |folder_index| index of |folder| has been updated, so advance it.
155 // Remove any extra children of |folder| that haven't been reused.
156 while (folder->child_count() != folder_index)
157 model_->Remove(folder, folder_index);
161 bool ManagedBookmarksTracker::LoadBookmark(const base::ListValue* list,
163 base::string16* title,
165 const base::ListValue** children) {
169 const base::DictionaryValue* dict = NULL;
170 if (!list->GetDictionary(index, &dict) ||
171 !dict->GetString(kName, title) ||
172 (!dict->GetString(kUrl, &spec) &&
173 !dict->GetList(kChildren, children))) {
174 // Should never happen after policy validation.
183 } // namespace policy