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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/power_bookmarks/core/power_bookmark_utils.h"
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/i18n/string_search.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/power_bookmarks/core/proto/power_bookmark_meta.pb.h"
#include "ui/base/models/tree_node_iterator.h"
#include "url/gurl.h"
namespace power_bookmarks {
namespace {
// Backfill old shopping_specifics field to the new one. This is necessary
// as we're transitioning from oneof powers to allowing multiple.
// TODO(crbug.com/40233844): Also invoke this in meta updates once available.
void BackfillShoppingSpecifics(PowerBookmarkMeta* meta) {
if (meta->has_old_shopping_specifics() && !meta->has_shopping_specifics()) {
meta->mutable_shopping_specifics()->CopyFrom(
meta->old_shopping_specifics());
meta->clear_old_shopping_specifics();
}
}
} // namespace
const char kPowerBookmarkMetaKey[] = "power_bookmark_meta";
PowerBookmarkQueryFields::PowerBookmarkQueryFields() = default;
PowerBookmarkQueryFields::~PowerBookmarkQueryFields() = default;
std::unique_ptr<PowerBookmarkMeta> GetNodePowerBookmarkMeta(
bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) {
if (!model)
return nullptr;
std::string raw_meta;
if (!node || !node->GetMetaInfo(kPowerBookmarkMetaKey, &raw_meta))
return nullptr;
std::unique_ptr<PowerBookmarkMeta> meta =
std::make_unique<PowerBookmarkMeta>();
if (!DecodeMetaFromStorage(raw_meta, meta.get())) {
meta.reset();
DeleteNodePowerBookmarkMeta(model, node);
}
BackfillShoppingSpecifics(meta.get());
return meta;
}
void SetNodePowerBookmarkMeta(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node,
std::unique_ptr<PowerBookmarkMeta> meta) {
if (!model || !node)
return;
CHECK(meta);
BackfillShoppingSpecifics(meta.get());
std::string data;
EncodeMetaForStorage(*meta.get(), &data);
model->SetNodeMetaInfo(node, kPowerBookmarkMetaKey, data);
}
void DeleteNodePowerBookmarkMeta(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) {
if (model && node)
model->DeleteNodeMetaInfo(node, kPowerBookmarkMetaKey);
}
bool DoBookmarkTagsContainWords(const std::unique_ptr<PowerBookmarkMeta>& meta,
const std::vector<std::u16string>& words) {
if (!meta)
return false;
for (const std::u16string& word : words) {
bool found_tag_for_word = false;
for (const PowerBookmarkMeta_Tag& tag : meta->tags()) {
if (base::i18n::StringSearchIgnoringCaseAndAccents(
word, base::UTF8ToUTF16(tag.display_name()), nullptr, nullptr)) {
found_tag_for_word = true;
break;
}
}
if (!found_tag_for_word)
return false;
}
return true;
}
template <class type>
std::vector<const bookmarks::BookmarkNode*> GetBookmarksMatchingPropertiesImpl(
type& iterator,
bookmarks::BookmarkModel* model,
const PowerBookmarkQueryFields& query,
const std::vector<std::u16string>& query_words,
size_t max_count) {
std::vector<const bookmarks::BookmarkNode*> nodes;
while (iterator.has_next()) {
const bookmarks::BookmarkNode* node = iterator.Next();
// The query words are allowed to be empty for search.
bool title_or_url_match_query =
query_words.empty() || bookmarks::DoesBookmarkContainWords(
node->GetTitle(), node->url(), query_words);
std::unique_ptr<PowerBookmarkMeta> meta =
GetNodePowerBookmarkMeta(model, node);
// Similarly, if the query is empty, we want this test to pass.
bool tags_match_query =
query_words.empty() || DoBookmarkTagsContainWords(meta, query_words);
// Add vertical-specific support by adding a case below.
if (query.type.has_value()) {
if (!meta)
continue;
bool type_present = false;
switch (query.type.value()) {
case PowerBookmarkType::SHOPPING:
type_present = meta->has_shopping_specifics();
break;
default:
NOTREACHED();
}
if (!type_present)
continue;
}
if (!title_or_url_match_query && !tags_match_query)
continue;
if (model->is_permanent_node(node))
continue;
if (query.title && node->GetTitle() != *query.title)
continue;
// If the tag list is empty, don't try to filter by tag.
if (!query.tags.empty()) {
if (!meta)
continue;
// TODO(mdjones): This tag comparison can be faster in a lot of ways.
// We'll use the simple version while experimenting with
// the feature.
bool found_tag = false;
for (const std::u16string& tag : query.tags) {
found_tag = false;
for (const PowerBookmarkMeta_Tag& proto_tag : meta->tags()) {
if (base::EqualsCaseInsensitiveASCII(
tag, base::UTF8ToUTF16(proto_tag.display_name()))) {
found_tag = true;
break;
}
}
if (!found_tag)
break;
}
// if there was a missing tag, skip this bookmark node.
if (!found_tag)
continue;
}
nodes.push_back(node);
if (nodes.size() == max_count) {
break;
}
}
return nodes;
}
std::vector<const bookmarks::BookmarkNode*> GetBookmarksMatchingProperties(
bookmarks::BookmarkModel* model,
const PowerBookmarkQueryFields& query,
size_t max_count) {
// ParseBookmarkQuery and some of the other util methods come from
// bookmark_utils.
std::vector<std::u16string> query_words =
bookmarks::ParseBookmarkQuery(query);
if (query.word_phrase_query && query_words.empty() && query.tags.empty()) {
return {};
}
const bookmarks::BookmarkNode* search_folder = model->root_node();
if (query.folder && query.folder->is_folder())
search_folder = query.folder;
if (query.url) {
// Shortcut into the BookmarkModel if searching for URL.
GURL url(*query.url);
std::vector<raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>
url_matched_nodes;
if (url.is_valid()) {
url_matched_nodes = model->GetNodesByURL(url);
}
bookmarks::VectorIterator iterator(&url_matched_nodes);
return GetBookmarksMatchingPropertiesImpl<bookmarks::VectorIterator>(
iterator, model, query, query_words, max_count);
}
ui::TreeNodeIterator<const bookmarks::BookmarkNode> iterator(search_folder);
return GetBookmarksMatchingPropertiesImpl<
ui::TreeNodeIterator<const bookmarks::BookmarkNode>>(
iterator, model, query, query_words, max_count);
}
void EncodeMetaForStorage(const PowerBookmarkMeta& meta, std::string* out) {
std::string data;
meta.SerializeToString(&data);
*out = base::Base64Encode(data);
}
bool DecodeMetaFromStorage(const std::string& data, PowerBookmarkMeta* out) {
if (!out)
return false;
std::string decoded_data;
if (!base::Base64Decode(data, &decoded_data))
return false;
return out->ParseFromString(decoded_data);
}
} // namespace power_bookmarks
|