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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 return node; | 121 return node; |
117 | 122 |
118 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) { | 123 for (int i = 0, child_count = node->child_count(); i < child_count; ++i) { |
119 const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); | 124 const BookmarkNode* result = GetNodeByID(node->GetChild(i), id); |
120 if (result) | 125 if (result) |
121 return result; | 126 return result; |
122 } | 127 } |
123 return NULL; | 128 return NULL; |
124 } | 129 } |
125 | 130 |
| 131 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL |
| 132 // from being in the middle of an escape sequence) to no more than |
| 133 // kCleanedUpUrlMaxLength characters, returning the result. |
| 134 std::string TruncateUrl(const std::string& url) { |
| 135 if (url.length() <= kCleanedUpUrlMaxLength) |
| 136 return url; |
| 137 |
| 138 // If we're in the middle of an escape sequence, truncate just before it. |
| 139 if (url[kCleanedUpUrlMaxLength - 1] == '%') |
| 140 return url.substr(0, kCleanedUpUrlMaxLength - 1); |
| 141 if (url[kCleanedUpUrlMaxLength - 2] == '%') |
| 142 return url.substr(0, kCleanedUpUrlMaxLength - 2); |
| 143 |
| 144 return url.substr(0, kCleanedUpUrlMaxLength); |
| 145 } |
| 146 |
126 } // namespace | 147 } // namespace |
127 | 148 |
128 namespace bookmark_utils { | 149 namespace bookmark_utils { |
129 | 150 |
130 QueryFields::QueryFields() {} | 151 QueryFields::QueryFields() {} |
131 QueryFields::~QueryFields() {} | 152 QueryFields::~QueryFields() {} |
132 | 153 |
133 void CloneBookmarkNode(BookmarkModel* model, | 154 void CloneBookmarkNode(BookmarkModel* model, |
134 const std::vector<BookmarkNodeData::Element>& elements, | 155 const std::vector<BookmarkNodeData::Element>& elements, |
135 const BookmarkNode* parent, | 156 const BookmarkNode* parent, |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 | 400 |
380 // Remove all the bookmarks. | 401 // Remove all the bookmarks. |
381 for (size_t i = 0; i < bookmarks.size(); ++i) { | 402 for (size_t i = 0; i < bookmarks.size(); ++i) { |
382 const BookmarkNode* node = bookmarks[i]; | 403 const BookmarkNode* node = bookmarks[i]; |
383 int index = node->parent()->GetIndexOf(node); | 404 int index = node->parent()->GetIndexOf(node); |
384 if (index > -1) | 405 if (index > -1) |
385 model->Remove(node->parent(), index); | 406 model->Remove(node->parent(), index); |
386 } | 407 } |
387 } | 408 } |
388 | 409 |
| 410 base::string16 CleanUpUrlForMatching(const GURL& gurl, |
| 411 const std::string& languages) { |
| 412 return base::i18n::ToLower(net::FormatUrl( |
| 413 GURL(TruncateUrl(gurl.spec())), languages, |
| 414 net::kFormatUrlOmitUsernamePassword, |
| 415 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS, |
| 416 NULL, NULL, NULL)); |
| 417 } |
| 418 |
| 419 base::string16 CleanUpTitleForMatching(const base::string16& title) { |
| 420 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength)); |
| 421 } |
| 422 |
389 } // namespace bookmark_utils | 423 } // namespace bookmark_utils |
390 | 424 |
391 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | 425 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { |
392 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | 426 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
393 return GetNodeByID(model->root_node(), id); | 427 return GetNodeByID(model->root_node(), id); |
394 } | 428 } |
OLD | NEW |