OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/bookmarks/bookmark_utils.h" | 5 #include "chrome/browser/bookmarks/bookmark_utils.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
12 #include "base/i18n/string_search.h" | 12 #include "base/i18n/string_search.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "chrome/browser/bookmarks/bookmark_model.h" | 17 #include "chrome/browser/bookmarks/bookmark_model.h" |
18 #include "components/bookmarks/core/common/bookmark_pref_names.h" | 18 #include "components/bookmarks/core/common/bookmark_pref_names.h" |
19 #include "components/query_parser/query_parser.h" | 19 #include "components/query_parser/query_parser.h" |
20 #include "components/user_prefs/pref_registry_syncable.h" | 20 #include "components/user_prefs/pref_registry_syncable.h" |
21 #include "content/public/browser/user_metrics.h" | 21 #include "content/public/browser/user_metrics.h" |
22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
23 #include "ui/base/models/tree_node_iterator.h" | 23 #include "ui/base/models/tree_node_iterator.h" |
| 24 #include "url/gurl.h" |
24 | 25 |
25 #if !defined(OS_ANDROID) | 26 #if !defined(OS_ANDROID) |
26 #include "chrome/browser/bookmarks/scoped_group_bookmark_actions.h" | 27 #include "chrome/browser/bookmarks/scoped_group_bookmark_actions.h" |
27 #endif | 28 #endif |
28 | 29 |
29 using base::Time; | 30 using base::Time; |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 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 |
33 void CloneBookmarkNodeImpl(BookmarkModel* model, | 38 void CloneBookmarkNodeImpl(BookmarkModel* model, |
34 const BookmarkNodeData::Element& element, | 39 const BookmarkNodeData::Element& element, |
35 const BookmarkNode* parent, | 40 const BookmarkNode* parent, |
36 int index_to_add_at, | 41 int index_to_add_at, |
37 bool reset_node_times) { | 42 bool reset_node_times) { |
38 const BookmarkNode* cloned_node = NULL; | 43 const BookmarkNode* cloned_node = NULL; |
39 if (element.is_url) { | 44 if (element.is_url) { |
40 if (reset_node_times) { | 45 if (reset_node_times) { |
41 cloned_node = model->AddURL(parent, index_to_add_at, element.title, | 46 cloned_node = model->AddURL(parent, index_to_add_at, element.title, |
42 element.url); | 47 element.url); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (!node || model->is_permanent_node(node)) | 109 if (!node || model->is_permanent_node(node)) |
105 return false; | 110 return false; |
106 | 111 |
107 for (size_t i = 0; i < selected_nodes.size(); ++i) | 112 for (size_t i = 0; i < selected_nodes.size(); ++i) |
108 if (node->id() == selected_nodes[i]->id()) | 113 if (node->id() == selected_nodes[i]->id()) |
109 return true; | 114 return true; |
110 | 115 |
111 return HasSelectedAncestor(model, selected_nodes, node->parent()); | 116 return HasSelectedAncestor(model, selected_nodes, node->parent()); |
112 } | 117 } |
113 | 118 |
| 119 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL |
| 120 // from being in the middle of an escape sequence) to no more than |
| 121 // kCleanedUpUrlMaxLength characters, returning the result. |
| 122 std::string TruncateUrl(const std::string& url) { |
| 123 if (url.length() <= kCleanedUpUrlMaxLength) |
| 124 return url; |
| 125 |
| 126 // If we're in the middle of an escape sequence, truncate just before it. |
| 127 if (url[kCleanedUpUrlMaxLength - 1] == '%') |
| 128 return url.substr(0, kCleanedUpUrlMaxLength - 1); |
| 129 if (url[kCleanedUpUrlMaxLength - 2] == '%') |
| 130 return url.substr(0, kCleanedUpUrlMaxLength - 2); |
| 131 |
| 132 return url.substr(0, kCleanedUpUrlMaxLength); |
| 133 } |
| 134 |
114 } // namespace | 135 } // namespace |
115 | 136 |
116 namespace bookmark_utils { | 137 namespace bookmark_utils { |
117 | 138 |
118 QueryFields::QueryFields() {} | 139 QueryFields::QueryFields() {} |
119 QueryFields::~QueryFields() {} | 140 QueryFields::~QueryFields() {} |
120 | 141 |
121 void CloneBookmarkNode(BookmarkModel* model, | 142 void CloneBookmarkNode(BookmarkModel* model, |
122 const std::vector<BookmarkNodeData::Element>& elements, | 143 const std::vector<BookmarkNodeData::Element>& elements, |
123 const BookmarkNode* parent, | 144 const BookmarkNode* parent, |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 388 |
368 // Remove all the bookmarks. | 389 // Remove all the bookmarks. |
369 for (size_t i = 0; i < bookmarks.size(); ++i) { | 390 for (size_t i = 0; i < bookmarks.size(); ++i) { |
370 const BookmarkNode* node = bookmarks[i]; | 391 const BookmarkNode* node = bookmarks[i]; |
371 int index = node->parent()->GetIndexOf(node); | 392 int index = node->parent()->GetIndexOf(node); |
372 if (index > -1) | 393 if (index > -1) |
373 model->Remove(node->parent(), index); | 394 model->Remove(node->parent(), index); |
374 } | 395 } |
375 } | 396 } |
376 | 397 |
| 398 base::string16 CleanUpUrlForMatching(const GURL& gurl, |
| 399 const std::string& languages) { |
| 400 return base::i18n::ToLower(net::FormatUrl( |
| 401 GURL(TruncateUrl(gurl.spec())), languages, |
| 402 net::kFormatUrlOmitUsernamePassword, |
| 403 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS, |
| 404 NULL, NULL, NULL)); |
| 405 } |
| 406 |
| 407 base::string16 CleanUpTitleForMatching(const base::string16& title) { |
| 408 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); |
| 409 } |
| 410 |
377 } // namespace bookmark_utils | 411 } // namespace bookmark_utils |
OLD | NEW |