Update bookmark_utils.cc

This commit is contained in:
Alexander David Frick 2022-10-14 20:54:03 -05:00 committed by GitHub
parent 3e6df460d7
commit fede97686f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
// Copyright 2022 The Chromium Authors and Alex313031. // Copyright 2022 The Chromium Authors and Alex313031
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
@ -116,12 +116,15 @@ bool HasSelectedAncestor(BookmarkModel* model,
return HasSelectedAncestor(model, selected_nodes, node->parent()); return HasSelectedAncestor(model, selected_nodes, node->parent());
} }
const BookmarkNode* GetNodeByID(const BookmarkNode* node, int64_t id) { // Recursively searches for a node satisfying the functor |pred| . Returns
if (node->id() == id) // nullptr if not found.
template <typename Predicate>
const BookmarkNode* FindNode(const BookmarkNode* node, Predicate pred) {
if (pred(node))
return node; return node;
for (const auto& child : node->children()) { for (const auto& child : node->children()) {
const BookmarkNode* result = GetNodeByID(child.get(), id); const BookmarkNode* result = FindNode(child.get(), pred);
if (result) if (result)
return result; return result;
} }
@ -558,8 +561,15 @@ bool IsBookmarkedByUser(BookmarkModel* model, const GURL& url) {
const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model,
int64_t id) { int64_t id) {
// TODO(sky): TreeNode needs a method that visits all nodes using a predicate. return FindNode(model->root_node(),
return GetNodeByID(model->root_node(), id); [id](const BookmarkNode* node) { return node->id() == id; });
}
const BookmarkNode* GetBookmarkNodeByGUID(const BookmarkModel* model,
const base::GUID& guid) {
return FindNode(model->root_node(), [&guid](const BookmarkNode* node) {
return node->guid() == guid;
});
} }
bool IsDescendantOf(const BookmarkNode* node, const BookmarkNode* root) { bool IsDescendantOf(const BookmarkNode* node, const BookmarkNode* root) {