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 "components/omnibox/browser/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 "components/omnibox/browser/autocomplete_match.h"
14 #include "components/omnibox/browser/omnibox_client.h"
15 #include "components/omnibox/browser/omnibox_edit_controller.h"
16 #include "components/toolbar/toolbar_model.h"
17 #include "grit/components_scaled_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
21 base::string16
OmniboxView::StripJavascriptSchemas(const base::string16
& text
) {
22 const base::string16
kJsPrefix(
23 base::ASCIIToUTF16(url::kJavaScriptScheme
) + base::ASCIIToUTF16(":"));
24 base::string16
out(text
);
25 while (base::StartsWith(out
, kJsPrefix
,
26 base::CompareCase::INSENSITIVE_ASCII
)) {
27 base::TrimWhitespace(out
.substr(kJsPrefix
.length()), base::TRIM_LEADING
,
34 base::string16
OmniboxView::SanitizeTextForPaste(const base::string16
& text
) {
35 // Check for non-newline whitespace; if found, collapse whitespace runs down
37 // TODO(shess): It may also make sense to ignore leading or
38 // trailing whitespace when making this determination.
39 for (size_t i
= 0; i
< text
.size(); ++i
) {
40 if (base::IsUnicodeWhitespace(text
[i
]) &&
41 text
[i
] != '\n' && text
[i
] != '\r') {
42 const base::string16 collapsed
= base::CollapseWhitespace(text
, false);
43 // If the user is pasting all-whitespace, paste a single space
44 // rather than nothing, since pasting nothing feels broken.
45 return collapsed
.empty() ?
46 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed
);
50 // Otherwise, all whitespace is newlines; remove it entirely.
51 return StripJavascriptSchemas(base::CollapseWhitespace(text
, true));
54 OmniboxView::~OmniboxView() {
57 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
58 WindowOpenDisposition disposition
,
59 const GURL
& alternate_nav_url
,
60 const base::string16
& pasted_text
,
61 size_t selected_line
) {
62 // Invalid URLs such as chrome://history can end up here.
63 if (!match
.destination_url
.is_valid() || !model_
)
66 match
, disposition
, alternate_nav_url
, pasted_text
, selected_line
);
70 bool OmniboxView::IsEditingOrEmpty() const {
71 return (model_
.get() && model_
->user_input_in_progress()) ||
72 (GetOmniboxTextLength() == 0);
75 int OmniboxView::GetIcon() const {
76 if (!IsEditingOrEmpty())
77 return controller_
->GetToolbarModel()->GetIcon();
78 int id
= AutocompleteMatch::TypeToIcon(model_
.get() ?
79 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
80 return (id
== IDR_OMNIBOX_HTTP
) ? IDR_LOCATION_BAR_HTTP
: id
;
83 void OmniboxView::SetUserText(const base::string16
& text
) {
84 SetUserText(text
, text
, true);
87 void OmniboxView::SetUserText(const base::string16
& text
,
88 const base::string16
& display_text
,
91 model_
->SetUserText(text
);
92 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
96 void OmniboxView::ShowURL() {
98 controller_
->GetToolbarModel()->set_url_replacement_enabled(false);
99 model_
->UpdatePermanentText();
100 RevertWithoutResettingSearchTermReplacement();
104 void OmniboxView::HideURL() {
105 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
106 model_
->UpdatePermanentText();
107 RevertWithoutResettingSearchTermReplacement();
110 void OmniboxView::RevertAll() {
111 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
112 RevertWithoutResettingSearchTermReplacement();
115 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
122 void OmniboxView::CloseOmniboxPopup() {
124 model_
->StopAutocomplete();
127 bool OmniboxView::IsImeShowingPopup() const {
128 // Default to claiming that the IME is not showing a popup, since hiding the
129 // omnibox dropdown is a bad user experience when we don't know for sure that
134 void OmniboxView::ShowImeIfNeeded() {
137 bool OmniboxView::IsIndicatingQueryRefinement() const {
138 // The default implementation always returns false. Mobile ports can override
139 // this method and implement as needed.
143 void OmniboxView::OnMatchOpened(const AutocompleteMatch
& match
) {
146 OmniboxView::OmniboxView(OmniboxEditController
* controller
,
147 scoped_ptr
<OmniboxClient
> client
)
148 : controller_(controller
) {
149 // |client| can be null in tests.
152 new OmniboxEditModel(this, controller
, client
.Pass()));
156 void OmniboxView::TextChanged() {
157 EmphasizeURLComponents();