Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_utils.cc
blob6d60a4f882888152310581f742d4a52353db77df
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/browser/bookmark_utils.h"
7 #include <stdint.h>
8 #include <utility>
10 #include "base/bind.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/files/file_path.h"
13 #include "base/i18n/case_conversion.h"
14 #include "base/i18n/string_search.h"
15 #include "base/macros.h"
16 #include "base/metrics/user_metrics_action.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "components/bookmarks/browser/bookmark_client.h"
23 #include "components/bookmarks/browser/bookmark_model.h"
24 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h"
25 #include "components/bookmarks/common/bookmark_pref_names.h"
26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "components/query_parser/query_parser.h"
28 #include "components/url_formatter/url_formatter.h"
29 #include "ui/base/clipboard/clipboard.h"
30 #include "ui/base/models/tree_node_iterator.h"
31 #include "url/gurl.h"
33 using base::Time;
35 namespace bookmarks {
37 namespace {
39 // The maximum length of URL or title returned by the Cleanup functions.
40 const size_t kCleanedUpUrlMaxLength = 1024u;
41 const size_t kCleanedUpTitleMaxLength = 1024u;
43 void CloneBookmarkNodeImpl(BookmarkModel* model,
44 const BookmarkNodeData::Element& element,
45 const BookmarkNode* parent,
46 int index_to_add_at,
47 bool reset_node_times) {
48 // Make sure to not copy non clonable keys.
49 BookmarkNode::MetaInfoMap meta_info_map = element.meta_info_map;
50 for (const std::string& key : model->non_cloned_keys())
51 meta_info_map.erase(key);
53 if (element.is_url) {
54 Time date_added = reset_node_times ? Time::Now() : element.date_added;
55 DCHECK(!date_added.is_null());
57 model->AddURLWithCreationTimeAndMetaInfo(parent,
58 index_to_add_at,
59 element.title,
60 element.url,
61 date_added,
62 &meta_info_map);
63 } else {
64 const BookmarkNode* cloned_node = model->AddFolderWithMetaInfo(
65 parent, index_to_add_at, element.title, &meta_info_map);
66 if (!reset_node_times) {
67 DCHECK(!element.date_folder_modified.is_null());
68 model->SetDateFolderModified(cloned_node, element.date_folder_modified);
70 for (int i = 0; i < static_cast<int>(element.children.size()); ++i)
71 CloneBookmarkNodeImpl(model, element.children[i], cloned_node, i,
72 reset_node_times);
76 // Comparison function that compares based on date modified of the two nodes.
77 bool MoreRecentlyModified(const BookmarkNode* n1, const BookmarkNode* n2) {
78 return n1->date_folder_modified() > n2->date_folder_modified();
81 // Returns true if |text| contains each string in |words|. This is used when
82 // searching for bookmarks.
83 bool DoesBookmarkTextContainWords(const base::string16& text,
84 const std::vector<base::string16>& words) {
85 for (size_t i = 0; i < words.size(); ++i) {
86 if (!base::i18n::StringSearchIgnoringCaseAndAccents(
87 words[i], text, NULL, NULL)) {
88 return false;
91 return true;
94 // Returns true if |node|s title or url contains the strings in |words|.
95 // |languages| argument is user's accept-language setting to decode IDN.
96 bool DoesBookmarkContainWords(const BookmarkNode* node,
97 const std::vector<base::string16>& words,
98 const std::string& languages) {
99 return DoesBookmarkTextContainWords(node->GetTitle(), words) ||
100 DoesBookmarkTextContainWords(base::UTF8ToUTF16(node->url().spec()),
101 words) ||
102 DoesBookmarkTextContainWords(
103 url_formatter::FormatUrl(
104 node->url(), languages, url_formatter::kFormatUrlOmitNothing,
105 net::UnescapeRule::NORMAL, NULL, NULL, NULL),
106 words);
109 // This is used with a tree iterator to skip subtrees which are not visible.
110 bool PruneInvisibleFolders(const BookmarkNode* node) {
111 return !node->IsVisible();
114 // This traces parents up to root, determines if node is contained in a
115 // selected folder.
116 bool HasSelectedAncestor(BookmarkModel* model,
117 const std::vector<const BookmarkNode*>& selected_nodes,
118 const BookmarkNode* node) {
119 if (!node || model->is_permanent_node(node))
120 return false;
122 for (size_t i = 0; i < selected_nodes.size(); ++i)
123 if (node->id() == selected_nodes[i]->id())
124 return true;
126 return HasSelectedAncestor(model, selected_nodes, node->parent());
129 const BookmarkNode* GetNodeByID(const BookmarkNode* node, int64_t id) {
130 if (node->id() == id)
131 return node;
133 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) {
134 const BookmarkNode* result = GetNodeByID(node->GetChild(i), id);
135 if (result)
136 return result;
138 return NULL;
141 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL
142 // from being in the middle of an escape sequence) to no more than
143 // kCleanedUpUrlMaxLength characters, returning the result.
144 std::string TruncateUrl(const std::string& url) {
145 if (url.length() <= kCleanedUpUrlMaxLength)
146 return url;
148 // If we're in the middle of an escape sequence, truncate just before it.
149 if (url[kCleanedUpUrlMaxLength - 1] == '%')
150 return url.substr(0, kCleanedUpUrlMaxLength - 1);
151 if (url[kCleanedUpUrlMaxLength - 2] == '%')
152 return url.substr(0, kCleanedUpUrlMaxLength - 2);
154 return url.substr(0, kCleanedUpUrlMaxLength);
157 // Returns the URL from the clipboard. If there is no URL an empty URL is
158 // returned.
159 GURL GetUrlFromClipboard() {
160 base::string16 url_text;
161 #if !defined(OS_IOS)
162 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
163 &url_text);
164 #endif
165 return GURL(url_text);
168 class VectorIterator {
169 public:
170 explicit VectorIterator(std::vector<const BookmarkNode*>* nodes)
171 : nodes_(nodes), current_(nodes->begin()) {}
172 bool has_next() { return (current_ != nodes_->end()); }
173 const BookmarkNode* Next() {
174 const BookmarkNode* result = *current_;
175 ++current_;
176 return result;
179 private:
180 std::vector<const BookmarkNode*>* nodes_;
181 std::vector<const BookmarkNode*>::iterator current_;
183 DISALLOW_COPY_AND_ASSIGN(VectorIterator);
186 template <class type>
187 void GetBookmarksMatchingPropertiesImpl(
188 type& iterator,
189 BookmarkModel* model,
190 const QueryFields& query,
191 const std::vector<base::string16>& query_words,
192 size_t max_count,
193 const std::string& languages,
194 std::vector<const BookmarkNode*>* nodes) {
195 while (iterator.has_next()) {
196 const BookmarkNode* node = iterator.Next();
197 if ((!query_words.empty() &&
198 !DoesBookmarkContainWords(node, query_words, languages)) ||
199 model->is_permanent_node(node)) {
200 continue;
202 if (query.title && node->GetTitle() != *query.title)
203 continue;
205 nodes->push_back(node);
206 if (nodes->size() == max_count)
207 return;
211 } // namespace
213 QueryFields::QueryFields() {}
214 QueryFields::~QueryFields() {}
216 void CloneBookmarkNode(BookmarkModel* model,
217 const std::vector<BookmarkNodeData::Element>& elements,
218 const BookmarkNode* parent,
219 int index_to_add_at,
220 bool reset_node_times) {
221 if (!parent->is_folder() || !model) {
222 NOTREACHED();
223 return;
225 for (size_t i = 0; i < elements.size(); ++i) {
226 CloneBookmarkNodeImpl(model, elements[i], parent,
227 index_to_add_at + static_cast<int>(i),
228 reset_node_times);
232 void CopyToClipboard(BookmarkModel* model,
233 const std::vector<const BookmarkNode*>& nodes,
234 bool remove_nodes) {
235 if (nodes.empty())
236 return;
238 // Create array of selected nodes with descendants filtered out.
239 std::vector<const BookmarkNode*> filtered_nodes;
240 for (size_t i = 0; i < nodes.size(); ++i)
241 if (!HasSelectedAncestor(model, nodes, nodes[i]->parent()))
242 filtered_nodes.push_back(nodes[i]);
244 BookmarkNodeData(filtered_nodes).
245 WriteToClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE);
247 if (remove_nodes) {
248 ScopedGroupBookmarkActions group_cut(model);
249 for (size_t i = 0; i < filtered_nodes.size(); ++i) {
250 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]);
251 if (index > -1)
252 model->Remove(filtered_nodes[i]);
257 // Updates |title| such that |url| and |title| pair are unique among the
258 // children of |parent|.
259 void MakeTitleUnique(const BookmarkModel* model,
260 const BookmarkNode* parent,
261 const GURL& url,
262 base::string16* title) {
263 base::hash_set<base::string16> titles;
264 base::string16 original_title_lower = base::i18n::ToLower(*title);
265 for (int i = 0; i < parent->child_count(); i++) {
266 const BookmarkNode* node = parent->GetChild(i);
267 if (node->is_url() && (url == node->url()) &&
268 base::StartsWith(base::i18n::ToLower(node->GetTitle()),
269 original_title_lower,
270 base::CompareCase::SENSITIVE)) {
271 titles.insert(node->GetTitle());
275 if (titles.find(*title) == titles.end())
276 return;
278 for (size_t i = 0; i < titles.size(); i++) {
279 const base::string16 new_title(*title +
280 base::ASCIIToUTF16(base::StringPrintf(
281 " (%lu)", (unsigned long)(i + 1))));
282 if (titles.find(new_title) == titles.end()) {
283 *title = new_title;
284 return;
287 NOTREACHED();
290 void PasteFromClipboard(BookmarkModel* model,
291 const BookmarkNode* parent,
292 int index) {
293 if (!parent)
294 return;
296 BookmarkNodeData bookmark_data;
297 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) {
298 GURL url = GetUrlFromClipboard();
299 if (!url.is_valid())
300 return;
301 BookmarkNode node(url);
302 node.SetTitle(base::ASCIIToUTF16(url.spec()));
303 bookmark_data = BookmarkNodeData(&node);
305 if (index == -1)
306 index = parent->child_count();
307 ScopedGroupBookmarkActions group_paste(model);
309 if (bookmark_data.size() == 1 &&
310 model->IsBookmarked(bookmark_data.elements[0].url)) {
311 MakeTitleUnique(model,
312 parent,
313 bookmark_data.elements[0].url,
314 &bookmark_data.elements[0].title);
317 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true);
320 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) {
321 if (!node || !model->client()->CanBeEditedByUser(node))
322 return false;
323 return (BookmarkNodeData::ClipboardContainsBookmarks() ||
324 GetUrlFromClipboard().is_valid());
327 std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
328 BookmarkModel* model,
329 size_t max_count) {
330 std::vector<const BookmarkNode*> nodes;
331 ui::TreeNodeIterator<const BookmarkNode> iterator(
332 model->root_node(), base::Bind(&PruneInvisibleFolders));
334 while (iterator.has_next()) {
335 const BookmarkNode* parent = iterator.Next();
336 if (!model->client()->CanBeEditedByUser(parent))
337 continue;
338 if (parent->is_folder() && parent->date_folder_modified() > Time()) {
339 if (max_count == 0) {
340 nodes.push_back(parent);
341 } else {
342 std::vector<const BookmarkNode*>::iterator i =
343 std::upper_bound(nodes.begin(), nodes.end(), parent,
344 &MoreRecentlyModified);
345 if (nodes.size() < max_count || i != nodes.end()) {
346 nodes.insert(i, parent);
347 while (nodes.size() > max_count)
348 nodes.pop_back();
351 } // else case, the root node, which we don't care about or imported nodes
352 // (which have a time of 0).
355 if (nodes.size() < max_count) {
356 // Add the permanent nodes if there is space. The permanent nodes are the
357 // only children of the root_node.
358 const BookmarkNode* root_node = model->root_node();
360 for (int i = 0; i < root_node->child_count(); ++i) {
361 const BookmarkNode* node = root_node->GetChild(i);
362 if (node->IsVisible() && model->client()->CanBeEditedByUser(node) &&
363 std::find(nodes.begin(), nodes.end(), node) == nodes.end()) {
364 nodes.push_back(node);
366 if (nodes.size() == max_count)
367 break;
371 return nodes;
374 void GetMostRecentlyAddedEntries(BookmarkModel* model,
375 size_t count,
376 std::vector<const BookmarkNode*>* nodes) {
377 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node());
378 while (iterator.has_next()) {
379 const BookmarkNode* node = iterator.Next();
380 if (node->is_url()) {
381 std::vector<const BookmarkNode*>::iterator insert_position =
382 std::upper_bound(nodes->begin(), nodes->end(), node,
383 &MoreRecentlyAdded);
384 if (nodes->size() < count || insert_position != nodes->end()) {
385 nodes->insert(insert_position, node);
386 while (nodes->size() > count)
387 nodes->pop_back();
393 bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2) {
394 return n1->date_added() > n2->date_added();
397 void GetBookmarksMatchingProperties(BookmarkModel* model,
398 const QueryFields& query,
399 size_t max_count,
400 const std::string& languages,
401 std::vector<const BookmarkNode*>* nodes) {
402 std::vector<base::string16> query_words;
403 query_parser::QueryParser parser;
404 if (query.word_phrase_query) {
405 parser.ParseQueryWords(base::i18n::ToLower(*query.word_phrase_query),
406 query_parser::MatchingAlgorithm::DEFAULT,
407 &query_words);
408 if (query_words.empty())
409 return;
412 if (query.url) {
413 // Shortcut into the BookmarkModel if searching for URL.
414 GURL url(*query.url);
415 std::vector<const BookmarkNode*> url_matched_nodes;
416 if (url.is_valid())
417 model->GetNodesByURL(url, &url_matched_nodes);
418 bookmarks::VectorIterator iterator(&url_matched_nodes);
419 GetBookmarksMatchingPropertiesImpl<bookmarks::VectorIterator>(
420 iterator, model, query, query_words, max_count, languages, nodes);
421 } else {
422 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node());
423 GetBookmarksMatchingPropertiesImpl<
424 ui::TreeNodeIterator<const BookmarkNode>>(
425 iterator, model, query, query_words, max_count, languages, nodes);
429 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
430 registry->RegisterBooleanPref(
431 prefs::kShowBookmarkBar,
432 false,
433 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
434 registry->RegisterBooleanPref(prefs::kEditBookmarksEnabled, true);
435 registry->RegisterBooleanPref(
436 prefs::kShowAppsShortcutInBookmarkBar,
437 true,
438 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
439 registry->RegisterBooleanPref(
440 prefs::kShowManagedBookmarksInBookmarkBar,
441 true,
442 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
443 // Don't sync this, as otherwise, due to a limitation in sync, it
444 // will cause a deadlock (see http://crbug.com/97955). If we truly
445 // want to sync the expanded state of folders, it should be part of
446 // bookmark sync itself (i.e., a property of the sync folder nodes).
447 registry->RegisterListPref(prefs::kBookmarkEditorExpandedNodes,
448 new base::ListValue);
449 registry->RegisterListPref(prefs::kManagedBookmarks);
450 registry->RegisterListPref(prefs::kSupervisedBookmarks);
453 const BookmarkNode* GetParentForNewNodes(
454 const BookmarkNode* parent,
455 const std::vector<const BookmarkNode*>& selection,
456 int* index) {
457 const BookmarkNode* real_parent = parent;
459 if (selection.size() == 1 && selection[0]->is_folder())
460 real_parent = selection[0];
462 if (index) {
463 if (selection.size() == 1 && selection[0]->is_url()) {
464 *index = real_parent->GetIndexOf(selection[0]) + 1;
465 if (*index == 0) {
466 // Node doesn't exist in parent, add to end.
467 NOTREACHED();
468 *index = real_parent->child_count();
470 } else {
471 *index = real_parent->child_count();
475 return real_parent;
478 void DeleteBookmarkFolders(BookmarkModel* model,
479 const std::vector<int64_t>& ids) {
480 // Remove the folders that were removed. This has to be done after all the
481 // other changes have been committed.
482 for (std::vector<int64_t>::const_iterator iter = ids.begin();
483 iter != ids.end();
484 ++iter) {
485 const BookmarkNode* node = GetBookmarkNodeByID(model, *iter);
486 if (!node)
487 continue;
488 model->Remove(node);
492 void AddIfNotBookmarked(BookmarkModel* model,
493 const GURL& url,
494 const base::string16& title) {
495 if (IsBookmarkedByUser(model, url))
496 return; // Nothing to do, a user bookmark with that url already exists.
497 model->client()->RecordAction(base::UserMetricsAction("BookmarkAdded"));
498 const BookmarkNode* parent = model->GetParentForNewNodes();
499 model->AddURL(parent, parent->child_count(), title, url);
502 void RemoveAllBookmarks(BookmarkModel* model, const GURL& url) {
503 std::vector<const BookmarkNode*> bookmarks;
504 model->GetNodesByURL(url, &bookmarks);
506 // Remove all the user bookmarks.
507 for (size_t i = 0; i < bookmarks.size(); ++i) {
508 const BookmarkNode* node = bookmarks[i];
509 int index = node->parent()->GetIndexOf(node);
510 if (index > -1 && model->client()->CanBeEditedByUser(node))
511 model->Remove(node);
515 base::string16 CleanUpUrlForMatching(
516 const GURL& gurl,
517 const std::string& languages,
518 base::OffsetAdjuster::Adjustments* adjustments) {
519 base::OffsetAdjuster::Adjustments tmp_adjustments;
520 return base::i18n::ToLower(url_formatter::FormatUrlWithAdjustments(
521 GURL(TruncateUrl(gurl.spec())), languages,
522 url_formatter::kFormatUrlOmitUsernamePassword,
523 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS, NULL,
524 NULL, adjustments ? adjustments : &tmp_adjustments));
527 base::string16 CleanUpTitleForMatching(const base::string16& title) {
528 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength));
531 bool CanAllBeEditedByUser(BookmarkClient* client,
532 const std::vector<const BookmarkNode*>& nodes) {
533 for (size_t i = 0; i < nodes.size(); ++i) {
534 if (!client->CanBeEditedByUser(nodes[i]))
535 return false;
537 return true;
540 bool IsBookmarkedByUser(BookmarkModel* model, const GURL& url) {
541 std::vector<const BookmarkNode*> nodes;
542 model->GetNodesByURL(url, &nodes);
543 for (size_t i = 0; i < nodes.size(); ++i) {
544 if (model->client()->CanBeEditedByUser(nodes[i]))
545 return true;
547 return false;
550 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model,
551 int64_t id) {
552 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate.
553 return GetNodeByID(model->root_node(), id);
556 bool IsDescendantOf(const BookmarkNode* node, const BookmarkNode* root) {
557 return node && node->HasAncestor(root);
560 bool HasDescendantsOf(const std::vector<const BookmarkNode*>& list,
561 const BookmarkNode* root) {
562 for (const BookmarkNode* node : list) {
563 if (IsDescendantOf(node, root))
564 return true;
566 return false;
569 } // namespace bookmarks