This source file includes following definitions.
- RegisterMessages
- OnResultChanged
- AddResultToDictionary
- LookupIsTypedHost
- StartOmniboxQuery
- ResetController
#include "chrome/browser/ui/webui/omnibox/omnibox_ui_handler.h"
#include <string>
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/autocomplete/autocomplete_classifier.h"
#include "chrome/browser/autocomplete/autocomplete_controller.h"
#include "chrome/browser/autocomplete/autocomplete_input.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/autocomplete_provider.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/url_database.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url.h"
#include "content/public/browser/web_ui.h"
OmniboxUIHandler::OmniboxUIHandler(Profile* profile): profile_(profile) {
ResetController();
}
OmniboxUIHandler::~OmniboxUIHandler() {}
void OmniboxUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("startOmniboxQuery",
base::Bind(&OmniboxUIHandler::StartOmniboxQuery,
base::Unretained(this)));
}
void OmniboxUIHandler::OnResultChanged(bool default_match_changed) {
base::DictionaryValue result_to_output;
result_to_output.SetBoolean("done", controller_->done());
result_to_output.SetInteger("time_since_omnibox_started_ms",
(base::Time::Now() - time_omnibox_started_).InMilliseconds());
const base::string16& host = controller_->input().text().substr(
controller_->input().parts().host.begin,
controller_->input().parts().host.len);
result_to_output.SetString("host", host);
bool is_typed_host;
if (LookupIsTypedHost(host, &is_typed_host)) {
result_to_output.SetBoolean("is_typed_host", is_typed_host);
}
AddResultToDictionary("combined_results", controller_->result().begin(),
controller_->result().end(), &result_to_output);
for (ACProviders::const_iterator it(controller_->providers()->begin());
it != controller_->providers()->end(); ++it) {
AddResultToDictionary(
std::string("results_by_provider.") + (*it)->GetName(),
(*it)->matches().begin(), (*it)->matches().end(), &result_to_output);
}
web_ui()->CallJavascriptFunction("omniboxDebug.handleNewAutocompleteResult",
result_to_output);
}
void OmniboxUIHandler::AddResultToDictionary(const std::string& prefix,
ACMatches::const_iterator it,
ACMatches::const_iterator end,
base::DictionaryValue* output) {
int i = 0;
for (; it != end; ++it, ++i) {
std::string item_prefix(prefix + base::StringPrintf(".item_%d", i));
if (it->provider != NULL) {
output->SetString(item_prefix + ".provider_name",
it->provider->GetName());
output->SetBoolean(item_prefix + ".provider_done", it->provider->done());
}
output->SetInteger(item_prefix + ".relevance", it->relevance);
output->SetBoolean(item_prefix + ".deletable", it->deletable);
output->SetString(item_prefix + ".fill_into_edit", it->fill_into_edit);
output->SetString(item_prefix + ".inline_autocompletion",
it->inline_autocompletion);
output->SetString(item_prefix + ".destination_url",
it->destination_url.spec());
output->SetString(item_prefix + ".contents", it->contents);
output->SetString(item_prefix + ".description", it->description);
output->SetInteger(item_prefix + ".transition", it->transition);
output->SetBoolean(item_prefix + ".is_history_what_you_typed_match",
it->is_history_what_you_typed_match);
output->SetBoolean(item_prefix + ".allowed_to_be_default_match",
it->allowed_to_be_default_match);
output->SetString(item_prefix + ".type",
AutocompleteMatchType::ToString(it->type));
if (it->associated_keyword.get() != NULL) {
output->SetString(item_prefix + ".associated_keyword",
it->associated_keyword->keyword);
}
output->SetString(item_prefix + ".keyword", it->keyword);
output->SetBoolean(item_prefix + ".starred", it->starred);
output->SetInteger(item_prefix + ".duplicates",
static_cast<int>(it->duplicate_matches.size()));
output->SetBoolean(item_prefix + ".from_previous", it->from_previous);
for (AutocompleteMatch::AdditionalInfo::const_iterator j =
it->additional_info.begin(); j != it->additional_info.end(); ++j) {
output->SetString(item_prefix + ".additional_info." + j->first,
j->second);
}
}
output->SetInteger(prefix + ".num_items", i);
}
bool OmniboxUIHandler::LookupIsTypedHost(const base::string16& host,
bool* is_typed_host) const {
HistoryService* const history_service =
HistoryServiceFactory::GetForProfile(profile_,
Profile::EXPLICIT_ACCESS);
if (!history_service)
return false;
history::URLDatabase* url_db = history_service->InMemoryDatabase();
if (!url_db)
return false;
*is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host));
return true;
}
void OmniboxUIHandler::StartOmniboxQuery(const base::ListValue* input) {
DCHECK_EQ(5u, input->GetSize());
base::string16 input_string;
bool return_val = input->GetString(0, &input_string);
DCHECK(return_val);
int cursor_position;
return_val = input->GetInteger(1, &cursor_position);
DCHECK(return_val);
bool prevent_inline_autocomplete;
return_val = input->GetBoolean(2, &prevent_inline_autocomplete);
DCHECK(return_val);
bool prefer_keyword;
return_val = input->GetBoolean(3, &prefer_keyword);
DCHECK(return_val);
int current_page_classification;
return_val = input->GetInteger(4, ¤t_page_classification);
DCHECK(return_val);
ResetController();
time_omnibox_started_ = base::Time::Now();
controller_->Start(AutocompleteInput(
input_string,
cursor_position,
base::string16(),
GURL(),
static_cast<AutocompleteInput::PageClassification>(
current_page_classification),
prevent_inline_autocomplete,
prefer_keyword,
true,
AutocompleteInput::ALL_MATCHES));
}
void OmniboxUIHandler::ResetController() {
controller_.reset(new AutocompleteController(profile_, this,
AutocompleteClassifier::kDefaultOmniboxProviders));
}