This source file includes following definitions.
- languages_
- Start
- DeleteMatch
- DoAutocomplete
- QuickMatchToACMatch
- GetIndex
#include "chrome/browser/autocomplete/history_quick_provider.h"
#include <vector>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/i18n/break_iterator.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/autocomplete/autocomplete_result.h"
#include "chrome/browser/autocomplete/history_url_provider.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/in_memory_url_index.h"
#include "chrome/browser/history/in_memory_url_index_types.h"
#include "chrome/browser/history/scored_history_match.h"
#include "chrome/browser/omnibox/omnibox_field_trial.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/autocomplete_match_type.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "net/base/escape.h"
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/url_parse.h"
#include "url/url_util.h"
using history::InMemoryURLIndex;
using history::ScoredHistoryMatch;
using history::ScoredHistoryMatches;
bool HistoryQuickProvider::disabled_ = false;
HistoryQuickProvider::HistoryQuickProvider(
AutocompleteProviderListener* listener,
Profile* profile)
: HistoryProvider(listener, profile,
AutocompleteProvider::TYPE_HISTORY_QUICK),
languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) {
}
void HistoryQuickProvider::Start(const AutocompleteInput& input,
bool minimal_changes) {
matches_.clear();
if (disabled_)
return;
if ((input.type() == AutocompleteInput::INVALID) ||
(input.type() == AutocompleteInput::FORCED_QUERY) ||
(input.matches_requested() == AutocompleteInput::BEST_MATCH &&
input.prevent_inline_autocomplete()))
return;
autocomplete_input_ = input;
if (GetIndex()) {
base::TimeTicks start_time = base::TimeTicks::Now();
DoAutocomplete();
if (input.text().length() < 6) {
base::TimeTicks end_time = base::TimeTicks::Now();
std::string name = "HistoryQuickProvider.QueryIndexTime." +
base::IntToString(input.text().length());
base::HistogramBase* counter = base::Histogram::FactoryGet(
name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
}
UpdateStarredStateOfMatches();
}
}
void HistoryQuickProvider::DeleteMatch(const AutocompleteMatch& match) {
DCHECK(match.deletable);
DCHECK(match.destination_url.is_valid());
GetIndex()->DeleteURL(match.destination_url);
DeleteMatchFromMatches(match);
}
HistoryQuickProvider::~HistoryQuickProvider() {}
void HistoryQuickProvider::DoAutocomplete() {
ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(
autocomplete_input_.text(),
autocomplete_input_.cursor_position());
if (matches.empty())
return;
bool will_have_url_what_you_typed_match_first = false;
int url_what_you_typed_match_score = -1;
const bool can_have_url_what_you_typed_match_first =
autocomplete_input_.canonicalized_url().is_valid() &&
(autocomplete_input_.type() != AutocompleteInput::QUERY) &&
(autocomplete_input_.type() != AutocompleteInput::FORCED_QUERY) &&
(!autocomplete_input_.parts().username.is_nonempty() ||
autocomplete_input_.parts().password.is_nonempty() ||
autocomplete_input_.parts().path.is_nonempty());
if (can_have_url_what_you_typed_match_first) {
HistoryService* const history_service =
HistoryServiceFactory::GetForProfile(profile_,
Profile::EXPLICIT_ACCESS);
if (history_service) {
history::URLDatabase* url_db = history_service->InMemoryDatabase();
if (url_db) {
const std::string host(base::UTF16ToUTF8(
autocomplete_input_.text().substr(
autocomplete_input_.parts().host.begin,
autocomplete_input_.parts().host.len)));
if (url_db->GetRowForURL(
autocomplete_input_.canonicalized_url(), NULL) != 0) {
will_have_url_what_you_typed_match_first = true;
url_what_you_typed_match_score =
HistoryURLProvider::kScoreForBestInlineableResult;
} else if (url_db->IsTypedHost(host) &&
(!autocomplete_input_.parts().path.is_nonempty() ||
((autocomplete_input_.parts().path.len == 1) &&
(autocomplete_input_.text()[
autocomplete_input_.parts().path.begin] == '/'))) &&
!autocomplete_input_.parts().query.is_nonempty() &&
!autocomplete_input_.parts().ref.is_nonempty()) {
will_have_url_what_you_typed_match_first = true;
const size_t registry_length =
net::registry_controlled_domains::GetRegistryLength(
host,
net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
if (registry_length == 0) {
url_what_you_typed_match_score =
HistoryURLProvider::kScoreForUnvisitedIntranetResult;
} else {
url_what_you_typed_match_score =
HistoryURLProvider::kScoreForWhatYouTypedResult;
}
}
}
}
}
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_);
TemplateURL* template_url = template_url_service ?
template_url_service->GetDefaultSearchProvider() : NULL;
int max_match_score =
(OmniboxFieldTrial::ReorderForLegalDefaultMatch(
autocomplete_input_.current_page_classification()) ||
(!PreventInlineAutocomplete(autocomplete_input_) &&
matches.begin()->can_inline())) ?
matches.begin()->raw_score() :
(AutocompleteResult::kLowestDefaultScore - 1);
if (will_have_url_what_you_typed_match_first) {
max_match_score = std::min(max_match_score,
url_what_you_typed_match_score - 1);
}
for (ScoredHistoryMatches::const_iterator match_iter = matches.begin();
match_iter != matches.end(); ++match_iter) {
const ScoredHistoryMatch& history_match(*match_iter);
if (!template_url ||
!template_url->IsSearchURL(history_match.url_info.url())) {
max_match_score = std::min(max_match_score, history_match.raw_score());
matches_.push_back(QuickMatchToACMatch(history_match, max_match_score));
max_match_score--;
}
}
}
AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch(
const ScoredHistoryMatch& history_match,
int score) {
const history::URLRow& info = history_match.url_info;
AutocompleteMatch match(
this, score, !!info.visit_count(),
history_match.url_matches().empty() ?
AutocompleteMatchType::HISTORY_TITLE :
AutocompleteMatchType::HISTORY_URL);
match.typed_count = info.typed_count();
match.destination_url = info.url();
DCHECK(match.destination_url.is_valid());
std::vector<size_t> offsets =
OffsetsFromTermMatches(history_match.url_matches());
const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll &
~(!history_match.match_in_scheme ? 0 : net::kFormatUrlOmitHTTP);
match.fill_into_edit =
AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(),
net::FormatUrlWithOffsets(info.url(), languages_, format_types,
net::UnescapeRule::SPACES, NULL, NULL, &offsets));
history::TermMatches new_matches =
ReplaceOffsetsInTermMatches(history_match.url_matches(), offsets);
match.contents = net::FormatUrl(info.url(), languages_, format_types,
net::UnescapeRule::SPACES, NULL, NULL, NULL);
match.contents_class =
SpansFromTermMatch(new_matches, match.contents.length(), true);
if (history_match.can_inline()) {
DCHECK(!new_matches.empty());
size_t inline_autocomplete_offset = new_matches[0].offset +
new_matches[0].length;
if (inline_autocomplete_offset < match.fill_into_edit.length()) {
match.inline_autocompletion =
match.fill_into_edit.substr(inline_autocomplete_offset);
}
match.allowed_to_be_default_match = match.inline_autocompletion.empty() ||
!PreventInlineAutocomplete(autocomplete_input_);
}
match.description = info.title();
match.description_class = SpansFromTermMatch(
history_match.title_matches(), match.description.length(), false);
match.RecordAdditionalInfo("typed count", info.typed_count());
match.RecordAdditionalInfo("visit count", info.visit_count());
match.RecordAdditionalInfo("last visit", info.last_visit());
return match;
}
history::InMemoryURLIndex* HistoryQuickProvider::GetIndex() {
if (index_for_testing_.get())
return index_for_testing_.get();
HistoryService* const history_service =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (!history_service)
return NULL;
return history_service->InMemoryIndex();
}