Commit a9422c2d authored by noyau's avatar noyau Committed by Commit bot

Moving bookmarks utility function to ios/

The CL http://crrev.com/1638413003 will remove enhanced bookmarks but
the iOS code (and only the iOS code) dealing with bookmarks uses four
utility functions. This CL copies those functions to iOS before they
disapear.

BUG=None

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

Cr-Commit-Position: refs/heads/master@{#372081}
parent 137a3c7b
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
#include "ios/chrome/browser/pref_names.h" #include "ios/chrome/browser/pref_names.h"
#include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;
void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) { void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) {
DCHECK(launch_location < BOOKMARK_LAUNCH_LOCATION_COUNT); DCHECK(launch_location < BOOKMARK_LAUNCH_LOCATION_COUNT);
UMA_HISTOGRAM_ENUMERATION("Stars.LaunchLocation", launch_location, UMA_HISTOGRAM_ENUMERATION("Stars.LaunchLocation", launch_location,
...@@ -19,7 +22,7 @@ void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) { ...@@ -19,7 +22,7 @@ void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location) {
} }
bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) { bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) {
bookmarks::BookmarkModel* bookmark_model = BookmarkModel* bookmark_model =
ios::BookmarkModelFactory::GetForBrowserState(browser_state); ios::BookmarkModelFactory::GetForBrowserState(browser_state);
if (!bookmark_model->loaded()) if (!bookmark_model->loaded())
...@@ -40,3 +43,55 @@ bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) { ...@@ -40,3 +43,55 @@ bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) {
return true; return true;
} }
std::vector<const BookmarkNode*> PrimaryPermanentNodes(BookmarkModel* model) {
DCHECK(model->loaded());
std::vector<const BookmarkNode*> nodes;
nodes.push_back(model->mobile_node());
nodes.push_back(model->bookmark_bar_node());
nodes.push_back(model->other_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);
}
}
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;
}
...@@ -5,8 +5,16 @@ ...@@ -5,8 +5,16 @@
#ifndef IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_ #ifndef IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_
#define IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_ #define IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_
#include <set>
#include <vector>
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
namespace bookmarks {
class BookmarkModel;
class BookmarkNode;
}
namespace ios { namespace ios {
class ChromeBrowserState; class ChromeBrowserState;
} }
...@@ -32,4 +40,27 @@ void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location); ...@@ -32,4 +40,27 @@ void RecordBookmarkLaunch(BookmarkLaunchLocation launch_location);
bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state) bool RemoveAllUserBookmarksIOS(ios::ChromeBrowserState* browser_state)
WARN_UNUSED_RESULT; WARN_UNUSED_RESULT;
// 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 bookmarks::BookmarkNode*> PrimaryPermanentNodes(
bookmarks::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.
std::vector<const bookmarks::BookmarkNode*> RootLevelFolders(
bookmarks::BookmarkModel* model);
// Returns whether |node| is a primary permanent node in the sense of
// |PrimaryPermanentNodes|.
bool IsPrimaryPermanentNode(const bookmarks::BookmarkNode* node,
bookmarks::BookmarkModel* model);
// Returns the root level folder in which this node is directly, or indirectly
// via subfolders, located.
const bookmarks::BookmarkNode* RootLevelFolderForNode(
const bookmarks::BookmarkNode* node,
bookmarks::BookmarkModel* model);
#endif // IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_ #endif // IOS_CHROME_BROWSER_BOOKMARKS_BOOKMARKS_UTILS_H_
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