history_quick_provider.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 3// found in the LICENSE file. 4 5#include "chrome/browser/autocomplete/history_quick_provider.h" 6 7#include <vector> 8 9#include "base/basictypes.h" 10#include "base/command_line.h" 11#include "base/i18n/break_iterator.h" 12#include "base/logging.h" 13#include "base/metrics/field_trial.h" 14#include "base/metrics/histogram.h" 15#include "base/prefs/pref_service.h" 16#include "base/strings/string_number_conversions.h" 17#include "base/strings/string_util.h" 18#include "base/strings/utf_string_conversions.h" 19#include "base/time/time.h" 20#include "chrome/browser/autocomplete/autocomplete_result.h" 21#include "chrome/browser/autocomplete/history_url_provider.h" 22#include "chrome/browser/history/history_database.h" 23#include "chrome/browser/history/history_service.h" 24#include "chrome/browser/history/history_service_factory.h" 25#include "chrome/browser/history/in_memory_url_index.h" 26#include "chrome/browser/history/in_memory_url_index_types.h" 27#include "chrome/browser/history/scored_history_match.h" 28#include "chrome/browser/omnibox/omnibox_field_trial.h" 29#include "chrome/browser/profiles/profile.h" 30#include "chrome/browser/search/search.h" 31#include "chrome/browser/search_engines/template_url.h" 32#include "chrome/browser/search_engines/template_url_service.h" 33#include "chrome/browser/search_engines/template_url_service_factory.h" 34#include "chrome/common/autocomplete_match_type.h" 35#include "chrome/common/chrome_switches.h" 36#include "chrome/common/net/url_fixer_upper.h" 37#include "chrome/common/pref_names.h" 38#include "chrome/common/url_constants.h" 39#include "content/public/browser/notification_source.h" 40#include "content/public/browser/notification_types.h" 41#include "net/base/escape.h" 42#include "net/base/net_util.h" 43#include "net/base/registry_controlled_domains/registry_controlled_domain.h" 44#include "url/url_parse.h" 45#include "url/url_util.h" 46 47using history::InMemoryURLIndex; 48using history::ScoredHistoryMatch; 49using history::ScoredHistoryMatches; 50 51bool HistoryQuickProvider::disabled_ = false; 52 53HistoryQuickProvider::HistoryQuickProvider( 54 AutocompleteProviderListener* listener, 55 Profile* profile) 56 : HistoryProvider(listener, profile, 57 AutocompleteProvider::TYPE_HISTORY_QUICK), 58 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) { 59} 60 61void HistoryQuickProvider::Start(const AutocompleteInput& input, 62 bool minimal_changes) { 63 matches_.clear(); 64 if (disabled_) 65 return; 66 67 // Don't bother with INVALID and FORCED_QUERY. 68 if ((input.type() == AutocompleteInput::INVALID) || 69 (input.type() == AutocompleteInput::FORCED_QUERY)) 70 return; 71 72 autocomplete_input_ = input; 73 74 // TODO(pkasting): We should just block here until this loads. Any time 75 // someone unloads the history backend, we'll get inconsistent inline 76 // autocomplete behavior here. 77 if (GetIndex()) { 78 base::TimeTicks start_time = base::TimeTicks::Now(); 79 DoAutocomplete(); 80 if (input.text().length() < 6) { 81 base::TimeTicks end_time = base::TimeTicks::Now(); 82 std::string name = "HistoryQuickProvider.QueryIndexTime." + 83 base::IntToString(input.text().length()); 84 base::HistogramBase* counter = base::Histogram::FactoryGet( 85 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag); 86 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds())); 87 } 88 UpdateStarredStateOfMatches(); 89 } 90} 91 92void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) { 93 DCHECK(match.deletable); 94 DCHECK(match.destination_url.is_valid()); 95 // Delete the match from the InMemoryURLIndex. 96 GetIndex()->DeleteURL(match.destination_url); 97 DeleteMatchFromMatches(match); 98} 99 100HistoryQuickProvider::~HistoryQuickProvider() {} 101 102void HistoryQuickProvider::DoAutocomplete() { 103 // Get the matching URLs from the DB. 104 ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms( 105 autocomplete_input_.text(), 106 autocomplete_input_.cursor_position()); 107 if (matches.empty()) 108 return; 109 110 // Figure out if HistoryURL provider has a URL-what-you-typed match 111 // that ought to go first and what its score will be. 112 bool will_have_url_what_you_typed_match_first = false; 113 int url_what_you_typed_match_score = -1; // undefined 114 // These are necessary (but not sufficient) conditions for the omnibox 115 // input to be a URL-what-you-typed match. The username test checks that 116 // either the username does not exist (a regular URL such as http://site/) 117 // or, if the username exists (http://user@site/), there must be either 118 // a password or a port. Together these exclude pure username@site 119 // inputs because these are likely to be an e-mail address. HistoryURL 120 // provider won't promote the URL-what-you-typed match to first 121 // for these inputs. 122 const bool can_have_url_what_you_typed_match_first = 123 autocomplete_input_.canonicalized_url().is_valid() && 124 (autocomplete_input_.type() != AutocompleteInput::QUERY) && 125 (autocomplete_input_.type() != AutocompleteInput::FORCED_QUERY) && 126 (!autocomplete_input_.parts().username.is_nonempty() || 127 autocomplete_input_.parts().password.is_nonempty() || 128 autocomplete_input_.parts().path.is_nonempty()); 129 if (can_have_url_what_you_typed_match_first) { 130 HistoryService* const history_service = 131 HistoryServiceFactory::GetForProfile(profile_, 132 Profile::EXPLICIT_ACCESS); 133 // We expect HistoryService to be available. In case it's not, 134 // (e.g., due to Profile corruption) we let HistoryQuick provider 135 // completions (which may be available because it's a different 136 // data structure) compete with the URL-what-you-typed match as 137 // normal. 138 if (history_service) { 139 history::URLDatabase* url_db = history_service->InMemoryDatabase(); 140 // url_db can be NULL if it hasn't finished initializing (or 141 // failed to to initialize). In this case, we let HistoryQuick 142 // provider completions compete with the URL-what-you-typed 143 // match as normal. 144 if (url_db) { 145 const std::string host(base::UTF16ToUTF8( 146 autocomplete_input_.text().substr( 147 autocomplete_input_.parts().host.begin, 148 autocomplete_input_.parts().host.len))); 149 // We want to put the URL-what-you-typed match first if either 150 // * the user visited the URL before (intranet or internet). 151 // * it's a URL on a host that user visited before and this 152 // is the root path of the host. (If the user types some 153 // of a path--more than a simple "/"--we let autocomplete compete 154 // normally with the URL-what-you-typed match.) 155 // TODO(mpearson): Remove this hacky code and simply score URL-what- 156 // you-typed in some sane way relative to possible completions: 157 // URL-what-you-typed should get some sort of a boost relative 158 // to completions, but completions should naturally win if 159 // they're a lot more popular. In this process, if the input 160 // is a bare intranet hostname that has been visited before, we 161 // may want to enforce that the only completions that can outscore 162 // the URL-what-you-typed match are on the same host (i.e., aren't 163 // from a longer internet hostname for which the omnibox input is 164 // a prefix). 165 if (url_db->GetRowForURL( 166 autocomplete_input_.canonicalized_url(), NULL) != 0) { 167 // We visited this URL before. 168 will_have_url_what_you_typed_match_first = true; 169 // HistoryURLProvider gives visited what-you-typed URLs a high score. 170 url_what_you_typed_match_score = 171 HistoryURLProvider::kScoreForBestInlineableResult; 172 } else if (url_db->IsTypedHost(host) && 173 (!autocomplete_input_.parts().path.is_nonempty() || 174 ((autocomplete_input_.parts().path.len == 1) && 175 (autocomplete_input_.text()[ 176 autocomplete_input_.parts().path.begin] == '/'))) && 177 !autocomplete_input_.parts().query.is_nonempty() && 178 !autocomplete_input_.parts().ref.is_nonempty()) { 179 // Not visited, but we've seen the host before. 180 will_have_url_what_you_typed_match_first = true; 181 const size_t registry_length = 182 net::registry_controlled_domains::GetRegistryLength( 183 host, 184 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 185 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 186 if (registry_length == 0) { 187 // Known intranet hosts get one score. 188 url_what_you_typed_match_score = 189 HistoryURLProvider::kScoreForUnvisitedIntranetResult; 190 } else { 191 // Known internet hosts get another. 192 url_what_you_typed_match_score = 193 HistoryURLProvider::kScoreForWhatYouTypedResult; 194 } 195 } 196 } 197 } 198 } 199 200 // Loop over every result and add it to matches_. In the process, 201 // guarantee that scores are decreasing. |max_match_score| keeps 202 // track of the highest score we can assign to any later results we 203 // see. Also, if we're not allowing inline autocompletions in 204 // general or the current best suggestion isn't inlineable, 205 // artificially reduce the starting |max_match_score| (which 206 // therefore applies to all results) to something low enough that 207 // guarantees no result will be offered as an inline autocomplete 208 // suggestion. Also do a similar reduction if we think there will be 209 // a URL-what-you-typed match. (We want URL-what-you-typed matches for 210 // visited URLs to beat out any longer URLs, no matter how frequently 211 // they're visited.) The strength of this last reduction depends on the 212 // likely score for the URL-what-you-typed result. 213 214 // |template_url_service| or |template_url| can be NULL in unit tests. 215 TemplateURLService* template_url_service = 216 TemplateURLServiceFactory::GetForProfile(profile_); 217 TemplateURL* template_url = template_url_service ? 218 template_url_service->GetDefaultSearchProvider() : NULL; 219 int max_match_score = 220 (OmniboxFieldTrial::ReorderForLegalDefaultMatch( 221 autocomplete_input_.current_page_classification()) || 222 (!PreventInlineAutocomplete(autocomplete_input_) && 223 matches.begin()->can_inline())) ? 224 matches.begin()->raw_score() : 225 (AutocompleteResult::kLowestDefaultScore - 1); 226 if (will_have_url_what_you_typed_match_first) { 227 max_match_score = std::min(max_match_score, 228 url_what_you_typed_match_score - 1); 229 } 230 for (ScoredHistoryMatches::const_iterator match_iter = matches.begin(); 231 match_iter != matches.end(); ++match_iter) { 232 const ScoredHistoryMatch& history_match(*match_iter); 233 // Culls results corresponding to queries from the default search engine. 234 // These are low-quality, difficult-to-understand matches for users, and the 235 // SearchProvider should surface past queries in a better way anyway. 236 if (!template_url || 237 !template_url->IsSearchURL(history_match.url_info.url())) { 238 // Set max_match_score to the score we'll assign this result: 239 max_match_score = std::min(max_match_score, history_match.raw_score()); 240 matches_.push_back(QuickMatchToACMatch(history_match, max_match_score)); 241 // Mark this max_match_score as being used: 242 max_match_score--; 243 } 244 } 245} 246 247AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch( 248 const ScoredHistoryMatch& history_match, 249 int score) { 250 const history::URLRow& info = history_match.url_info; 251 AutocompleteMatch match( 252 this, score, !!info.visit_count(), 253 history_match.url_matches().empty() ? 254 AutocompleteMatchType::HISTORY_TITLE : 255 AutocompleteMatchType::HISTORY_URL); 256 match.typed_count = info.typed_count(); 257 match.destination_url = info.url(); 258 DCHECK(match.destination_url.is_valid()); 259 260 // Format the URL autocomplete presentation. 261 std::vector<size_t> offsets = 262 OffsetsFromTermMatches(history_match.url_matches()); 263 const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & 264 ~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP); 265 match.fill_into_edit = 266 AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), 267 net::FormatUrlWithOffsets(info.url(), languages_, format_types, 268 net::UnescapeRule::SPACES, NULL, NULL, &offsets)); 269 history::TermMatches new_matches = 270 ReplaceOffsetsInTermMatches(history_match.url_matches(), offsets); 271 match.contents = net::FormatUrl(info.url(), languages_, format_types, 272 net::UnescapeRule::SPACES, NULL, NULL, NULL); 273 match.contents_class = 274 SpansFromTermMatch(new_matches, match.contents.length(), true); 275 276 if (history_match.can_inline()) { 277 DCHECK(!new_matches.empty()); 278 size_t inline_autocomplete_offset = new_matches[0].offset + 279 new_matches[0].length; 280 // |inline_autocomplete_offset| may be beyond the end of the 281 // |fill_into_edit| if the user has typed an URL with a scheme and the 282 // last character typed is a slash. That slash is removed by the 283 // FormatURLWithOffsets call above. 284 if (inline_autocomplete_offset < match.fill_into_edit.length()) { 285 match.inline_autocompletion = 286 match.fill_into_edit.substr(inline_autocomplete_offset); 287 } 288 match.allowed_to_be_default_match = match.inline_autocompletion.empty() || 289 !PreventInlineAutocomplete(autocomplete_input_); 290 } 291 292 // Format the description autocomplete presentation. 293 match.description = info.title(); 294 match.description_class = SpansFromTermMatch( 295 history_match.title_matches(), match.description.length(), false); 296 297 match.RecordAdditionalInfo("typed count", info.typed_count()); 298 match.RecordAdditionalInfo("visit count", info.visit_count()); 299 match.RecordAdditionalInfo("last visit", info.last_visit()); 300 301 return match; 302} 303 304history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() { 305 if (index_for_testing_.get()) 306 return index_for_testing_.get(); 307 308 HistoryService* const history_service = 309 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 310 if (!history_service) 311 return NULL; 312 313 return history_service->InMemoryIndex(); 314} 315