| 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/history_quick_provider.h" | 5 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/history/history.h" | 17 #include "chrome/browser/history/history.h" |
| 18 #include "chrome/browser/history/in_memory_url_index.h" | 18 #include "chrome/browser/history/in_memory_url_index.h" |
| 19 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 19 #include "chrome/browser/net/url_fixer_upper.h" | 20 #include "chrome/browser/net/url_fixer_upper.h" |
| 20 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 24 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| 25 #include "content/public/browser/notification_types.h" | 26 #include "content/public/browser/notification_types.h" |
| 26 #include "googleurl/src/url_parse.h" | 27 #include "googleurl/src/url_parse.h" |
| 27 #include "googleurl/src/url_util.h" | 28 #include "googleurl/src/url_util.h" |
| 28 #include "net/base/escape.h" | 29 #include "net/base/escape.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 std::string name = "HistoryQuickProvider.QueryIndexTime." + | 75 std::string name = "HistoryQuickProvider.QueryIndexTime." + |
| 75 base::IntToString(input.text().length()); | 76 base::IntToString(input.text().length()); |
| 76 base::Histogram* counter = base::Histogram::FactoryGet( | 77 base::Histogram* counter = base::Histogram::FactoryGet( |
| 77 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); | 78 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); |
| 78 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); | 79 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); |
| 79 } | 80 } |
| 80 UpdateStarredStateOfMatches(); | 81 UpdateStarredStateOfMatches(); |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 // HistoryQuickProvider matches are currently not deletable. | 85 // TODO(mrossetti): Implement this function. (Will happen in next CL.) |
| 85 // TODO(mrossetti): Determine when a match should be deletable. | |
| 86 void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) {} | 86 void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) {} |
| 87 | 87 |
| 88 void HistoryQuickProvider::DoAutocomplete() { | 88 void HistoryQuickProvider::DoAutocomplete() { |
| 89 // Get the matching URLs from the DB. | 89 // Get the matching URLs from the DB. |
| 90 string16 term_string = autocomplete_input_.text(); | 90 string16 term_string = autocomplete_input_.text(); |
| 91 term_string = net::UnescapeURLComponent(term_string, | 91 term_string = net::UnescapeURLComponent(term_string, |
| 92 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 92 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
| 93 history::InMemoryURLIndex::String16Vector terms( | 93 history::String16Vector terms( |
| 94 InMemoryURLIndex::WordVectorFromString16(term_string, false)); | 94 history::String16VectorFromString16(term_string, false)); |
| 95 ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(terms); | 95 ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(terms); |
| 96 if (matches.empty()) | 96 if (matches.empty()) |
| 97 return; | 97 return; |
| 98 | 98 |
| 99 // Artificially reduce the score of high-scoring matches which should not be | 99 // Artificially reduce the score of high-scoring matches which should not be |
| 100 // inline autocompletd. Each such result gets the next available | 100 // inline autocompletd. Each such result gets the next available |
| 101 // |max_match_score|. Upon use of |max_match_score| it is decremented. | 101 // |max_match_score|. Upon use of |max_match_score| it is decremented. |
| 102 // All subsequent matches must be clamped to retain match results ordering. | 102 // All subsequent matches must be clamped to retain match results ordering. |
| 103 int max_match_score = autocomplete_input_.prevent_inline_autocomplete() ? | 103 int max_match_score = autocomplete_input_.prevent_inline_autocomplete() ? |
| 104 (AutocompleteResult::kLowestDefaultScore - 1) : -1; | 104 (AutocompleteResult::kLowestDefaultScore - 1) : -1; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 const history::URLRow& info = history_match.url_info; | 124 const history::URLRow& info = history_match.url_info; |
| 125 int score = CalculateRelevance(history_match, max_match_score); | 125 int score = CalculateRelevance(history_match, max_match_score); |
| 126 AutocompleteMatch match(this, score, !!info.visit_count(), | 126 AutocompleteMatch match(this, score, !!info.visit_count(), |
| 127 history_match.url_matches.empty() ? | 127 history_match.url_matches.empty() ? |
| 128 AutocompleteMatch::HISTORY_URL : AutocompleteMatch::HISTORY_TITLE); | 128 AutocompleteMatch::HISTORY_URL : AutocompleteMatch::HISTORY_TITLE); |
| 129 match.destination_url = info.url(); | 129 match.destination_url = info.url(); |
| 130 DCHECK(match.destination_url.is_valid()); | 130 DCHECK(match.destination_url.is_valid()); |
| 131 | 131 |
| 132 // Format the URL autocomplete presentation. | 132 // Format the URL autocomplete presentation. |
| 133 std::vector<size_t> offsets = | 133 std::vector<size_t> offsets = |
| 134 InMemoryURLIndex::OffsetsFromTermMatches(history_match.url_matches); | 134 OffsetsFromTermMatches(history_match.url_matches); |
| 135 match.contents = | 135 match.contents = |
| 136 net::FormatUrlWithOffsets(info.url(), languages_, net::kFormatUrlOmitAll, | 136 net::FormatUrlWithOffsets(info.url(), languages_, net::kFormatUrlOmitAll, |
| 137 UnescapeRule::SPACES, NULL, NULL, &offsets); | 137 UnescapeRule::SPACES, NULL, NULL, &offsets); |
| 138 history::TermMatches new_matches = | 138 history::TermMatches new_matches = |
| 139 InMemoryURLIndex::ReplaceOffsetsInTermMatches(history_match.url_matches, | 139 ReplaceOffsetsInTermMatches(history_match.url_matches, offsets); |
| 140 offsets); | |
| 141 match.contents_class = | 140 match.contents_class = |
| 142 SpansFromTermMatch(new_matches, match.contents.length(), true); | 141 SpansFromTermMatch(new_matches, match.contents.length(), true); |
| 143 match.fill_into_edit = match.contents; | 142 match.fill_into_edit = match.contents; |
| 144 | 143 |
| 145 if (prevent_inline_autocomplete || !history_match.can_inline) { | 144 if (prevent_inline_autocomplete || !history_match.can_inline) { |
| 146 match.inline_autocomplete_offset = string16::npos; | 145 match.inline_autocomplete_offset = string16::npos; |
| 147 } else { | 146 } else { |
| 148 match.inline_autocomplete_offset = | 147 match.inline_autocomplete_offset = |
| 149 history_match.input_location + match.fill_into_edit.length(); | 148 history_match.input_location + match.fill_into_edit.length(); |
| 150 DCHECK_LE(match.inline_autocomplete_offset, match.fill_into_edit.length()); | 149 DCHECK_LE(match.inline_autocomplete_offset, match.fill_into_edit.length()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 163 return index_for_testing_.get(); | 162 return index_for_testing_.get(); |
| 164 | 163 |
| 165 HistoryService* const history_service = | 164 HistoryService* const history_service = |
| 166 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 165 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 167 if (!history_service) | 166 if (!history_service) |
| 168 return NULL; | 167 return NULL; |
| 169 | 168 |
| 170 return history_service->InMemoryIndex(); | 169 return history_service->InMemoryIndex(); |
| 171 } | 170 } |
| 172 | 171 |
| 173 void HistoryQuickProvider::SetIndexForTesting( | |
| 174 history::InMemoryURLIndex* index) { | |
| 175 DCHECK(index); | |
| 176 index_for_testing_.reset(index); | |
| 177 } | |
| 178 | |
| 179 // static | 172 // static |
| 180 int HistoryQuickProvider::CalculateRelevance( | 173 int HistoryQuickProvider::CalculateRelevance( |
| 181 const ScoredHistoryMatch& history_match, | 174 const ScoredHistoryMatch& history_match, |
| 182 int* max_match_score) { | 175 int* max_match_score) { |
| 183 DCHECK(max_match_score); | 176 DCHECK(max_match_score); |
| 184 // Note that |can_inline| will only be true if what the user typed starts | 177 // Note that |can_inline| will only be true if what the user typed starts |
| 185 // at the beginning of the result's URL and there is exactly one substring | 178 // at the beginning of the result's URL and there is exactly one substring |
| 186 // match in the URL. | 179 // match in the URL. |
| 187 int score = (history_match.can_inline) ? history_match.raw_score : | 180 int score = (history_match.can_inline) ? history_match.raw_score : |
| 188 std::min(AutocompleteResult::kLowestDefaultScore - 1, | 181 std::min(AutocompleteResult::kLowestDefaultScore - 1, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 216 do { | 209 do { |
| 217 offset += matches[i].length; | 210 offset += matches[i].length; |
| 218 ++i; | 211 ++i; |
| 219 } while ((i < match_count) && (offset == matches[i].offset)); | 212 } while ((i < match_count) && (offset == matches[i].offset)); |
| 220 if (offset < text_length) | 213 if (offset < text_length) |
| 221 spans.push_back(ACMatchClassification(offset, url_style)); | 214 spans.push_back(ACMatchClassification(offset, url_style)); |
| 222 } | 215 } |
| 223 | 216 |
| 224 return spans; | 217 return spans; |
| 225 } | 218 } |
| OLD | NEW |