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/search/search.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/omnibox/autocomplete_match.h"
19 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_service.h"
21 #include "grit/components_scaled_resources.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/clipboard/clipboard.h"
24 #include "ui/base/l10n/l10n_util.h"
27 base::string16
OmniboxView::StripJavascriptSchemas(const base::string16
& text
) {
28 const base::string16
kJsPrefix(
29 base::ASCIIToUTF16(url::kJavaScriptScheme
) + base::ASCIIToUTF16(":"));
30 base::string16
out(text
);
31 while (StartsWith(out
, kJsPrefix
, false)) {
32 base::TrimWhitespace(out
.substr(kJsPrefix
.length()), base::TRIM_LEADING
,
39 base::string16
OmniboxView::SanitizeTextForPaste(const base::string16
& text
) {
40 // Check for non-newline whitespace; if found, collapse whitespace runs down
42 // TODO(shess): It may also make sense to ignore leading or
43 // trailing whitespace when making this determination.
44 for (size_t i
= 0; i
< text
.size(); ++i
) {
45 if (IsWhitespace(text
[i
]) && text
[i
] != '\n' && text
[i
] != '\r') {
46 const base::string16 collapsed
= base::CollapseWhitespace(text
, false);
47 // If the user is pasting all-whitespace, paste a single space
48 // rather than nothing, since pasting nothing feels broken.
49 return collapsed
.empty() ?
50 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed
);
54 // Otherwise, all whitespace is newlines; remove it entirely.
55 return StripJavascriptSchemas(base::CollapseWhitespace(text
, true));
59 base::string16
OmniboxView::GetClipboardText() {
61 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
62 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
63 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
65 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &text
);
66 return SanitizeTextForPaste(text
);
69 // Try bookmark format.
71 // It is tempting to try bookmark format first, but the URL we get out of a
72 // bookmark has been cannonicalized via GURL. This means if a user copies
73 // and pastes from the URL bar to itself, the text will get fixed up and
74 // cannonicalized, which is not what the user expects. By pasting in this
75 // order, we are sure to paste what the user copied.
76 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
77 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
79 clipboard
->ReadBookmark(NULL
, &url_str
);
80 // pass resulting url string through GURL to normalize
83 return StripJavascriptSchemas(base::UTF8ToUTF16(url
.spec()));
86 return base::string16();
89 OmniboxView::~OmniboxView() {
92 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
93 WindowOpenDisposition disposition
,
94 const GURL
& alternate_nav_url
,
95 const base::string16
& pasted_text
,
96 size_t selected_line
) {
97 // Invalid URLs such as chrome://history can end up here.
98 if (!match
.destination_url
.is_valid() || !model_
)
101 match
, disposition
, alternate_nav_url
, pasted_text
, selected_line
);
102 OnMatchOpened(match
, controller_
->GetWebContents());
105 bool OmniboxView::IsEditingOrEmpty() const {
106 return (model_
.get() && model_
->user_input_in_progress()) ||
107 (GetOmniboxTextLength() == 0);
110 int OmniboxView::GetIcon() const {
111 if (!IsEditingOrEmpty())
112 return controller_
->GetToolbarModel()->GetIcon();
113 int id
= AutocompleteMatch::TypeToIcon(model_
.get() ?
114 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
115 return (id
== IDR_OMNIBOX_HTTP
) ? IDR_LOCATION_BAR_HTTP
: id
;
118 base::string16
OmniboxView::GetHintText() const {
119 // If the user is in keyword mode (the "Search <some site>:" chip is showing)
120 // then it doesn't make sense to show the "Search <default search engine>"
122 if (model_
->is_keyword_selected())
123 return base::string16();
125 // Attempt to determine the default search provider and use that in the hint
127 TemplateURLService
* template_url_service
=
128 TemplateURLServiceFactory::GetForProfile(model_
->profile());
129 if (template_url_service
) {
130 TemplateURL
* template_url
=
131 template_url_service
->GetDefaultSearchProvider();
133 return l10n_util::GetStringFUTF16(
134 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER
,
135 template_url
->AdjustedShortNameForLocaleDirection());
138 // Otherwise return a hint based on there being no default search provider.
139 return l10n_util::GetStringUTF16(
140 IDS_OMNIBOX_EMPTY_HINT_NO_DEFAULT_SEARCH_PROVIDER
);
143 void OmniboxView::SetUserText(const base::string16
& text
) {
144 SetUserText(text
, text
, true);
147 void OmniboxView::SetUserText(const base::string16
& text
,
148 const base::string16
& display_text
,
151 model_
->SetUserText(text
);
152 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
156 void OmniboxView::ShowURL() {
158 controller_
->GetToolbarModel()->set_url_replacement_enabled(false);
159 model_
->UpdatePermanentText();
160 RevertWithoutResettingSearchTermReplacement();
164 void OmniboxView::HideURL() {
165 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
166 model_
->UpdatePermanentText();
167 RevertWithoutResettingSearchTermReplacement();
170 void OmniboxView::RevertAll() {
171 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
172 RevertWithoutResettingSearchTermReplacement();
175 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
182 void OmniboxView::CloseOmniboxPopup() {
184 model_
->StopAutocomplete();
187 bool OmniboxView::IsImeShowingPopup() const {
188 // Default to claiming that the IME is not showing a popup, since hiding the
189 // omnibox dropdown is a bad user experience when we don't know for sure that
194 void OmniboxView::ShowImeIfNeeded() {
197 bool OmniboxView::IsIndicatingQueryRefinement() const {
198 // The default implementation always returns false. Mobile ports can override
199 // this method and implement as needed.
203 void OmniboxView::OnMatchOpened(const AutocompleteMatch
& match
,
204 content::WebContents
* web_contents
) {
207 OmniboxView::OmniboxView(Profile
* profile
,
208 OmniboxEditController
* controller
,
209 CommandUpdater
* command_updater
)
210 : controller_(controller
),
211 command_updater_(command_updater
) {
212 // |profile| can be NULL in tests.
214 model_
.reset(new OmniboxEditModel(this, controller
, profile
));
217 void OmniboxView::TextChanged() {
218 EmphasizeURLComponents();