1 // Copyright (c) 2012 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 #include "chrome/browser/bookmarks/bookmark_utils.h"
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/i18n/case_conversion.h"
12 #include "base/i18n/string_search.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/bookmarks/bookmark_model.h"
18 #include "chrome/browser/history/query_parser.h"
19 #include "chrome/browser/undo/bookmark_undo_service.h"
20 #include "chrome/browser/undo/bookmark_undo_service_factory.h"
21 #include "chrome/browser/undo/bookmark_undo_utils.h"
22 #include "chrome/common/pref_names.h"
23 #include "components/user_prefs/pref_registry_syncable.h"
24 #include "content/public/browser/user_metrics.h"
25 #include "net/base/net_util.h"
26 #include "ui/base/models/tree_node_iterator.h"
32 void CloneBookmarkNodeImpl(BookmarkModel
* model
,
33 const BookmarkNodeData::Element
& element
,
34 const BookmarkNode
* parent
,
36 bool reset_node_times
) {
37 const BookmarkNode
* cloned_node
= NULL
;
39 if (reset_node_times
) {
40 cloned_node
= model
->AddURL(parent
, index_to_add_at
, element
.title
,
43 DCHECK(!element
.date_added
.is_null());
44 cloned_node
= model
->AddURLWithCreationTime(parent
, index_to_add_at
,
45 element
.title
, element
.url
,
49 cloned_node
= model
->AddFolder(parent
, index_to_add_at
, element
.title
);
50 if (!reset_node_times
) {
51 DCHECK(!element
.date_folder_modified
.is_null());
52 model
->SetDateFolderModified(cloned_node
, element
.date_folder_modified
);
54 for (int i
= 0; i
< static_cast<int>(element
.children
.size()); ++i
)
55 CloneBookmarkNodeImpl(model
, element
.children
[i
], cloned_node
, i
,
58 model
->SetNodeMetaInfoMap(cloned_node
, element
.meta_info_map
);
61 // Comparison function that compares based on date modified of the two nodes.
62 bool MoreRecentlyModified(const BookmarkNode
* n1
, const BookmarkNode
* n2
) {
63 return n1
->date_folder_modified() > n2
->date_folder_modified();
66 // Returns true if |text| contains each string in |words|. This is used when
67 // searching for bookmarks.
68 bool DoesBookmarkTextContainWords(const base::string16
& text
,
69 const std::vector
<base::string16
>& words
) {
70 for (size_t i
= 0; i
< words
.size(); ++i
) {
71 if (!base::i18n::StringSearchIgnoringCaseAndAccents(
72 words
[i
], text
, NULL
, NULL
)) {
79 // Returns true if |node|s title or url contains the strings in |words|.
80 // |languages| argument is user's accept-language setting to decode IDN.
81 bool DoesBookmarkContainWords(const BookmarkNode
* node
,
82 const std::vector
<base::string16
>& words
,
83 const std::string
& languages
) {
85 DoesBookmarkTextContainWords(node
->GetTitle(), words
) ||
86 DoesBookmarkTextContainWords(
87 base::UTF8ToUTF16(node
->url().spec()), words
) ||
88 DoesBookmarkTextContainWords(net::FormatUrl(
89 node
->url(), languages
, net::kFormatUrlOmitNothing
,
90 net::UnescapeRule::NORMAL
, NULL
, NULL
, NULL
), words
);
93 // This is used with a tree iterator to skip subtrees which are not visible.
94 bool PruneInvisibleFolders(const BookmarkNode
* node
) {
95 return !node
->IsVisible();
98 // This traces parents up to root, determines if node is contained in a
100 bool HasSelectedAncestor(BookmarkModel
* model
,
101 const std::vector
<const BookmarkNode
*>& selectedNodes
,
102 const BookmarkNode
* node
) {
103 if (!node
|| model
->is_permanent_node(node
))
106 for (size_t i
= 0; i
< selectedNodes
.size(); ++i
)
107 if (node
->id() == selectedNodes
[i
]->id())
110 return HasSelectedAncestor(model
, selectedNodes
, node
->parent());
115 namespace bookmark_utils
{
117 QueryFields::QueryFields() {}
118 QueryFields::~QueryFields() {}
120 void CloneBookmarkNode(BookmarkModel
* model
,
121 const std::vector
<BookmarkNodeData::Element
>& elements
,
122 const BookmarkNode
* parent
,
124 bool reset_node_times
) {
125 if (!parent
->is_folder() || !model
) {
129 for (size_t i
= 0; i
< elements
.size(); ++i
) {
130 CloneBookmarkNodeImpl(model
, elements
[i
], parent
, index_to_add_at
+ i
,
135 void CopyToClipboard(BookmarkModel
* model
,
136 const std::vector
<const BookmarkNode
*>& nodes
,
141 // Create array of selected nodes with descendants filtered out.
142 std::vector
<const BookmarkNode
*> filteredNodes
;
143 for (size_t i
= 0; i
< nodes
.size(); ++i
)
144 if (!HasSelectedAncestor(model
, nodes
, nodes
[i
]->parent()))
145 filteredNodes
.push_back(nodes
[i
]);
147 BookmarkNodeData(filteredNodes
).
148 WriteToClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE
);
151 #if !defined(OS_ANDROID)
152 ScopedGroupBookmarkActions
group_cut(model
->profile());
154 for (size_t i
= 0; i
< filteredNodes
.size(); ++i
) {
155 int index
= filteredNodes
[i
]->parent()->GetIndexOf(filteredNodes
[i
]);
157 model
->Remove(filteredNodes
[i
]->parent(), index
);
162 void PasteFromClipboard(BookmarkModel
* model
,
163 const BookmarkNode
* parent
,
168 BookmarkNodeData bookmark_data
;
169 if (!bookmark_data
.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE
))
173 index
= parent
->child_count();
174 #if !defined(OS_ANDROID)
175 ScopedGroupBookmarkActions
group_paste(model
->profile());
177 CloneBookmarkNode(model
, bookmark_data
.elements
, parent
, index
, true);
180 bool CanPasteFromClipboard(const BookmarkNode
* node
) {
183 return BookmarkNodeData::ClipboardContainsBookmarks();
186 std::vector
<const BookmarkNode
*> GetMostRecentlyModifiedFolders(
187 BookmarkModel
* model
,
189 std::vector
<const BookmarkNode
*> nodes
;
190 ui::TreeNodeIterator
<const BookmarkNode
> iterator(model
->root_node(),
191 PruneInvisibleFolders
);
193 while (iterator
.has_next()) {
194 const BookmarkNode
* parent
= iterator
.Next();
195 if (parent
->is_folder() && parent
->date_folder_modified() > base::Time()) {
196 if (max_count
== 0) {
197 nodes
.push_back(parent
);
199 std::vector
<const BookmarkNode
*>::iterator i
=
200 std::upper_bound(nodes
.begin(), nodes
.end(), parent
,
201 &MoreRecentlyModified
);
202 if (nodes
.size() < max_count
|| i
!= nodes
.end()) {
203 nodes
.insert(i
, parent
);
204 while (nodes
.size() > max_count
)
208 } // else case, the root node, which we don't care about or imported nodes
209 // (which have a time of 0).
212 if (nodes
.size() < max_count
) {
213 // Add the permanent nodes if there is space. The permanent nodes are the
214 // only children of the root_node.
215 const BookmarkNode
* root_node
= model
->root_node();
217 for (int i
= 0; i
< root_node
->child_count(); ++i
) {
218 const BookmarkNode
* node
= root_node
->GetChild(i
);
219 if (node
->IsVisible() &&
220 std::find(nodes
.begin(), nodes
.end(), node
) == nodes
.end()) {
221 nodes
.push_back(node
);
223 if (nodes
.size() == max_count
)
231 void GetMostRecentlyAddedEntries(BookmarkModel
* model
,
233 std::vector
<const BookmarkNode
*>* nodes
) {
234 ui::TreeNodeIterator
<const BookmarkNode
> iterator(model
->root_node());
235 while (iterator
.has_next()) {
236 const BookmarkNode
* node
= iterator
.Next();
237 if (node
->is_url()) {
238 std::vector
<const BookmarkNode
*>::iterator insert_position
=
239 std::upper_bound(nodes
->begin(), nodes
->end(), node
,
241 if (nodes
->size() < count
|| insert_position
!= nodes
->end()) {
242 nodes
->insert(insert_position
, node
);
243 while (nodes
->size() > count
)
250 bool MoreRecentlyAdded(const BookmarkNode
* n1
, const BookmarkNode
* n2
) {
251 return n1
->date_added() > n2
->date_added();
254 void GetBookmarksMatchingProperties(BookmarkModel
* model
,
255 const QueryFields
& query
,
257 const std::string
& languages
,
258 std::vector
<const BookmarkNode
*>* nodes
) {
259 std::vector
<base::string16
> query_words
;
261 if (query
.word_phrase_query
) {
262 parser
.ParseQueryWords(base::i18n::ToLower(*query
.word_phrase_query
),
264 if (query_words
.empty())
268 ui::TreeNodeIterator
<const BookmarkNode
> iterator(model
->root_node());
269 while (iterator
.has_next()) {
270 const BookmarkNode
* node
= iterator
.Next();
271 if ((!query_words
.empty() &&
272 !DoesBookmarkContainWords(node
, query_words
, languages
)) ||
273 model
->is_permanent_node(node
)) {
277 // Check against bare url spec and IDN-decoded url.
278 if (!node
->is_url() ||
279 !(base::UTF8ToUTF16(node
->url().spec()) == *query
.url
||
281 node
->url(), languages
, net::kFormatUrlOmitNothing
,
282 net::UnescapeRule::NORMAL
, NULL
, NULL
, NULL
) == *query
.url
)) {
286 if (query
.title
&& node
->GetTitle() != *query
.title
)
289 nodes
->push_back(node
);
290 if (nodes
->size() == max_count
)
295 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
296 registry
->RegisterBooleanPref(
297 prefs::kShowBookmarkBar
,
299 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
300 registry
->RegisterBooleanPref(
301 prefs::kEditBookmarksEnabled
,
303 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
304 registry
->RegisterBooleanPref(
305 prefs::kShowAppsShortcutInBookmarkBar
,
307 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
310 const BookmarkNode
* GetParentForNewNodes(
311 const BookmarkNode
* parent
,
312 const std::vector
<const BookmarkNode
*>& selection
,
314 const BookmarkNode
* real_parent
= parent
;
316 if (selection
.size() == 1 && selection
[0]->is_folder())
317 real_parent
= selection
[0];
320 if (selection
.size() == 1 && selection
[0]->is_url()) {
321 *index
= real_parent
->GetIndexOf(selection
[0]) + 1;
323 // Node doesn't exist in parent, add to end.
325 *index
= real_parent
->child_count();
328 *index
= real_parent
->child_count();
335 void DeleteBookmarkFolders(BookmarkModel
* model
,
336 const std::vector
<int64
>& ids
) {
337 // Remove the folders that were removed. This has to be done after all the
338 // other changes have been committed.
339 for (std::vector
<int64
>::const_iterator iter
= ids
.begin();
342 const BookmarkNode
* node
= model
->GetNodeByID(*iter
);
345 const BookmarkNode
* parent
= node
->parent();
346 model
->Remove(parent
, parent
->GetIndexOf(node
));
350 void AddIfNotBookmarked(BookmarkModel
* model
,
352 const base::string16
& title
) {
353 std::vector
<const BookmarkNode
*> bookmarks
;
354 model
->GetNodesByURL(url
, &bookmarks
);
355 if (!bookmarks
.empty())
356 return; // Nothing to do, a bookmark with that url already exists.
358 content::RecordAction(base::UserMetricsAction("BookmarkAdded"));
359 const BookmarkNode
* parent
= model
->GetParentForNewNodes();
360 model
->AddURL(parent
, parent
->child_count(), title
, url
);
363 void RemoveAllBookmarks(BookmarkModel
* model
, const GURL
& url
) {
364 std::vector
<const BookmarkNode
*> bookmarks
;
365 model
->GetNodesByURL(url
, &bookmarks
);
367 // Remove all the bookmarks.
368 for (size_t i
= 0; i
< bookmarks
.size(); ++i
) {
369 const BookmarkNode
* node
= bookmarks
[i
];
370 int index
= node
->parent()->GetIndexOf(node
);
372 model
->Remove(node
->parent(), index
);
376 } // namespace bookmark_utils