summaryrefslogtreecommitdiff
path: root/chromium/components/bookmarks/browser/url_index.h
blob: 8036ebb9584598dd26b3073afc537b14f2b5cdd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_BOOKMARKS_BROWSER_URL_INDEX_H_
#define COMPONENTS_BOOKMARKS_BROWSER_URL_INDEX_H_

#include <memory>
#include <set>
#include <vector>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/history_bookmark_model.h"
#include "url/gurl.h"

namespace bookmarks {

class BookmarkNode;

struct UrlAndTitle;

// UrlIndex maintains the bookmark nodes of type url. The nodes are ordered by
// url for lookup using the url. The mapping is done based on the nodes in the
// model. This class may outlive the BookmarkModel, necessitating this class
// owning all the nodes.
//
// This class is accessed on multiple threads, so all mutation to the underlying
// set must be done while holding a lock. While this class is accessed on
// multiple threads, all mutation happens on main thread.
//
// This class is an implementation detail of BookmarkModel and is not intended
// to be public. The public functions implemented by way of
// HistoryBookmarkModel define the public API of this class.
class UrlIndex : public HistoryBookmarkModel {
 public:
  explicit UrlIndex(std::unique_ptr<BookmarkNode> root);

  BookmarkNode* root() { return root_.get(); }

  // Adds |node| to |parent| at |index|.
  void Add(BookmarkNode* parent,
           size_t index,
           std::unique_ptr<BookmarkNode> node);

  // Removes |node| and all its descendants from the map, adds urls that are no
  // longer contained in the index to the |removed_urls| set if provided
  // (doesn't clean up existing items in the set).
  std::unique_ptr<BookmarkNode> Remove(BookmarkNode* node,
                                       std::set<GURL>* removed_urls);

  // Mutation of bookmark node fields that are exposed to HistoryBookmarkModel,
  // which means must acquire a lock. Must be called from the UI thread.
  void SetUrl(BookmarkNode* node, const GURL& url);
  void SetTitle(BookmarkNode* node, const base::string16& title);

  // Returns the nodes whose icon_url is |icon_url|.
  void GetNodesWithIconUrl(const GURL& icon_url,
                           std::set<const BookmarkNode*>* nodes);

  void GetNodesByUrl(const GURL& url, std::vector<const BookmarkNode*>* nodes);

  // Returns true if there is at least one bookmark.
  bool HasBookmarks() const;

  // Returns some stats about number of URL bookmarks stored, for UMA purposes.
  struct Stats {
    // Number of bookmark in the index excluding folders.
    size_t total_url_bookmark_count = 0;
    // Number of bookmarks (excluding folders) with a URL that is used by at
    // least one other bookmark, excluding one bookmark per unique URL (i.e. all
    // except one are considered duplicates).
    size_t duplicate_url_bookmark_count = 0;
    // Number of bookmarks (excluding folders) with the pair <URL, title> that
    // is used by at least one other bookmark, excluding one bookmark per unique
    // URL (i.e. all except one are considered duplicates).
    size_t duplicate_url_and_title_bookmark_count = 0;
    // Number of bookmarks (excluding folders) with the triple <URL, title,
    // parent> that is used by at least one other bookmark, excluding one
    // bookmark per unique URL (i.e. all except one are considered duplicates).
    size_t duplicate_url_and_title_and_parent_bookmark_count = 0;
  };
  Stats ComputeStats() const;

  // HistoryBookmarkModel:
  bool IsBookmarked(const GURL& url) override;
  void GetBookmarks(std::vector<UrlAndTitle>* bookmarks) override;

 private:
  friend class base::RefCountedThreadSafe<UrlIndex>;

  ~UrlIndex() override;

  // Used to order BookmarkNodes by URL.
  class NodeUrlComparator {
   public:
    bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const {
      return n1->url() < n2->url();
    }
  };

  bool IsBookmarkedNoLock(const GURL& url);

  void AddImpl(BookmarkNode* node);
  void RemoveImpl(BookmarkNode* node, std::set<GURL>* removed_urls);

  std::unique_ptr<BookmarkNode> root_;

  // Set of nodes ordered by URL. This is not a map to avoid copying the
  // urls.
  // WARNING: |nodes_ordered_by_url_set_| is accessed on multiple threads. As
  // such, be sure and wrap all usage of it around |url_lock_|.
  using NodesOrderedByUrlSet = std::multiset<BookmarkNode*, NodeUrlComparator>;
  NodesOrderedByUrlSet nodes_ordered_by_url_set_;
  mutable base::Lock url_lock_;

  DISALLOW_COPY_AND_ASSIGN(UrlIndex);
};

}  // namespace bookmarks

#endif  // COMPONENTS_BOOKMARKS_BROWSER_URL_INDEX_H_