| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_provider.h" | 5 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/i18n/break_iterator.h" | 12 #include "base/i18n/break_iterator.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/browser/history/history.h" | 20 #include "chrome/browser/history/history.h" |
| 21 #include "chrome/browser/history/history_notifications.h" | 21 #include "chrome/browser/history/history_notifications.h" |
| 22 #include "chrome/browser/net/url_fixer_upper.h" | 22 #include "chrome/browser/net/url_fixer_upper.h" |
| 23 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/common/chrome_notification_types.h" |
| 25 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 27 #include "content/common/notification_details.h" | 28 #include "content/common/notification_details.h" |
| 28 #include "content/common/notification_source.h" | 29 #include "content/common/notification_source.h" |
| 29 #include "content/common/notification_type.h" | |
| 30 #include "googleurl/src/url_parse.h" | 30 #include "googleurl/src/url_parse.h" |
| 31 #include "googleurl/src/url_util.h" | 31 #include "googleurl/src/url_util.h" |
| 32 #include "net/base/escape.h" | 32 #include "net/base/escape.h" |
| 33 #include "net/base/net_util.h" | 33 #include "net/base/net_util.h" |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 // Adds match at the end if and only if its style is different from the last | 36 // Adds match at the end if and only if its style is different from the last |
| 37 // match. | 37 // match. |
| 38 void AddLastMatchIfNeeded(ACMatchClassifications* matches, | 38 void AddLastMatchIfNeeded(ACMatchClassifications* matches, |
| 39 int position, | 39 int position, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 // A match needs to score at least 1200 to be default, so set the max below | 75 // A match needs to score at least 1200 to be default, so set the max below |
| 76 // this. For ease of unit testing, make it divisible by 4 (since some tests | 76 // this. For ease of unit testing, make it divisible by 4 (since some tests |
| 77 // check for half or quarter of the max score). | 77 // check for half or quarter of the max score). |
| 78 const int ShortcutsProvider::kMaxScore = 1196; | 78 const int ShortcutsProvider::kMaxScore = 1196; |
| 79 | 79 |
| 80 ShortcutsProvider::ShortcutsProvider(ACProviderListener* listener, | 80 ShortcutsProvider::ShortcutsProvider(ACProviderListener* listener, |
| 81 Profile* profile) | 81 Profile* profile) |
| 82 : AutocompleteProvider(listener, profile, "ShortcutsProvider"), | 82 : AutocompleteProvider(listener, profile, "ShortcutsProvider"), |
| 83 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) { | 83 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) { |
| 84 notification_registrar_.Add(this, NotificationType::OMNIBOX_OPENED_URL, | 84 notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 85 Source<Profile>(profile)); | 85 Source<Profile>(profile)); |
| 86 notification_registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, | 86 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 87 Source<Profile>(profile)); | 87 Source<Profile>(profile)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 ShortcutsProvider::~ShortcutsProvider() { | 90 ShortcutsProvider::~ShortcutsProvider() { |
| 91 } | 91 } |
| 92 | 92 |
| 93 void ShortcutsProvider::Start(const AutocompleteInput& input, | 93 void ShortcutsProvider::Start(const AutocompleteInput& input, |
| 94 bool minimal_changes) { | 94 bool minimal_changes) { |
| 95 matches_.clear(); | 95 matches_.clear(); |
| 96 | 96 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 122 | 122 |
| 123 // Delete the match from the history DB. This will eventually result in a | 123 // Delete the match from the history DB. This will eventually result in a |
| 124 // second call to DeleteShortcutsWithURLs(), which is harmless. | 124 // second call to DeleteShortcutsWithURLs(), which is harmless. |
| 125 HistoryService* const history_service = | 125 HistoryService* const history_service = |
| 126 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 126 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 127 | 127 |
| 128 DCHECK(history_service && match.destination_url.is_valid()); | 128 DCHECK(history_service && match.destination_url.is_valid()); |
| 129 history_service->DeleteURL(match.destination_url); | 129 history_service->DeleteURL(match.destination_url); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void ShortcutsProvider::Observe(NotificationType type, | 132 void ShortcutsProvider::Observe(int type, |
| 133 const NotificationSource& source, | 133 const NotificationSource& source, |
| 134 const NotificationDetails& details) { | 134 const NotificationDetails& details) { |
| 135 if (type == NotificationType::HISTORY_URLS_DELETED) { | 135 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { |
| 136 const std::set<GURL>& urls = | 136 const std::set<GURL>& urls = |
| 137 Details<const history::URLsDeletedDetails>(details)->urls; | 137 Details<const history::URLsDeletedDetails>(details)->urls; |
| 138 DeleteShortcutsWithURLs(urls); | 138 DeleteShortcutsWithURLs(urls); |
| 139 return; | 139 return; |
| 140 } | 140 } |
| 141 | 141 |
| 142 DCHECK(type == NotificationType::OMNIBOX_OPENED_URL); | 142 DCHECK(type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL); |
| 143 | 143 |
| 144 AutocompleteLog* log = Details<AutocompleteLog>(details).ptr(); | 144 AutocompleteLog* log = Details<AutocompleteLog>(details).ptr(); |
| 145 string16 text_lowercase(base::i18n::ToLower(log->text)); | 145 string16 text_lowercase(base::i18n::ToLower(log->text)); |
| 146 | 146 |
| 147 int number_of_hits = 1; | 147 int number_of_hits = 1; |
| 148 const AutocompleteMatch& match(log->result.match_at(log->selected_index)); | 148 const AutocompleteMatch& match(log->result.match_at(log->selected_index)); |
| 149 for (ShortcutMap::iterator it = FindFirstMatch(text_lowercase); | 149 for (ShortcutMap::iterator it = FindFirstMatch(text_lowercase); |
| 150 it != shortcuts_map_.end() && | 150 it != shortcuts_map_.end() && |
| 151 StartsWith(it->first, text_lowercase, true); ++it) { | 151 StartsWith(it->first, text_lowercase, true); ++it) { |
| 152 if (match.destination_url == it->second.url) { | 152 if (match.destination_url == it->second.url) { |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 StripMatchMarkersFromClassifications(&description_class); | 393 StripMatchMarkersFromClassifications(&description_class); |
| 394 } | 394 } |
| 395 | 395 |
| 396 ShortcutsProvider::Shortcut::Shortcut() | 396 ShortcutsProvider::Shortcut::Shortcut() |
| 397 : last_access_time(base::Time::Now()), | 397 : last_access_time(base::Time::Now()), |
| 398 number_of_hits(0) { | 398 number_of_hits(0) { |
| 399 } | 399 } |
| 400 | 400 |
| 401 ShortcutsProvider::Shortcut::~Shortcut() { | 401 ShortcutsProvider::Shortcut::~Shortcut() { |
| 402 } | 402 } |
| OLD | NEW |