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.
5 // This file defines helper functions shared by the various implementations
8 #include "chrome/browser/ui/omnibox/omnibox_view.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_service.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
19 #include "chrome/browser/ui/toolbar/toolbar_model.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/clipboard/clipboard.h"
22 #include "ui/base/l10n/l10n_util.h"
25 base::string16
OmniboxView::StripJavascriptSchemas(const base::string16
& text
) {
26 const base::string16
kJsPrefix(
27 base::ASCIIToUTF16(content::kJavaScriptScheme
) + base::ASCIIToUTF16(":"));
28 base::string16
out(text
);
29 while (StartsWith(out
, kJsPrefix
, false)) {
30 base::TrimWhitespace(out
.substr(kJsPrefix
.length()), base::TRIM_LEADING
,
37 base::string16
OmniboxView::SanitizeTextForPaste(const base::string16
& text
) {
38 // Check for non-newline whitespace; if found, collapse whitespace runs down
40 // TODO(shess): It may also make sense to ignore leading or
41 // trailing whitespace when making this determination.
42 for (size_t i
= 0; i
< text
.size(); ++i
) {
43 if (IsWhitespace(text
[i
]) && text
[i
] != '\n' && text
[i
] != '\r') {
44 const base::string16 collapsed
= base::CollapseWhitespace(text
, false);
45 // If the user is pasting all-whitespace, paste a single space
46 // rather than nothing, since pasting nothing feels broken.
47 return collapsed
.empty() ?
48 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed
);
52 // Otherwise, all whitespace is newlines; remove it entirely.
53 return StripJavascriptSchemas(base::CollapseWhitespace(text
, true));
57 base::string16
OmniboxView::GetClipboardText() {
59 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
60 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
61 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
63 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &text
);
64 return SanitizeTextForPaste(text
);
67 // Try bookmark format.
69 // It is tempting to try bookmark format first, but the URL we get out of a
70 // bookmark has been cannonicalized via GURL. This means if a user copies
71 // and pastes from the URL bar to itself, the text will get fixed up and
72 // cannonicalized, which is not what the user expects. By pasting in this
73 // order, we are sure to paste what the user copied.
74 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
75 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
77 clipboard
->ReadBookmark(NULL
, &url_str
);
78 // pass resulting url string through GURL to normalize
81 return StripJavascriptSchemas(base::UTF8ToUTF16(url
.spec()));
84 return base::string16();
87 OmniboxView::~OmniboxView() {
90 void OmniboxView::HandleOriginChipMouseRelease() {
91 // HIDE_ON_MOUSE_RELEASE only hides if there isn't any current text in the
92 // Omnibox (e.g. search terms).
93 if ((chrome::GetOriginChipV2HideTrigger() ==
94 chrome::ORIGIN_CHIP_V2_HIDE_ON_MOUSE_RELEASE
) &&
95 controller()->GetToolbarModel()->GetText().empty()) {
96 controller()->HideOriginChip();
100 void OmniboxView::OnDidKillFocus() {
101 if (chrome::ShouldDisplayOriginChipV2() &&
102 !model()->user_input_in_progress()) {
103 controller()->ShowOriginChip();
107 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
108 WindowOpenDisposition disposition
,
109 const GURL
& alternate_nav_url
,
110 const base::string16
& pasted_text
,
111 size_t selected_line
) {
112 // Invalid URLs such as chrome://history can end up here.
113 if (!match
.destination_url
.is_valid() || !model_
)
116 match
, disposition
, alternate_nav_url
, pasted_text
, selected_line
);
117 OnMatchOpened(match
, model_
->profile(), controller_
->GetWebContents());
120 bool OmniboxView::IsEditingOrEmpty() const {
121 return (model_
.get() && model_
->user_input_in_progress()) ||
122 (GetOmniboxTextLength() == 0);
125 int OmniboxView::GetIcon() const {
126 if (!IsEditingOrEmpty())
127 return controller_
->GetToolbarModel()->GetIcon();
128 return AutocompleteMatch::TypeToLocationBarIcon(model_
.get() ?
129 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
132 base::string16
OmniboxView::GetHintText() const {
133 // Attempt to determine the default search provider and use that in the hint
135 TemplateURLService
* template_url_service
=
136 TemplateURLServiceFactory::GetForProfile(model_
->profile());
137 if (template_url_service
) {
138 TemplateURL
* template_url
=
139 template_url_service
->GetDefaultSearchProvider();
141 return l10n_util::GetStringFUTF16(
142 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER
,
143 template_url
->AdjustedShortNameForLocaleDirection());
146 // Otherwise return a hint based on there being no default search provider.
147 return l10n_util::GetStringUTF16(
148 IDS_OMNIBOX_EMPTY_HINT_NO_DEFAULT_SEARCH_PROVIDER
);
151 void OmniboxView::SetUserText(const base::string16
& text
) {
152 SetUserText(text
, text
, true);
155 void OmniboxView::SetUserText(const base::string16
& text
,
156 const base::string16
& display_text
,
159 model_
->SetUserText(text
);
160 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
164 void OmniboxView::ShowURL() {
166 controller_
->GetToolbarModel()->set_origin_chip_enabled(false);
167 controller_
->GetToolbarModel()->set_url_replacement_enabled(false);
168 model_
->UpdatePermanentText();
169 RevertWithoutResettingSearchTermReplacement();
173 void OmniboxView::HideURL() {
174 controller_
->GetToolbarModel()->set_origin_chip_enabled(true);
175 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
176 model_
->UpdatePermanentText();
177 RevertWithoutResettingSearchTermReplacement();
180 void OmniboxView::RevertAll() {
181 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
182 RevertWithoutResettingSearchTermReplacement();
185 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
192 void OmniboxView::CloseOmniboxPopup() {
194 model_
->StopAutocomplete();
197 bool OmniboxView::IsImeShowingPopup() const {
198 // Default to claiming that the IME is not showing a popup, since hiding the
199 // omnibox dropdown is a bad user experience when we don't know for sure that
204 void OmniboxView::ShowImeIfNeeded() {
207 bool OmniboxView::IsIndicatingQueryRefinement() const {
208 // The default implementation always returns false. Mobile ports can override
209 // this method and implement as needed.
213 void OmniboxView::OnMatchOpened(const AutocompleteMatch
& match
,
215 content::WebContents
* web_contents
) const {}
217 OmniboxView::OmniboxView(Profile
* profile
,
218 OmniboxEditController
* controller
,
219 CommandUpdater
* command_updater
)
220 : controller_(controller
),
221 command_updater_(command_updater
) {
222 // |profile| can be NULL in tests.
224 model_
.reset(new OmniboxEditModel(this, controller
, profile
));
227 void OmniboxView::TextChanged() {
228 EmphasizeURLComponents();