1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_offset_string_conversions.h"
13 #include "components/bookmarks/browser/bookmark_node_data.h"
17 namespace user_prefs
{
18 class PrefRegistrySyncable
;
21 // A collection of bookmark utility functions used by various parts of the UI
22 // that show bookmarks (bookmark manager, bookmark bar view, ...) and other
23 // systems that involve indexing and searching bookmarks.
30 // Fields to use when finding matching bookmarks.
35 scoped_ptr
<base::string16
> word_phrase_query
;
36 scoped_ptr
<base::string16
> url
;
37 scoped_ptr
<base::string16
> title
;
40 // Clones bookmark node, adding newly created nodes to |parent| starting at
41 // |index_to_add_at|. If |reset_node_times| is true cloned bookmarks and
42 // folders will receive new creation times and folder modification times
43 // instead of using the values stored in |elements|.
44 void CloneBookmarkNode(BookmarkModel
* model
,
45 const std::vector
<BookmarkNodeData::Element
>& elements
,
46 const BookmarkNode
* parent
,
48 bool reset_node_times
);
50 // Copies nodes onto the clipboard. If |remove_nodes| is true the nodes are
51 // removed after copied to the clipboard. The nodes are copied in such a way
52 // that if pasted again copies are made.
53 void CopyToClipboard(BookmarkModel
* model
,
54 const std::vector
<const BookmarkNode
*>& nodes
,
57 // Pastes from the clipboard. The new nodes are added to |parent|, unless
58 // |parent| is null in which case this does nothing. The nodes are inserted
59 // at |index|. If |index| is -1 the nodes are added to the end.
60 void PasteFromClipboard(BookmarkModel
* model
,
61 const BookmarkNode
* parent
,
64 // Returns true if the user can copy from the pasteboard.
65 bool CanPasteFromClipboard(BookmarkModel
* model
, const BookmarkNode
* node
);
67 // Returns a vector containing up to |max_count| of the most recently modified
68 // user folders. This never returns an empty vector.
69 std::vector
<const BookmarkNode
*> GetMostRecentlyModifiedUserFolders(
70 BookmarkModel
* model
, size_t max_count
);
72 // Returns the most recently added bookmarks. This does not return folders,
73 // only nodes of type url.
74 void GetMostRecentlyAddedEntries(BookmarkModel
* model
,
76 std::vector
<const BookmarkNode
*>* nodes
);
78 // Returns true if |n1| was added more recently than |n2|.
79 bool MoreRecentlyAdded(const BookmarkNode
* n1
, const BookmarkNode
* n2
);
81 // Returns up to |max_count| bookmarks from |model| whose url or title contain
82 // the text |query.word_phrase_query| and exactly match |query.url| and
83 // |query.title|, for all of the preceding fields that are not NULL.
84 // |languages| is user's accept-language setting to decode IDN.
85 void GetBookmarksMatchingProperties(BookmarkModel
* model
,
86 const QueryFields
& query
,
88 const std::string
& languages
,
89 std::vector
<const BookmarkNode
*>* nodes
);
91 // Register user preferences for Bookmarks Bar.
92 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
94 // Returns the parent for newly created folders/bookmarks. If |selection| has
95 // one element and it is a folder, |selection[0]| is returned, otherwise
96 // |parent| is returned. If |index| is non-null it is set to the index newly
97 // added nodes should be added at.
98 const BookmarkNode
* GetParentForNewNodes(
99 const BookmarkNode
* parent
,
100 const std::vector
<const BookmarkNode
*>& selection
,
103 // Deletes the bookmark folders for the given list of |ids|.
104 void DeleteBookmarkFolders(BookmarkModel
* model
, const std::vector
<int64
>& ids
);
106 // If there are no user bookmarks for url, a bookmark is created.
107 void AddIfNotBookmarked(BookmarkModel
* model
,
109 const base::string16
& title
);
111 // Removes all bookmarks for the given |url|.
112 void RemoveAllBookmarks(BookmarkModel
* model
, const GURL
& url
);
114 // Truncates an overly-long URL, unescapes it and interprets the characters
115 // as UTF-8 (both via net::FormatUrl()), and lower-cases it, returning the
116 // result. |languages| is passed to net::FormatUrl(). |adjustments|, if
117 // non-NULL, is set to reflect the transformations the URL spec underwent to
118 // become the return value. If a caller computes offsets (e.g., for the
119 // position of matched text) in this cleaned-up string, it can use
120 // |adjustments| to calculate the location of these offsets in the original
121 // string (via base::OffsetAdjuster::UnadjustOffsets()). This is useful if
122 // later the original string gets formatted in a different way for displaying.
123 // In this case, knowing the offsets in the original string will allow them to
124 // be properly translated to offsets in the newly-formatted string.
126 // The unescaping done by this function makes it possible to match substrings
127 // that were originally escaped for navigation; for example, if the user
128 // searched for "a&p", the query would be escaped as "a%26p", so without
129 // unescaping, an input string of "a&p" would no longer match this URL. Note
130 // that the resulting unescaped URL may not be directly navigable (which is
131 // why it was escaped to begin with).
132 base::string16
CleanUpUrlForMatching(
134 const std::string
& languages
,
135 base::OffsetAdjuster::Adjustments
* adjustments
);
137 // Returns the lower-cased title, possibly truncated if the original title
139 base::string16
CleanUpTitleForMatching(const base::string16
& title
);
141 // Returns true if all the |nodes| can be edited by the user,
142 // as determined by BookmarkClient::CanBeEditedByUser().
143 bool CanAllBeEditedByUser(BookmarkClient
* client
,
144 const std::vector
<const BookmarkNode
*>& nodes
);
146 // Returns true if |url| has a bookmark in the |model| that can be edited
148 bool IsBookmarkedByUser(BookmarkModel
* model
, const GURL
& url
);
150 // Returns the node with |id|, or NULL if there is no node with |id|.
151 const BookmarkNode
* GetBookmarkNodeByID(const BookmarkModel
* model
, int64 id
);
153 // Returns true if |node| is a descendant of |root|.
154 bool IsDescendantOf(const BookmarkNode
* node
, const BookmarkNode
* root
);
156 // Returns true if any node in |list| is a descendant of |root|.
157 bool HasDescendantsOf(const std::vector
<const BookmarkNode
*>& list
,
158 const BookmarkNode
* root
);
160 } // namespace bookmarks
162 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_UTILS_H_