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/bookmarks/managed/managed_bookmarks_tracker.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "components/bookmarks/common/bookmark_pref_names.h"
19 #include "grit/components_strings.h"
20 #include "ui/base/l10n/l10n_util.h"
25 const char ManagedBookmarksTracker::kName
[] = "name";
26 const char ManagedBookmarksTracker::kUrl
[] = "url";
27 const char ManagedBookmarksTracker::kChildren
[] = "children";
29 ManagedBookmarksTracker::ManagedBookmarksTracker(
33 const GetManagementDomainCallback
& callback
)
35 is_supervised_(is_supervised
),
38 get_management_domain_callback_(callback
) {
41 ManagedBookmarksTracker::~ManagedBookmarksTracker() {}
43 scoped_ptr
<base::ListValue
>
44 ManagedBookmarksTracker::GetInitialManagedBookmarks() {
45 const base::ListValue
* list
= prefs_
->GetList(GetPrefName());
46 return make_scoped_ptr(list
->DeepCopy());
50 int64
ManagedBookmarksTracker::LoadInitial(BookmarkNode
* folder
,
51 const base::ListValue
* list
,
53 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
54 // Extract the data for the next bookmark from the |list|.
57 const base::ListValue
* children
= NULL
;
58 if (!LoadBookmark(list
, i
, &title
, &url
, &children
))
61 BookmarkNode
* child
= new BookmarkNode(next_node_id
++, url
);
62 child
->SetTitle(title
);
63 folder
->Add(child
, folder
->child_count());
65 child
->set_type(BookmarkNode::FOLDER
);
66 child
->set_date_folder_modified(base::Time::Now());
67 next_node_id
= LoadInitial(child
, children
, next_node_id
);
69 child
->set_type(BookmarkNode::URL
);
70 child
->set_date_added(base::Time::Now());
77 void ManagedBookmarksTracker::Init(BookmarkPermanentNode
* managed_node
) {
78 managed_node_
= managed_node
;
79 registrar_
.Init(prefs_
);
80 registrar_
.Add(GetPrefName(),
81 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks
,
82 base::Unretained(this)));
83 // Reload now just in case something changed since the initial load started.
84 ReloadManagedBookmarks();
88 const char* ManagedBookmarksTracker::GetPrefName(bool is_supervised
) {
89 return is_supervised
? prefs::kSupervisedBookmarks
90 : prefs::kManagedBookmarks
;
93 const char* ManagedBookmarksTracker::GetPrefName() const {
94 return GetPrefName(is_supervised_
);
97 base::string16
ManagedBookmarksTracker::GetBookmarksFolderTitle() const {
99 return l10n_util::GetStringUTF16(
100 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME
);
102 const std::string domain
= get_management_domain_callback_
.Run();
103 if (domain
.empty()) {
104 return l10n_util::GetStringUTF16(
105 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME
);
107 return l10n_util::GetStringFUTF16(
108 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME
,
109 base::UTF8ToUTF16(domain
));
114 void ManagedBookmarksTracker::ReloadManagedBookmarks() {
115 // In case the user just signed into or out of the account.
116 model_
->SetTitle(managed_node_
, GetBookmarksFolderTitle());
118 // Recursively update all the managed bookmarks and folders.
119 const base::ListValue
* list
= prefs_
->GetList(GetPrefName());
120 UpdateBookmarks(managed_node_
, list
);
122 // The managed bookmarks folder isn't visible when that pref isn't present.
123 managed_node_
->set_visible(!managed_node_
->empty());
126 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode
* folder
,
127 const base::ListValue
* list
) {
128 int folder_index
= 0;
129 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
130 // Extract the data for the next bookmark from the |list|.
131 base::string16 title
;
133 const base::ListValue
* children
= NULL
;
134 if (!LoadBookmark(list
, i
, &title
, &url
, &children
)) {
135 // Skip this bookmark from |list| but don't advance |folder_index|.
139 // Look for a bookmark at |folder_index| or ahead that matches the current
140 // bookmark from the pref.
141 const BookmarkNode
* existing
= NULL
;
142 for (int k
= folder_index
; k
< folder
->child_count(); ++k
) {
143 const BookmarkNode
* node
= folder
->GetChild(k
);
144 if (node
->GetTitle() == title
&&
145 ((children
&& node
->is_folder()) ||
146 (!children
&& node
->url() == url
))) {
153 // Reuse the existing node. The Move() is a nop if |existing| is already
154 // at |folder_index|.
155 model_
->Move(existing
, folder
, folder_index
);
157 UpdateBookmarks(existing
, children
);
159 // Create a new node for this bookmark now.
161 const BookmarkNode
* sub
=
162 model_
->AddFolder(folder
, folder_index
, title
);
163 UpdateBookmarks(sub
, children
);
165 model_
->AddURL(folder
, folder_index
, title
, url
);
169 // The |folder_index| index of |folder| has been updated, so advance it.
173 // Remove any extra children of |folder| that haven't been reused.
174 while (folder
->child_count() != folder_index
)
175 model_
->Remove(folder
, folder_index
);
179 bool ManagedBookmarksTracker::LoadBookmark(const base::ListValue
* list
,
181 base::string16
* title
,
183 const base::ListValue
** children
) {
187 const base::DictionaryValue
* dict
= NULL
;
188 if (!list
->GetDictionary(index
, &dict
) ||
189 !dict
->GetString(kName
, title
) ||
190 (!dict
->GetString(kUrl
, &spec
) &&
191 !dict
->GetList(kChildren
, children
))) {
192 // Should never happen after policy validation.
198 DCHECK(url
->is_valid());
203 } // namespace policy