OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/bookmarks/bookmark_utils.h" | |
6 | |
7 #include <utility> | |
8 | |
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/metrics/user_metrics_action.h" | |
14 #include "base/prefs/pref_service.h" | |
15 #include "base/strings/string16.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "base/time/time.h" | |
18 #include "chrome/browser/bookmarks/bookmark_model.h" | |
19 #include "components/bookmarks/core/common/bookmark_pref_names.h" | |
20 #include "components/query_parser/query_parser.h" | |
21 #include "components/user_prefs/pref_registry_syncable.h" | |
22 #include "net/base/net_util.h" | |
23 #include "ui/base/models/tree_node_iterator.h" | |
24 #include "url/gurl.h" | |
25 | |
26 #if !defined(OS_ANDROID) | |
27 #include "chrome/browser/bookmarks/scoped_group_bookmark_actions.h" | |
28 #endif | |
29 | |
30 using base::Time; | |
31 | |
32 namespace { | |
33 | |
34 // The maximum length of URL or title returned by the Cleanup functions. | |
35 const size_t kCleanedUpUrlMaxLength = 1024u; | |
36 const size_t kCleanedUpTitleMaxLength = 1024u; | |
37 | |
38 void CloneBookmarkNodeImpl(BookmarkModel* model, | |
39 const BookmarkNodeData::Element& element, | |
40 const BookmarkNode* parent, | |
41 int index_to_add_at, | |
42 bool reset_node_times) { | |
43 if (element.is_url) { | |
44 base::Time date_added = reset_node_times ? Time::Now() : element.date_added; | |
45 DCHECK(!date_added.is_null()); | |
46 | |
47 model->AddURLWithCreationTimeAndMetaInfo(parent, | |
48 index_to_add_at, | |
49 element.title, | |
50 element.url, | |
51 date_added, | |
52 &element.meta_info_map); | |
53 } else { | |
54 const BookmarkNode* cloned_node = model->AddFolderWithMetaInfo( | |
55 parent, index_to_add_at, element.title, &element.meta_info_map); | |
56 if (!reset_node_times) { | |
57 DCHECK(!element.date_folder_modified.is_null()); | |
58 model->SetDateFolderModified(cloned_node, element.date_folder_modified); | |
59 } | |
60 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | |
61 CloneBookmarkNodeImpl(model, element.children[i], cloned_node, i, | |
62 reset_node_times); | |
63 } | |
64 } | |
65 | |
66 // Comparison function that compares based on date modified of the two nodes. | |
67 bool MoreRecentlyModified(const BookmarkNode* n1, const BookmarkNode* n2) { | |
68 return n1->date_folder_modified() > n2->date_folder_modified(); | |
69 } | |
70 | |
71 // Returns true if |text| contains each string in |words|. This is used when | |
72 // searching for bookmarks. | |
73 bool DoesBookmarkTextContainWords(const base::string16& text, | |
74 const std::vector<base::string16>& words) { | |
75 for (size_t i = 0; i < words.size(); ++i) { | |
76 if (!base::i18n::StringSearchIgnoringCaseAndAccents( | |
77 words[i], text, NULL, NULL)) { | |
78 return false; | |
79 } | |
80 } | |
81 return true; | |
82 } | |
83 | |
84 // Returns true if |node|s title or url contains the strings in |words|. | |
85 // |languages| argument is user's accept-language setting to decode IDN. | |
86 bool DoesBookmarkContainWords(const BookmarkNode* node, | |
87 const std::vector<base::string16>& words, | |
88 const std::string& languages) { | |
89 return | |
90 DoesBookmarkTextContainWords(node->GetTitle(), words) || | |
91 DoesBookmarkTextContainWords( | |
92 base::UTF8ToUTF16(node->url().spec()), words) || | |
93 DoesBookmarkTextContainWords(net::FormatUrl( | |
94 node->url(), languages, net::kFormatUrlOmitNothing, | |
95 net::UnescapeRule::NORMAL, NULL, NULL, NULL), words); | |
96 } | |
97 | |
98 // This is used with a tree iterator to skip subtrees which are not visible. | |
99 bool PruneInvisibleFolders(const BookmarkNode* node) { | |
100 return !node->IsVisible(); | |
101 } | |
102 | |
103 // This traces parents up to root, determines if node is contained in a | |
104 // selected folder. | |
105 bool HasSelectedAncestor(BookmarkModel* model, | |
106 const std::vector<const BookmarkNode*>& selected_nodes, | |
107 const BookmarkNode* node) { | |
108 if (!node || model->is_permanent_node(node)) | |
109 return false; | |
110 | |
111 for (size_t i = 0; i < selected_nodes.size(); ++i) | |
112 if (node->id() == selected_nodes[i]->id()) | |
113 return true; | |
114 | |
115 return HasSelectedAncestor(model, selected_nodes, node->parent()); | |
116 } | |
117 | |
118 const BookmarkNode* GetNodeByID(const BookmarkNode* node, int64 id) { | |
119 if (node->id() == id) | |
120 return node; | |
121 | |
122 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) { | |
123 const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); | |
124 if (result) | |
125 return result; | |
126 } | |
127 return NULL; | |
128 } | |
129 | |
130 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL | |
131 // from being in the middle of an escape sequence) to no more than | |
132 // kCleanedUpUrlMaxLength characters, returning the result. | |
133 std::string TruncateUrl(const std::string& url) { | |
134 if (url.length() <= kCleanedUpUrlMaxLength) | |
135 return url; | |
136 | |
137 // If we're in the middle of an escape sequence, truncate just before it. | |
138 if (url[kCleanedUpUrlMaxLength - 1] == '%') | |
139 return url.substr(0, kCleanedUpUrlMaxLength - 1); | |
140 if (url[kCleanedUpUrlMaxLength - 2] == '%') | |
141 return url.substr(0, kCleanedUpUrlMaxLength - 2); | |
142 | |
143 return url.substr(0, kCleanedUpUrlMaxLength); | |
144 } | |
145 | |
146 } // namespace | |
147 | |
148 namespace bookmark_utils { | |
149 | |
150 QueryFields::QueryFields() {} | |
151 QueryFields::~QueryFields() {} | |
152 | |
153 void CloneBookmarkNode(BookmarkModel* model, | |
154 const std::vector<BookmarkNodeData::Element>& elements, | |
155 const BookmarkNode* parent, | |
156 int index_to_add_at, | |
157 bool reset_node_times) { | |
158 if (!parent->is_folder() || !model) { | |
159 NOTREACHED(); | |
160 return; | |
161 } | |
162 for (size_t i = 0; i < elements.size(); ++i) { | |
163 CloneBookmarkNodeImpl(model, elements[i], parent, index_to_add_at + i, | |
164 reset_node_times); | |
165 } | |
166 } | |
167 | |
168 void CopyToClipboard(BookmarkModel* model, | |
169 const std::vector<const BookmarkNode*>& nodes, | |
170 bool remove_nodes) { | |
171 if (nodes.empty()) | |
172 return; | |
173 | |
174 // Create array of selected nodes with descendants filtered out. | |
175 std::vector<const BookmarkNode*> filtered_nodes; | |
176 for (size_t i = 0; i < nodes.size(); ++i) | |
177 if (!HasSelectedAncestor(model, nodes, nodes[i]->parent())) | |
178 filtered_nodes.push_back(nodes[i]); | |
179 | |
180 BookmarkNodeData(filtered_nodes). | |
181 WriteToClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
182 | |
183 if (remove_nodes) { | |
184 #if !defined(OS_ANDROID) | |
185 ScopedGroupBookmarkActions group_cut(model); | |
186 #endif | |
187 for (size_t i = 0; i < filtered_nodes.size(); ++i) { | |
188 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); | |
189 if (index > -1) | |
190 model->Remove(filtered_nodes[i]->parent(), index); | |
191 } | |
192 } | |
193 } | |
194 | |
195 void PasteFromClipboard(BookmarkModel* model, | |
196 const BookmarkNode* parent, | |
197 int index) { | |
198 if (!parent) | |
199 return; | |
200 | |
201 BookmarkNodeData bookmark_data; | |
202 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) | |
203 return; | |
204 | |
205 if (index == -1) | |
206 index = parent->child_count(); | |
207 #if !defined(OS_ANDROID) | |
208 ScopedGroupBookmarkActions group_paste(model); | |
209 #endif | |
210 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | |
211 } | |
212 | |
213 bool CanPasteFromClipboard(const BookmarkNode* node) { | |
214 if (!node) | |
215 return false; | |
216 return BookmarkNodeData::ClipboardContainsBookmarks(); | |
217 } | |
218 | |
219 std::vector<const BookmarkNode*> GetMostRecentlyModifiedFolders( | |
220 BookmarkModel* model, | |
221 size_t max_count) { | |
222 std::vector<const BookmarkNode*> nodes; | |
223 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node(), | |
224 PruneInvisibleFolders); | |
225 | |
226 while (iterator.has_next()) { | |
227 const BookmarkNode* parent = iterator.Next(); | |
228 if (parent->is_folder() && parent->date_folder_modified() > base::Time()) { | |
229 if (max_count == 0) { | |
230 nodes.push_back(parent); | |
231 } else { | |
232 std::vector<const BookmarkNode*>::iterator i = | |
233 std::upper_bound(nodes.begin(), nodes.end(), parent, | |
234 &MoreRecentlyModified); | |
235 if (nodes.size() < max_count || i != nodes.end()) { | |
236 nodes.insert(i, parent); | |
237 while (nodes.size() > max_count) | |
238 nodes.pop_back(); | |
239 } | |
240 } | |
241 } // else case, the root node, which we don't care about or imported nodes | |
242 // (which have a time of 0). | |
243 } | |
244 | |
245 if (nodes.size() < max_count) { | |
246 // Add the permanent nodes if there is space. The permanent nodes are the | |
247 // only children of the root_node. | |
248 const BookmarkNode* root_node = model->root_node(); | |
249 | |
250 for (int i = 0; i < root_node->child_count(); ++i) { | |
251 const BookmarkNode* node = root_node->GetChild(i); | |
252 if (node->IsVisible() && | |
253 std::find(nodes.begin(), nodes.end(), node) == nodes.end()) { | |
254 nodes.push_back(node); | |
255 | |
256 if (nodes.size() == max_count) | |
257 break; | |
258 } | |
259 } | |
260 } | |
261 return nodes; | |
262 } | |
263 | |
264 void GetMostRecentlyAddedEntries(BookmarkModel* model, | |
265 size_t count, | |
266 std::vector<const BookmarkNode*>* nodes) { | |
267 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | |
268 while (iterator.has_next()) { | |
269 const BookmarkNode* node = iterator.Next(); | |
270 if (node->is_url()) { | |
271 std::vector<const BookmarkNode*>::iterator insert_position = | |
272 std::upper_bound(nodes->begin(), nodes->end(), node, | |
273 &MoreRecentlyAdded); | |
274 if (nodes->size() < count || insert_position != nodes->end()) { | |
275 nodes->insert(insert_position, node); | |
276 while (nodes->size() > count) | |
277 nodes->pop_back(); | |
278 } | |
279 } | |
280 } | |
281 } | |
282 | |
283 bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2) { | |
284 return n1->date_added() > n2->date_added(); | |
285 } | |
286 | |
287 void GetBookmarksMatchingProperties(BookmarkModel* model, | |
288 const QueryFields& query, | |
289 size_t max_count, | |
290 const std::string& languages, | |
291 std::vector<const BookmarkNode*>* nodes) { | |
292 std::vector<base::string16> query_words; | |
293 query_parser::QueryParser parser; | |
294 if (query.word_phrase_query) { | |
295 parser.ParseQueryWords(base::i18n::ToLower(*query.word_phrase_query), | |
296 &query_words); | |
297 if (query_words.empty()) | |
298 return; | |
299 } | |
300 | |
301 ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node()); | |
302 while (iterator.has_next()) { | |
303 const BookmarkNode* node = iterator.Next(); | |
304 if ((!query_words.empty() && | |
305 !DoesBookmarkContainWords(node, query_words, languages)) || | |
306 model->is_permanent_node(node)) { | |
307 continue; | |
308 } | |
309 if (query.url) { | |
310 // Check against bare url spec and IDN-decoded url. | |
311 if (!node->is_url() || | |
312 !(base::UTF8ToUTF16(node->url().spec()) == *query.url || | |
313 net::FormatUrl( | |
314 node->url(), languages, net::kFormatUrlOmitNothing, | |
315 net::UnescapeRule::NORMAL, NULL, NULL, NULL) == *query.url)) { | |
316 continue; | |
317 } | |
318 } | |
319 if (query.title && node->GetTitle() != *query.title) | |
320 continue; | |
321 | |
322 nodes->push_back(node); | |
323 if (nodes->size() == max_count) | |
324 return; | |
325 } | |
326 } | |
327 | |
328 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | |
329 registry->RegisterBooleanPref( | |
330 prefs::kShowBookmarkBar, | |
331 false, | |
332 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
333 registry->RegisterBooleanPref( | |
334 prefs::kEditBookmarksEnabled, | |
335 true, | |
336 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
337 registry->RegisterBooleanPref( | |
338 prefs::kShowAppsShortcutInBookmarkBar, | |
339 true, | |
340 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
341 } | |
342 | |
343 const BookmarkNode* GetParentForNewNodes( | |
344 const BookmarkNode* parent, | |
345 const std::vector<const BookmarkNode*>& selection, | |
346 int* index) { | |
347 const BookmarkNode* real_parent = parent; | |
348 | |
349 if (selection.size() == 1 && selection[0]->is_folder()) | |
350 real_parent = selection[0]; | |
351 | |
352 if (index) { | |
353 if (selection.size() == 1 && selection[0]->is_url()) { | |
354 *index = real_parent->GetIndexOf(selection[0]) + 1; | |
355 if (*index == 0) { | |
356 // Node doesn't exist in parent, add to end. | |
357 NOTREACHED(); | |
358 *index = real_parent->child_count(); | |
359 } | |
360 } else { | |
361 *index = real_parent->child_count(); | |
362 } | |
363 } | |
364 | |
365 return real_parent; | |
366 } | |
367 | |
368 void DeleteBookmarkFolders(BookmarkModel* model, | |
369 const std::vector<int64>& ids) { | |
370 // Remove the folders that were removed. This has to be done after all the | |
371 // other changes have been committed. | |
372 for (std::vector<int64>::const_iterator iter = ids.begin(); | |
373 iter != ids.end(); | |
374 ++iter) { | |
375 const BookmarkNode* node = GetBookmarkNodeByID(model, *iter); | |
376 if (!node) | |
377 continue; | |
378 const BookmarkNode* parent = node->parent(); | |
379 model->Remove(parent, parent->GetIndexOf(node)); | |
380 } | |
381 } | |
382 | |
383 void AddIfNotBookmarked(BookmarkModel* model, | |
384 const GURL& url, | |
385 const base::string16& title) { | |
386 std::vector<const BookmarkNode*> bookmarks; | |
387 model->GetNodesByURL(url, &bookmarks); | |
388 if (!bookmarks.empty()) | |
389 return; // Nothing to do, a bookmark with that url already exists. | |
390 | |
391 model->client()->RecordAction(base::UserMetricsAction("BookmarkAdded")); | |
392 const BookmarkNode* parent = model->GetParentForNewNodes(); | |
393 model->AddURL(parent, parent->child_count(), title, url); | |
394 } | |
395 | |
396 void RemoveAllBookmarks(BookmarkModel* model, const GURL& url) { | |
397 std::vector<const BookmarkNode*> bookmarks; | |
398 model->GetNodesByURL(url, &bookmarks); | |
399 | |
400 // Remove all the bookmarks. | |
401 for (size_t i = 0; i < bookmarks.size(); ++i) { | |
402 const BookmarkNode* node = bookmarks[i]; | |
403 int index = node->parent()->GetIndexOf(node); | |
404 if (index > -1) | |
405 model->Remove(node->parent(), index); | |
406 } | |
407 } | |
408 | |
409 base::string16 CleanUpUrlForMatching( | |
410 const GURL& gurl, | |
411 const std::string& languages, | |
412 base::OffsetAdjuster::Adjustments* adjustments) { | |
413 base::OffsetAdjuster::Adjustments tmp_adjustments; | |
414 return base::i18n::ToLower(net::FormatUrlWithAdjustments( | |
415 GURL(TruncateUrl(gurl.spec())), languages, | |
416 net::kFormatUrlOmitUsernamePassword, | |
417 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS, | |
418 NULL, NULL, adjustments ? adjustments : &tmp_adjustments)); | |
419 } | |
420 | |
421 base::string16 CleanUpTitleForMatching(const base::string16& title) { | |
422 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); | |
423 } | |
424 | |
425 } // namespace bookmark_utils | |
426 | |
427 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | |
428 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | |
429 return GetNodeByID(model->root_node(), id); | |
430 } | |
OLD | NEW |