Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Side by Side Diff: chrome/browser/bookmarks/bookmark_utils.cc

Issue 184663002: Omnibox: Make URLs of Bookmarks Searchable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Peter's comments Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/bookmarks/bookmark_model_factory.h" 18 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "components/query_parser/query_parser.h" 20 #include "components/query_parser/query_parser.h"
21 #include "components/user_prefs/pref_registry_syncable.h" 21 #include "components/user_prefs/pref_registry_syncable.h"
22 #include "content/public/browser/user_metrics.h" 22 #include "content/public/browser/user_metrics.h"
23 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
24 #include "ui/base/models/tree_node_iterator.h" 24 #include "ui/base/models/tree_node_iterator.h"
25 #include "url/gurl.h"
25 26
26 #if !defined(OS_ANDROID) 27 #if !defined(OS_ANDROID)
27 #include "chrome/browser/bookmarks/scoped_group_bookmark_actions.h" 28 #include "chrome/browser/bookmarks/scoped_group_bookmark_actions.h"
28 #endif 29 #endif
29 30
30 using base::Time; 31 using base::Time;
31 32
32 namespace { 33 namespace {
33 34
35 // The maximum length of URL or title returned by the Cleanup functions.
36 static const size_t kCleanedUpUrlMaxLength = 1024u;
tfarina 2014/04/18 00:26:04 No static needed. You are already in an unnamed na
Mark P 2014/04/18 14:52:43 Done. (here and below)
37 static const size_t kCleanedUpTitleMaxLength = 1024u;
38
34 void CloneBookmarkNodeImpl(BookmarkModel* model, 39 void CloneBookmarkNodeImpl(BookmarkModel* model,
35 const BookmarkNodeData::Element& element, 40 const BookmarkNodeData::Element& element,
36 const BookmarkNode* parent, 41 const BookmarkNode* parent,
37 int index_to_add_at, 42 int index_to_add_at,
38 bool reset_node_times) { 43 bool reset_node_times) {
39 const BookmarkNode* cloned_node = NULL; 44 const BookmarkNode* cloned_node = NULL;
40 if (element.is_url) { 45 if (element.is_url) {
41 if (reset_node_times) { 46 if (reset_node_times) {
42 cloned_node = model->AddURL(parent, index_to_add_at, element.title, 47 cloned_node = model->AddURL(parent, index_to_add_at, element.title,
43 element.url); 48 element.url);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 if (!node || model->is_permanent_node(node)) 110 if (!node || model->is_permanent_node(node))
106 return false; 111 return false;
107 112
108 for (size_t i = 0; i < selected_nodes.size(); ++i) 113 for (size_t i = 0; i < selected_nodes.size(); ++i)
109 if (node->id() == selected_nodes[i]->id()) 114 if (node->id() == selected_nodes[i]->id())
110 return true; 115 return true;
111 116
112 return HasSelectedAncestor(model, selected_nodes, node->parent()); 117 return HasSelectedAncestor(model, selected_nodes, node->parent());
113 } 118 }
114 119
120 // Attempts to shorten a URL safely (i.e., by preventing the end of the URL
121 // from being in the middle of an escape sequence) to no more than
122 // kCleanedUpUrlMaxLength characters, returning the result.
123 std::string TruncateUrl(const std::string& url) {
124 if (url.length() <= kCleanedUpUrlMaxLength)
125 return url;
126
127 // If we're in the middle of an escape sequence, truncate just before it.
128 if (url[kCleanedUpUrlMaxLength - 1] == '%')
129 return url.substr(0, kCleanedUpUrlMaxLength - 1);
130 if (url[kCleanedUpUrlMaxLength - 2] == '%')
131 return url.substr(0, kCleanedUpUrlMaxLength - 2);
132
133 return url.substr(0, kCleanedUpUrlMaxLength);
134 }
135
115 } // namespace 136 } // namespace
116 137
117 namespace bookmark_utils { 138 namespace bookmark_utils {
118 139
119 QueryFields::QueryFields() {} 140 QueryFields::QueryFields() {}
120 QueryFields::~QueryFields() {} 141 QueryFields::~QueryFields() {}
121 142
122 void CloneBookmarkNode(BookmarkModel* model, 143 void CloneBookmarkNode(BookmarkModel* model,
123 const std::vector<BookmarkNodeData::Element>& elements, 144 const std::vector<BookmarkNodeData::Element>& elements,
124 const BookmarkNode* parent, 145 const BookmarkNode* parent,
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 389
369 // Remove all the bookmarks. 390 // Remove all the bookmarks.
370 for (size_t i = 0; i < bookmarks.size(); ++i) { 391 for (size_t i = 0; i < bookmarks.size(); ++i) {
371 const BookmarkNode* node = bookmarks[i]; 392 const BookmarkNode* node = bookmarks[i];
372 int index = node->parent()->GetIndexOf(node); 393 int index = node->parent()->GetIndexOf(node);
373 if (index > -1) 394 if (index > -1)
374 model->Remove(node->parent(), index); 395 model->Remove(node->parent(), index);
375 } 396 }
376 } 397 }
377 398
399 base::string16 CleanUpUrlForMatching(const GURL& gurl,
400 const std::string& languages) {
401 return base::i18n::ToLower(net::FormatUrl(
402 GURL(TruncateUrl(gurl.spec())), languages,
403 net::kFormatUrlOmitUsernamePassword,
404 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS,
405 NULL, NULL, NULL));
406 }
407
408 base::string16 CleanUpTitleForMatching(const base::string16& title) {
409 return base::i18n::ToLower(title.substr(0u, kCleanedUpTitleMaxLength));
410 }
411
378 } // namespace bookmark_utils 412 } // namespace bookmark_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698