bookmark_utils.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.
  4. #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/utf_offset_string_conversions.h"
  12. #include "components/bookmarks/browser/bookmark_node_data.h"
  13. #include "components/prefs/pref_registry_simple.h"
  14. class GURL;
  15. namespace user_prefs {
  16. class PrefRegistrySyncable;
  17. }
  18. // A collection of bookmark utility functions used by various parts of the UI
  19. // that show bookmarks (bookmark manager, bookmark bar view, ...) and other
  20. // systems that involve indexing and searching bookmarks.
  21. namespace bookmarks {
  22. class BookmarkClient;
  23. class BookmarkModel;
  24. class BookmarkNode;
  25. // Fields to use when finding matching bookmarks.
  26. struct QueryFields {
  27. QueryFields();
  28. ~QueryFields();
  29. std::unique_ptr<std::u16string> word_phrase_query;
  30. std::unique_ptr<std::u16string> url;
  31. std::unique_ptr<std::u16string> title;
  32. };
  33. class VectorIterator {
  34. public:
  35. explicit VectorIterator(std::vector<const BookmarkNode*>* nodes);
  36. VectorIterator(const VectorIterator& other) = delete;
  37. VectorIterator& operator=(const VectorIterator& other) = delete;
  38. ~VectorIterator();
  39. bool has_next();
  40. const BookmarkNode* Next();
  41. private:
  42. raw_ptr<std::vector<const BookmarkNode*>> nodes_;
  43. std::vector<const BookmarkNode*>::iterator current_;
  44. };
  45. // Clones bookmark node, adding newly created nodes to |parent| starting at
  46. // |index_to_add_at|. If |reset_node_times| is true cloned bookmarks and
  47. // folders will receive new creation times and folder modification times
  48. // instead of using the values stored in |elements|.
  49. void CloneBookmarkNode(BookmarkModel* model,
  50. const std::vector<BookmarkNodeData::Element>& elements,
  51. const BookmarkNode* parent,
  52. size_t index_to_add_at,
  53. bool reset_node_times);
  54. // Copies nodes onto the clipboard. If |remove_nodes| is true the nodes are
  55. // removed after copied to the clipboard. The nodes are copied in such a way
  56. // that if pasted again copies are made.
  57. void CopyToClipboard(BookmarkModel* model,
  58. const std::vector<const BookmarkNode*>& nodes,
  59. bool remove_nodes);
  60. // Pastes from the clipboard. The new nodes are added to |parent|, unless
  61. // |parent| is null in which case this does nothing. The nodes are inserted
  62. // at |index|.
  63. void PasteFromClipboard(BookmarkModel* model,
  64. const BookmarkNode* parent,
  65. size_t index);
  66. // Returns true if the user can copy from the pasteboard.
  67. bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node);
  68. // Returns a vector containing up to |max_count| of the most recently modified
  69. // user folders. This never returns an empty vector.
  70. std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
  71. BookmarkModel* model, size_t max_count);
  72. // Returns the most recently added bookmarks. This does not return folders,
  73. // only nodes of type url.
  74. void GetMostRecentlyAddedEntries(BookmarkModel* model,
  75. size_t count,
  76. std::vector<const BookmarkNode*>* nodes);
  77. // Returns true if |n1| was added more recently than |n2|.
  78. bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2);
  79. // Returns up to |max_count| bookmarks from |model| whose url or title contain
  80. // the text |query.word_phrase_query| and exactly match |query.url| and
  81. // |query.title|, for all of the preceding fields that are not NULL.
  82. void GetBookmarksMatchingProperties(BookmarkModel* model,
  83. const QueryFields& query,
  84. size_t max_count,
  85. std::vector<const BookmarkNode*>* nodes);
  86. // Parses the provided query and returns a vector of query words.
  87. std::vector<std::u16string> ParseBookmarkQuery(
  88. const bookmarks::QueryFields& query);
  89. // Returns true iff |title| or |url| contains each string in |words|. This is
  90. // used when searching for bookmarks.
  91. bool DoesBookmarkContainWords(const std::u16string& title,
  92. const GURL& url,
  93. const std::vector<std::u16string>& words);
  94. // Register user preferences for Bookmarks Bar.
  95. void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
  96. // Register managed bookmarks preferences.
  97. void RegisterManagedBookmarksPrefs(PrefRegistrySimple* registry);
  98. // Returns the parent for newly created folders/bookmarks. If |selection| has
  99. // one element and it is a folder, |selection[0]| is returned, otherwise
  100. // |parent| is returned. If |index| is non-null it is set to the index newly
  101. // added nodes should be added at.
  102. const BookmarkNode* GetParentForNewNodes(
  103. const BookmarkNode* parent,
  104. const std::vector<const BookmarkNode*>& selection,
  105. size_t* index);
  106. // Deletes the bookmark folders for the given list of |ids|.
  107. void DeleteBookmarkFolders(BookmarkModel* model,
  108. const std::vector<int64_t>& ids);
  109. // If there are no user bookmarks for url, a bookmark is created.
  110. const BookmarkNode* AddIfNotBookmarked(BookmarkModel* model,
  111. const GURL& url,
  112. const std::u16string& title);
  113. // Removes all bookmarks for the given |url|.
  114. void RemoveAllBookmarks(BookmarkModel* model, const GURL& url);
  115. // Truncates an overly-long URL, unescapes it and interprets the
  116. // characters as UTF-8 (both via url_formatter::FormatUrl()), and
  117. // lower-cases it, returning the result. |adjustments|, if non-NULL, is
  118. // set to reflect the transformations the URL spec underwent to become the
  119. // return value. If a caller computes offsets (e.g., for the position
  120. // of matched text) in this cleaned-up string, it can use |adjustments|
  121. // to calculate the location of these offsets in the original string
  122. // (via base::OffsetAdjuster::UnadjustOffsets()). This is useful if later
  123. // the original string gets formatted in a different way for displaying.
  124. // In this case, knowing the offsets in the original string will allow them
  125. // to be properly translated to offsets in the newly-formatted string.
  126. //
  127. // The unescaping done by this function makes it possible to match substrings
  128. // that were originally escaped for navigation; for example, if the user
  129. // searched for "a&p", the query would be escaped as "a%26p", so without
  130. // unescaping, an input string of "a&p" would no longer match this URL. Note
  131. // that the resulting unescaped URL may not be directly navigable (which is
  132. // why it was escaped to begin with).
  133. std::u16string CleanUpUrlForMatching(
  134. const GURL& gurl,
  135. base::OffsetAdjuster::Adjustments* adjustments);
  136. // Returns the lower-cased title, possibly truncated if the original title
  137. // is overly-long.
  138. std::u16string CleanUpTitleForMatching(const std::u16string& title);
  139. // Returns true if all the |nodes| can be edited by the user,
  140. // as determined by BookmarkClient::CanBeEditedByUser().
  141. bool CanAllBeEditedByUser(BookmarkClient* client,
  142. const std::vector<const BookmarkNode*>& nodes);
  143. // Returns true if |url| has a bookmark in the |model| that can be edited
  144. // by the user.
  145. bool IsBookmarkedByUser(BookmarkModel* model, const GURL& url);
  146. // Returns the node with |id|, or NULL if there is no node with |id|.
  147. const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64_t id);
  148. // Returns true if |node| is a descendant of |root|.
  149. bool IsDescendantOf(const BookmarkNode* node, const BookmarkNode* root);
  150. // Returns true if any node in |list| is a descendant of |root|.
  151. bool HasDescendantsOf(const std::vector<const BookmarkNode*>& list,
  152. const BookmarkNode* root);
  153. // Returns the parent to add new nodes to, never returns null (as long as
  154. // the model is loaded).
  155. const BookmarkNode* GetParentForNewNodes(BookmarkModel* model);
  156. } // namespace bookmarks
  157. #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_