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/ui/omnibox/omnibox_client.h"
14 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/omnibox/browser/autocomplete_match.h"
18 #include "grit/components_scaled_resources.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/clipboard/clipboard.h"
21 #include "ui/base/l10n/l10n_util.h"
24 base::string16
OmniboxView::StripJavascriptSchemas(const base::string16
& text
) {
25 const base::string16
kJsPrefix(
26 base::ASCIIToUTF16(url::kJavaScriptScheme
) + base::ASCIIToUTF16(":"));
27 base::string16
out(text
);
28 while (base::StartsWith(out
, kJsPrefix
,
29 base::CompareCase::INSENSITIVE_ASCII
)) {
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 (base::IsUnicodeWhitespace(text
[i
]) &&
44 text
[i
] != '\n' && text
[i
] != '\r') {
45 const base::string16 collapsed
= base::CollapseWhitespace(text
, false);
46 // If the user is pasting all-whitespace, paste a single space
47 // rather than nothing, since pasting nothing feels broken.
48 return collapsed
.empty() ?
49 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed
);
53 // Otherwise, all whitespace is newlines; remove it entirely.
54 return StripJavascriptSchemas(base::CollapseWhitespace(text
, true));
58 base::string16
OmniboxView::GetClipboardText() {
60 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
61 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
62 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
64 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &text
);
65 return SanitizeTextForPaste(text
);
68 // Try bookmark format.
70 // It is tempting to try bookmark format first, but the URL we get out of a
71 // bookmark has been cannonicalized via GURL. This means if a user copies
72 // and pastes from the URL bar to itself, the text will get fixed up and
73 // cannonicalized, which is not what the user expects. By pasting in this
74 // order, we are sure to paste what the user copied.
75 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
76 ui::CLIPBOARD_TYPE_COPY_PASTE
)) {
78 clipboard
->ReadBookmark(NULL
, &url_str
);
79 // pass resulting url string through GURL to normalize
82 return StripJavascriptSchemas(base::UTF8ToUTF16(url
.spec()));
85 return base::string16();
88 OmniboxView::~OmniboxView() {
91 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
92 WindowOpenDisposition disposition
,
93 const GURL
& alternate_nav_url
,
94 const base::string16
& pasted_text
,
95 size_t selected_line
) {
96 // Invalid URLs such as chrome://history can end up here.
97 if (!match
.destination_url
.is_valid() || !model_
)
100 match
, disposition
, alternate_nav_url
, pasted_text
, selected_line
);
101 OnMatchOpened(match
);
104 bool OmniboxView::IsEditingOrEmpty() const {
105 return (model_
.get() && model_
->user_input_in_progress()) ||
106 (GetOmniboxTextLength() == 0);
109 int OmniboxView::GetIcon() const {
110 if (!IsEditingOrEmpty())
111 return controller_
->GetToolbarModel()->GetIcon();
112 int id
= AutocompleteMatch::TypeToIcon(model_
.get() ?
113 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
114 return (id
== IDR_OMNIBOX_HTTP
) ? IDR_LOCATION_BAR_HTTP
: id
;
117 void OmniboxView::SetUserText(const base::string16
& text
) {
118 SetUserText(text
, text
, true);
121 void OmniboxView::SetUserText(const base::string16
& text
,
122 const base::string16
& display_text
,
125 model_
->SetUserText(text
);
126 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
130 void OmniboxView::ShowURL() {
132 controller_
->GetToolbarModel()->set_url_replacement_enabled(false);
133 model_
->UpdatePermanentText();
134 RevertWithoutResettingSearchTermReplacement();
138 void OmniboxView::HideURL() {
139 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
140 model_
->UpdatePermanentText();
141 RevertWithoutResettingSearchTermReplacement();
144 void OmniboxView::RevertAll() {
145 controller_
->GetToolbarModel()->set_url_replacement_enabled(true);
146 RevertWithoutResettingSearchTermReplacement();
149 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
156 void OmniboxView::CloseOmniboxPopup() {
158 model_
->StopAutocomplete();
161 bool OmniboxView::IsImeShowingPopup() const {
162 // Default to claiming that the IME is not showing a popup, since hiding the
163 // omnibox dropdown is a bad user experience when we don't know for sure that
168 void OmniboxView::ShowImeIfNeeded() {
171 bool OmniboxView::IsIndicatingQueryRefinement() const {
172 // The default implementation always returns false. Mobile ports can override
173 // this method and implement as needed.
177 void OmniboxView::OnMatchOpened(const AutocompleteMatch
& match
) {
180 OmniboxView::OmniboxView(Profile
* profile
,
181 OmniboxEditController
* controller
,
182 scoped_ptr
<OmniboxClient
> client
,
183 CommandUpdater
* command_updater
)
184 : controller_(controller
), command_updater_(command_updater
) {
185 // |profile| can be NULL in tests.
188 new OmniboxEditModel(this, controller
, client
.Pass(), profile
));
191 void OmniboxView::TextChanged() {
192 EmphasizeURLComponents();