| 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_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 Loading... |
| 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 Loading... |
| 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); |
| 145 const AutocompleteMatch& normalized_match = |
| 146 AutocompleteMatch::IsSpecializedSearchType(match.type) ? |
| 147 BaseSearchProvider::CreateSearchSuggestion( |
| 148 suggestion, match_type, suggestion, |
| 149 (match.transition == content::PAGE_TRANSITION_KEYWORD), |
| 150 match.GetTemplateURL(profile, false)) : |
| 151 match; |
| 142 return history::ShortcutsDatabase::Shortcut::MatchCore( | 152 return history::ShortcutsDatabase::Shortcut::MatchCore( |
| 143 match.fill_into_edit, match.destination_url, match.contents, | 153 normalized_match.fill_into_edit, normalized_match.destination_url, |
| 144 StripMatchMarkers(match.contents_class), match.description, | 154 normalized_match.contents, |
| 145 StripMatchMarkers(match.description_class), match.transition, | 155 StripMatchMarkers(normalized_match.contents_class), |
| 146 GetTypeForShortcut(match.type), match.keyword); | 156 normalized_match.description, |
| 157 StripMatchMarkers(normalized_match.description_class), |
| 158 normalized_match.transition, match_type, normalized_match.keyword); |
| 147 } | 159 } |
| 148 | 160 |
| 149 void ShortcutsBackend::ShutdownOnUIThread() { | 161 void ShortcutsBackend::ShutdownOnUIThread() { |
| 150 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 162 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
| 151 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 163 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 152 notification_registrar_.RemoveAll(); | 164 notification_registrar_.RemoveAll(); |
| 153 } | 165 } |
| 154 | 166 |
| 155 void ShortcutsBackend::Observe(int type, | 167 void ShortcutsBackend::Observe(int type, |
| 156 const content::NotificationSource& source, | 168 const content::NotificationSource& source, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 guid_map_.clear(); | 312 guid_map_.clear(); |
| 301 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 313 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
| 302 OnShortcutsChanged()); | 314 OnShortcutsChanged()); |
| 303 return no_db_access_ || | 315 return no_db_access_ || |
| 304 BrowserThread::PostTask( | 316 BrowserThread::PostTask( |
| 305 BrowserThread::DB, FROM_HERE, | 317 BrowserThread::DB, FROM_HERE, |
| 306 base::Bind(base::IgnoreResult( | 318 base::Bind(base::IgnoreResult( |
| 307 &history::ShortcutsDatabase::DeleteAllShortcuts), | 319 &history::ShortcutsDatabase::DeleteAllShortcuts), |
| 308 db_.get())); | 320 db_.get())); |
| 309 } | 321 } |
| OLD | NEW |