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/ui/omnibox/omnibox_edit_controller.h"
15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
16 #include "ui/base/clipboard/clipboard.h"
19 string16
OmniboxView::StripJavascriptSchemas(const string16
& text
) {
20 const string16
kJsPrefix(ASCIIToUTF16(content::kJavaScriptScheme
) +
23 while (StartsWith(out
, kJsPrefix
, false))
24 TrimWhitespace(out
.substr(kJsPrefix
.length()), TRIM_LEADING
, &out
);
29 string16
OmniboxView::SanitizeTextForPaste(const string16
& text
) {
30 // Check for non-newline whitespace; if found, collapse whitespace runs down
32 // TODO(shess): It may also make sense to ignore leading or
33 // trailing whitespace when making this determination.
34 for (size_t i
= 0; i
< text
.size(); ++i
) {
35 if (IsWhitespace(text
[i
]) && text
[i
] != '\n' && text
[i
] != '\r') {
36 const string16 collapsed
= CollapseWhitespace(text
, false);
37 // If the user is pasting all-whitespace, paste a single space
38 // rather than nothing, since pasting nothing feels broken.
39 return collapsed
.empty() ?
40 ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed
);
44 // Otherwise, all whitespace is newlines; remove it entirely.
45 return StripJavascriptSchemas(CollapseWhitespace(text
, true));
49 string16
OmniboxView::GetClipboardText() {
51 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
52 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
53 ui::Clipboard::BUFFER_STANDARD
)) {
55 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &text
);
56 return SanitizeTextForPaste(text
);
59 // Try bookmark format.
61 // It is tempting to try bookmark format first, but the URL we get out of a
62 // bookmark has been cannonicalized via GURL. This means if a user copies
63 // and pastes from the URL bar to itself, the text will get fixed up and
64 // cannonicalized, which is not what the user expects. By pasting in this
65 // order, we are sure to paste what the user copied.
66 if (clipboard
->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
67 ui::Clipboard::BUFFER_STANDARD
)) {
69 clipboard
->ReadBookmark(NULL
, &url_str
);
70 // pass resulting url string through GURL to normalize
73 return StripJavascriptSchemas(UTF8ToUTF16(url
.spec()));
79 OmniboxView::~OmniboxView() {
82 void OmniboxView::OpenMatch(const AutocompleteMatch
& match
,
83 WindowOpenDisposition disposition
,
84 const GURL
& alternate_nav_url
,
85 size_t selected_line
) {
86 // Invalid URLs such as chrome://history can end up here.
87 if (!match
.destination_url
.is_valid())
90 model_
->OpenMatch(match
, disposition
, alternate_nav_url
, selected_line
);
93 bool OmniboxView::IsEditingOrEmpty() const {
94 return (model_
.get() && model_
->user_input_in_progress()) ||
95 (GetOmniboxTextLength() == 0);
98 int OmniboxView::GetIcon() const {
99 if (!IsEditingOrEmpty())
100 return controller_
->GetToolbarModel()->GetIcon();
101 return AutocompleteMatch::TypeToLocationBarIcon(model_
.get() ?
102 model_
->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
105 void OmniboxView::SetUserText(const string16
& text
) {
106 SetUserText(text
, text
, true);
109 void OmniboxView::SetUserText(const string16
& text
,
110 const string16
& display_text
,
113 model_
->SetUserText(text
);
114 SetWindowTextAndCaretPos(display_text
, display_text
.length(), update_popup
,
118 void OmniboxView::RevertAll() {
125 void OmniboxView::CloseOmniboxPopup() {
127 model_
->StopAutocomplete();
130 bool OmniboxView::IsImeShowingPopup() const {
131 // Default to claiming that the IME is not showing a popup, since hiding the
132 // omnibox dropdown is a bad user experience when we don't know for sure that
137 bool OmniboxView::IsIndicatingQueryRefinement() const {
138 // The default implementation always returns false. Mobile ports can override
139 // this method and implement as needed.
143 OmniboxView::OmniboxView(Profile
* profile
,
144 OmniboxEditController
* controller
,
145 CommandUpdater
* command_updater
)
146 : controller_(controller
),
147 command_updater_(command_updater
) {
148 // |profile| can be NULL in tests.
150 model_
.reset(new OmniboxEditModel(this, controller
, profile
));
153 void OmniboxView::TextChanged() {
154 EmphasizeURLComponents();