url_index.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 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_URL_INDEX_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_URL_INDEX_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/synchronization/lock.h"
  11. #include "components/bookmarks/browser/bookmark_node.h"
  12. #include "components/bookmarks/browser/history_bookmark_model.h"
  13. #include "url/gurl.h"
  14. namespace bookmarks {
  15. class BookmarkNode;
  16. struct UrlLoadStats;
  17. struct UrlAndTitle;
  18. // UrlIndex maintains the bookmark nodes of type url. The nodes are ordered by
  19. // url for lookup using the url. The mapping is done based on the nodes in the
  20. // model. This class may outlive the BookmarkModel, necessitating this class
  21. // owning all the nodes.
  22. //
  23. // This class is accessed on multiple threads, so all mutation to the underlying
  24. // set must be done while holding a lock. While this class is accessed on
  25. // multiple threads, all mutation happens on main thread.
  26. //
  27. // This class is an implementation detail of BookmarkModel and is not intended
  28. // to be public. The public functions implemented by way of
  29. // HistoryBookmarkModel define the public API of this class.
  30. class UrlIndex : public HistoryBookmarkModel {
  31. public:
  32. explicit UrlIndex(std::unique_ptr<BookmarkNode> root);
  33. UrlIndex(const UrlIndex&) = delete;
  34. UrlIndex& operator=(const UrlIndex&) = delete;
  35. BookmarkNode* root() { return root_.get(); }
  36. // Adds |node| to |parent| at |index|.
  37. void Add(BookmarkNode* parent,
  38. size_t index,
  39. std::unique_ptr<BookmarkNode> node);
  40. // Removes |node| and all its descendants from the map, adds urls that are no
  41. // longer contained in the index to the |removed_urls| set if provided
  42. // (doesn't clean up existing items in the set).
  43. std::unique_ptr<BookmarkNode> Remove(BookmarkNode* node,
  44. std::set<GURL>* removed_urls);
  45. // Mutation of bookmark node fields that are exposed to HistoryBookmarkModel,
  46. // which means must acquire a lock. Must be called from the UI thread.
  47. void SetUrl(BookmarkNode* node, const GURL& url);
  48. void SetTitle(BookmarkNode* node, const std::u16string& title);
  49. // Returns the nodes whose icon_url is |icon_url|.
  50. void GetNodesWithIconUrl(const GURL& icon_url,
  51. std::set<const BookmarkNode*>* nodes);
  52. void GetNodesByUrl(const GURL& url, std::vector<const BookmarkNode*>* nodes);
  53. // Returns true if there is at least one bookmark.
  54. bool HasBookmarks() const;
  55. // Compute stats from the load.
  56. UrlLoadStats ComputeStats() const;
  57. // HistoryBookmarkModel:
  58. bool IsBookmarked(const GURL& url) override;
  59. void GetBookmarks(std::vector<UrlAndTitle>* bookmarks) override;
  60. private:
  61. friend class base::RefCountedThreadSafe<UrlIndex>;
  62. ~UrlIndex() override;
  63. // Used to order BookmarkNodes by URL.
  64. class NodeUrlComparator {
  65. public:
  66. bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const {
  67. return n1->url() < n2->url();
  68. }
  69. };
  70. bool IsBookmarkedNoLock(const GURL& url);
  71. void AddImpl(BookmarkNode* node);
  72. void RemoveImpl(BookmarkNode* node, std::set<GURL>* removed_urls);
  73. std::unique_ptr<BookmarkNode> root_;
  74. // Set of nodes ordered by URL. This is not a map to avoid copying the
  75. // urls.
  76. // WARNING: |nodes_ordered_by_url_set_| is accessed on multiple threads. As
  77. // such, be sure and wrap all usage of it around |url_lock_|.
  78. using NodesOrderedByUrlSet = std::multiset<BookmarkNode*, NodeUrlComparator>;
  79. NodesOrderedByUrlSet nodes_ordered_by_url_set_;
  80. mutable base::Lock url_lock_;
  81. };
  82. } // namespace bookmarks
  83. #endif // COMPONENTS_BOOKMARKS_BROWSER_URL_INDEX_H_