| 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_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> |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener, | 51 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener, |
| 52 Profile* profile) | 52 Profile* profile) |
| 53 : AutocompleteProvider(listener, profile, | 53 : AutocompleteProvider(listener, profile, |
| 54 AutocompleteProvider::TYPE_SHORTCUTS), | 54 AutocompleteProvider::TYPE_SHORTCUTS), |
| 55 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), | 55 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), |
| 56 initialized_(false) { | 56 initialized_(false) { |
| 57 scoped_refptr<history::ShortcutsBackend> backend = | 57 scoped_refptr<history::ShortcutsBackend> backend = |
| 58 ShortcutsBackendFactory::GetForProfile(profile_); | 58 ShortcutsBackendFactory::GetForProfile(profile_); |
| 59 if (backend) { | 59 if (backend.get()) { |
| 60 backend->AddObserver(this); | 60 backend->AddObserver(this); |
| 61 if (backend->initialized()) | 61 if (backend->initialized()) |
| 62 initialized_ = true; | 62 initialized_ = true; |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 void ShortcutsProvider::Start(const AutocompleteInput& input, | 66 void ShortcutsProvider::Start(const AutocompleteInput& input, |
| 67 bool minimal_changes) { | 67 bool minimal_changes) { |
| 68 matches_.clear(); | 68 matches_.clear(); |
| 69 | 69 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 HistoryService* const history_service = | 112 HistoryService* const history_service = |
| 113 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 113 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 114 | 114 |
| 115 DCHECK(history_service && url.is_valid()); | 115 DCHECK(history_service && url.is_valid()); |
| 116 history_service->DeleteURL(url); | 116 history_service->DeleteURL(url); |
| 117 } | 117 } |
| 118 | 118 |
| 119 ShortcutsProvider::~ShortcutsProvider() { | 119 ShortcutsProvider::~ShortcutsProvider() { |
| 120 scoped_refptr<history::ShortcutsBackend> backend = | 120 scoped_refptr<history::ShortcutsBackend> backend = |
| 121 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 121 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
| 122 if (backend) | 122 if (backend.get()) |
| 123 backend->RemoveObserver(this); | 123 backend->RemoveObserver(this); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void ShortcutsProvider::OnShortcutsLoaded() { | 126 void ShortcutsProvider::OnShortcutsLoaded() { |
| 127 initialized_ = true; | 127 initialized_ = true; |
| 128 } | 128 } |
| 129 | 129 |
| 130 void ShortcutsProvider::DeleteMatchesWithURLs(const std::set<GURL>& urls) { | 130 void ShortcutsProvider::DeleteMatchesWithURLs(const std::set<GURL>& urls) { |
| 131 std::remove_if(matches_.begin(), matches_.end(), RemoveMatchPredicate(urls)); | 131 std::remove_if(matches_.begin(), matches_.end(), RemoveMatchPredicate(urls)); |
| 132 listener_->OnProviderUpdate(true); | 132 listener_->OnProviderUpdate(true); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void ShortcutsProvider::DeleteShortcutsWithURLs(const std::set<GURL>& urls) { | 135 void ShortcutsProvider::DeleteShortcutsWithURLs(const std::set<GURL>& urls) { |
| 136 scoped_refptr<history::ShortcutsBackend> backend = | 136 scoped_refptr<history::ShortcutsBackend> backend = |
| 137 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 137 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
| 138 if (!backend) | 138 if (!backend.get()) |
| 139 return; // We are off the record. | 139 return; // We are off the record. |
| 140 for (std::set<GURL>::const_iterator url = urls.begin(); url != urls.end(); | 140 for (std::set<GURL>::const_iterator url = urls.begin(); url != urls.end(); |
| 141 ++url) | 141 ++url) |
| 142 backend->DeleteShortcutsWithUrl(*url); | 142 backend->DeleteShortcutsWithUrl(*url); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { | 145 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { |
| 146 scoped_refptr<history::ShortcutsBackend> backend = | 146 scoped_refptr<history::ShortcutsBackend> backend = |
| 147 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 147 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
| 148 if (!backend) | 148 if (!backend.get()) |
| 149 return; | 149 return; |
| 150 // Get the URLs from the shortcuts database with keys that partially or | 150 // Get the URLs from the shortcuts database with keys that partially or |
| 151 // completely match the search term. | 151 // completely match the search term. |
| 152 string16 term_string(base::i18n::ToLower(input.text())); | 152 string16 term_string(base::i18n::ToLower(input.text())); |
| 153 DCHECK(!term_string.empty()); | 153 DCHECK(!term_string.empty()); |
| 154 | 154 |
| 155 for (history::ShortcutsBackend::ShortcutMap::const_iterator it = | 155 for (history::ShortcutsBackend::ShortcutMap::const_iterator it = |
| 156 FindFirstMatch(term_string, backend.get()); | 156 FindFirstMatch(term_string, backend.get()); |
| 157 it != backend->shortcuts_map().end() && | 157 it != backend->shortcuts_map().end() && |
| 158 StartsWith(it->first, term_string, true); ++it) { | 158 StartsWith(it->first, term_string, true); ++it) { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 347 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
| 348 const double kMaxDecaySpeedDivisor = 5.0; | 348 const double kMaxDecaySpeedDivisor = 5.0; |
| 349 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 349 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
| 350 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 350 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
| 351 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 351 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
| 352 kNumUsesPerDecaySpeedDivisorIncrement); | 352 kNumUsesPerDecaySpeedDivisorIncrement); |
| 353 | 353 |
| 354 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 354 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
| 355 0.5); | 355 0.5); |
| 356 } | 356 } |
| OLD | NEW |