Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view.cc
blob177bb7ecbf5ca84fe9bf8fe026200730d9b4cb87
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
6 // of OmniboxView.
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"
26 // static
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 (base::StartsWith(out, kJsPrefix, false)) {
32 base::TrimWhitespace(out.substr(kJsPrefix.length()), base::TRIM_LEADING,
33 &out);
35 return out;
38 // static
39 base::string16 OmniboxView::SanitizeTextForPaste(const base::string16& text) {
40 // Check for non-newline whitespace; if found, collapse whitespace runs down
41 // to single spaces.
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));
58 // static
59 base::string16 OmniboxView::GetClipboardText() {
60 // Try text format.
61 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
62 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
63 ui::CLIPBOARD_TYPE_COPY_PASTE)) {
64 base::string16 text;
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)) {
78 std::string url_str;
79 clipboard->ReadBookmark(NULL, &url_str);
80 // pass resulting url string through GURL to normalize
81 GURL url(url_str);
82 if (url.is_valid())
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_)
99 return;
100 model_->OpenMatch(
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 void OmniboxView::SetUserText(const base::string16& text) {
119 SetUserText(text, text, true);
122 void OmniboxView::SetUserText(const base::string16& text,
123 const base::string16& display_text,
124 bool update_popup) {
125 if (model_.get())
126 model_->SetUserText(text);
127 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup,
128 true);
131 void OmniboxView::ShowURL() {
132 SetFocus();
133 controller_->GetToolbarModel()->set_url_replacement_enabled(false);
134 model_->UpdatePermanentText();
135 RevertWithoutResettingSearchTermReplacement();
136 SelectAll(true);
139 void OmniboxView::HideURL() {
140 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
141 model_->UpdatePermanentText();
142 RevertWithoutResettingSearchTermReplacement();
145 void OmniboxView::RevertAll() {
146 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
147 RevertWithoutResettingSearchTermReplacement();
150 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
151 CloseOmniboxPopup();
152 if (model_.get())
153 model_->Revert();
154 TextChanged();
157 void OmniboxView::CloseOmniboxPopup() {
158 if (model_.get())
159 model_->StopAutocomplete();
162 bool OmniboxView::IsImeShowingPopup() const {
163 // Default to claiming that the IME is not showing a popup, since hiding the
164 // omnibox dropdown is a bad user experience when we don't know for sure that
165 // we have to.
166 return false;
169 void OmniboxView::ShowImeIfNeeded() {
172 bool OmniboxView::IsIndicatingQueryRefinement() const {
173 // The default implementation always returns false. Mobile ports can override
174 // this method and implement as needed.
175 return false;
178 void OmniboxView::OnMatchOpened(const AutocompleteMatch& match,
179 content::WebContents* web_contents) {
182 OmniboxView::OmniboxView(Profile* profile,
183 OmniboxEditController* controller,
184 CommandUpdater* command_updater)
185 : controller_(controller),
186 command_updater_(command_updater) {
187 // |profile| can be NULL in tests.
188 if (profile)
189 model_.reset(new OmniboxEditModel(this, controller, profile));
192 void OmniboxView::TextChanged() {
193 EmphasizeURLComponents();
194 if (model_.get())
195 model_->OnChanged();