Commit ce789a9b authored by noyau@chromium.org's avatar noyau@chromium.org

First implementation of enhanced_bookmark_utils.

These utility functions are used on iOS and Android.

BUG=None

Review URL: https://codereview.chromium.org/452913002

Cr-Commit-Position: refs/heads/master@{#288408}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288408 0039d316-1c4b-4281-b951-d872f2087c98
parent 755211fe
...@@ -105,6 +105,7 @@ ...@@ -105,6 +105,7 @@
'domain_reliability/uploader_unittest.cc', 'domain_reliability/uploader_unittest.cc',
'domain_reliability/util_unittest.cc', 'domain_reliability/util_unittest.cc',
# Note: GN tests converted to here, need to do the rest. # Note: GN tests converted to here, need to do the rest.
'enhanced_bookmarks/enhanced_bookmark_utils_unittest.cc',
'enhanced_bookmarks/image_store_ios_unittest.mm', 'enhanced_bookmarks/image_store_ios_unittest.mm',
'enhanced_bookmarks/image_store_unittest.cc', 'enhanced_bookmarks/image_store_unittest.cc',
'enhanced_bookmarks/metadata_accessor_unittest.cc', 'enhanced_bookmarks/metadata_accessor_unittest.cc',
......
...@@ -15,9 +15,12 @@ ...@@ -15,9 +15,12 @@
'../sql/sql.gyp:sql', '../sql/sql.gyp:sql',
'../ui/gfx/gfx.gyp:gfx', '../ui/gfx/gfx.gyp:gfx',
'../url/url.gyp:url_lib', '../url/url.gyp:url_lib',
'bookmarks_browser',
'enhanced_bookmarks_proto', 'enhanced_bookmarks_proto',
], ],
'sources': [ 'sources': [
'enhanced_bookmarks/enhanced_bookmark_utils.cc',
'enhanced_bookmarks/enhanced_bookmark_utils.h',
'enhanced_bookmarks/image_store.cc', 'enhanced_bookmarks/image_store.cc',
'enhanced_bookmarks/image_store.h', 'enhanced_bookmarks/image_store.h',
'enhanced_bookmarks/image_store_util.cc', 'enhanced_bookmarks/image_store_util.cc',
......
// Copyright 2014 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.
#include "components/enhanced_bookmarks/enhanced_bookmark_utils.h"
#include "base/i18n/string_compare.h"
#include "base/i18n/string_search.h"
#include "base/strings/utf_string_conversions.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "ui/base/models/tree_node_iterator.h"
namespace enhanced_bookmark_utils {
std::vector<const BookmarkNode*> FindBookmarksWithQuery(
BookmarkModel* bookmark_model,
const std::string& query) {
std::vector<const BookmarkNode*> results;
base::string16 query16 = base::UTF8ToUTF16(query);
base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents pattern16(query16);
ui::TreeNodeIterator<const BookmarkNode> iterator(
bookmark_model->root_node());
while (iterator.has_next()) {
const BookmarkNode* node = iterator.Next();
if (!node->is_url())
continue;
// Search the title for the query.
size_t match_index;
size_t match_length;
bool found =
pattern16.Search(node->GetTitle(), &match_index, &match_length);
if (found)
results.push_back(node);
}
return results;
}
// Comparator used to sort bookmarks. No folders are allowed.
class BookmarkNameComparator : public std::binary_function<const BookmarkNode*,
const BookmarkNode*,
bool> {
public:
explicit BookmarkNameComparator(icu::Collator* collator)
: collator_(collator) {}
// Returns true if |n1| preceeds |n2|.
bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) {
DCHECK(!n1->is_folder());
DCHECK(!n2->is_folder());
if (!collator_)
return n1->GetTitle() < n2->GetTitle();
return base::i18n::CompareString16WithCollator(
collator_, n1->GetTitle(), n2->GetTitle()) == UCOL_LESS;
}
private:
icu::Collator* collator_;
};
void SortBookmarksByName(std::vector<const BookmarkNode*>& nodes) {
UErrorCode error = U_ZERO_ERROR;
scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error));
if (U_FAILURE(error))
collator.reset(NULL);
std::sort(nodes.begin(), nodes.end(), BookmarkNameComparator(collator.get()));
}
std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model) {
DCHECK(model->loaded());
std::vector<const BookmarkNode*> nodes;
nodes.push_back(model->other_node());
nodes.push_back(model->mobile_node());
return nodes;
}
std::vector<const BookmarkNode*> RootLevelFolders(BookmarkModel* model) {
std::vector<const BookmarkNode*> root_level_folders;
// Find the direct folder children of the primary permanent nodes.
std::vector<const BookmarkNode*> primary_permanent_nodes =
PrimaryPermanentNodes(model);
for (const BookmarkNode* parent : primary_permanent_nodes) {
int child_count = parent->child_count();
for (int i = 0; i < child_count; ++i) {
const BookmarkNode* node = parent->GetChild(i);
if (node->is_folder() && node->IsVisible())
root_level_folders.push_back(node);
}
}
// Add the bookmark bar if it has children.
const BookmarkNode* bb_node = model->bookmark_bar_node();
if (bb_node->child_count() > 0)
root_level_folders.push_back(bb_node);
return root_level_folders;
}
bool IsPrimaryPermanentNode(const BookmarkNode* node, BookmarkModel* model) {
std::vector<const BookmarkNode*> primary_nodes(PrimaryPermanentNodes(model));
if (std::find(primary_nodes.begin(), primary_nodes.end(), node) !=
primary_nodes.end()) {
return true;
}
return false;
}
const BookmarkNode* RootLevelFolderForNode(const BookmarkNode* node,
BookmarkModel* model) {
// This helper function doesn't work for managed bookmarks. This checks that
// |node| is editable by the user, which currently covers all the other
// bookmarks except the managed bookmarks.
DCHECK(model->client()->CanBeEditedByUser(node));
const std::vector<const BookmarkNode*> root_folders(RootLevelFolders(model));
const BookmarkNode* top = node;
while (top &&
std::find(root_folders.begin(), root_folders.end(), top) ==
root_folders.end()) {
top = top->parent();
}
return top;
}
} // namespace enhanced_bookmark_utils
// Copyright 2014 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_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_UTILS_H_
#define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_UTILS_H_
#include <set>
#include <string>
#include <vector>
class BookmarkModel;
class BookmarkNode;
namespace enhanced_bookmark_utils {
// Returns an ordered vector of bookmarks that are urls that match |query|.
// |query| must be UTF8 encoded.
std::vector<const BookmarkNode*> FindBookmarksWithQuery(
BookmarkModel* bookmark_model,
const std::string& query);
// The vector is sorted in place.
// All of the bookmarks in |nodes| must be urls.
void SortBookmarksByName(std::vector<const BookmarkNode*>& nodes);
// Returns the permanent nodes whose url children are considered uncategorized
// and whose folder children should be shown in the bookmark menu.
// |model| must be loaded.
std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model);
// Returns an unsorted vector of folders that are considered to be at the "root"
// level of the bookmark hierarchy. Functionally, this means all direct
// descendants of PrimaryPermanentNodes, and possibly a node for the bookmarks
// bar.
std::vector<const BookmarkNode*> RootLevelFolders(BookmarkModel* model);
// Returns whether |node| is a primary permanent node in the sense of
// |PrimaryPermanentNodes|.
bool IsPrimaryPermanentNode(const BookmarkNode* node, BookmarkModel* model);
// Returns the root level folder in which this node is directly, or indirectly
// via subfolders, located.
const BookmarkNode* RootLevelFolderForNode(const BookmarkNode* node,
BookmarkModel* model);
} // namespace enhanced_bookmark_utils
#endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_UTILS_H_
// Copyright 2014 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.
#include "base/base64.h"
#include "base/strings/utf_string_conversions.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/test/test_bookmark_client.h"
#include "components/enhanced_bookmarks/enhanced_bookmark_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const GURL bookmark_url("http://example.com/index.html");
class EnhancedBookmarkUtilsTest : public testing::Test {
public:
EnhancedBookmarkUtilsTest() {}
virtual ~EnhancedBookmarkUtilsTest() {}
protected:
DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarkUtilsTest);
// Adds a bookmark as the subnode at index 0 to other_node.
// |name| should be ASCII encoded.
// Returns the newly added bookmark.
const BookmarkNode* AddBookmark(BookmarkModel* model, std::string name) {
return model->AddURL(model->other_node(),
0, // index.
base::ASCIIToUTF16(name),
bookmark_url);
}
};
TEST_F(EnhancedBookmarkUtilsTest, TestBookmarkSearch) {
test::TestBookmarkClient bookmark_client;
scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel(false));
const BookmarkNode* node1 = AddBookmark(bookmark_model.get(), "john hopkins");
const BookmarkNode* node2 = AddBookmark(bookmark_model.get(), "JohN hopkins");
const BookmarkNode* node3 = AddBookmark(bookmark_model.get(), "katy perry");
const BookmarkNode* node4 = AddBookmark(bookmark_model.get(), "lil'john13");
const BookmarkNode* node5 = AddBookmark(bookmark_model.get(), "jo hn");
std::vector<const BookmarkNode*> result =
enhanced_bookmark_utils::FindBookmarksWithQuery(bookmark_model.get(),
"john");
ASSERT_EQ(result.size(), 3u);
CHECK(std::find(result.begin(), result.end(), node1) != result.end());
CHECK(std::find(result.begin(), result.end(), node2) != result.end());
CHECK(std::find(result.begin(), result.end(), node4) != result.end());
CHECK(std::find(result.begin(), result.end(), node3) == result.end());
CHECK(std::find(result.begin(), result.end(), node5) == result.end());
};
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment