| 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" | |
| 17 #include "chrome/browser/autocomplete/autocomplete_match.h" | 16 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_result.h" | 17 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 19 #include "chrome/browser/autocomplete/base_search_provider.h" | |
| 20 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/history/history_notifications.h" | 19 #include "chrome/browser/history/history_notifications.h" |
| 22 #include "chrome/browser/history/history_service.h" | 20 #include "chrome/browser/history/history_service.h" |
| 23 #include "chrome/browser/history/shortcuts_database.h" | 21 #include "chrome/browser/history/shortcuts_database.h" |
| 24 #include "chrome/browser/omnibox/omnibox_log.h" | 22 #include "chrome/browser/omnibox/omnibox_log.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/autocomplete_match_type.h" | |
| 27 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 28 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
| 30 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 31 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 32 | 29 |
| 33 using content::BrowserThread; | 30 using content::BrowserThread; |
| 34 | 31 |
| 35 namespace { | 32 namespace { |
| 36 | 33 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 48 | 45 |
| 49 // Normally shortcuts have the same match type as the original match they were | 46 // Normally shortcuts have the same match type as the original match they were |
| 50 // created from, but for certain match types, we should modify the shortcut's | 47 // created from, but for certain match types, we should modify the shortcut's |
| 51 // type slightly to reflect that the origin of the shortcut is historical. | 48 // type slightly to reflect that the origin of the shortcut is historical. |
| 52 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) { | 49 AutocompleteMatch::Type GetTypeForShortcut(AutocompleteMatch::Type type) { |
| 53 switch (type) { | 50 switch (type) { |
| 54 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: | 51 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: |
| 55 case AutocompleteMatchType::NAVSUGGEST: | 52 case AutocompleteMatchType::NAVSUGGEST: |
| 56 return AutocompleteMatchType::HISTORY_URL; | 53 return AutocompleteMatchType::HISTORY_URL; |
| 57 | 54 |
| 58 case AutocompleteMatchType::SEARCH_OTHER_ENGINE: | 55 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: |
| 59 return type; | 56 case AutocompleteMatchType::SEARCH_SUGGEST: |
| 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; |
| 60 | 62 |
| 61 default: | 63 default: |
| 62 return AutocompleteMatch::IsSearchType(type) ? | 64 return type; |
| 63 AutocompleteMatchType::SEARCH_HISTORY : type; | |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 } // namespace | 68 } // namespace |
| 68 | 69 |
| 69 | 70 |
| 70 // ShortcutsBackend ----------------------------------------------------------- | 71 // ShortcutsBackend ----------------------------------------------------------- |
| 71 | 72 |
| 72 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) | 73 ShortcutsBackend::ShortcutsBackend(Profile* profile, bool suppress_db) |
| 73 : profile_(profile), | 74 : current_state_(NOT_INITIALIZED), |
| 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, profile_), now, | 127 it->second.id, text, MatchToMatchCore(match), 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, profile_), now, 1)); | 133 base::GenerateGUID(), text, MatchToMatchCore(match), 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, | |
| 149 (match.transition == content::PAGE_TRANSITION_KEYWORD), | |
| 150 match.GetTemplateURL(profile, false)) : | |
| 151 match; | |
| 152 return history::ShortcutsDatabase::Shortcut::MatchCore( | 142 return history::ShortcutsDatabase::Shortcut::MatchCore( |
| 153 normalized_match.fill_into_edit, normalized_match.destination_url, | 143 match.fill_into_edit, match.destination_url, match.contents, |
| 154 normalized_match.contents, | 144 StripMatchMarkers(match.contents_class), match.description, |
| 155 StripMatchMarkers(normalized_match.contents_class), | 145 StripMatchMarkers(match.description_class), match.transition, |
| 156 normalized_match.description, | 146 GetTypeForShortcut(match.type), match.keyword); |
| 157 StripMatchMarkers(normalized_match.description_class), | |
| 158 normalized_match.transition, match_type, normalized_match.keyword); | |
| 159 } | 147 } |
| 160 | 148 |
| 161 void ShortcutsBackend::ShutdownOnUIThread() { | 149 void ShortcutsBackend::ShutdownOnUIThread() { |
| 162 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 150 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
| 163 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 notification_registrar_.RemoveAll(); | 152 notification_registrar_.RemoveAll(); |
| 165 } | 153 } |
| 166 | 154 |
| 167 void ShortcutsBackend::Observe(int type, | 155 void ShortcutsBackend::Observe(int type, |
| 168 const content::NotificationSource& source, | 156 const content::NotificationSource& source, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 guid_map_.clear(); | 300 guid_map_.clear(); |
| 313 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, | 301 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, |
| 314 OnShortcutsChanged()); | 302 OnShortcutsChanged()); |
| 315 return no_db_access_ || | 303 return no_db_access_ || |
| 316 BrowserThread::PostTask( | 304 BrowserThread::PostTask( |
| 317 BrowserThread::DB, FROM_HERE, | 305 BrowserThread::DB, FROM_HERE, |
| 318 base::Bind(base::IgnoreResult( | 306 base::Bind(base::IgnoreResult( |
| 319 &history::ShortcutsDatabase::DeleteAllShortcuts), | 307 &history::ShortcutsDatabase::DeleteAllShortcuts), |
| 320 db_.get())); | 308 db_.get())); |
| 321 } | 309 } |
| OLD | NEW |