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

Side by Side Diff: chrome/browser/autocomplete/shortcuts_backend.cc

Issue 165163002: Handle Search suggest subtypes for Shortcuts DB (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed tests Created 6 years, 9 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/autocomplete/shortcuts_backend.h" 5 #include "chrome/browser/autocomplete/shortcuts_backend.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/i18n/case_conversion.h" 14 #include "base/i18n/case_conversion.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "chrome/browser/autocomplete/autocomplete_input.h"
16 #include "chrome/browser/autocomplete/autocomplete_match.h" 17 #include "chrome/browser/autocomplete/autocomplete_match.h"
17 #include "chrome/browser/autocomplete/autocomplete_result.h" 18 #include "chrome/browser/autocomplete/autocomplete_result.h"
19 #include "chrome/browser/autocomplete/base_search_provider.h"
18 #include "chrome/browser/chrome_notification_types.h" 20 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/history/history_notifications.h" 21 #include "chrome/browser/history/history_notifications.h"
20 #include "chrome/browser/history/history_service.h" 22 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/shortcuts_database.h" 23 #include "chrome/browser/history/shortcuts_database.h"
22 #include "chrome/browser/omnibox/omnibox_log.h" 24 #include "chrome/browser/omnibox/omnibox_log.h"
23 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/autocomplete_match_type.h"
24 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
25 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_details.h" 29 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_source.h" 30 #include "content/public/browser/notification_source.h"
28 #include "extensions/common/extension.h" 31 #include "extensions/common/extension.h"
29 32
30 using content::BrowserThread; 33 using content::BrowserThread;
31 34
32 namespace { 35 namespace {
33 36
(...skipping 11 matching lines...) Expand all
45 48
46 // Normally shortcuts have the same match type as the original match they were 49 // Normally shortcuts have the same match type as the original match they were
47 // created from, but for certain match types, we should modify the shortcut's 50 // created from, but for certain match types, we should modify the shortcut's
48 // type slightly to reflect that the origin of the shortcut is historical. 51 // type slightly to reflect that the origin of the shortcut is historical.
49 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) { 52 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) {
50 switch (type) { 53 switch (type) {
51 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: 54 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
52 case AutocompleteMatchType::NAVSUGGEST: 55 case AutocompleteMatchType::NAVSUGGEST:
53 return AutocompleteMatchType::HISTORY_URL; 56 return AutocompleteMatchType::HISTORY_URL;
54 57
55 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: 58 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
56 case AutocompleteMatchType::SEARCH_SUGGEST: 59 return type;
57 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
58 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE:
59 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
60 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
61 return AutocompleteMatchType::SEARCH_HISTORY;
62 60
63 default: 61 default:
64 return type; 62 return AutocompleteMatch::IsSearchType(type) ?
63 AutocompleteMatchType::SEARCH_HISTORY : type;
65 } 64 }
66 } 65 }
67 66
68 } // namespace 67 } // namespace
69 68
70 69
71 // ShortcutsBackend ----------------------------------------------------------- 70 // ShortcutsBackend -----------------------------------------------------------
72 71
73 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) 72 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db)
74 : current_state_(NOT_INITIALIZED), 73 : profile_(profile),
74 current_state_(NOT_INITIALIZED),
75 no_db_access_(suppress_db) { 75 no_db_access_(suppress_db) {
76 if (!suppress_db) { 76 if (!suppress_db) {
77 db_ = new history::ShortcutsDatabase( 77 db_ = new history::ShortcutsDatabase(
78 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); 78 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
79 } 79 }
80 // |profile| can be NULL in tests. 80 // |profile| can be NULL in tests.
81 if (profile) { 81 if (profile) {
82 notification_registrar_.Add( 82 notification_registrar_.Add(
83 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 83 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
84 content::Source<Profile>(profile)); 84 content::Source<Profile>(profile));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text, 117 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text,
118 const AutocompleteMatch& match) { 118 const AutocompleteMatch& match) {
119 const base::string16 text_lowercase(base::i18n::ToLower(text)); 119 const base::string16 text_lowercase(base::i18n::ToLower(text));
120 const base::Time now(base::Time::Now()); 120 const base::Time now(base::Time::Now());
121 for (ShortcutMap::const_iterator it( 121 for (ShortcutMap::const_iterator it(
122 shortcuts_map_.lower_bound(text_lowercase)); 122 shortcuts_map_.lower_bound(text_lowercase));
123 it != shortcuts_map_.end() && 123 it != shortcuts_map_.end() &&
124 StartsWith(it->first, text_lowercase, true); ++it) { 124 StartsWith(it->first, text_lowercase, true); ++it) {
125 if (match.destination_url == it->second.match_core.destination_url) { 125 if (match.destination_url == it->second.match_core.destination_url) {
126 UpdateShortcut(history::ShortcutsDatabase::Shortcut( 126 UpdateShortcut(history::ShortcutsDatabase::Shortcut(
127 it->second.id, text, MatchToMatchCore(match), now, 127 it->second.id, text, MatchToMatchCore(match, profile_), now,
128 it->second.number_of_hits + 1)); 128 it->second.number_of_hits + 1));
129 return; 129 return;
130 } 130 }
131 } 131 }
132 AddShortcut(history::ShortcutsDatabase::Shortcut( 132 AddShortcut(history::ShortcutsDatabase::Shortcut(
133 base::GenerateGUID(), text, MatchToMatchCore(match), now, 1)); 133 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1));
134 } 134 }
135 135
136 ShortcutsBackend::~ShortcutsBackend() { 136 ShortcutsBackend::~ShortcutsBackend() {
137 } 137 }
138 138
139 // static 139 // static
140 history::ShortcutsDatabase::Shortcut::MatchCore 140 history::ShortcutsDatabase::Shortcut::MatchCore
141 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match) { 141 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match,
142 Profile* profile) {
143 const base::string16& suggestion = match.search_terms_args->search_terms;
144 const AutocompleteMatch::Type match_type = GetTypeForShortcut(match.type);
Peter Kasting 2014/03/24 18:59:19 Nit: Can you inline this into the MatchCore() call
Anuj 2014/03/24 21:45:50 I ended up using match_type below.
145 const AutocompleteMatch& normalized_match =
146 AutocompleteMatch::IsSpecializedSearchType(match.type) ?
147 BaseSearchProvider::CreateSearchSuggestion(
148 NULL,
149 AutocompleteInput(),
150 BaseSearchProvider::SuggestResult(
151 suggestion,
Peter Kasting 2014/03/24 18:59:19 Hmm. 4 levels of indenting, for many lines, is ki
Anuj 2014/03/24 21:45:50 Done.
152 match.type,
153 suggestion,
154 base::string16(), // Omit match_contents_prefix.
155 base::string16(),
156 std::string(), // Omit suggest_query_params.
157 std::string(), // Omit deletion_url.
158 (match.transition == content::PAGE_TRANSITION_KEYWORD),
159 0, // Ignore relevance.
160 false, // Ignore relevance_from_server.
161 false, // Ignore should_prefetch.
162 base::string16()), // Omit input_test.
163 match.GetTemplateURL(profile, false),
164 0, // accepted_suggestion
165 0, // omnibox_start_margin,
166 false) : match;
Peter Kasting 2014/03/24 18:59:19 Nit: Break after ':' and unindent
Anuj 2014/03/24 21:45:50 Done.
142 return history::ShortcutsDatabase::Shortcut::MatchCore( 167 return history::ShortcutsDatabase::Shortcut::MatchCore(
143 match.fill_into_edit, match.destination_url, match.contents, 168 normalized_match.fill_into_edit, normalized_match.destination_url,
144 StripMatchMarkers(match.contents_class), match.description, 169 normalized_match.contents,
145 StripMatchMarkers(match.description_class), match.transition, 170 StripMatchMarkers(normalized_match.contents_class),
146 GetTypeForShortcut(match.type), match.keyword); 171 normalized_match.description,
172 StripMatchMarkers(normalized_match.description_class),
173 normalized_match.transition, match_type,
174 normalized_match.keyword);
147 } 175 }
148 176
149 void ShortcutsBackend::ShutdownOnUIThread() { 177 void ShortcutsBackend::ShutdownOnUIThread() {
150 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || 178 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
151 BrowserThread::CurrentlyOn(BrowserThread::UI)); 179 BrowserThread::CurrentlyOn(BrowserThread::UI));
152 notification_registrar_.RemoveAll(); 180 notification_registrar_.RemoveAll();
153 } 181 }
154 182
155 void ShortcutsBackend::Observe(int type, 183 void ShortcutsBackend::Observe(int type,
156 const content::NotificationSource& source, 184 const content::NotificationSource& source,
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 guid_map_.clear(); 328 guid_map_.clear();
301 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, 329 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
302 OnShortcutsChanged()); 330 OnShortcutsChanged());
303 return no_db_access_ || 331 return no_db_access_ ||
304 BrowserThread::PostTask( 332 BrowserThread::PostTask(
305 BrowserThread::DB, FROM_HERE, 333 BrowserThread::DB, FROM_HERE,
306 base::Bind(base::IgnoreResult( 334 base::Bind(base::IgnoreResult(
307 &history::ShortcutsDatabase::DeleteAllShortcuts), 335 &history::ShortcutsDatabase::DeleteAllShortcuts),
308 db_.get())); 336 db_.get()));
309 } 337 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/shortcuts_backend.h ('k') | chrome/browser/autocomplete/shortcuts_backend_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698