Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / bookmarks / managed / managed_bookmarks_tracker.cc
blob17eff02f91d22c33cb50633a4e3205140ef594ab
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"
7 #include <string>
9 #include "base/bind.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"
21 #include "url/gurl.h"
23 namespace bookmarks {
25 const char ManagedBookmarksTracker::kName[] = "name";
26 const char ManagedBookmarksTracker::kUrl[] = "url";
27 const char ManagedBookmarksTracker::kChildren[] = "children";
29 ManagedBookmarksTracker::ManagedBookmarksTracker(
30 BookmarkModel* model,
31 PrefService* prefs,
32 bool is_supervised,
33 const GetManagementDomainCallback& callback)
34 : model_(model),
35 is_supervised_(is_supervised),
36 managed_node_(NULL),
37 prefs_(prefs),
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());
49 // static
50 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder,
51 const base::ListValue* list,
52 int64 next_node_id) {
53 for (size_t i = 0; i < list->GetSize(); ++i) {
54 // Extract the data for the next bookmark from the |list|.
55 base::string16 title;
56 GURL url;
57 const base::ListValue* children = NULL;
58 if (!LoadBookmark(list, i, &title, &url, &children))
59 continue;
61 BookmarkNode* child = new BookmarkNode(next_node_id++, url);
62 child->SetTitle(title);
63 folder->Add(child, folder->child_count());
64 if (children) {
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);
68 } else {
69 child->set_type(BookmarkNode::URL);
70 child->set_date_added(base::Time::Now());
74 return next_node_id;
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();
87 // static
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 {
98 if (is_supervised_) {
99 return l10n_util::GetStringUTF16(
100 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME);
101 } else {
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);
106 } else {
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;
132 GURL url;
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|.
136 continue;
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))) {
147 existing = node;
148 break;
152 if (existing) {
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);
156 if (children)
157 UpdateBookmarks(existing, children);
158 } else {
159 // Create a new node for this bookmark now.
160 if (children) {
161 const BookmarkNode* sub =
162 model_->AddFolder(folder, folder_index, title);
163 UpdateBookmarks(sub, children);
164 } else {
165 model_->AddURL(folder, folder_index, title, url);
169 // The |folder_index| index of |folder| has been updated, so advance it.
170 ++folder_index;
173 // Remove any extra children of |folder| that haven't been reused.
174 while (folder->child_count() != folder_index)
175 model_->Remove(folder->GetChild(folder_index));
178 // static
179 bool ManagedBookmarksTracker::LoadBookmark(const base::ListValue* list,
180 size_t index,
181 base::string16* title,
182 GURL* url,
183 const base::ListValue** children) {
184 std::string spec;
185 *url = GURL();
186 *children = NULL;
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.
193 NOTREACHED();
194 return false;
196 if (!*children) {
197 *url = GURL(spec);
198 DCHECK(url->is_valid());
200 return true;
203 } // namespace policy