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/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_match.h" | 16 #include "chrome/browser/autocomplete/autocomplete_match.h" |
17 #include "chrome/browser/autocomplete/autocomplete_result.h" | 17 #include "chrome/browser/autocomplete/autocomplete_result.h" |
18 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
19 #include "chrome/browser/history/history_notifications.h" | 19 #include "chrome/browser/history/history_notifications.h" |
20 #include "chrome/browser/history/history_service.h" | 20 #include "chrome/browser/history/history_service.h" |
21 #include "chrome/browser/history/shortcuts_database.h" | 21 #include "chrome/browser/history/shortcuts_database.h" |
22 #include "chrome/browser/omnibox/omnibox_log.h" | 22 #include "chrome/browser/omnibox/omnibox_log.h" |
23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/common/autocomplete_match_type.h" | |
24 #include "chrome/common/chrome_constants.h" | 25 #include "chrome/common/chrome_constants.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
27 #include "content/public/browser/notification_source.h" | 28 #include "content/public/browser/notification_source.h" |
28 #include "extensions/common/extension.h" | 29 #include "extensions/common/extension.h" |
29 | 30 |
30 using content::BrowserThread; | 31 using content::BrowserThread; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
35 const GURL GetDestinationURLForShortcut( | |
Peter Kasting
2014/03/21 18:36:26
Discussed briefly in person: Let's use CreateSearc
Anuj
2014/03/24 07:16:16
The simple implementation doesn't seem too bad.
| |
36 const AutocompleteMatch& match, | |
37 Profile* profile) { | |
38 if (AutocompleteMatch::IsSearchSuggestType(match.type) && profile) { | |
39 scoped_ptr<TemplateURLRef::SearchTermsArgs> search_terms_args( | |
40 new TemplateURLRef::SearchTermsArgs(match.fill_into_edit)); | |
41 const TemplateURL* template_url = match.GetTemplateURL(profile, false); | |
42 if (template_url) { | |
43 return GURL(template_url->url_ref().ReplaceSearchTerms( | |
44 *search_terms_args.get())); | |
45 } | |
46 } | |
47 return match.destination_url; | |
48 } | |
49 | |
50 const base::string16 GetContentsForShortcut(const AutocompleteMatch& match) { | |
51 return AutocompleteMatch::IsSearchSuggestType(match.type) ? | |
52 match.fill_into_edit : match.contents; | |
53 } | |
54 | |
55 const base::string16 GetDescriptionForShortcut(const AutocompleteMatch& match) { | |
56 return (match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | |
57 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE) ? | |
58 base::string16() : match.description; | |
59 } | |
60 | |
34 // Takes Match classification vector and removes all matched positions, | 61 // Takes Match classification vector and removes all matched positions, |
35 // compacting repetitions if necessary. | 62 // compacting repetitions if necessary. |
36 std::string StripMatchMarkers(const ACMatchClassifications& matches) { | 63 std::string StripMatchMarkers(const ACMatchClassifications& matches) { |
37 ACMatchClassifications unmatched; | 64 ACMatchClassifications unmatched; |
38 for (ACMatchClassifications::const_iterator i(matches.begin()); | 65 for (ACMatchClassifications::const_iterator i(matches.begin()); |
39 i != matches.end(); ++i) { | 66 i != matches.end(); ++i) { |
40 AutocompleteMatch::AddLastClassificationIfNecessary( | 67 AutocompleteMatch::AddLastClassificationIfNecessary( |
41 &unmatched, i->offset, i->style & ~ACMatchClassification::MATCH); | 68 &unmatched, i->offset, i->style & ~ACMatchClassification::MATCH); |
42 } | 69 } |
43 return AutocompleteMatch::ClassificationsToString(unmatched); | 70 return AutocompleteMatch::ClassificationsToString(unmatched); |
44 } | 71 } |
45 | 72 |
73 std::string GetClassificationsForShortcut( | |
74 AutocompleteMatchType::Type type, | |
75 const ACMatchClassifications& classifications) { | |
76 return AutocompleteMatch::IsSearchSuggestType(type) ? | |
77 "" : StripMatchMarkers(classifications); | |
78 } | |
79 | |
46 // Normally shortcuts have the same match type as the original match they were | 80 // 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 | 81 // 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. | 82 // type slightly to reflect that the origin of the shortcut is historical. |
49 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) { | 83 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) { |
50 switch (type) { | 84 switch (type) { |
51 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: | 85 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: |
52 case AutocompleteMatchType::NAVSUGGEST: | 86 case AutocompleteMatchType::NAVSUGGEST: |
53 return AutocompleteMatchType::HISTORY_URL; | 87 return AutocompleteMatchType::HISTORY_URL; |
54 | 88 |
55 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: | 89 case AutocompleteMatchType::SEARCH_OTHER_ENGINE: |
56 case AutocompleteMatchType::SEARCH_SUGGEST: | 90 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 | 91 |
63 default: | 92 default: |
64 return type; | 93 return AutocompleteMatch::IsSearchType(type) ? |
94 AutocompleteMatchType::SEARCH_HISTORY : type; | |
65 } | 95 } |
66 } | 96 } |
67 | 97 |
68 } // namespace | 98 } // namespace |
69 | 99 |
70 | 100 |
71 // ShortcutsBackend ----------------------------------------------------------- | 101 // ShortcutsBackend ----------------------------------------------------------- |
72 | 102 |
73 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 103 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
74 : current_state_(NOT_INITIALIZED), | 104 : profile_(profile), |
105 current_state_(NOT_INITIALIZED), | |
75 no_db_access_(suppress_db) { | 106 no_db_access_(suppress_db) { |
76 if (!suppress_db) { | 107 if (!suppress_db) { |
77 db_ = new history::ShortcutsDatabase( | 108 db_ = new history::ShortcutsDatabase( |
78 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); | 109 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); |
79 } | 110 } |
80 // |profile| can be NULL in tests. | 111 // |profile| can be NULL in tests. |
81 if (profile) { | 112 if (profile) { |
82 notification_registrar_.Add( | 113 notification_registrar_.Add( |
83 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 114 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
84 content::Source<Profile>(profile)); | 115 content::Source<Profile>(profile)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text, | 148 void ShortcutsBackend::AddOrUpdateShortcut(const base::string16& text, |
118 const AutocompleteMatch& match) { | 149 const AutocompleteMatch& match) { |
119 const base::string16 text_lowercase(base::i18n::ToLower(text)); | 150 const base::string16 text_lowercase(base::i18n::ToLower(text)); |
120 const base::Time now(base::Time::Now()); | 151 const base::Time now(base::Time::Now()); |
121 for (ShortcutMap::const_iterator it( | 152 for (ShortcutMap::const_iterator it( |
122 shortcuts_map_.lower_bound(text_lowercase)); | 153 shortcuts_map_.lower_bound(text_lowercase)); |
123 it != shortcuts_map_.end() && | 154 it != shortcuts_map_.end() && |
124 StartsWith(it->first, text_lowercase, true); ++it) { | 155 StartsWith(it->first, text_lowercase, true); ++it) { |
125 if (match.destination_url == it->second.match_core.destination_url) { | 156 if (match.destination_url == it->second.match_core.destination_url) { |
126 UpdateShortcut(history::ShortcutsDatabase::Shortcut( | 157 UpdateShortcut(history::ShortcutsDatabase::Shortcut( |
127 it->second.id, text, MatchToMatchCore(match), now, | 158 it->second.id, text, MatchToMatchCore(match, profile_), now, |
128 it->second.number_of_hits + 1)); | 159 it->second.number_of_hits + 1)); |
129 return; | 160 return; |
130 } | 161 } |
131 } | 162 } |
132 AddShortcut(history::ShortcutsDatabase::Shortcut( | 163 AddShortcut(history::ShortcutsDatabase::Shortcut( |
133 base::GenerateGUID(), text, MatchToMatchCore(match), now, 1)); | 164 base::GenerateGUID(), text, MatchToMatchCore(match, profile_), now, 1)); |
134 } | 165 } |
135 | 166 |
136 ShortcutsBackend::~ShortcutsBackend() { | 167 ShortcutsBackend::~ShortcutsBackend() { |
137 } | 168 } |
138 | 169 |
139 // static | 170 // static |
140 history::ShortcutsDatabase::Shortcut::MatchCore | 171 history::ShortcutsDatabase::Shortcut::MatchCore |
141 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match) { | 172 ShortcutsBackend::MatchToMatchCore(const AutocompleteMatch& match, |
173 Profile* profile) { | |
142 return history::ShortcutsDatabase::Shortcut::MatchCore( | 174 return history::ShortcutsDatabase::Shortcut::MatchCore( |
143 match.fill_into_edit, match.destination_url, match.contents, | 175 match.fill_into_edit, |
144 StripMatchMarkers(match.contents_class), match.description, | 176 GetDestinationURLForShortcut(match, profile), |
145 StripMatchMarkers(match.description_class), match.transition, | 177 GetContentsForShortcut(match), |
146 GetTypeForShortcut(match.type), match.keyword); | 178 GetClassificationsForShortcut(match.type, match.contents_class), |
179 GetDescriptionForShortcut(match), | |
180 GetClassificationsForShortcut(match.type, match.description_class), | |
181 match.transition, | |
182 GetTypeForShortcut(match.type), | |
183 match.keyword); | |
147 } | 184 } |
148 | 185 |
149 void ShortcutsBackend::ShutdownOnUIThread() { | 186 void ShortcutsBackend::ShutdownOnUIThread() { |
150 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 187 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
151 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 188 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
152 notification_registrar_.RemoveAll(); | 189 notification_registrar_.RemoveAll(); |
153 } | 190 } |
154 | 191 |
155 void ShortcutsBackend::Observe(int type, | 192 void ShortcutsBackend::Observe(int type, |
156 const content::NotificationSource& source, | 193 const content::NotificationSource& source, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 guid_map_.clear(); | 337 guid_map_.clear(); |
301 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 338 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
302 OnShortcutsChanged()); | 339 OnShortcutsChanged()); |
303 return no_db_access_ || | 340 return no_db_access_ || |
304 BrowserThread::PostTask( | 341 BrowserThread::PostTask( |
305 BrowserThread::DB, FROM_HERE, | 342 BrowserThread::DB, FROM_HERE, |
306 base::Bind(base::IgnoreResult( | 343 base::Bind(base::IgnoreResult( |
307 &history::ShortcutsDatabase::DeleteAllShortcuts), | 344 &history::ShortcutsDatabase::DeleteAllShortcuts), |
308 db_.get())); | 345 db_.get())); |
309 } | 346 } |
OLD | NEW |