File: bookmark_utils.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (222 lines) | stat: -rw-r--r-- 9,058 bytes parent folder | download | duplicates (5)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_
#define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "components/bookmarks/browser/bookmark_node_data.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/prefs/pref_registry_simple.h"

class GURL;

namespace user_prefs {
class PrefRegistrySyncable;
}

// A collection of bookmark utility functions used by various parts of the UI
// that show bookmarks (bookmark manager, bookmark bar view, ...) and other
// systems that involve indexing and searching bookmarks.
namespace bookmarks {

class BookmarkModel;
class BookmarkNode;

// Fields to use when finding matching bookmarks.
struct QueryFields {
  QueryFields();
  ~QueryFields();

  std::unique_ptr<std::u16string> word_phrase_query;
  std::unique_ptr<std::u16string> url;
  std::unique_ptr<std::u16string> title;
};

class VectorIterator {
 public:
  explicit VectorIterator(
      std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>* nodes);
  VectorIterator(const VectorIterator& other) = delete;
  VectorIterator& operator=(const VectorIterator& other) = delete;
  ~VectorIterator();
  bool has_next();
  const BookmarkNode* Next();

 private:
  raw_ptr<std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>> nodes_;
  std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>::iterator
      current_;
};

// Clones bookmark node, adding newly created nodes to `parent` starting at
// `index_to_add_at`. If `reset_node_times` is true cloned bookmarks and
// folders will receive new creation times and folder modification times
// instead of using the values stored in `elements`.
void CloneBookmarkNode(BookmarkModel* model,
                       const std::vector<BookmarkNodeData::Element>& elements,
                       const BookmarkNode* parent,
                       size_t index_to_add_at,
                       bool reset_node_times);

// Copies nodes onto the clipboard. If `remove_nodes` is true the nodes are
// removed after copied to the clipboard. The nodes are copied in such a way
// that if pasted again copies are made. Pass the calling context through as
// `source`.
void CopyToClipboard(
    BookmarkModel* model,
    const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>& nodes,
    bool remove_nodes,
    metrics::BookmarkEditSource source,
    bool is_off_the_record);

// Returns a vector containing of the most recently modified user folders. This
// never returns an empty vector.
std::vector<const BookmarkNode*> GetMostRecentlyModifiedUserFolders(
    BookmarkModel* model);

// If this should be used on mobile we need to reevaluate if this implementation
// makes sense. See tests in bookmark_utils_unittest.cc which currently fail
// outside of desktop. Enable and update those if this is to be used on mobile.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Bookmark nodes, split by account/local bookmarks.
struct BookmarkNodesSplitByAccountAndLocal final {
  BookmarkNodesSplitByAccountAndLocal();
  BookmarkNodesSplitByAccountAndLocal(
      const BookmarkNodesSplitByAccountAndLocal&);
  BookmarkNodesSplitByAccountAndLocal& operator=(
      const BookmarkNodesSplitByAccountAndLocal&);
  ~BookmarkNodesSplitByAccountAndLocal();

  std::vector<const BookmarkNode*> account_nodes;
  std::vector<const BookmarkNode*> local_nodes;
};

// Get recently-used-folders, including permanent nodes for display split up by
// "account" and "local" nodes. If there are no "account" bookmarks all entries
// are returned as local nodes even if sync'd, to be displayed as a single list
// without any headers/labels.
//
// In case of a mixed account and local bookmarks in the MRU nodes, this would
// display:
//   - Account Bookmark Heading
//     - Most recently used custom account bookmarks
//     - Account permanent folders if visible
//   - Local Bookmark Heading
//     - Most recently used custom local bookmarks
//     - Local permanent folder if it is the most recently used folder
//
// If MRU nodes are only local or account, this would display:
//   - Most recently used custom
//   - Account/Local and syncable permanent nodes
//
// Note: The parent of `display_node` is pushed on top of its corresponding list
// if it is a non-permanent folder or at the end if it is a permanent folder
// that is not already included.
BookmarkNodesSplitByAccountAndLocal GetMostRecentlyUsedFoldersForDisplay(
    BookmarkModel* model,
    const BookmarkNode* displayed_node);

// Returns permanent nodes for display. Either account or local+syncable types
// are always available, sometimes both (non-empty visible local permanent
// nodes).
BookmarkNodesSplitByAccountAndLocal GetPermanentNodesForDisplay(
    const BookmarkModel* model);
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

// Returns true if any local permanent nodes contain bookmarks.
bool HasLocalOrSyncableBookmarks(const BookmarkModel* model);

// Returns the most recently added bookmarks. This does not return folders,
// only nodes of type url.
void GetMostRecentlyAddedEntries(BookmarkModel* model,
                                 size_t count,
                                 std::vector<const BookmarkNode*>* nodes);

// Returns true if `n1` was added more recently than `n2`.
bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2);

// Returns the most recently used bookmarks. This does not return folders,
// only nodes of type url. Note: If the bookmarks have the same used time, this
// will return the more recent added bookmarks. Normally, this happens when the
// bookmarks are never used.
void GetMostRecentlyUsedEntries(BookmarkModel* model,
                                size_t count,
                                std::vector<const BookmarkNode*>* nodes);

// Returns up to `max_count` bookmarks from `model` whose url or title contain
// the text `query.word_phrase_query` and exactly match `query.url` and
// `query.title`, for all of the preceding fields that are not NULL.
std::vector<const BookmarkNode*> GetBookmarksMatchingProperties(
    BookmarkModel* model,
    const QueryFields& query,
    size_t max_count);

// Parses the provided query and returns a vector of query words.
std::vector<std::u16string> ParseBookmarkQuery(
    const bookmarks::QueryFields& query);

// Returns true iff `title` or `url` contains each string in `words`. This is
// used when searching for bookmarks.
bool DoesBookmarkContainWords(const std::u16string& title,
                              const GURL& url,
                              const std::vector<std::u16string>& words);

// Register user preferences for Bookmarks Bar.
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

// Register managed bookmarks preferences.
void RegisterManagedBookmarksPrefs(PrefRegistrySimple* registry);

// Deletes the bookmark folders for the given list of `ids`.
void DeleteBookmarkFolders(BookmarkModel* model,
                           const std::vector<int64_t>& ids,
                           const base::Location& location);

// If there are no user bookmarks for url, a bookmark is created.
const BookmarkNode* AddIfNotBookmarked(BookmarkModel* model,
                                       const GURL& url,
                                       const std::u16string& title);

// Removes all bookmarks for the given `url`.
void RemoveAllBookmarks(BookmarkModel* model,
                        const GURL& url,
                        const base::Location& location);

// Returns true if `url` has a bookmark in the `model` that can be edited
// by the user.
bool IsBookmarkedByUser(BookmarkModel* model, const GURL& url);

// Returns the node with `id`, or NULL if there is no node with `id`.
const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64_t id);

// Returns true if `node` is a descendant of `root`.
bool IsDescendantOf(const BookmarkNode* node, const BookmarkNode* root);

// Returns true if any node in `list` is a descendant of `root`.
bool HasDescendantsOf(
    const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>& list,
    const BookmarkNode* root);

// Returns the parent to add new nodes to, never returns null (as long as
// the model is loaded). If `url` is non-empty, features will have the
// opportunity to suggest contextually relevant folders.
const BookmarkNode* GetParentForNewNodes(BookmarkModel* model,
                                         const GURL& url = GURL());

// This pruning keeps visible, non-managed folder nodes.
bool PruneFoldersForDisplay(const BookmarkModel* model,
                            const BookmarkNode* node);

}  // namespace bookmarks

#endif  // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_