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 // Only hide if there isn't any current text in the Omnibox (e.g. search
93 if (controller()->GetToolbarModel()->GetText().empty())
94 controller()->HideOriginChip();
97 void OmniboxView::OnDidKillFocus() {
98 if (chrome::ShouldDisplayOriginChip() && !model()->user_input_in_progress())
99 controller()->ShowOriginChip();
102 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
103 WindowOpenDisposition disposition
,
104 const GURL
& alternate_nav_url
,
105 const base::string16
& pasted_text
,
106 size_t selected_line
) {
107 // Invalid URLs such as chrome://history can end up here.
108 if (!match
.destination_url
.is_valid() || !model_
)
111 match
, disposition
, alternate_nav_url
, pasted_text
, selected_line
);
112 OnMatchOpened(match
, model_
->profile(), controller_
->GetWebContents());
115 bool OmniboxView::IsEditingOrEmpty() const {
116 return (model_
.get() && model_
->user_input_in_progress()) ||
117 (GetOmniboxTextLength() == 0);
120 int OmniboxView::GetIcon() const {
121 if (!IsEditingOrEmpty())
122 return controller_
->GetToolbarModel()->GetIcon();
123 return AutocompleteMatch::TypeToLocationBarIcon(model_
.get() ?
124 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
127 base::string16
OmniboxView::GetHintText() const {
128 // Attempt to determine the default search provider and use that in the hint
130 TemplateURLService
* template_url_service
=
131 TemplateURLServiceFactory::GetForProfile(model_
->profile());
132 if (template_url_service
) {
133 TemplateURL
* template_url
=
134 template_url_service
->GetDefaultSearchProvider();
136 return l10n_util::GetStringFUTF16(
137 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER
,
138 template_url
->AdjustedShortNameForLocaleDirection());
141 // Otherwise return a hint based on there being no default search provider.
142 return l10n_util::GetStringUTF16(
143 IDS_OMNIBOX_EMPTY_HINT_NO_DEFAULT_SEARCH_PROVIDER
);
146 void OmniboxView::SetUserText(const base::string16
& text
) {
147 SetUserText(text
, text
, true);
150 void OmniboxView::SetUserText(const base::string16
& text
,
151 const base::string16
& display_text
,
154 model_
->SetUserText(text
);
155 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
159 void OmniboxView::ShowURL() {
161 controller_
->GetToolbarModel()->set_origin_chip_enabled(false);
162 controller_
->GetToolbarModel()->set_url_replacement_enabled(false);
163 model_
->UpdatePermanentText();
164 RevertWithoutResettingSearchTermReplacement();
168 void OmniboxView::HideURL() {
169 controller_
->GetToolbarModel()->set_origin_chip_enabled(true);
170 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
171 model_
->UpdatePermanentText();
172 RevertWithoutResettingSearchTermReplacement();
175 void OmniboxView::RevertAll() {
176 controller_
->GetToolbarModel()->set_origin_chip_enabled(true);
177 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
178 RevertWithoutResettingSearchTermReplacement();
181 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
188 void OmniboxView::CloseOmniboxPopup() {
190 model_
->StopAutocomplete();
193 bool OmniboxView::IsImeShowingPopup() const {
194 // Default to claiming that the IME is not showing a popup, since hiding the
195 // omnibox dropdown is a bad user experience when we don't know for sure that
200 void OmniboxView::ShowImeIfNeeded() {
203 bool OmniboxView::IsIndicatingQueryRefinement() const {
204 // The default implementation always returns false. Mobile ports can override
205 // this method and implement as needed.
209 void OmniboxView::OnMatchOpened(const AutocompleteMatch
& match
,
211 content::WebContents
* web_contents
) const {}
213 OmniboxView::OmniboxView(Profile
* profile
,
214 OmniboxEditController
* controller
,
215 CommandUpdater
* command_updater
)
216 : controller_(controller
),
217 command_updater_(command_updater
) {
218 // |profile| can be NULL in tests.
220 model_
.reset(new OmniboxEditModel(this, controller
, profile
));
223 void OmniboxView::TextChanged() {
224 EmphasizeURLComponents();