This source file includes following definitions.
- StripJavascriptSchemas
- SanitizeTextForPaste
- GetClipboardText
- HandleOriginChipMouseRelease
- OnDidKillFocus
- OpenMatch
- IsEditingOrEmpty
- GetIcon
- GetHintText
- SetUserText
- SetUserText
- ShowURL
- HideURL
- RevertAll
- RevertWithoutResettingSearchTermReplacement
- CloseOmniboxPopup
- IsImeShowingPopup
- ShowImeIfNeeded
- IsIndicatingQueryRefinement
- OnMatchOpened
- command_updater_
- TextChanged
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_match.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/browser/ui/omnibox/omnibox_edit_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_model.h"
#include "grit/generated_resources.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/l10n/l10n_util.h"
base::string16 OmniboxView::StripJavascriptSchemas(const base::string16& text) {
const base::string16 kJsPrefix(
base::ASCIIToUTF16(content::kJavaScriptScheme) + base::ASCIIToUTF16(":"));
base::string16 out(text);
while (StartsWith(out, kJsPrefix, false)) {
base::TrimWhitespace(out.substr(kJsPrefix.length()), base::TRIM_LEADING,
&out);
}
return out;
}
base::string16 OmniboxView::SanitizeTextForPaste(const base::string16& text) {
for (size_t i = 0; i < text.size(); ++i) {
if (IsWhitespace(text[i]) && text[i] != '\n' && text[i] != '\r') {
const base::string16 collapsed = base::CollapseWhitespace(text, false);
return collapsed.empty() ?
base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed);
}
}
return StripJavascriptSchemas(base::CollapseWhitespace(text, true));
}
base::string16 OmniboxView::GetClipboardText() {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
ui::CLIPBOARD_TYPE_COPY_PASTE)) {
base::string16 text;
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &text);
return SanitizeTextForPaste(text);
}
if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
ui::CLIPBOARD_TYPE_COPY_PASTE)) {
std::string url_str;
clipboard->ReadBookmark(NULL, &url_str);
GURL url(url_str);
if (url.is_valid())
return StripJavascriptSchemas(base::UTF8ToUTF16(url.spec()));
}
return base::string16();
}
OmniboxView::~OmniboxView() {
}
void OmniboxView::HandleOriginChipMouseRelease() {
if ((chrome::GetOriginChipV2HideTrigger() ==
chrome::ORIGIN_CHIP_V2_HIDE_ON_MOUSE_RELEASE) &&
controller()->GetToolbarModel()->GetText().empty()) {
controller()->HideOriginChip();
}
}
void OmniboxView::OnDidKillFocus() {
if (chrome::ShouldDisplayOriginChipV2() &&
!model()->user_input_in_progress()) {
controller()->ShowOriginChip();
}
}
void OmniboxView::OpenMatch(const AutocompleteMatch& match,
WindowOpenDisposition disposition,
const GURL& alternate_nav_url,
const base::string16& pasted_text,
size_t selected_line) {
if (!match.destination_url.is_valid() || !model_)
return;
model_->OpenMatch(
match, disposition, alternate_nav_url, pasted_text, selected_line);
OnMatchOpened(match, model_->profile(), controller_->GetWebContents());
}
bool OmniboxView::IsEditingOrEmpty() const {
return (model_.get() && model_->user_input_in_progress()) ||
(GetOmniboxTextLength() == 0);
}
int OmniboxView::GetIcon() const {
if (!IsEditingOrEmpty())
return controller_->GetToolbarModel()->GetIcon();
return AutocompleteMatch::TypeToLocationBarIcon(model_.get() ?
model_->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED);
}
base::string16 OmniboxView::GetHintText() const {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(model_->profile());
if (template_url_service) {
TemplateURL* template_url =
template_url_service->GetDefaultSearchProvider();
if (template_url)
return l10n_util::GetStringFUTF16(
IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER,
template_url->AdjustedShortNameForLocaleDirection());
}
return l10n_util::GetStringUTF16(
IDS_OMNIBOX_EMPTY_HINT_NO_DEFAULT_SEARCH_PROVIDER);
}
void OmniboxView::SetUserText(const base::string16& text) {
SetUserText(text, text, true);
}
void OmniboxView::SetUserText(const base::string16& text,
const base::string16& display_text,
bool update_popup) {
if (model_.get())
model_->SetUserText(text);
SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup,
true);
}
void OmniboxView::ShowURL() {
SetFocus();
controller_->GetToolbarModel()->set_origin_chip_enabled(false);
controller_->GetToolbarModel()->set_url_replacement_enabled(false);
model_->UpdatePermanentText();
RevertWithoutResettingSearchTermReplacement();
SelectAll(true);
}
void OmniboxView::HideURL() {
controller_->GetToolbarModel()->set_origin_chip_enabled(true);
controller_->GetToolbarModel()->set_url_replacement_enabled(true);
model_->UpdatePermanentText();
RevertWithoutResettingSearchTermReplacement();
}
void OmniboxView::RevertAll() {
controller_->GetToolbarModel()->set_url_replacement_enabled(true);
RevertWithoutResettingSearchTermReplacement();
}
void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
CloseOmniboxPopup();
if (model_.get())
model_->Revert();
TextChanged();
}
void OmniboxView::CloseOmniboxPopup() {
if (model_.get())
model_->StopAutocomplete();
}
bool OmniboxView::IsImeShowingPopup() const {
return false;
}
void OmniboxView::ShowImeIfNeeded() {
}
bool OmniboxView::IsIndicatingQueryRefinement() const {
return false;
}
void OmniboxView::OnMatchOpened(const AutocompleteMatch& match,
Profile* profile,
content::WebContents* web_contents) const {}
OmniboxView::OmniboxView(Profile* profile,
OmniboxEditController* controller,
CommandUpdater* command_updater)
: controller_(controller),
command_updater_(command_updater) {
if (profile)
model_.reset(new OmniboxEditModel(this, controller, profile));
}
void OmniboxView::TextChanged() {
EmphasizeURLComponents();
if (model_.get())
model_->OnChanged();
}